feat: add condition field in pricing rule

This commit is contained in:
Abhishek Balam 2020-08-12 20:18:31 +05:30
parent 72fc188706
commit fdfab03c11
2 changed files with 25 additions and 2 deletions

View File

@ -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",

View File

@ -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:<br>" + 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")