refactor(stock): convert DN billed-amount SUM to get_all

update_billed_amount_based_on_so: raw "select sum(amount) ... where
dn_detail=%s and docstatus=1" -> frappe.get_all(fields=[{SUM: amount}]); the
bare aggregate needs no GROUP BY and the NULL-sum still resolves to 0. Same
result on MariaDB; valid under Postgres. Covered by the existing billing tests
in test_delivery_note.py.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mihir Kandoi
2026-06-19 23:19:47 +05:30
parent 6c96606c18
commit 28f6994520

View File

@@ -104,12 +104,12 @@ def update_billed_amount_based_on_so(so_detail: str, update_modified: bool = Tru
billed_against_so -= billed_amt_against_dn
else:
# Get billed amount directly against Delivery Note
billed_amt_against_dn = frappe.db.sql(
"""select sum(amount) from `tabSales Invoice Item`
where dn_detail=%s and docstatus=1""",
dnd.name,
billed_amt_against_dn = frappe.get_all(
"Sales Invoice Item",
filters={"dn_detail": dnd.name, "docstatus": 1},
fields=[{"SUM": "amount", "as": "amount"}],
)
billed_amt_against_dn = billed_amt_against_dn and billed_amt_against_dn[0][0] or 0
billed_amt_against_dn = billed_amt_against_dn[0].amount or 0 if billed_amt_against_dn else 0
# Distribute billed amount directly against SO between DNs based on FIFO
if billed_against_so and billed_amt_against_dn < dnd.amount: