C05 — حفظ موجودی قابلتأمین Variant هنگام لغو رسید چندخطی #59
2 changed files with 196 additions and 0 deletions
|
|
@ -4519,6 +4519,58 @@ def bulk_delete_warehouse_documents(db: Session, business_id: int, doc_ids: List
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_manual_receipt_variant_cancellation(
|
||||||
|
db: Session,
|
||||||
|
receipt: WarehouseDocument,
|
||||||
|
lines: List[WarehouseDocumentLine],
|
||||||
|
) -> None:
|
||||||
|
from adapters.db.models.product_variant_allocation import WarehouseLineVariantAllocation
|
||||||
|
from app.core.business_calendar import business_today
|
||||||
|
from app.services.variant_inventory_service import (
|
||||||
|
acquire_variant_stock_locks,
|
||||||
|
compute_available_variant_stock,
|
||||||
|
)
|
||||||
|
|
||||||
|
business_id = int(receipt.business_id)
|
||||||
|
incoming = {line.id: line for line in lines if line.movement == "in" and line.warehouse_id is not None}
|
||||||
|
if not incoming:
|
||||||
|
return
|
||||||
|
quantities = defaultdict(Decimal)
|
||||||
|
for allocation in db.query(WarehouseLineVariantAllocation).filter(
|
||||||
|
WarehouseLineVariantAllocation.business_id == business_id,
|
||||||
|
WarehouseLineVariantAllocation.warehouse_document_line_id.in_(incoming),
|
||||||
|
):
|
||||||
|
line = incoming[allocation.warehouse_document_line_id]
|
||||||
|
key = (int(line.warehouse_id), int(line.product_id), int(allocation.variant_id))
|
||||||
|
quantities[key] += allocation.quantity
|
||||||
|
if not quantities:
|
||||||
|
return
|
||||||
|
|
||||||
|
acquire_variant_stock_locks(db, business_id, list(quantities))
|
||||||
|
db.refresh(receipt, attribute_names=["status"])
|
||||||
|
if receipt.status != "posted":
|
||||||
|
raise ApiError("NOT_CANCELLABLE", "فقط حوالههای posted قابل لغو هستند", http_status=400)
|
||||||
|
as_of = max(business_today(business_id), receipt.document_date)
|
||||||
|
for (warehouse_id, product_id, variant_id), quantity in sorted(quantities.items()):
|
||||||
|
remaining = compute_available_variant_stock(
|
||||||
|
db, business_id, product_id, warehouse_id, variant_id, as_of,
|
||||||
|
exclude_warehouse_document_ids=[int(receipt.id)],
|
||||||
|
)
|
||||||
|
if remaining < 0:
|
||||||
|
raise ApiError(
|
||||||
|
"VARIANT_RECEIPT_CANCEL_INSUFFICIENT_STOCK",
|
||||||
|
"لغو رسید، موجودی قابلتأمین این ترکیب در انبار را منفی میکند",
|
||||||
|
http_status=409,
|
||||||
|
details={
|
||||||
|
"warehouse_id": warehouse_id,
|
||||||
|
"product_id": product_id,
|
||||||
|
"variant_id": variant_id,
|
||||||
|
"receipt_quantity": str(quantity),
|
||||||
|
"remaining_available_quantity": str(remaining),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def cancel_warehouse_document(
|
def cancel_warehouse_document(
|
||||||
db: Session,
|
db: Session,
|
||||||
business_id: int,
|
business_id: int,
|
||||||
|
|
@ -4555,6 +4607,14 @@ def cancel_warehouse_document(
|
||||||
raise ApiError("INVALID_PRODUCTION_ORDER_LINK", "شناسهٔ سفارش تولید در حواله نامعتبر است", http_status=409)
|
raise ApiError("INVALID_PRODUCTION_ORDER_LINK", "شناسهٔ سفارش تولید در حواله نامعتبر است", http_status=409)
|
||||||
if len(linked_order_ids) > 1:
|
if len(linked_order_ids) > 1:
|
||||||
raise ApiError("MULTIPLE_PRODUCTION_ORDER_LINKS", "حواله به چند سفارش تولید متصل است و قابل ابطال نیست", http_status=409)
|
raise ApiError("MULTIPLE_PRODUCTION_ORDER_LINKS", "حواله به چند سفارش تولید متصل است و قابل ابطال نیست", http_status=409)
|
||||||
|
if (
|
||||||
|
wh.doc_type == "receipt"
|
||||||
|
and wh.source_type == "manual"
|
||||||
|
and wh.source_document_id is None
|
||||||
|
and not (wh.extra_info or {}).get("cancels_warehouse_document_id")
|
||||||
|
and not linked_order_ids
|
||||||
|
):
|
||||||
|
_validate_manual_receipt_variant_cancellation(db, wh, lines)
|
||||||
if linked_order_ids:
|
if linked_order_ids:
|
||||||
_reverse_production_order_cancelled_document(db, wh, lines, next(iter(linked_order_ids)))
|
_reverse_production_order_cancelled_document(db, wh, lines, next(iter(linked_order_ids)))
|
||||||
|
|
||||||
|
|
|
||||||
136
hesabixAPI/tests/test_variant_receipt_cancel_commitments.py
Normal file
136
hesabixAPI/tests/test_variant_receipt_cancel_commitments.py
Normal file
|
|
@ -0,0 +1,136 @@
|
||||||
|
from datetime import date
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy.schema import CreateTable
|
||||||
|
|
||||||
|
from adapters.db.models.document import Document
|
||||||
|
from adapters.db.models.invoice_item_line import InvoiceItemLine
|
||||||
|
from adapters.db.models.product import Product
|
||||||
|
from adapters.db.models.product_variant_allocation import (
|
||||||
|
InvoiceItemLineVariantAllocation,
|
||||||
|
WarehouseLineVariantAllocation,
|
||||||
|
)
|
||||||
|
from adapters.db.models.warehouse_document import WarehouseDocument
|
||||||
|
from adapters.db.models.warehouse_document_line import WarehouseDocumentLine
|
||||||
|
from app.core import business_calendar
|
||||||
|
from app.core.responses import ApiError
|
||||||
|
from app.services.warehouse_service import _validate_manual_receipt_variant_cancellation
|
||||||
|
|
||||||
|
DAY = date(2026, 11, 11)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("replacement", [4, 5])
|
||||||
|
def test_receipt_cancel_checks_combined_quantity_in_the_same_warehouse_and_variant(
|
||||||
|
monkeypatch, replacement
|
||||||
|
):
|
||||||
|
monkeypatch.setattr(business_calendar, "business_today", lambda _: DAY)
|
||||||
|
engine = create_engine("sqlite://")
|
||||||
|
try:
|
||||||
|
with engine.begin() as connection:
|
||||||
|
for model in (
|
||||||
|
Product,
|
||||||
|
Document,
|
||||||
|
InvoiceItemLine,
|
||||||
|
InvoiceItemLineVariantAllocation,
|
||||||
|
WarehouseDocument,
|
||||||
|
WarehouseDocumentLine,
|
||||||
|
WarehouseLineVariantAllocation,
|
||||||
|
):
|
||||||
|
connection.execute(CreateTable(model.__table__, include_foreign_key_constraints=[]))
|
||||||
|
with Session(engine, autoflush=False) as db:
|
||||||
|
invoice = Document(
|
||||||
|
id=1,
|
||||||
|
business_id=1,
|
||||||
|
fiscal_year_id=1,
|
||||||
|
currency_id=1,
|
||||||
|
created_by_user_id=1,
|
||||||
|
document_type="invoice_sales",
|
||||||
|
document_date=DAY,
|
||||||
|
code="SALE",
|
||||||
|
is_proforma=False,
|
||||||
|
)
|
||||||
|
db.add_all(
|
||||||
|
[
|
||||||
|
Product(id=1, business_id=1, code="P", name="P", variant_mode="inventory"),
|
||||||
|
invoice,
|
||||||
|
InvoiceItemLine(
|
||||||
|
id=1,
|
||||||
|
document_id=1,
|
||||||
|
product_id=1,
|
||||||
|
quantity=5,
|
||||||
|
extra_info={
|
||||||
|
"warehouse_id": 1,
|
||||||
|
"movement": "out",
|
||||||
|
"inventory_tracked": True,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
InvoiceItemLineVariantAllocation(
|
||||||
|
id=1,
|
||||||
|
business_id=1,
|
||||||
|
invoice_item_line_id=1,
|
||||||
|
product_id=1,
|
||||||
|
variant_id=1,
|
||||||
|
quantity=5,
|
||||||
|
attributes_snapshot=[],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
for doc_id in (1, 2):
|
||||||
|
db.add(
|
||||||
|
WarehouseDocument(
|
||||||
|
id=doc_id,
|
||||||
|
business_id=1,
|
||||||
|
code=f"IN-{doc_id}",
|
||||||
|
document_date=DAY,
|
||||||
|
status="posted",
|
||||||
|
doc_type="receipt",
|
||||||
|
source_type="manual",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
lines = []
|
||||||
|
for line_id, (doc_id, warehouse_id, variant_id, quantity) in enumerate(
|
||||||
|
[(1, 1, 1, 5), (1, 1, 1, 5), (2, 1, 1, replacement), (2, 1, 2, 20), (2, 2, 1, 20)],
|
||||||
|
1,
|
||||||
|
):
|
||||||
|
line = WarehouseDocumentLine(
|
||||||
|
id=line_id,
|
||||||
|
warehouse_document_id=doc_id,
|
||||||
|
warehouse_id=warehouse_id,
|
||||||
|
product_id=1,
|
||||||
|
movement="in",
|
||||||
|
quantity=quantity,
|
||||||
|
)
|
||||||
|
lines.append(line)
|
||||||
|
db.add_all(
|
||||||
|
[
|
||||||
|
line,
|
||||||
|
WarehouseLineVariantAllocation(
|
||||||
|
id=line_id,
|
||||||
|
business_id=1,
|
||||||
|
warehouse_document_line_id=line_id,
|
||||||
|
product_id=1,
|
||||||
|
variant_id=variant_id,
|
||||||
|
quantity=quantity,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
receipt = db.get(WarehouseDocument, 1)
|
||||||
|
if replacement == 4:
|
||||||
|
with pytest.raises(ApiError) as error:
|
||||||
|
_validate_manual_receipt_variant_cancellation(db, receipt, lines[:2])
|
||||||
|
assert (error.value.code, error.value.status_code) == (
|
||||||
|
"VARIANT_RECEIPT_CANCEL_INSUFFICIENT_STOCK",
|
||||||
|
409,
|
||||||
|
)
|
||||||
|
details = error.value.detail["error"]["details"]
|
||||||
|
assert Decimal(details["receipt_quantity"]) == 10
|
||||||
|
assert Decimal(details["remaining_available_quantity"]) == -1
|
||||||
|
else:
|
||||||
|
_validate_manual_receipt_variant_cancellation(db, receipt, lines[:2])
|
||||||
|
assert receipt.status == "posted"
|
||||||
|
finally:
|
||||||
|
engine.dispose()
|
||||||
Loading…
Reference in a new issue