From bfaa93b0ca641352917be16a50e602a7fe458386 Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Mon, 20 Nov 2023 09:43:02 +0000 Subject: [PATCH 1/3] fix: honour currency precision while fetching balance --- erpnext/accounts/utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 7d91309fcced..2636a8d4f6ab 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -237,7 +237,6 @@ def get_balance_on( cond.append("""gle.cost_center = %s """ % (frappe.db.escape(cost_center, percent=False),)) if account: - if not (frappe.flags.ignore_account_permission or ignore_account_permission): acc.check_permission("read") @@ -283,11 +282,11 @@ def get_balance_on( cond.append("""gle.company = %s """ % (frappe.db.escape(company, percent=False))) if account or (party_type and party) or account_type: - + precision = get_currency_precision() if in_account_currency: - select_field = "sum(debit_in_account_currency) - sum(credit_in_account_currency)" + select_field = f"sum(round(debit_in_account_currency, {precision})) - sum(round(credit_in_account_currency, {precision}))" else: - select_field = "sum(debit) - sum(credit)" + select_field = f"sum(round(debit, {precision})) - sum(round(credit, {precision}))" bal = frappe.db.sql( """ SELECT {0} From 383a4b132ed5cc3383b035de8c22ba759b1e1241 Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Tue, 21 Nov 2023 12:46:56 +0000 Subject: [PATCH 2/3] chore: change f-string to sql params --- erpnext/accounts/utils.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 2636a8d4f6ab..857796b17b58 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -282,18 +282,22 @@ def get_balance_on( cond.append("""gle.company = %s """ % (frappe.db.escape(company, percent=False))) if account or (party_type and party) or account_type: - precision = get_currency_precision() + precision = frappe.db.escape(get_currency_precision()) if in_account_currency: - select_field = f"sum(round(debit_in_account_currency, {precision})) - sum(round(credit_in_account_currency, {precision}))" + select_field = ( + "sum(round(debit_in_account_currency, %s)) - sum(round(credit_in_account_currency, %s))" + ) else: - select_field = f"sum(round(debit, {precision})) - sum(round(credit, {precision}))" + select_field = "sum(round(debit, %s)) - sum(round(credit, %s))" + bal = frappe.db.sql( """ SELECT {0} FROM `tabGL Entry` gle WHERE {1}""".format( select_field, " and ".join(cond) - ) + ), + (precision, precision), )[0][0] # if bal is None, return 0 return flt(bal) From a8949174c878499e14228517abe66050f9b9ddb9 Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Sat, 9 Dec 2023 09:30:17 +0530 Subject: [PATCH 3/3] chore: avoid explicit escaping for precision Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com> --- erpnext/accounts/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 857796b17b58..950660f59df9 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -282,7 +282,7 @@ def get_balance_on( cond.append("""gle.company = %s """ % (frappe.db.escape(company, percent=False))) if account or (party_type and party) or account_type: - precision = frappe.db.escape(get_currency_precision()) + precision = get_currency_precision() if in_account_currency: select_field = ( "sum(round(debit_in_account_currency, %s)) - sum(round(credit_in_account_currency, %s))"