From f40c5cb68190e129136b8c6bf844d3a79f7dfa5b Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Fri, 25 Mar 2022 12:17:51 +0530 Subject: [PATCH] fix: Add back calculation for discount (cherry picked from commit 067564bd260437290b7cb190fb1cdc02eb658d0a) --- erpnext/public/js/controllers/transaction.js | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 2f4308d3bad..90b8d54b4e6 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -14,6 +14,31 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ frappe.model.round_floats_in(item, ["rate", "price_list_rate"]); + if(item.price_list_rate) { + if(item.rate > item.price_list_rate && has_margin_field) { + // if rate is greater than price_list_rate, set margin + // or set discount + item.discount_percentage = 0; + item.margin_type = 'Amount'; + item.margin_rate_or_amount = flt(item.rate - item.price_list_rate, + precision("margin_rate_or_amount", item)); + item.rate_with_margin = item.rate; + } else { + item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, + precision("discount_percentage", item)); + item.discount_amount = flt(item.price_list_rate) - flt(item.rate); + item.margin_type = ''; + item.margin_rate_or_amount = 0; + item.rate_with_margin = 0; + } + } else { + item.discount_percentage = 0.0; + item.margin_type = ''; + item.margin_rate_or_amount = 0; + item.rate_with_margin = 0; + } + item.base_rate_with_margin = item.rate_with_margin * flt(frm.doc.conversion_rate); + cur_frm.cscript.set_gross_profit(item); cur_frm.cscript.calculate_taxes_and_totals(); cur_frm.cscript.calculate_stock_uom_rate(frm, cdt, cdn);