refactor(sales_invoice): replace sql with qb in update_billing_status… (backport #55380) (#55411)

Co-authored-by: Loic Oberle <loic@dokos.io>
This commit is contained in:
mergify[bot]
2026-06-03 14:09:15 +05:30
committed by GitHub
parent 9eb0e3c82e
commit 44adfbea33

View File

@@ -2024,15 +2024,24 @@ class SalesInvoice(SellingController):
def update_billing_status_in_dn(self, update_modified=True): def update_billing_status_in_dn(self, update_modified=True):
if self.is_return and not self.update_billed_amount_in_delivery_note: if self.is_return and not self.update_billed_amount_in_delivery_note:
return return
updated_delivery_notes = [] updated_delivery_notes = []
SalesInvoiceItem = frappe.qb.DocType("Sales Invoice Item")
from frappe.query_builder.functions import Coalesce, Sum
for d in self.get("items"): for d in self.get("items"):
if d.dn_detail: if d.dn_detail:
billed_amt = frappe.db.sql( query = (
"""select sum(amount) from `tabSales Invoice Item` frappe.qb.from_(SalesInvoiceItem)
where dn_detail=%s and docstatus=1""", .select(Coalesce(Sum(SalesInvoiceItem.amount), 0))
d.dn_detail, .where(SalesInvoiceItem.dn_detail == d.dn_detail)
.where(SalesInvoiceItem.docstatus == 1)
) )
billed_amt = billed_amt and billed_amt[0][0] or 0
res = query.run()
billed_amt = res[0][0] if res else 0
frappe.db.set_value( frappe.db.set_value(
"Delivery Note Item", "Delivery Note Item",
d.dn_detail, d.dn_detail,