mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-26 00:14:50 +00:00
Merge pull request #43669 from frappe/mergify/bp/version-14-hotfix/pr-43557
fix: delete invalid pricing rule on change of applicable_for values (backport #43557)
This commit is contained in:
@@ -5,6 +5,8 @@
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
from frappe.query_builder import Criterion
|
||||||
|
from frappe.query_builder.functions import IfNull
|
||||||
|
|
||||||
pricing_rule_fields = [
|
pricing_rule_fields = [
|
||||||
"apply_on",
|
"apply_on",
|
||||||
@@ -91,22 +93,50 @@ class PromotionalScheme(Document):
|
|||||||
if self.is_new():
|
if self.is_new():
|
||||||
return
|
return
|
||||||
|
|
||||||
transaction_exists = False
|
invalid_pricing_rule = self.get_invalid_pricing_rules()
|
||||||
docnames = []
|
|
||||||
|
|
||||||
# If user has changed applicable for
|
if not invalid_pricing_rule:
|
||||||
if self.get_doc_before_save() and self.get_doc_before_save().applicable_for == self.applicable_for:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
docnames = frappe.get_all("Pricing Rule", filters={"promotional_scheme": self.name})
|
if frappe.db.exists(
|
||||||
|
"Pricing Rule Detail",
|
||||||
|
{
|
||||||
|
"pricing_rule": ["in", invalid_pricing_rule],
|
||||||
|
"docstatus": ["<", 2],
|
||||||
|
},
|
||||||
|
):
|
||||||
|
raise_for_transaction_exists(self.name)
|
||||||
|
|
||||||
for docname in docnames:
|
for doc in invalid_pricing_rule:
|
||||||
if frappe.db.exists("Pricing Rule Detail", {"pricing_rule": docname.name, "docstatus": ("<", 2)}):
|
frappe.delete_doc("Pricing Rule", doc)
|
||||||
raise_for_transaction_exists(self.name)
|
|
||||||
|
|
||||||
if docnames and not transaction_exists:
|
frappe.msgprint(
|
||||||
for docname in docnames:
|
_("The following invalid Pricing Rules are deleted:")
|
||||||
frappe.delete_doc("Pricing Rule", docname.name)
|
+ "<br><br><ul><li>"
|
||||||
|
+ "</li><li>".join(invalid_pricing_rule)
|
||||||
|
+ "</li></ul>"
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_invalid_pricing_rules(self):
|
||||||
|
pr = frappe.qb.DocType("Pricing Rule")
|
||||||
|
conditions = []
|
||||||
|
conditions.append(pr.promotional_scheme == self.name)
|
||||||
|
|
||||||
|
if self.applicable_for:
|
||||||
|
applicable_for = frappe.scrub(self.applicable_for)
|
||||||
|
applicable_for_list = [d.get(applicable_for) for d in self.get(applicable_for)]
|
||||||
|
|
||||||
|
conditions.append(
|
||||||
|
(IfNull(pr.applicable_for, "") != self.applicable_for)
|
||||||
|
| (
|
||||||
|
(IfNull(pr.applicable_for, "") == self.applicable_for)
|
||||||
|
& IfNull(pr[applicable_for], "").notin(applicable_for_list)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
conditions.append(IfNull(pr.applicable_for, "") != "")
|
||||||
|
|
||||||
|
return frappe.qb.from_(pr).select(pr.name).where(Criterion.all(conditions)).run(pluck=True)
|
||||||
|
|
||||||
def on_update(self):
|
def on_update(self):
|
||||||
self.validate()
|
self.validate()
|
||||||
|
|||||||
@@ -90,6 +90,31 @@ class TestPromotionalScheme(unittest.TestCase):
|
|||||||
price_rules = frappe.get_all("Pricing Rule", filters={"promotional_scheme": ps.name})
|
price_rules = frappe.get_all("Pricing Rule", filters={"promotional_scheme": ps.name})
|
||||||
self.assertEqual(price_rules, [])
|
self.assertEqual(price_rules, [])
|
||||||
|
|
||||||
|
def test_change_applicable_for_values_in_promotional_scheme(self):
|
||||||
|
ps = make_promotional_scheme(applicable_for="Customer", customer="_Test Customer")
|
||||||
|
ps.append("customer", {"customer": "_Test Customer 2"})
|
||||||
|
ps.save()
|
||||||
|
|
||||||
|
price_rules = frappe.get_all(
|
||||||
|
"Pricing Rule", filters={"promotional_scheme": ps.name, "applicable_for": "Customer"}
|
||||||
|
)
|
||||||
|
self.assertTrue(len(price_rules), 2)
|
||||||
|
|
||||||
|
ps.set("customer", [])
|
||||||
|
ps.append("customer", {"customer": "_Test Customer 2"})
|
||||||
|
ps.save()
|
||||||
|
|
||||||
|
price_rules = frappe.get_all(
|
||||||
|
"Pricing Rule",
|
||||||
|
filters={
|
||||||
|
"promotional_scheme": ps.name,
|
||||||
|
"applicable_for": "Customer",
|
||||||
|
"customer": "_Test Customer",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(price_rules, [])
|
||||||
|
frappe.delete_doc("Promotional Scheme", ps.name)
|
||||||
|
|
||||||
def test_min_max_amount_configuration(self):
|
def test_min_max_amount_configuration(self):
|
||||||
ps = make_promotional_scheme()
|
ps = make_promotional_scheme()
|
||||||
ps.price_discount_slabs[0].min_amount = 10
|
ps.price_discount_slabs[0].min_amount = 10
|
||||||
|
|||||||
Reference in New Issue
Block a user