fix(accounts): clear legacy coupon discounts

(cherry picked from commit 2b14064f9c)
This commit is contained in:
pandiyan
2026-10-28 12:20:55 +05:30
parent 44c35a9a31
commit c4c9885389
+35 -1
View File
@@ -651,7 +651,41 @@ def get_transaction_pricing_rules(doc):
if doc_before_save := doc.get_doc_before_save():
rows += doc_before_save.get("pricing_rules") or []
return {d.pricing_rule for d in rows if d.pricing_rule and not d.item_code and not d.child_docname}
pricing_rules = {
d.pricing_rule for d in rows if d.pricing_rule and not d.item_code and not d.child_docname
}
if (
pricing_rules
or not doc_before_save
or not doc_before_save.get("coupon_code")
or doc_before_save.coupon_code == doc.get("coupon_code")
or doc_before_save.ignore_pricing_rule
):
return pricing_rules
# Older documents did not track transaction rules. Check the saved coupon before removing its discount.
values = {"pricing_rule": frappe.db.get_value("Coupon Code", doc_before_save.coupon_code, "pricing_rule")}
conditions = get_other_conditions(
"name = %(pricing_rule)s and apply_on = 'Transaction' and price_or_product_discount = 'Price'"
" and coupon_code_based = 1 and disable = 0",
values,
doc_before_save,
)
coupon_rules = frappe.db.sql(f"select * from `tabPricing Rule` where {conditions}", values, as_dict=True)
coupon_rules = filter_pricing_rules_for_qty_amount(
doc_before_save.total_qty, doc_before_save.total, coupon_rules
)
for rule in filter_pricing_rule_based_on_condition(coupon_rules, doc_before_save):
field = "additional_discount_percentage" if rule.discount_percentage else "discount_amount"
discount = rule.discount_percentage or rule.discount_amount
if (
discount
and doc_before_save.apply_discount_on == rule.apply_discount_on
and doc_before_save.get(field) == flt(discount, doc_before_save.precision(field))
):
pricing_rules.add(rule.name)
return pricing_rules
def set_transaction_pricing_rules(doc, applied_rules):