C08a: جلوگیری از لغو انتقال Variant با موجودی ناکافی برای تعهد معتبر #67
2 changed files with 79 additions and 5 deletions
|
|
@ -4625,10 +4625,14 @@ def _validate_manual_variant_cancellation(
|
|||
)
|
||||
if remaining < 0:
|
||||
is_receipt = document.doc_type == "receipt"
|
||||
error_kind, label = {
|
||||
"receipt": ("RECEIPT", "رسید"),
|
||||
"adjustment": ("ADJUSTMENT", "تعدیل"),
|
||||
"transfer": ("TRANSFER", "انتقال"),
|
||||
}[document.doc_type]
|
||||
raise ApiError(
|
||||
"VARIANT_RECEIPT_CANCEL_INSUFFICIENT_STOCK" if is_receipt else "VARIANT_ADJUSTMENT_CANCEL_INSUFFICIENT_STOCK",
|
||||
"لغو رسید، موجودی قابلتأمین این ترکیب در انبار را منفی میکند" if is_receipt
|
||||
else "لغو تعدیل، موجودی قابلتأمین این ترکیب در انبار را منفی میکند",
|
||||
f"VARIANT_{error_kind}_CANCEL_INSUFFICIENT_STOCK",
|
||||
f"لغو {label}، موجودی قابلتأمین این ترکیب در انبار را منفی میکند",
|
||||
http_status=409,
|
||||
details={
|
||||
"warehouse_id": warehouse_id,
|
||||
|
|
@ -4657,7 +4661,7 @@ def cancel_warehouse_document(
|
|||
wh = db.query(WarehouseDocument).filter(WarehouseDocument.id == wh_id).first()
|
||||
if not wh or wh.business_id != business_id:
|
||||
raise ApiError("NOT_FOUND", "Warehouse document not found", http_status=404)
|
||||
if wh.doc_type == "adjustment":
|
||||
if wh.doc_type in ("adjustment", "transfer"):
|
||||
db.refresh(wh)
|
||||
|
||||
if wh.status != "posted":
|
||||
|
|
@ -4687,7 +4691,7 @@ def cancel_warehouse_document(
|
|||
):
|
||||
_validate_manual_receipt_variant_cancellation(db, wh, lines)
|
||||
elif (
|
||||
wh.doc_type == "adjustment"
|
||||
wh.doc_type in ("adjustment", "transfer")
|
||||
and wh.source_type == "manual"
|
||||
and wh.source_document_id is None
|
||||
and not (wh.extra_info or {}).get("cancels_warehouse_document_id")
|
||||
|
|
|
|||
70
hesabixAPI/tests/test_variant_transfer_cancel_commitments.py
Normal file
70
hesabixAPI/tests/test_variant_transfer_cancel_commitments.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
from decimal import Decimal
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import update
|
||||
|
||||
from adapters.db.models.invoice_item_line import InvoiceItemLine
|
||||
from adapters.db.models.product_variant_allocation import WarehouseLineVariantAllocation
|
||||
from adapters.db.models.warehouse_document import WarehouseDocument
|
||||
from app.core.responses import ApiError
|
||||
from app.services.warehouse_service import (
|
||||
_validate_manual_variant_cancellation,
|
||||
cancel_warehouse_document,
|
||||
)
|
||||
from tests.test_variant_adjustment_cancel_commitments import stock_case as stock_case
|
||||
|
||||
|
||||
@pytest.mark.parametrize("replacement,allowed", [(4, False), (5, True)])
|
||||
@pytest.mark.parametrize("destination", [1, 2])
|
||||
@pytest.mark.parametrize("reverse_order", [False, True])
|
||||
def test_whole_transfer_preserves_each_warehouse_commitment(stock_case, replacement, allowed, destination, reverse_order):
|
||||
source = 3 - destination
|
||||
entries = [(source, 1, "out", 5), (destination, 1, "in", 5)] * 2
|
||||
db, document, lines = stock_case([(source, 1, 10), (destination, 1, replacement)], entries, 5)
|
||||
document.doc_type = "transfer"
|
||||
document.warehouse_id_from, document.warehouse_id_to = source, destination
|
||||
invoice_line = db.get(InvoiceItemLine, 1)
|
||||
invoice_line.extra_info = {**invoice_line.extra_info, "warehouse_id": destination}
|
||||
db.commit()
|
||||
if reverse_order:
|
||||
lines.reverse()
|
||||
before = [(row.id, row.quantity) for row in db.query(WarehouseLineVariantAllocation)]
|
||||
if allowed:
|
||||
_validate_manual_variant_cancellation(db, document, lines)
|
||||
else:
|
||||
with pytest.raises(ApiError) as error:
|
||||
cancel_warehouse_document(db, 1, document.id, 1)
|
||||
assert (error.value.code, error.value.status_code) == ("VARIANT_TRANSFER_CANCEL_INSUFFICIENT_STOCK", 409)
|
||||
details = error.value.detail["error"]["details"]
|
||||
assert (details["warehouse_id"], details["variant_id"]) == (destination, 1)
|
||||
assert Decimal(details["document_net_quantity"]) == 10
|
||||
assert Decimal(details["remaining_available_quantity"]) == -1
|
||||
assert document.status == "posted" and db.query(WarehouseDocument).count() == 2
|
||||
assert [(row.id, row.quantity) for row in db.query(WarehouseLineVariantAllocation)] == before
|
||||
|
||||
|
||||
def test_surplus_of_another_variant_does_not_cover_transfer_shortage(stock_case):
|
||||
entries = [(1, 1, "out", 5), (2, 1, "in", 5)] * 2 + [(1, 2, "out", 5), (2, 2, "in", 5)]
|
||||
db, document, lines = stock_case([(1, 1, 10), (2, 1, 4), (1, 2, 10), (2, 2, 100)], entries, 5)
|
||||
document.doc_type = "transfer"
|
||||
invoice = db.get(InvoiceItemLine, 1)
|
||||
invoice.extra_info = {**invoice.extra_info, "warehouse_id": 2}
|
||||
db.commit()
|
||||
with pytest.raises(ApiError) as error:
|
||||
_validate_manual_variant_cancellation(db, document, lines)
|
||||
assert error.value.code == "VARIANT_TRANSFER_CANCEL_INSUFFICIENT_STOCK"
|
||||
assert error.value.detail["error"]["details"]["variant_id"] == 1
|
||||
|
||||
|
||||
def test_stale_posted_transfer_is_reloaded_before_cancellation(stock_case):
|
||||
db, document, _ = stock_case([(1, 1, 10), (2, 1, 5)], [(1, 1, "out", 10), (2, 1, "in", 10)], 1)
|
||||
document.doc_type = "transfer"
|
||||
db.commit()
|
||||
db.execute(update(WarehouseDocument).where(WarehouseDocument.id == document.id).values(status="cancelled"),
|
||||
execution_options={"synchronize_session": False})
|
||||
db.commit()
|
||||
assert document.status == "posted"
|
||||
with pytest.raises(ApiError) as error:
|
||||
cancel_warehouse_document(db, 1, document.id, 1)
|
||||
assert (error.value.code, error.value.status_code) == ("NOT_CANCELLABLE", 400)
|
||||
assert document.status == "cancelled" and db.query(WarehouseDocument).count() == 2
|
||||
Loading…
Reference in a new issue