fix in pricing rule

This commit is contained in:
Nabin Hait 2014-02-28 15:47:22 +05:30
parent e7e615cf18
commit 177b86a15b
3 changed files with 20 additions and 26 deletions

View File

@ -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,
}],
]
frappe.db.sql("delete from `tabPricing Rule`")

View File

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

View File

@ -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):