From bf1c60661010ce3358669f09d4ff4540cde0e597 Mon Sep 17 00:00:00 2001 From: ravibharathi656 Date: Fri, 5 Dec 2025 15:40:34 +0530 Subject: [PATCH 1/2] fix: include return invoice discount in discount validation (cherry picked from commit fab1ef5d765a4eff0edff9ee28124368619dafda) # Conflicts: # erpnext/controllers/taxes_and_totals.py --- erpnext/controllers/taxes_and_totals.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index eda94351515..9581d35f418 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -6,7 +6,12 @@ import json import frappe from frappe import _, scrub +<<<<<<< HEAD from frappe.model.document import Document +======= +from frappe.model.document import Document, bulk_insert +from frappe.query_builder import functions +>>>>>>> fab1ef5d76 (fix: include return invoice discount in discount validation) from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction from frappe.utils.deprecations import deprecated @@ -685,6 +690,22 @@ class calculate_taxes_and_totals: discount_amount = self.doc.discount_amount or 0 grand_total = self.doc.grand_total + if self.doc.get("is_return") and self.doc.get("return_against"): + doctype = frappe.qb.DocType(self.doc.doctype) + + result = ( + frappe.qb.from_(doctype) + .select(functions.Sum(doctype.discount_amount).as_("total_return_discount")) + .where( + (doctype.return_against == self.doc.return_against) + & (doctype.is_return == 1) + & (doctype.docstatus == 1) + ) + ).run(as_dict=True) + + total_return_discount = abs(result[0].get("total_return_discount") or 0) + discount_amount += total_return_discount + # validate that discount amount cannot exceed the total before discount if ( (grand_total >= 0 and discount_amount > grand_total) From 9ccf4900fed1981c16f430981db4447bb9ad0e1f Mon Sep 17 00:00:00 2001 From: Diptanil Saha Date: Tue, 9 Dec 2025 20:52:33 +0530 Subject: [PATCH 2/2] chore: resolve conflict --- erpnext/controllers/taxes_and_totals.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 9581d35f418..d6c4b99588c 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -6,12 +6,8 @@ import json import frappe from frappe import _, scrub -<<<<<<< HEAD from frappe.model.document import Document -======= -from frappe.model.document import Document, bulk_insert from frappe.query_builder import functions ->>>>>>> fab1ef5d76 (fix: include return invoice discount in discount validation) from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction from frappe.utils.deprecations import deprecated