From 13a84a26dcc42f0b5e729278c911faa3e4d50ef4 Mon Sep 17 00:00:00 2001 From: Seyyed Date: Fri, 20 Nov 2026 16:13:32 +0330 Subject: [PATCH] fix: flush manual production status before rebuilding variant balances --- hesabixAPI/app/services/warehouse_service.py | 2 +- .../test_production_variant_balance_post.py | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 hesabixAPI/tests/test_production_variant_balance_post.py diff --git a/hesabixAPI/app/services/warehouse_service.py b/hesabixAPI/app/services/warehouse_service.py index cf1926e5..7d614309 100755 --- a/hesabixAPI/app/services/warehouse_service.py +++ b/hesabixAPI/app/services/warehouse_service.py @@ -3755,7 +3755,7 @@ def post_warehouse_document( wh.status = "posted" from app.services.variant_inventory_service import rebuild_balances_for_warehouse_document - if doc_type in ("issue", "receipt", "adjustment", "transfer") and not (wh.extra_info or {}).get("cancels_warehouse_document_id"): + if doc_type in ("issue", "receipt", "adjustment", "transfer", "production_in", "production_out") and not (wh.extra_info or {}).get("cancels_warehouse_document_id"): # The balance query filters on the persisted posted status. db.flush([wh]) rebuild_balances_for_warehouse_document(db, wh) diff --git a/hesabixAPI/tests/test_production_variant_balance_post.py b/hesabixAPI/tests/test_production_variant_balance_post.py new file mode 100644 index 00000000..e4432996 --- /dev/null +++ b/hesabixAPI/tests/test_production_variant_balance_post.py @@ -0,0 +1,64 @@ +from decimal import Decimal + +import pytest +from sqlalchemy import select + +from adapters.db import models +from adapters.db.models.product_variant_allocation import WarehouseLineVariantAllocation +from app.services import variant_inventory_service as balances +from app.services.product_variant_service import list_variant_balances +from app.services.warehouse_service import create_manual_warehouse_document, post_warehouse_document +from tests.test_opening_variant_sync import opening_db as opening_db +from tests.test_warehouse_transfer_currency import DAY +from tests.test_warehouse_transfer_currency import transfer_db as transfer_db + + +@pytest.mark.parametrize("kind,quantities,expected", [ + ("production_in", (10, 12), {1: 10, 2: 12}), + ("production_out", (2, 3), {1: 8, 2: 9}), +]) +def test_production_post_flushes_status_and_rolls_back_with_balance(opening_db, monkeypatch, kind, quantities, expected): + db = opening_db + + def create(doc_type, amounts): + result = create_manual_warehouse_document(db, 1, 1, { + "doc_type": doc_type, "document_date": DAY.isoformat(), + "warehouse_id_from" if doc_type == "production_out" else "warehouse_id_to": 11, + "lines": [{"product_id": 1, "quantity": qty, "allocations": [{"variant_id": vid, "quantity": qty}], + "extra_info": {"unit": "main", "cost_price": 100}} for vid, qty in zip((1, 2), amounts, strict=True)], + }) + db.commit() + return result.id + + def snapshot(): + return ([(r.id, r.status) for r in db.query(models.WarehouseDocument).order_by(models.WarehouseDocument.id)], + list_variant_balances(db, 1, 1, 11), + [(r.id, r.warehouse_document_line_id, r.variant_id, r.quantity) for r in db.query(WarehouseLineVariantAllocation)]) + + if kind == "production_out": + arrival = create("receipt", (10, 12)) + post_warehouse_document(db, arrival) + db.commit() + did = create(kind, quantities) + before = snapshot() + original = balances.rebuild_balances_for_warehouse_document + + def observe(post_db, document): + assert document.status == "posted" + with post_db.no_autoflush: + assert post_db.scalar(select(models.WarehouseDocument.status).where(models.WarehouseDocument.id == did)) == "posted" + original(post_db, document) + + monkeypatch.setattr(balances, "rebuild_balances_for_warehouse_document", observe) + post_warehouse_document(db, did) + assert {r["variant_id"]: Decimal(r["quantity"]) for r in list_variant_balances(db, 1, 1, 11)} == expected + db.rollback() # Caller failure after the real rebuild, before commit. + db.expire_all() + assert snapshot() == before + post_warehouse_document(db, did) + db.commit() + db.expire_all() + assert {r["variant_id"]: Decimal(r["quantity"]) for r in list_variant_balances(db, 1, 1, 11)} == expected + committed = snapshot() + post_warehouse_document(db, did) + assert snapshot() == committed -- 2.43.0