diff --git a/erpnext/accounts/doctype/pricing_rule/utils.py b/erpnext/accounts/doctype/pricing_rule/utils.py index fb1fbe484e..bd9d0b3815 100644 --- a/erpnext/accounts/doctype/pricing_rule/utils.py +++ b/erpnext/accounts/doctype/pricing_rule/utils.py @@ -41,10 +41,11 @@ def get_pricing_rules(args, doc=None): if not pricing_rules: return [] if apply_multiple_pricing_rules(pricing_rules): - pricing_rules = sorted_by_priority(pricing_rules) + pricing_rules = sorted_by_priority(pricing_rules, args, doc) for pricing_rule in pricing_rules: - pricing_rule = filter_pricing_rules(args, pricing_rule, doc) - if pricing_rule: + if isinstance(pricing_rule, list): + rules.extend(pricing_rule) + else: rules.append(pricing_rule) else: pricing_rule = filter_pricing_rules(args, pricing_rules, doc) @@ -53,17 +54,23 @@ def get_pricing_rules(args, doc=None): return rules -def sorted_by_priority(pricing_rules): +def sorted_by_priority(pricing_rules, args, doc=None): # If more than one pricing rules, then sort by priority pricing_rules_list = [] pricing_rule_dict = {} + + priority = [] for pricing_rule in pricing_rules: - if not pricing_rule.get("priority"): continue + pricing_rule = filter_pricing_rules(args, pricing_rule, doc) + if pricing_rule: + if not pricing_rule.get('priority'): + pricing_rules_list.append(pricing_rule) + else: + priority.append(cint(pricing_rule.get('priority'))) + pricing_rule_dict.setdefault(cint(pricing_rule.get("priority")), []).append(pricing_rule) - pricing_rule_dict.setdefault(cint(pricing_rule.get("priority")), []).append(pricing_rule) - - for key in sorted(pricing_rule_dict): - pricing_rules_list.append(pricing_rule_dict.get(key)) + if priority: + pricing_rules_list.extend(pricing_rule_dict.get(min(priority))) return pricing_rules_list or pricing_rules @@ -144,9 +151,7 @@ def apply_multiple_pricing_rules(pricing_rules): if not apply_multiple_rule: return False - if (apply_multiple_rule - and len(apply_multiple_rule) == len(pricing_rules)): - return True + return True def _get_tree_conditions(args, parenttype, table, allow_blank=True): field = frappe.scrub(parenttype) @@ -264,18 +269,6 @@ def filter_pricing_rules(args, pricing_rules, doc=None): if max_priority: pricing_rules = list(filter(lambda x: cint(x.priority)==max_priority, pricing_rules)) - # apply internal priority - all_fields = ["item_code", "item_group", "brand", "customer", "customer_group", "territory", - "supplier", "supplier_group", "campaign", "sales_partner", "variant_of"] - - if len(pricing_rules) > 1: - for field_set in [["item_code", "variant_of", "item_group", "brand"], - ["customer", "customer_group", "territory"], ["supplier", "supplier_group"]]: - remaining_fields = list(set(all_fields) - set(field_set)) - if if_all_rules_same(pricing_rules, remaining_fields): - pricing_rules = apply_internal_priority(pricing_rules, field_set, args) - break - if pricing_rules and not isinstance(pricing_rules, list): pricing_rules = list(pricing_rules) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 9d9d1b363a..35c6cd33f9 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -121,7 +121,7 @@ class AccountsController(TransactionBase): def before_cancel(self): validate_einvoice_fields(self) - + def on_trash(self): # delete sl and gl entries on deletion of transaction if frappe.db.get_single_value('Accounts Settings', 'delete_linked_ledger_entries'): diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 76309f8799..452246e89c 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -616,7 +616,6 @@ class calculate_taxes_and_totals(object): self.doc.precision("base_write_off_amount")) def calculate_margin(self, item): - rate_with_margin = 0.0 base_rate_with_margin = 0.0 if item.price_list_rate: @@ -625,8 +624,8 @@ class calculate_taxes_and_totals(object): for d in get_applied_pricing_rules(item.pricing_rules): pricing_rule = frappe.get_cached_doc('Pricing Rule', d) - if (pricing_rule.margin_type in ['Amount', 'Percentage'] and pricing_rule.currency == self.doc.currency)\ - or (pricing_rule.margin_type == 'Percentage'): + if (pricing_rule.margin_rate_or_amount and pricing_rule.currency == self.doc.currency and + pricing_rule.margin_type in ['Amount', 'Percentage']): item.margin_type = pricing_rule.margin_type item.margin_rate_or_amount = pricing_rule.margin_rate_or_amount has_margin = True