mirror of
https://github.com/frappe/erpnext.git
synced 2026-08-22 18:59:58 +00:00
payment_terms_status_for_sales_order grouped invoice rows by `sii.parent` and took `Max(sii.sales_order)`, so an invoice that bills several Sales Orders was credited in full to one arbitrary order and the rest were starved of that payment. get_so_with_invoices now returns one row per (invoice, sales_order) and splits the invoice's grand total across the orders in proportion to each order's net line amount on that invoice. A single-order invoice keeps the full grand total (ratio 1), so the common case is unchanged; the split is pure Python over deterministic SQL, so MariaDB and Postgres produce identical results (100% parity). Test (fails on the old code, passes on both engines): - test_invoice_billing_multiple_orders_splits_proportionally: one invoice billing two SOs 600/400 -> each order credited its share, summing to the grand total. Old Max(sales_order) collapsed the invoice onto one order. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>