fix: Calculate rate based on discount on server side only if not rate or pricing rule applied
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index b3880be..fe12cf2 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -59,19 +59,22 @@
if item.discount_percentage == 100:
item.rate = 0.0
- elif (not item.rate or item.discount_percentage > 0) and item.price_list_rate:
- item.rate = flt(item.price_list_rate *
- (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
- item.discount_amount = item.price_list_rate * (item.discount_percentage / 100.0)
- elif item.discount_amount and item.price_list_rate:
- item.rate = item.price_list_rate - item.discount_amount
+ elif item.price_list_rate:
+ 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"))
+ item.discount_amount = item.price_list_rate * (item.discount_percentage / 100.0)
+ 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']:
item.rate_with_margin, item.base_rate_with_margin = self.calculate_margin(item)
-
- if flt(item.rate_with_margin) > 0:
- item.rate = flt(item.rate_with_margin * (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
- item.discount_amount = item.rate_with_margin - item.rate
+ if item.discount_percentage:
+ if flt(item.rate_with_margin) > 0:
+ item.rate = flt(item.rate_with_margin * (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
+ item.discount_amount = item.rate_with_margin - item.rate
+ elif flt(item.price_list_rate) > 0:
+ item.discount_amount = item.price_list_rate - item.rate
elif flt(item.price_list_rate) > 0 and not item.discount_amount:
item.discount_amount = item.price_list_rate - item.rate
diff --git a/erpnext/public/js/controllers/buying.js b/erpnext/public/js/controllers/buying.js
index 3ec27fc..f2fe3fe 100644
--- a/erpnext/public/js/controllers/buying.js
+++ b/erpnext/public/js/controllers/buying.js
@@ -147,6 +147,8 @@
},
discount_amount: function(doc, cdt, cdn) {
+ var item = frappe.get_doc(cdt, cdn);
+ item.discount_percentage = 0.0;
this.price_list_rate(doc, cdt, cdn);
},
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index 5291d4f..cb01c52 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -23,6 +23,7 @@
} 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;
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 965d2b0..23dcaa1 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -145,6 +145,8 @@
},
discount_amount: function(doc, cdt, cdn) {
+ var item = frappe.get_doc(cdt, cdn);
+ item.discount_percentage = 0.0;
this.apply_discount_on_item(doc, cdt, cdn, 'discount_amount');
},