From fdfab03c11dac62073987c0de495e423c0ecfa21 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Wed, 12 Aug 2020 20:18:31 +0530 Subject: [PATCH] feat: add condition field in pricing rule --- .../doctype/pricing_rule/pricing_rule.json | 11 ++++++++++- .../doctype/pricing_rule/pricing_rule.py | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.json b/erpnext/accounts/doctype/pricing_rule/pricing_rule.json index 29d83783d0..87084c7560 100644 --- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.json +++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.json @@ -1,4 +1,5 @@ { + "actions": [], "allow_import": 1, "allow_rename": 1, "autoname": "field:title", @@ -12,6 +13,7 @@ "apply_on", "price_or_product_discount", "warehouse", + "condition", "column_break_7", "items", "item_groups", @@ -550,11 +552,18 @@ "fieldtype": "Link", "label": "Promotional Scheme", "options": "Promotional Scheme" + }, + { + "description": "Simple Python Expression, Example: territory != 'All Territories'", + "fieldname": "condition", + "fieldtype": "Code", + "label": "Condition" } ], "icon": "fa fa-gift", "idx": 1, - "modified": "2019-12-18 17:29:22.957077", + "links": [], + "modified": "2020-08-12 20:15:32.279592", "modified_by": "Administrator", "module": "Accounts", "name": "Pricing Rule", diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py index cff7d5ba22..d88ce222d6 100644 --- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py +++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py @@ -7,9 +7,10 @@ from __future__ import unicode_literals import frappe import json import copy +import re + from frappe import throw, _ from frappe.utils import flt, cint, getdate - from frappe.model.document import Document from six import string_types @@ -31,6 +32,7 @@ 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 @@ -141,6 +143,16 @@ 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) + else: + try: + return frappe.safe_eval(doc.condition, None, args) if bool(args) else True + except Exception as e: + frappe.throw(doc.name + " Pricing Rule 'Condition' field error:
" + str(e).capitalize() ) + return False + #-------------------------------------------------------------------------------- @frappe.whitelist() @@ -252,6 +264,8 @@ 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 + 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")