[fix] if rate is greater than price_list_rate, set margin instead of discount. Fixes frappe/erpnext#6468 (#8856)
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index 6539530..44bf21d 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -6,12 +6,31 @@
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 da9c771..7154343 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -337,14 +337,6 @@
})
},
- 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];