From f69ffeb0b46365098648bc110062deaee6751157 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Wed, 17 May 2017 19:40:40 +0530 Subject: [PATCH] [fix] if rate is greater than price_list_rate, set margin instead of discount. Fixes frappe/erpnext#6468 (#8856) --- erpnext/public/js/controllers/transaction.js | 21 +++++++++++++++++++- erpnext/selling/sales_common.js | 8 -------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 6539530292..44bf21da3e 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -6,12 +6,31 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ this._super(); frappe.ui.form.on(this.frm.doctype + " Item", "rate", function(frm, cdt, cdn) { var item = frappe.get_doc(cdt, cdn); + var has_margin_field = frappe.meta.has_field(cdt, 'margin_type'); + frappe.model.round_floats_in(item, ["rate", "price_list_rate"]); if(item.price_list_rate) { - item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item)); + 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 = 'Percentage'; + item.margin_rate_or_amount = flt(Math.abs(1 - item.rate / item.price_list_rate) * 100.0, + precision("discount_percentage", 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.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; } cur_frm.cscript.set_gross_profit(item); diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js index da9c771b05..715434384b 100644 --- a/erpnext/selling/sales_common.js +++ b/erpnext/selling/sales_common.js @@ -337,14 +337,6 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({ }) }, - rate: function(doc, cdt, cdn){ - // if user changes the rate then set margin Rate or amount to 0 - item = locals[cdt][cdn]; - item.margin_type = ""; - item.margin_rate_or_amount = 0.0; - cur_frm.refresh_fields(); - }, - margin_rate_or_amount: function(doc, cdt, cdn) { // calculated the revised total margin and rate on margin rate changes item = locals[cdt][cdn];