From 177b86a15b7cd387cc511353518b5b20ef839433 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 28 Feb 2014 15:47:22 +0530 Subject: [PATCH] fix in pricing rule --- .../doctype/pricing_rule/test_pricing_rule.py | 31 +++++++++---------- erpnext/stock/doctype/item/test_item.py | 4 +-- erpnext/stock/get_item_details.py | 11 +++---- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py index 9e7744e7d1..a28db322b2 100644 --- a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py +++ b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py @@ -10,6 +10,16 @@ class TestPricingRule(unittest.TestCase): def test_pricing_rule_for_discount(self): from erpnext.stock.get_item_details import get_item_details from frappe import MandatoryError + + test_record = [{ + "doctype": "Pricing Rule", + "apply_on": "Item Code", + "item_code": "_Test Item", + "price_or_discount": "Discount Percentage", + "price": 0, + "discount_percentage": 10, + }] + frappe.bean(copy=test_record).insert() args = frappe._dict({ "item_code": "_Test Item", @@ -28,7 +38,7 @@ class TestPricingRule(unittest.TestCase): details = get_item_details(args) self.assertEquals(details.get("discount_percentage"), 10) - prule = frappe.bean(copy=test_records[0]) + prule = frappe.bean(copy=test_record) prule.doc.applicable_for = "Customer" self.assertRaises(MandatoryError, prule.insert) prule.doc.customer = "_Test Customer" @@ -37,7 +47,7 @@ class TestPricingRule(unittest.TestCase): details = get_item_details(args) self.assertEquals(details.get("discount_percentage"), 20) - prule = frappe.bean(copy=test_records[0]) + prule = frappe.bean(copy=test_record) prule.doc.apply_on = "Item Group" prule.doc.item_group = "All Item Groups" prule.doc.discount_percentage = 15 @@ -47,9 +57,7 @@ class TestPricingRule(unittest.TestCase): details = get_item_details(args) self.assertEquals(details.get("discount_percentage"), 10) - - - prule = frappe.bean(copy=test_records[0]) + prule = frappe.bean(copy=test_record) prule.doc.applicable_for = "Campaign" prule.doc.campaign = "_Test Campaign" prule.doc.discount_percentage = 5 @@ -68,15 +76,4 @@ class TestPricingRule(unittest.TestCase): details = get_item_details(args) self.assertEquals(details.get("discount_percentage"), 15) - frappe.db.sql("delete from `tabPricing Rule`") - -test_records = [ - [{ - "doctype": "Pricing Rule", - "apply_on": "Item Code", - "item_code": "_Test Item", - "price_or_discount": "Discount Percentage", - "price": 0, - "discount_percentage": 10, - }], -] \ No newline at end of file + frappe.db.sql("delete from `tabPricing Rule`") \ No newline at end of file diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py index b5e6740626..e86b113212 100644 --- a/erpnext/stock/doctype/item/test_item.py +++ b/erpnext/stock/doctype/item/test_item.py @@ -43,9 +43,7 @@ class TestItem(unittest.TestCase): } make_test_records("Item Price") - - frappe.db.sql("delete from `tabPricing Rule`") - + details = get_item_details({ "item_code": "_Test Item", "company": "_Test Company", diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 4c3725ade9..fe596c4c7d 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -253,7 +253,6 @@ def apply_pricing_rule(out, args): for rule_for in ["price", "discount_percentage"]: pricing_rules = filter(lambda x: x[rule_for] > 0.0, all_pricing_rules) pricing_rules = filter_pricing_rules(args_dict, pricing_rules, rule_for) - if pricing_rules: if rule_for == "discount_percentage": out["discount_percentage"] = pricing_rules[-1]["discount_percentage"] @@ -265,17 +264,17 @@ def apply_pricing_rule(out, args): out["pricing_rule_for_price"] = pricing_rules[-1]["name"] def get_pricing_rules(args_dict): - def _get_tree_conditions(doctype): + def _get_tree_conditions(doctype, allow_blank=True): 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)) + where lft<=%s and rgt>=%s""" % (doctype, '%s', '%s'), (lft, rgt)) if parent_groups: - condition = " ifnull("+field+", '') in ('" + "', '".join(parent_groups)+"', '')" + if allow_blank: parent_groups.append('') + condition = " ifnull("+field+", '') in ('" + "', '".join(parent_groups)+"')" return condition @@ -303,7 +302,7 @@ def get_pricing_rules(args_dict): where (item_code=%(item_code)s or {item_group_condition} or brand=%(brand)s) and docstatus < 2 and ifnull(disable, 0) = 0 {conditions} order by priority desc, name desc""".format( - item_group_condition=_get_tree_conditions("Item Group"), conditions=conditions), + item_group_condition=_get_tree_conditions("Item Group", False), conditions=conditions), args_dict, as_dict=1) def filter_pricing_rules(args_dict, pricing_rules, price_or_discount):