refactor: simplify logic

This commit is contained in:
Sagar Vora
2025-10-18 00:00:36 +05:30
parent 0e026b9ccd
commit 95f604457d
2 changed files with 8 additions and 10 deletions

View File

@@ -3025,16 +3025,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()

View File

@@ -723,7 +723,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 "