mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-30 18:34:48 +00:00
fix in pricing rule
This commit is contained in:
@@ -11,6 +11,16 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
from erpnext.stock.get_item_details import get_item_details
|
from erpnext.stock.get_item_details import get_item_details
|
||||||
from frappe import MandatoryError
|
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({
|
args = frappe._dict({
|
||||||
"item_code": "_Test Item",
|
"item_code": "_Test Item",
|
||||||
"company": "_Test Company",
|
"company": "_Test Company",
|
||||||
@@ -28,7 +38,7 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
details = get_item_details(args)
|
details = get_item_details(args)
|
||||||
self.assertEquals(details.get("discount_percentage"), 10)
|
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"
|
prule.doc.applicable_for = "Customer"
|
||||||
self.assertRaises(MandatoryError, prule.insert)
|
self.assertRaises(MandatoryError, prule.insert)
|
||||||
prule.doc.customer = "_Test Customer"
|
prule.doc.customer = "_Test Customer"
|
||||||
@@ -37,7 +47,7 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
details = get_item_details(args)
|
details = get_item_details(args)
|
||||||
self.assertEquals(details.get("discount_percentage"), 20)
|
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.apply_on = "Item Group"
|
||||||
prule.doc.item_group = "All Item Groups"
|
prule.doc.item_group = "All Item Groups"
|
||||||
prule.doc.discount_percentage = 15
|
prule.doc.discount_percentage = 15
|
||||||
@@ -47,9 +57,7 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
details = get_item_details(args)
|
details = get_item_details(args)
|
||||||
self.assertEquals(details.get("discount_percentage"), 10)
|
self.assertEquals(details.get("discount_percentage"), 10)
|
||||||
|
|
||||||
|
prule = frappe.bean(copy=test_record)
|
||||||
|
|
||||||
prule = frappe.bean(copy=test_records[0])
|
|
||||||
prule.doc.applicable_for = "Campaign"
|
prule.doc.applicable_for = "Campaign"
|
||||||
prule.doc.campaign = "_Test Campaign"
|
prule.doc.campaign = "_Test Campaign"
|
||||||
prule.doc.discount_percentage = 5
|
prule.doc.discount_percentage = 5
|
||||||
@@ -69,14 +77,3 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
self.assertEquals(details.get("discount_percentage"), 15)
|
self.assertEquals(details.get("discount_percentage"), 15)
|
||||||
|
|
||||||
frappe.db.sql("delete from `tabPricing Rule`")
|
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,
|
|
||||||
}],
|
|
||||||
]
|
|
||||||
@@ -44,8 +44,6 @@ class TestItem(unittest.TestCase):
|
|||||||
|
|
||||||
make_test_records("Item Price")
|
make_test_records("Item Price")
|
||||||
|
|
||||||
frappe.db.sql("delete from `tabPricing Rule`")
|
|
||||||
|
|
||||||
details = get_item_details({
|
details = get_item_details({
|
||||||
"item_code": "_Test Item",
|
"item_code": "_Test Item",
|
||||||
"company": "_Test Company",
|
"company": "_Test Company",
|
||||||
|
|||||||
@@ -253,7 +253,6 @@ def apply_pricing_rule(out, args):
|
|||||||
for rule_for in ["price", "discount_percentage"]:
|
for rule_for in ["price", "discount_percentage"]:
|
||||||
pricing_rules = filter(lambda x: x[rule_for] > 0.0, all_pricing_rules)
|
pricing_rules = filter(lambda x: x[rule_for] > 0.0, all_pricing_rules)
|
||||||
pricing_rules = filter_pricing_rules(args_dict, pricing_rules, rule_for)
|
pricing_rules = filter_pricing_rules(args_dict, pricing_rules, rule_for)
|
||||||
|
|
||||||
if pricing_rules:
|
if pricing_rules:
|
||||||
if rule_for == "discount_percentage":
|
if rule_for == "discount_percentage":
|
||||||
out["discount_percentage"] = pricing_rules[-1]["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"]
|
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):
|
def _get_tree_conditions(doctype, allow_blank=True):
|
||||||
field = frappe.scrub(doctype)
|
field = frappe.scrub(doctype)
|
||||||
condition = ""
|
condition = ""
|
||||||
if args_dict.get(field):
|
if args_dict.get(field):
|
||||||
lft, rgt = frappe.db.get_value(doctype, args_dict[field], ["lft", "rgt"])
|
lft, rgt = frappe.db.get_value(doctype, args_dict[field], ["lft", "rgt"])
|
||||||
parent_groups = frappe.db.sql_list("""select name from `tab%s`
|
parent_groups = frappe.db.sql_list("""select name from `tab%s`
|
||||||
where lft<=%s and rgt>=%s""" %
|
where lft<=%s and rgt>=%s""" % (doctype, '%s', '%s'), (lft, rgt))
|
||||||
(doctype, '%s', '%s'), (lft, rgt))
|
|
||||||
|
|
||||||
if parent_groups:
|
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
|
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)
|
where (item_code=%(item_code)s or {item_group_condition} or brand=%(brand)s)
|
||||||
and docstatus < 2 and ifnull(disable, 0) = 0 {conditions}
|
and docstatus < 2 and ifnull(disable, 0) = 0 {conditions}
|
||||||
order by priority desc, name desc""".format(
|
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)
|
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):
|
||||||
|
|||||||
Reference in New Issue
Block a user