Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v13 & v14]: Pricing Rule is not getting applied properly for Item Groups and Item Codes in Combination #33210

Open
adityaduggal opened this issue Dec 2, 2022 · 2 comments
Assignees
Labels

Comments

@adityaduggal
Copy link
Contributor

Information about bug

We have customer specific pricing rule for Item Groups and their Child we also use a generic pricing rule for an Item Group so that the customers where there is no pricing rule the users cannot add discounts.

Use Case and Bug Scenario 1

Generic Pricing Rule:

  1. Apply on Item Group (Item Group selected is Node with childs)
  2. Priority = 1
  3. Discount Percentage is mentioned as 0%
  4. Apply Multiple Pricing Rules = 1 (checked)

Create 2nd Pricing Rules:

  1. Apply on Item Group (Item Group selected is Node with childs lets say its same as above one)
  2. Priority = 2
  3. Add customer name to the Pricing rule
  4. Discount percentage mention here as 10%
  5. Apply Multiple is = 0 (unchecked)

Now in the above case the person is expecting the Priority 2 pricing rule to be applied but in reality pricing rule with Priority 1 is applied and not with priority 2

Here the apply multiple is taking precedence over priority which seems to be incorrect. To avoid this we need to remove the apply multiple on the 1st Rule but then this rule is not applied at all. I guess apply multiple should be checked in same priority.

Use Case and Bug Scenario 2

Generic Pricing Rule:

  1. Apply on Item Group (Item Group selected is Node with childs)
  2. Priority = 1
  3. Discount Percentage is mentioned as 0%
  4. Apply Multiple Pricing Rules = 1 (checked)

Create 2nd Pricing Rules:

  1. Apply on Item Group (Item Group selected is Node with childs lets say its same as above one)
  2. Priority = 2
  3. Add customer name to the Pricing rule
  4. Discount percentage mention here as 10%
  5. Apply Multiple is = 0 (unchecked)

Create 3rd Pricing Rules:

  1. Apply on Item Code and select an Item Code which is falling under the above 2 Item Groups' node
  2. Priority = 20
  3. Add price for the item and lets say its applicable for a minimum qty of that item code. Min Qty = 500
  4. Apply Multiple is = 0 (unchecked)

Now if you make a Sales Order with Customer name in 2nd Rule and Item Code in Rule 3 with say qty = 501 then 3rd rule is applied as expected since its on item code and priority is 20 as well but if we have qty = 10 below the min qty then no rule gets applied since we have apply multiple unchecked for Item Code.

This is a problem since we are giving precedence to Apply Multiple whereas we should give precedence to priority this ways we can easily apply rules from Item Code, Item Groups, Brands etc

The above scenarios are working in version-12 but not in version-13 and version-14 since there is change in logic for Pricing Rule which has created a bug in such scenarios.

Module

accounts

Version

ERPNext: v13.42.1 (version-13)
Frappe Framework: v13.45.0 (version-13)

Installation method

None

Relevant log output / Stack trace / Full Error Message.

No response

@adityaduggal
Copy link
Contributor Author

adityaduggal commented Dec 2, 2022

In Scenario 2 for the Pricing Rule 2 to be applied in case the Pricing Rule 3's min qty condition is not met then we have to make both the Pricing rules 2 and 3 have apply multiple = 1 and for pricing rule 1 generic for Item Group we have to uncheck the apply multiple

Basically the priority of the rule has gone for a toss in version-13

We should redefine the below function

def get_pricing_rules(args, doc=None):

The scenario should be as below:

  1. Get applicable pricing rules.
  2. Sort them by priority or filter them out if not applicable by conditions.
  3. If highest priority applicable Pricing rule is apply multiple then only apply others with same priority since there is confusion in apply_multiple as to whether it applies with all priorities or not.

In this we should not use apply multiple in the below lines:

if pricing_rules and not apply_multiple_pricing_rules(pricing_rules):
break

Basically the above line is only allowing the Item Code based Rules to go forward on if they are allowing multiple pricing rules to be applied. So in this kind of scenario if we have qty based discount on item_code then if apply_multiple is checked then it would apply discount to the Item Price as well which is not what a person would be wanting the person would be wanting to apply this Item Code rate if above minimum qty and if not then apply generic discount as per the customer's discount policy.

I think this line was not there in version-12 and hence was working fine. We never checked on apply_multiple_pricing_rules for item_code related pricing rules which were qty based.

In the function sorted_by_priority there is use of apply_multiple_pricing_rules field which is kind of confusing for the logic

https://github.com/frappe/erpnext/blob/develop/erpnext/accounts/doctype/pricing_rule/utils.py#L60-L77

@adityaduggal
Copy link
Contributor Author

Solution:

Change line:

if pricing_rule.get("apply_multiple_pricing_rules"):
pricing_rule_dict.setdefault(cint(pricing_rule.get("priority")), []).append(pricing_rule)

to
pricing_rule_dict.setdefault(cint(pricing_rule.get("priority")), []).append(pricing_rule)

Also Change the lines:

for apply_on in ["Item Code", "Item Group", "Brand"]:
pricing_rules.extend(_get_pricing_rules(apply_on, args, values))
if pricing_rules and not apply_multiple_pricing_rules(pricing_rules):
break

	for apply_on in ["Item Code", "Item Group", "Brand"]:
		pricing_rules.extend(_get_pricing_rules(apply_on, args, values))
		pr_applicable = filter_pricing_rules(args, pricing_rules, doc)
		if pr_applicable and not apply_multiple_pricing_rules(pricing_rules):
		 	break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants