diff --git a/hesabixAPI/app/services/invoice_service.py b/hesabixAPI/app/services/invoice_service.py index 32af77b0..28ac625e 100755 --- a/hesabixAPI/app/services/invoice_service.py +++ b/hesabixAPI/app/services/invoice_service.py @@ -13157,6 +13157,37 @@ def _sum_receipt_doc_payments_for_invoice(doc: Document, invoice_id: int, *, via return _sum_receipt_doc_account_payments(doc) +def _receipt_amount_for_invoice_balance( + receipt: Document, invoice: Document, *, via_person_line: bool, +) -> Decimal: + """Prefer explicit receivable allocations in the invoice's currency.""" + extra = receipt.extra_info or {} + if ( + invoice.document_type == INVOICE_SALES + and receipt.document_type == "receipt" + and invoice.currency_id is not None + and receipt.currency_id == invoice.currency_id + and not any(extra.get(key) for key in ("cross_currency", "fx_settlement", "settles_currency_id", "settlements")) + ): + person_id = _person_id_from_header({"extra_info": invoice.extra_info}) + invoice_id = str(invoice.id) + allocations = [ + line for line in receipt.lines + if person_id is not None + and line.person_id == person_id + and str((line.extra_info or {}).get("invoice_id", "")).strip() == invoice_id + and line.account is not None + and line.account.code == "10401" + and line.debit == 0 + and line.credit > 0 + ] + # Ordinary receipts store debit/credit in Document.currency_id. + # A converted receipt can instead store base amounts on person lines. + if allocations and not any((line.extra_info or {}).get("fx_settlement") for line in allocations): + return sum((line.credit for line in allocations), Decimal(0)) + return _sum_receipt_doc_payments_for_invoice(receipt, invoice.id, via_person_line=via_person_line) + + def _get_receipt_payment_ids_linked_to_invoice( db: Session, business_id: int, @@ -13501,7 +13532,7 @@ def batch_calculate_invoices_remaining( if all_rp_doc_ids: rp_docs = ( db.query(Document) - .options(selectinload(Document.lines)) + .options(selectinload(Document.lines).joinedload(DocumentLine.account)) .filter( Document.id.in_(list(all_rp_doc_ids)), Document.business_id == business_id, @@ -13525,11 +13556,13 @@ def batch_calculate_invoices_remaining( doc_id_int = int(doc_id) except (ValueError, TypeError): continue + if doc_id_int in processed_doc_ids: + continue rp_doc = rp_docs_by_id.get(doc_id_int) if not rp_doc: continue processed_doc_ids.add(doc_id_int) - total_paid += _sum_receipt_doc_payments_for_invoice(rp_doc, inv_id, via_person_line=False) + total_paid += _receipt_amount_for_invoice_balance(rp_doc, doc, via_person_line=False) for doc_id_int in invoice_to_person_rp.get(inv_id, set()): if doc_id_int in processed_doc_ids: @@ -13541,8 +13574,8 @@ def batch_calculate_invoices_remaining( if (rp_doc.extra_info or {}).get("source") == "quick_sale_change": total_paid -= _quick_sale_change_document_amount(rp_doc) else: - total_paid += _sum_receipt_doc_payments_for_invoice( - rp_doc, inv_id, via_person_line=True + total_paid += _receipt_amount_for_invoice_balance( + rp_doc, doc, via_person_line=True ) change_doc_id = links.get(INVOICE_LINK_QUICK_SALE_CHANGE_PAYMENT_ID) @@ -13635,8 +13668,10 @@ def calculate_invoice_remaining( for doc_id in receipt_payment_ids: try: doc_id_int = int(doc_id) + if doc_id_int in processed_doc_ids: + continue doc = db.query(Document).options( - selectinload(Document.lines) + selectinload(Document.lines).joinedload(DocumentLine.account) ).filter( Document.id == doc_id_int, Document.business_id == business_id, @@ -13647,7 +13682,7 @@ def calculate_invoice_remaining( continue processed_doc_ids.add(doc_id_int) - total_paid += _sum_receipt_doc_payments_for_invoice(doc, invoice_id, via_person_line=False) + total_paid += _receipt_amount_for_invoice_balance(doc, invoice, via_person_line=False) except (ValueError, TypeError) as e: logger.warning("receipt_payment_id parse error %s: %s", doc_id, e) continue @@ -13673,7 +13708,7 @@ def calculate_invoice_remaining( if person_doc_ids: receipt_payment_docs = ( db.query(Document) - .options(selectinload(Document.lines)) + .options(selectinload(Document.lines).joinedload(DocumentLine.account)) .filter( Document.id.in_(person_doc_ids), Document.business_id == business_id, @@ -13687,8 +13722,8 @@ def calculate_invoice_remaining( if (doc.extra_info or {}).get("source") == "quick_sale_change": total_paid -= _quick_sale_change_document_amount(doc) else: - total_paid += _sum_receipt_doc_payments_for_invoice( - doc, invoice_id, via_person_line=True + total_paid += _receipt_amount_for_invoice_balance( + doc, invoice, via_person_line=True ) change_doc_id = links.get(INVOICE_LINK_QUICK_SALE_CHANGE_PAYMENT_ID) diff --git a/hesabixAPI/tests/test_invoice_shared_receipt_allocation.py b/hesabixAPI/tests/test_invoice_shared_receipt_allocation.py new file mode 100644 index 00000000..4431eb6e --- /dev/null +++ b/hesabixAPI/tests/test_invoice_shared_receipt_allocation.py @@ -0,0 +1,139 @@ +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 import models +from adapters.db.models.business import BusinessField, BusinessType +from adapters.db.models.cash_register import CashRegister +from adapters.db.models.document_monetization import DocumentUsagePolicy +from adapters.db.models.document_numbering import BusinessDocumentNumberingSetting, DocumentNumberCounter +from adapters.db.models.marketplace import BusinessPlugin, MarketplacePlugin +from app.core import cache +from app.services.invoice_service import ( + batch_calculate_invoices_remaining, + calculate_invoice_remaining, + create_invoice, +) +from app.services.receipt_payment_service import create_receipt_payment + +DAY = date(2026, 11, 11) +ACCOUNTS = ( + "10401", "10405", "10102", "10104", "10106", "10107", "10202", + "20101", "40001", "40003", "50001", "50002", "50003", + "60205", "70401", "70406", "70407", "70408", +) + + +@pytest.fixture +def db(monkeypatch): + monkeypatch.setattr(cache, "_redis_config", {"enabled": False}) + monkeypatch.setattr(cache, "_redis_config_expires_at", float("inf")) + monkeypatch.setattr(cache, "_redis_client", None) + monkeypatch.setattr(cache, "_cache_service", None) + engine = create_engine("sqlite://") + tables = models.Base.metadata.tables + names = ( + "businesses", "currencies", "fiscal_years", "products", "documents", "document_lines", + "invoice_item_lines", "document_usage_policies", "invoice_item_line_variant_allocations", + "invoice_item_line_lot_allocations", "users", "accounts", "persons", "fa_assets", + "cash_registers", "business_currencies", + "document_invoice_tags", "warehouse_documents", "wallet_transactions", + ) + with Session(engine, autoflush=False) as session: + for table in [*(tables[name] for name in names), BusinessDocumentNumberingSetting.__table__, + DocumentNumberCounter.__table__, BusinessPlugin.__table__, MarketplacePlugin.__table__]: + session.execute(CreateTable(table)) + session.add_all([ + models.Business(id=1, name="Test", owner_id=1, default_currency_id=1, + business_type=BusinessType.SHOP, business_field=BusinessField.TRADING), + models.FiscalYear(id=1, business_id=1, title="Test", start_date=DAY, end_date=DAY, is_last=True), + models.Currency(id=1, name="Base", title="Base", symbol="B", code="B", decimal_places=2), + models.Currency(id=2, name="Other", title="Other", symbol="O", code="O", decimal_places=2), + models.Product(id=1, business_id=1, code="P", name="P", track_inventory=False), + models.Person(id=1, business_id=1, alias_name="Buyer", person_types="[]"), + DocumentUsagePolicy(business_id=1, policy_type="free", title="Test", is_active=True), + CashRegister(id=1, business_id=1, currency_id=1, name="Cash"), + *(models.Account(code=code, name=code, account_type="system") for code in ACCOUNTS), + ]) + session.commit() + yield session + engine.dispose() + + +def _sale(db, code): + result = create_invoice(db, 1, 1, { + "invoice_type": "invoice_sales", "document_date": DAY.isoformat(), "currency_id": 1, + "code": code, "extra_info": {"person_id": 1, "post_inventory": False}, + "lines": [{"product_id": 1, "quantity": 1, "extra_info": { + "unit_price": 500, "tax_rate": 10, "tax_amount": 50, "line_total": 550, + }}], + }, commit=False) + db.commit() + return db.get(models.Document, result["data"]["id"]) + + +def _receipt(db, *allocations, extra=None): + result = create_receipt_payment(db, 1, 1, { + "document_type": "receipt", "document_date": DAY.isoformat(), "currency_id": 1, + "person_lines": [{"person_id": 1, "amount": amount, "extra_info": {"invoice_id": invoice_id}} + for invoice_id, amount in allocations], + "account_lines": [{"transaction_type": "cash_register", "cash_register_id": 1, + "amount": sum(amount for _, amount in allocations)}], + "extra_info": extra or {}, + }) + db.expire_all() + return db.get(models.Document, result["id"]) + + +def _assert_balances(db, invoices, expected): + single = [calculate_invoice_remaining(db, 1, invoice.id) for invoice in invoices] + batch = batch_calculate_invoices_remaining(db, 1, invoices) + observed = [(row["paid_amount"], row["remaining"]) for row in single] + print(f"single={observed} batch={batch}") + assert [batch[invoice.id] for invoice in invoices] == single + assert observed == expected + + +def test_shared_receipt_uses_each_stored_receivable_allocation_once(db): + a, b = _sale(db, "SALE-A"), _sale(db, "SALE-B") + receipt = _receipt(db, (a.id, 100), (b.id, 150)) + assert receipt.currency_id == a.currency_id == b.currency_id == 1 + assert all(invoice.extra_info["links"]["receipt_payment_document_ids"] == [receipt.id] for invoice in (a, b)) + assert [(line.extra_info["invoice_id"], line.person_id, line.debit, line.credit) + for line in receipt.lines if line.account.code == "10401"] == [ + (a.id, 1, 0, 100), (b.id, 1, 0, 150), + ] + assert [line.debit for line in receipt.lines if line.account.code == "10202"] == [Decimal(250)] + assert all(sum(line.debit for line in invoice.lines if line.account.code == "10401") == 550 + for invoice in (a, b)) + _assert_balances(db, (a, b), [(100, 450), (150, 400)]) + + # A duplicated header link and the matching person line still identify a single receipt. + extra = dict(a.extra_info) + extra["links"] = {**extra["links"], "receipt_payment_document_ids": [receipt.id, receipt.id]} + a.extra_info = extra + db.commit() + _receipt(db, (a.id, 25)) + _assert_balances(db, (a, b), [(125, 425), (150, 400)]) + + +@pytest.mark.parametrize("mode", ["legacy", "fx"]) +def test_single_invoice_receipt_preserves_existing_fallback(db, mode): + invoice = _sale(db, "SALE") + if mode == "legacy": + _receipt(db, (None, 100), extra={"invoice_id": invoice.id}) + _assert_balances(db, (invoice,), [(100, 450)]) + else: + # Reproduce the persisted FX shape: GL amounts are in base, while the + # receipt-level settlement amount belongs to the invoice's currency. + invoice.currency_id = 2 + db.commit() + _receipt(db, (invoice.id, 25), extra={ + "invoice_id": invoice.id, "cross_currency": True, "settles_currency_id": 2, + "fx_settlement": {"settles_amount": "250", "settles_currency_id": 2}, + }) + _assert_balances(db, (invoice,), [(250, 300)])