From 174a6ec3987ada0db7872ac917245176a110cfd2 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Fri, 25 Mar 2022 10:46:09 +0530 Subject: [PATCH 1/4] fix: Rate change issue on save and mapping from other doc (cherry picked from commit 13fcda57767c456dfca4c064c41ca4f80a1898b2) --- .../controllers/sales_and_purchase_return.py | 2 ++ erpnext/public/js/controllers/transaction.js | 33 +++---------------- 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/erpnext/controllers/sales_and_purchase_return.py b/erpnext/controllers/sales_and_purchase_return.py index 21666336e0..d6296eb589 100644 --- a/erpnext/controllers/sales_and_purchase_return.py +++ b/erpnext/controllers/sales_and_purchase_return.py @@ -399,6 +399,8 @@ def make_return_doc(doctype, source_name, target_doc=None): } }, target_doc, set_missing_values) + doclist.set_onload('ignore_price_list', True) + return doclist def get_rate_for_return(voucher_type, voucher_no, item_code, return_against=None, diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 43ee5b31c7..0b5c9124ad 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -14,31 +14,6 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe 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); @@ -1042,9 +1017,9 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe var me = this; this.set_dynamic_labels(); var company_currency = this.get_company_currency(); - // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc + // Added `ignore_price_list` to determine if document is loading after mapping from another doc if(this.frm.doc.currency && this.frm.doc.currency !== company_currency - && !this.frm.doc.ignore_pricing_rule) { + && !this.frm.doc.__onload.ignore_price_list) { this.get_exchange_rate(transaction_date, this.frm.doc.currency, company_currency, function(exchange_rate) { @@ -1144,8 +1119,8 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe this.set_dynamic_labels(); var company_currency = this.get_company_currency(); - // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc - if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) { + // Added `ignore_price_list` to determine if document is loading after mapping from another doc + if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.__onload.ignore_price_list) { this.get_exchange_rate(this.frm.doc.posting_date, this.frm.doc.price_list_currency, company_currency, function(exchange_rate) { me.frm.set_value("plc_conversion_rate", exchange_rate); From 54cdff7947e0d8126016c85edf221f35cbaf53a5 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Fri, 25 Mar 2022 12:17:51 +0530 Subject: [PATCH 2/4] 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 0b5c9124ad..19e12e3c69 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -14,6 +14,31 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe 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); From a83a0a03b126ad896693237f4292a404e3c7477e Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Fri, 25 Mar 2022 12:28:55 +0530 Subject: [PATCH 3/4] fix: Revert rate calculation (cherry picked from commit 6937a498e71cffc56cad65141f561b4c8785d185) --- erpnext/controllers/taxes_and_totals.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 19acc105b3..81001f5ad3 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -114,18 +114,14 @@ class calculate_taxes_and_totals(object): for item in self.doc.get("items"): self.doc.round_floats_in(item) - if not item.rate: - item.rate = item.price_list_rate - if item.discount_percentage == 100: item.rate = 0.0 elif item.price_list_rate: - if item.pricing_rules or abs(item.discount_percentage) > 0: + if not item.rate or (item.pricing_rules and item.discount_percentage > 0): item.rate = flt(item.price_list_rate * (1.0 - (item.discount_percentage / 100.0)), item.precision("rate")) - if abs(item.discount_percentage) > 0: - item.discount_amount = item.price_list_rate * (item.discount_percentage / 100.0) + item.discount_amount = item.price_list_rate * (item.discount_percentage / 100.0) elif item.discount_amount or item.pricing_rules: item.rate = item.price_list_rate - item.discount_amount From 97e102ceef209afd5f288967e1c3a5d35eb99b16 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Fri, 25 Mar 2022 12:39:59 +0530 Subject: [PATCH 4/4] fix: Condition (cherry picked from commit 2e0e6ca6b177043f673c05472ac3069f060d1563) --- erpnext/controllers/taxes_and_totals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 81001f5ad3..29da5f11ab 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -123,7 +123,7 @@ class calculate_taxes_and_totals(object): item.discount_amount = item.price_list_rate * (item.discount_percentage / 100.0) - elif item.discount_amount or item.pricing_rules: + elif item.discount_amount and item.pricing_rules: item.rate = item.price_list_rate - item.discount_amount if item.doctype in ['Quotation Item', 'Sales Order Item', 'Delivery Note Item', 'Sales Invoice Item',