From e559fafa83359d5d2e2240ffd4c50f35368af2c4 Mon Sep 17 00:00:00 2001 From: Sagar Vora <16315650+sagarvora@users.noreply.github.com> Date: Fri, 17 Oct 2025 16:12:30 +0530 Subject: [PATCH] fix: validate that discount amount cannot exceed total before discount (cherry picked from commit f4f79d99e45a4e1f785d26713bf9f00045e9571f) # Conflicts: # erpnext/controllers/taxes_and_totals.py --- erpnext/controllers/taxes_and_totals.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 5543129d323..0048ec9acab 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -7,8 +7,12 @@ import json import frappe from frappe import _, scrub from frappe.model.document import Document +<<<<<<< HEAD from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction from frappe.utils.deprecations import deprecated +======= +from frappe.utils import cint, flt, fmt_money, round_based_on_smallest_currency_fraction +>>>>>>> f4f79d99e4 (fix: validate that discount amount cannot exceed total before discount) import erpnext from erpnext.accounts.doctype.journal_entry.journal_entry import get_exchange_rate @@ -682,6 +686,22 @@ class calculate_taxes_and_totals: self.doc.precision("discount_amount"), ) + discount_amount = self.doc.discount_amount or 0 + grand_total = self.doc.grand_total + + # validate that discount amount cannot exceed the total before discount + if grand_total * (discount_amount - grand_total) > 0: + frappe.throw( + _( + "Additional Discount Amount ({discount_amount}) cannot exceed " + "the total before such discount ({total_before_discount})" + ).format( + discount_amount=self.doc.get_formatted("discount_amount"), + total_before_discount=self.doc.get_formatted("grand_total"), + ), + title=_("Invalid Discount Amount"), + ) + def apply_discount_amount(self): if self.doc.discount_amount: if not self.doc.apply_discount_on: