From 593242fa5ca2cb18dfb1cf0155a6297f8d85053a Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 5 Apr 2019 19:35:02 +0530 Subject: [PATCH] fix: Calculate rate based on discount on server side only if not rate or pricing rule applied --- erpnext/controllers/taxes_and_totals.py | 23 +++++++++++--------- erpnext/public/js/controllers/buying.js | 2 ++ erpnext/public/js/controllers/transaction.js | 1 + erpnext/selling/sales_common.js | 2 ++ 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index b3880be2c6..fe12cf2316 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -59,19 +59,22 @@ class calculate_taxes_and_totals(object): 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 3ec27fcc7f..f2fe3fe72b 100644 --- a/erpnext/public/js/controllers/buying.js +++ b/erpnext/public/js/controllers/buying.js @@ -147,6 +147,8 @@ erpnext.buying.BuyingController = erpnext.TransactionController.extend({ }, 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 5291d4ff6f..cb01c525d5 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -23,6 +23,7 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ } 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 965d2b0219..23dcaa152d 100644 --- a/erpnext/selling/sales_common.js +++ b/erpnext/selling/sales_common.js @@ -145,6 +145,8 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({ }, 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'); },