From b4df87e545143f8e21eea41bdc80224bdf60cb8f Mon Sep 17 00:00:00 2001 From: Sagar Vora <16315650+sagarvora@users.noreply.github.com> Date: Sat, 18 Oct 2025 00:00:36 +0530 Subject: [PATCH] refactor: simplify logic (cherry picked from commit 95f604457d42e601d39108a658d36cbe8c214df6) --- erpnext/controllers/accounts_controller.py | 13 ++++--------- erpnext/controllers/taxes_and_totals.py | 5 ++++- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 01b10e460c1..f60fe003b0d 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -3010,16 +3010,11 @@ class AccountsController(TransactionBase): # returns have negative discount discount_already_applied *= -1 - if (source_doc.discount_amount * (discount_already_applied - source_doc.discount_amount)) >= 0: - # full discount already applied or exceeded - self.discount_amount = 0 - else: - discount_amount = source_doc.discount_amount - discount_already_applied - if is_return: - # returns have negative discount - discount_amount *= -1 + discount_amount = max(source_doc.discount_amount - discount_already_applied, 0) + if discount_amount and is_return: + discount_amount *= -1 - self.discount_amount = flt(discount_amount, self.precision("discount_amount")) + self.discount_amount = flt(discount_amount, self.precision("discount_amount")) self.calculate_taxes_and_totals() diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 0048ec9acab..3aa2a88a17c 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -690,7 +690,10 @@ class calculate_taxes_and_totals: grand_total = self.doc.grand_total # validate that discount amount cannot exceed the total before discount - if grand_total * (discount_amount - grand_total) > 0: + if ( + (grand_total >= 0 and discount_amount > grand_total) + or (grand_total < 0 and discount_amount < grand_total) # returns + ): frappe.throw( _( "Additional Discount Amount ({discount_amount}) cannot exceed "