Skip to content

Commit

Permalink
Merge pull request #24053 from rohitwaghchaure/additional-product-inc…
Browse files Browse the repository at this point in the history
…orrect-rate

fix: pricing rule with transaction not working for additional product
  • Loading branch information
rohitwaghchaure authored Dec 4, 2020
2 parents a31731e + 28e86cf commit c4d9bb2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
4 changes: 3 additions & 1 deletion erpnext/accounts/doctype/pricing_rule/pricing_rule.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@
"fieldtype": "Column Break"
},
{
"default": "0",
"depends_on": "eval:doc.rate_or_discount==\"Rate\"",
"fieldname": "rate",
"fieldtype": "Currency",
Expand Down Expand Up @@ -469,6 +470,7 @@
"options": "UOM"
},
{
"description": "If rate is zero them item will be treated as \"Free Item\"",
"fieldname": "free_item_rate",
"fieldtype": "Currency",
"label": "Rate"
Expand Down Expand Up @@ -563,7 +565,7 @@
"icon": "fa fa-gift",
"idx": 1,
"links": [],
"modified": "2020-10-28 16:53:14.416172",
"modified": "2020-12-04 00:36:24.698219",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Pricing Rule",
Expand Down
31 changes: 25 additions & 6 deletions erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,22 @@ def test_item_price_with_pricing_rule(self):
frappe.get_doc("Item Price", {"item_code": "Water Flask"}).delete()
item.delete()

def test_pricing_rule_for_transaction(self):
make_item("Water Flask 1")
frappe.delete_doc_if_exists('Pricing Rule', '_Test Pricing Rule')
make_pricing_rule(selling=1, min_qty=5, price_or_product_discount="Product",
apply_on="Transaction", free_item="Water Flask 1", free_qty=1, free_item_rate=10)

si = create_sales_invoice(qty=5, do_not_submit=True)
self.assertEquals(len(si.items), 2)
self.assertEquals(si.items[1].rate, 10)

si1 = create_sales_invoice(qty=2, do_not_submit=True)
self.assertEquals(len(si1.items), 1)

for doc in [si, si1]:
doc.delete()

def make_pricing_rule(**args):
args = frappe._dict(args)

Expand All @@ -539,20 +555,23 @@ def make_pricing_rule(**args):
"rate_or_discount": args.rate_or_discount or "Discount Percentage",
"discount_percentage": args.discount_percentage or 0.0,
"rate": args.rate or 0.0,
"margin_type": args.margin_type,
"margin_rate_or_amount": args.margin_rate_or_amount or 0.0,
"condition": args.condition or '',
"apply_multiple_pricing_rules": args.apply_multiple_pricing_rules or 0
})

if args.get("priority"):
doc.priority = args.get("priority")
for field in ["free_item", "free_qty", "free_item_rate", "priority",
"margin_type", "price_or_product_discount"]:
if args.get(field):
doc.set(field, args.get(field))

apply_on = doc.apply_on.replace(' ', '_').lower()
child_table = {'Item Code': 'items', 'Item Group': 'item_groups', 'Brand': 'brands'}
doc.append(child_table.get(doc.apply_on), {
apply_on: args.get(apply_on) or "_Test Item"
})

if doc.apply_on != "Transaction":
doc.append(child_table.get(doc.apply_on), {
apply_on: args.get(apply_on) or "_Test Item"
})

doc.insert(ignore_permissions=True)
if args.get(apply_on) and apply_on != "item_code":
Expand Down
11 changes: 10 additions & 1 deletion erpnext/accounts/doctype/pricing_rule/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ def apply_pricing_rule_on_transaction(doc):
pricing_rules = filter_pricing_rules_for_qty_amount(doc.total_qty,
doc.total, pricing_rules)

if not pricing_rules:
remove_free_item(doc)

for d in pricing_rules:
if d.price_or_product_discount == 'Price':
if d.apply_discount_on:
Expand All @@ -480,6 +483,12 @@ def apply_pricing_rule_on_transaction(doc):
get_product_discount_rule(d, item_details, doc=doc)
apply_pricing_rule_for_free_items(doc, item_details.free_item_data)
doc.set_missing_values()
doc.calculate_taxes_and_totals()

def remove_free_item(doc):
for d in doc.items:
if d.is_free_item:
doc.remove(d)

def get_applied_pricing_rules(pricing_rules):
if pricing_rules:
Expand All @@ -492,7 +501,7 @@ def get_applied_pricing_rules(pricing_rules):

def get_product_discount_rule(pricing_rule, item_details, args=None, doc=None):
free_item = pricing_rule.free_item
if pricing_rule.same_item:
if pricing_rule.same_item and pricing_rule.get("apply_on") != 'Transaction':
free_item = item_details.item_code or args.item_code

if not free_item:
Expand Down

0 comments on commit c4d9bb2

Please sign in to comment.