pricing rule: condition for tree-type docs

This commit is contained in:
Nabin Hait 2014-02-28 12:52:06 +05:30
parent 311079a311
commit ed5f423e65
2 changed files with 28 additions and 6 deletions

View File

@ -39,7 +39,7 @@ class TestPricingRule(unittest.TestCase):
prule = frappe.bean(copy=test_records[0]) prule = frappe.bean(copy=test_records[0])
prule.doc.apply_on = "Item Group" prule.doc.apply_on = "Item Group"
prule.doc.item_group = "_Test Item Group" prule.doc.item_group = "All Item Groups"
prule.doc.discount_percentage = 15 prule.doc.discount_percentage = 15
prule.insert() prule.insert()

View File

@ -265,24 +265,46 @@ def apply_pricing_rule(out, args):
out["pricing_rule_for_price"] = pricing_rules[-1]["name"] out["pricing_rule_for_price"] = pricing_rules[-1]["name"]
def get_pricing_rules(args_dict): def get_pricing_rules(args_dict):
def _get_tree_conditions(doctype):
field = frappe.scrub(doctype)
condition = ""
if args_dict.get(field):
lft, rgt = frappe.db.get_value(doctype, args_dict[field], ["lft", "rgt"])
parent_groups = frappe.db.sql_list("""select name from `tab%s`
where lft<=%s and rgt>=%s""" %
(doctype, '%s', '%s'), (lft, rgt))
if parent_groups:
condition = " ifnull("+field+", '') in ('" + "', '".join(parent_groups)+"', '')"
return condition
conditions = "" conditions = ""
for field in ["customer", "customer_group", "territory", "supplier", "supplier_type", for field in ["customer", "territory", "supplier", "supplier_type",
"campaign", "sales_partner"]: "campaign", "sales_partner"]:
if args_dict.get(field): if args_dict.get(field):
conditions += " and ifnull("+field+", '') in (%("+field+")s, '')" conditions += " and ifnull("+field+", '') in (%("+field+")s, '')"
else: else:
conditions += " and ifnull("+field+", '') = ''" conditions += " and ifnull("+field+", '') = ''"
customer_group_condition = _get_tree_conditions("Customer Group")
if customer_group_condition:
conditions += " and " + customer_group_condition
conditions += " and ifnull(for_price_list, '') in (%(price_list)s, '')" conditions += " and ifnull(for_price_list, '') in (%(price_list)s, '')"
if args_dict.get("transaction_date"): if args_dict.get("transaction_date"):
conditions += """ and %(transaction_date)s between ifnull(valid_from, '2000-01-01') conditions += """ and %(transaction_date)s between ifnull(valid_from, '2000-01-01')
and ifnull(valid_upto, '2500-12-31')""" and ifnull(valid_upto, '2500-12-31')"""
return frappe.db.sql("""select * from `tabPricing Rule` return frappe.db.sql("""select * from `tabPricing Rule`
where (item_code=%(item_code)s or item_group=%(item_group)s or brand=%(brand)s) where (item_code=%(item_code)s or {item_group_condition} or brand=%(brand)s)
and docstatus < 2 and ifnull(disable, 0) = 0 {0} and docstatus < 2 and ifnull(disable, 0) = 0 {conditions}
order by priority desc, name desc""".format(conditions), args_dict, as_dict=1) order by priority desc, name desc""".format(
item_group_condition=_get_tree_conditions("Item Group"), conditions=conditions),
args_dict, as_dict=1)
def filter_pricing_rules(args_dict, pricing_rules, price_or_discount): def filter_pricing_rules(args_dict, pricing_rules, price_or_discount):
# filter for qty # filter for qty