From 28f6994520c940947df71c2d6448e8ef4faf9f67 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Fri, 19 Jun 2026 23:19:47 +0530 Subject: [PATCH] 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) --- .../doctype/delivery_note/services/billing_status.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/erpnext/stock/doctype/delivery_note/services/billing_status.py b/erpnext/stock/doctype/delivery_note/services/billing_status.py index 98d360389d4..70364c89fa7 100644 --- a/erpnext/stock/doctype/delivery_note/services/billing_status.py +++ b/erpnext/stock/doctype/delivery_note/services/billing_status.py @@ -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: