mirror of
https://github.com/frappe/erpnext.git
synced 2026-10-29 22:57:42 +00:00
fix(pricing rule): keep a typed rate on items no pricing rule was applied to
An item a pricing rule was considered for but not applied to (e.g. a coupon based rule without a coupon) stores pricing_rules as "[]". Saving the document treated that as an applied rule and reset the rate to the price list rate.
(cherry picked from commit 11462391dc)
# Conflicts:
# erpnext/controllers/taxes_and_totals.py
This commit is contained in:
@@ -563,7 +563,8 @@ def get_pricing_rule_for_item(args, doc=None, for_validate=False):
|
||||
if not doc:
|
||||
return item_details
|
||||
|
||||
elif args.get("pricing_rules"):
|
||||
# "[]" means no rule was applied, so there is none to remove and the rate typed in stays
|
||||
elif get_applied_pricing_rules(args.get("pricing_rules")):
|
||||
item_details = remove_pricing_rule_for_item(
|
||||
args.get("pricing_rules"),
|
||||
item_details,
|
||||
|
||||
@@ -187,6 +187,58 @@ class calculate_taxes_and_totals:
|
||||
|
||||
self.doc.conversion_rate = flt(self.doc.conversion_rate)
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
def calculate_item_rate(self, item):
|
||||
if not item.price_list_rate:
|
||||
remove_margin(item)
|
||||
remove_discount(item)
|
||||
item.rate_with_margin = 0
|
||||
return
|
||||
|
||||
# An item no rule applied to may still hold "[]", which must not override the rate typed in
|
||||
applied_rules = get_applied_pricing_rules(item.pricing_rules)
|
||||
has_pricing_rules = bool(applied_rules) and not self.doc.ignore_pricing_rule
|
||||
if has_pricing_rules:
|
||||
remove_margin(item)
|
||||
|
||||
for d in applied_rules:
|
||||
pricing_rule = frappe.get_cached_doc("Pricing Rule", d)
|
||||
apply_pricing_rule_margin(pricing_rule, item, self.doc)
|
||||
|
||||
item.rate_with_margin = get_rate_with_margin(item)
|
||||
if item.discount_percentage > 0:
|
||||
item.discount_amount = flt(
|
||||
item.rate_with_margin * item.discount_percentage / 100.0, item.precision("discount_amount")
|
||||
)
|
||||
|
||||
calculated_rate = flt(item.rate_with_margin - item.discount_amount, item.precision("rate"))
|
||||
|
||||
# if rate is 0 or pricing rules are applicable, calculated rate is preferred
|
||||
if has_pricing_rules or not item.rate:
|
||||
item.rate = calculated_rate
|
||||
return
|
||||
|
||||
# discount and margin are correct, exit early
|
||||
if item.rate == calculated_rate:
|
||||
return
|
||||
|
||||
# item rate does not match calculated rate. prefer item rate, reset margin / discount
|
||||
if item.rate > item.price_list_rate:
|
||||
item.margin_type = "Amount"
|
||||
item.margin_rate_or_amount = flt(
|
||||
item.rate - item.price_list_rate, item.precision("margin_rate_or_amount")
|
||||
)
|
||||
item.rate_with_margin = item.rate
|
||||
remove_discount(item)
|
||||
return
|
||||
|
||||
item.rate_with_margin = item.price_list_rate
|
||||
item.discount_amount = flt(item.rate_with_margin - item.rate, item.precision("discount_amount"))
|
||||
item.discount_percentage = 0
|
||||
remove_margin(item)
|
||||
|
||||
>>>>>>> 1146239 (fix(pricing rule): keep a typed rate on items no pricing rule was applied to)
|
||||
def calculate_item_values(self):
|
||||
if self.doc.get("is_consolidated"):
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user