diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index d9d6bc4d849..420b09d296a 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -7,7 +7,7 @@ import json import frappe from frappe import _, scrub from frappe.model.document import Document -from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction +from frappe.utils import cint, flt, fmt_money, round_based_on_smallest_currency_fraction import erpnext from erpnext.accounts.doctype.journal_entry.journal_entry import get_exchange_rate @@ -719,6 +719,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: