Skip to content

Commit

Permalink
perf(invoice): Faster return amount query (#36556)
Browse files Browse the repository at this point in the history
perf: Faster return amount query
(cherry picked from commit b0c79a0)
  • Loading branch information
ankush authored and mergify[bot] committed Aug 9, 2023
1 parent 5dbca09 commit 3c913ae
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions erpnext/accounts/doctype/sales_invoice/sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1580,15 +1580,13 @@ def set_loyalty_program_tier(self):
frappe.db.set_value("Customer", self.customer, "loyalty_program_tier", lp_details.tier_name)

def get_returned_amount(self):
from frappe.query_builder.functions import Coalesce, Sum
from frappe.query_builder.functions import Sum

doc = frappe.qb.DocType(self.doctype)
returned_amount = (
frappe.qb.from_(doc)
.select(Sum(doc.grand_total))
.where(
(doc.docstatus == 1) & (doc.is_return == 1) & (Coalesce(doc.return_against, "") == self.name)
)
.where((doc.docstatus == 1) & (doc.is_return == 1) & (doc.return_against == self.name))
).run()

return abs(returned_amount[0][0]) if returned_amount[0][0] else 0
Expand Down

0 comments on commit 3c913ae

Please sign in to comment.