diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py index d88ce222d6..98abadbff6 100644 --- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py +++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py @@ -32,7 +32,6 @@ class PricingRule(Document): self.validate_max_discount() self.validate_price_list_with_currency() self.validate_dates() - validate_condition(self) if not self.margin_type: self.margin_rate_or_amount = 0.0 @@ -143,16 +142,17 @@ class PricingRule(Document): if self.valid_from and self.valid_upto and getdate(self.valid_from) > getdate(self.valid_upto): frappe.throw(_("Valid from date must be less than valid upto date")) -def validate_condition(doc, args=None): - if doc.condition and ("=" in doc.condition) and re.match("""[\w\.:_]+\s*={1}\s*[\w\.@'"]+""", doc.condition): - frappe.throw(_("Invalid condition in Pricing Rule: {0}").format(doc.name), frappe.ValidationError) +def validate_condition(pricing_rule, doc=None): + if pricing_rule.condition and ("=" in pricing_rule.condition) and re.match("""[\w\.:_]+\s*={1}\s*[\w\.@'"]+""", pricing_rule.condition): + frappe.throw(_("Invalid condition in Pricing Rule: {0}").format(pricing_rule.name), frappe.ValidationError) else: try: - return frappe.safe_eval(doc.condition, None, args) if bool(args) else True + doc = doc.as_dict() + return frappe.safe_eval(pricing_rule.condition, None, doc) except Exception as e: frappe.throw(doc.name + " Pricing Rule 'Condition' field error:
" + str(e).capitalize() ) return False - + return True #-------------------------------------------------------------------------------- @frappe.whitelist() @@ -264,7 +264,7 @@ def get_pricing_rule_for_item(args, price_list_rate=0, doc=None, for_validate=Fa if pricing_rule.get('suggestion'): continue - if not validate_condition(pricing_rule, args): continue + if not validate_condition(pricing_rule, doc): continue item_details.validate_applied_rule = pricing_rule.get("validate_applied_rule", 0) item_details.price_or_product_discount = pricing_rule.get("price_or_product_discount")