def get_pricing_rule_discount(self): from erpnext.accounts.doctype.pricing_rule.utils import get_pricing_rules, filter_pricing_rules from erpnext.stock.get_item_details import process_args parent_dict = {} for fieldname in self.meta.get_valid_columns(): parent_dict[fieldname] = self.get(fieldname) parent_dict.update({"document_type": "Sales Order Item"}) for item in self.get("items"): if not item.original_discount: if item.get("item_code"): args = parent_dict.copy() args.update(item.as_dict()) args["doctype"] = self.doctype args["name"] = self.name if not args.get("transaction_date"): args["transaction_date"] = args.get("posting_date") if self.get("is_subcontracted"): args["is_subcontracted"] = self.is_subcontracted args = process_args(args) pricing_rules = get_pricing_rules(args) pricing_rule = filter_pricing_rules(args, pricing_rules) if pricing_rule: item.original_discount = pricing_rule.discount_percentage
def get_pricing_rule_for_item(args, price_list_rate=0, doc=None, for_validate=False): from erpnext.accounts.doctype.pricing_rule.utils import ( get_pricing_rules, get_applied_pricing_rules, get_pricing_rule_items, get_product_discount_rule) if isinstance(doc, string_types): doc = json.loads(doc) if doc: doc = frappe.get_doc(doc) if (args.get('is_free_item') or args.get("parenttype") == "Material Request"): return {} item_details = frappe._dict({ "doctype": args.doctype, "name": args.name, "parent": args.parent, "parenttype": args.parenttype, "child_docname": args.get('child_docname'), "discount_percentage_on_rate": [], "discount_amount_on_rate": [] }) if args.ignore_pricing_rule or args.item_ignore_pricing_rule or not args.item_code: if frappe.db.exists(args.doctype, args.name) and args.get("pricing_rules"): item_details = remove_pricing_rule_for_item( args.get("pricing_rules"), item_details, args.get('item_code')) return item_details update_args_for_pricing_rule(args) pricing_rules = (get_applied_pricing_rules(args) if for_validate and args.get("pricing_rules") else get_pricing_rules( args, doc)) if pricing_rules: rules = [] for pricing_rule in pricing_rules: if not pricing_rule: continue if isinstance(pricing_rule, string_types): pricing_rule = frappe.get_cached_doc("Pricing Rule", pricing_rule) pricing_rule.apply_rule_on_other_items = get_pricing_rule_items( pricing_rule) if pricing_rule.get('suggestion'): continue item_details.validate_applied_rule = pricing_rule.get( "validate_applied_rule", 0) item_details.price_or_product_discount = pricing_rule.get( "price_or_product_discount") rules.append(get_pricing_rule_details(args, pricing_rule)) if pricing_rule.mixed_conditions or pricing_rule.apply_rule_on_other: item_details.update({ 'apply_rule_on_other_items': json.dumps(pricing_rule.apply_rule_on_other_items), 'price_or_product_discount': pricing_rule.price_or_product_discount, 'apply_rule_on': (frappe.scrub(pricing_rule.apply_rule_on_other) if pricing_rule.apply_rule_on_other else frappe.scrub( pricing_rule.get('apply_on'))) }) if pricing_rule.coupon_code_based == 1 and args.coupon_code == None: return item_details if not pricing_rule.validate_applied_rule: if pricing_rule.price_or_product_discount == "Price": apply_price_discount_rule(pricing_rule, item_details, args) else: get_product_discount_rule(pricing_rule, item_details, args, doc) item_details.has_pricing_rule = 1 item_details.pricing_rules = ','.join([d.pricing_rule for d in rules]) if not doc: return item_details elif args.get("pricing_rules"): item_details = remove_pricing_rule_for_item(args.get("pricing_rules"), item_details, args.get('item_code')) return item_details
def get_pricing_rule_for_item(args, price_list_rate=0, doc=None): from erpnext.accounts.doctype.pricing_rule.utils import get_pricing_rules if isinstance(doc, string_types): doc = json.loads(doc) if doc: doc = frappe.get_doc(doc) if (args.get('is_free_item') or args.get("parenttype") == "Material Request"): return {} item_details = frappe._dict({ "doctype": args.doctype, "name": args.name, "parent": args.parent, "parenttype": args.parenttype, "child_docname": args.get('child_docname'), "discount_percentage_on_rate": [], "discount_amount_on_rate": [] }) if args.ignore_pricing_rule or not args.item_code: if frappe.db.exists(args.doctype, args.name) and args.get("pricing_rules"): item_details = remove_pricing_rule_for_item(args.get("pricing_rules"), item_details, args.get('item_code')) return item_details if not (args.item_group and args.brand): try: args.item_group, args.brand = frappe.get_cached_value("Item", args.item_code, ["item_group", "brand"]) except TypeError: # invalid item_code return item_details if not args.item_group: frappe.throw(_("Item Group not mentioned in item master for item {0}").format(args.item_code)) if args.transaction_type=="selling": if args.customer and not (args.customer_group and args.territory): customer = frappe.get_cached_value("Customer", args.customer, ["customer_group", "territory"]) if customer: args.customer_group, args.territory = customer args.supplier = args.supplier_group = None elif args.supplier and not args.supplier_group: args.supplier_group = frappe.get_cached_value("Supplier", args.supplier, "supplier_group") args.customer = args.customer_group = args.territory = None pricing_rules = get_pricing_rules(args, doc) if pricing_rules: rules = [] for pricing_rule in pricing_rules: if not pricing_rule or pricing_rule.get('suggestion'): continue item_details.validate_applied_rule = pricing_rule.get("validate_applied_rule", 0) rules.append(get_pricing_rule_details(args, pricing_rule)) if pricing_rule.mixed_conditions or pricing_rule.apply_rule_on_other: continue if (not pricing_rule.validate_applied_rule and pricing_rule.price_or_product_discount == "Price"): apply_price_discount_pricing_rule(pricing_rule, item_details, args) item_details.has_pricing_rule = 1 # if discount is applied on the rate and not on price list rate # if price_list_rate: # set_discount_amount(price_list_rate, item_details) item_details.pricing_rules = ','.join([d.pricing_rule for d in rules]) if not doc: return item_details for rule in rules: doc.append('pricing_rules', rule) elif args.get("pricing_rules"): item_details = remove_pricing_rule_for_item(args.get("pricing_rules"), item_details, args.get('item_code')) return item_details