feat: add condition field in pricing rule
diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.json b/erpnext/accounts/doctype/pricing_rule/pricing_rule.json
index 29d8378..87084c7 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 cff7d5b..d88ce22 100644
--- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
+++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
@@ -7,9 +7,10 @@
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 @@
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 @@
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:<br>" + str(e).capitalize() )
+ return False
+
#--------------------------------------------------------------------------------
@frappe.whitelist()
@@ -252,6 +264,8 @@
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")