def get_product_discount_rule(pricing_rule, item_details, args=None, doc=None): free_item = pricing_rule.free_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: frappe.throw( _("Free item not set in the pricing rule {0}").format( get_link_to_form("Pricing Rule", pricing_rule.name))) item_details.free_item_data = { 'item_code': free_item, 'qty': pricing_rule.free_qty or 1, 'rate': pricing_rule.free_item_rate or 0, 'price_list_rate': pricing_rule.free_item_rate or 0, 'is_free_item': 1 } item_data = frappe.get_cached_value( 'Item', free_item, ['item_name', 'description', 'stock_uom'], as_dict=1) item_details.free_item_data.update(item_data) item_details.free_item_data[ 'uom'] = pricing_rule.free_item_uom or item_data.stock_uom item_details.free_item_data['conversion_factor'] = get_conversion_factor( free_item, item_details.free_item_data['uom']).get("conversion_factor", 1) if item_details.get("parenttype") == 'Purchase Order': item_details.free_item_data[ 'schedule_date'] = doc.schedule_date if doc else today() if item_details.get("parenttype") == 'Sales Order': item_details.free_item_data[ 'delivery_date'] = doc.delivery_date if doc else today() company = doc.company if args and args.get("company"): company = args.get("company") if args: item_details.free_item_data[ 'income_account'] = get_default_income_account( args=args, item=get_item_defaults(free_item, company), item_group=get_item_group_defaults(free_item, company), brand=get_brand_defaults(free_item, company), )
def _get_basic_details(args, item): """ :param args: { "item_code": "", "warehouse": None, "customer": "", "conversion_rate": 1.0, "selling_price_list": None, "price_list_currency": None, "price_list_uom_dependant": None, "plc_conversion_rate": 1.0, "doctype": "", "name": "", "supplier": None, "transaction_date": None, "conversion_rate": 1.0, "buying_price_list": None, "is_subcontracted": "Yes" / "No", "ignore_pricing_rule": 0/1 "project": "", barcode: "", serial_no: "", currency: "", update_stock: "", price_list: "", company: "", order_type: "", is_pos: "", project: "", qty: "", stock_qty: "", conversion_factor: "" } :param item: `item_code` of Item object :return: frappe._dict """ if not item: item = frappe.get_doc("Item", args.get("item_code")) if item.variant_of: item.update_template_tables() from frappe.defaults import get_user_default_as_list user_default_warehouse_list = get_user_default_as_list("Warehouse") user_default_warehouse = (user_default_warehouse_list[0] if len(user_default_warehouse_list) == 1 else "") item_defaults = get_item_defaults(item.name, args.company) item_group_defaults = get_item_group_defaults(item.name, args.company) warehouse = (args.get("set_warehouse") or user_default_warehouse or item_defaults.get("default_warehouse") or item_group_defaults.get("default_warehouse") or args.warehouse) if not args.get("material_request_type"): args["material_request_type"] = frappe.db.get_value( "Material Request", args.get("name"), "material_request_type", cache=True) # Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master if not args.uom: args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom out = frappe._dict({ "item_code": item.name, "item_name": item.item_name, "description": cstr(item.description).strip(), "image": cstr(item.image).strip(), "warehouse": warehouse, "income_account": get_default_income_account(args, item_defaults, item_group_defaults), "expense_account": get_default_expense_account(args, item_defaults, item_group_defaults), "cost_center": get_default_cost_center(args, item_defaults, item_group_defaults), "has_serial_no": item.has_serial_no, "has_batch_no": item.has_batch_no, "batch_no": None, "item_tax_rate": json.dumps(dict( ([d.tax_type, d.tax_rate] for d in item.get("taxes")))), "uom": args.uom, "min_order_qty": flt(item.min_order_qty), "qty": args.qty or 1.0, "stock_qty": args.qty or 1.0, "price_list_rate": 0.0, "base_price_list_rate": 0.0, "rate": 0.0, "base_rate": 0.0, "amount": 0.0, "base_amount": 0.0, "net_rate": 0.0, "net_amount": 0.0, "discount_percentage": 0.0, "supplier": get_default_supplier(args, item_defaults, item_group_defaults), "update_stock": args.get("update_stock") if args.get("doctype") in ["Sales Invoice", "Purchase Invoice"] else 0, "delivered_by_supplier": item.delivered_by_supplier if args.get("doctype") in ["Sales Order", "Sales Invoice"] else 0, "is_fixed_asset": item.is_fixed_asset, "weight_per_unit": item.weight_per_unit, "weight_uom": item.weight_uom, "last_purchase_rate": item.last_purchase_rate if args.get("doctype") in ["Purchase Order"] else 0, "transaction_date": args.get("transaction_date"), }) if item.get("enable_deferred_revenue") or item.get( "enable_deferred_expense"): out.update(calculate_service_end_date(args, item)) # calculate conversion factor if item.stock_uom == args.uom: out.conversion_factor = 1.0 else: out.conversion_factor = args.conversion_factor or get_conversion_factor( item.name, args.uom).get("conversion_factor") args.conversion_factor = out.conversion_factor out.stock_qty = out.qty * out.conversion_factor # calculate last purchase rate from erpnext.buying.doctype.purchase_order.purchase_order import ( item_last_purchase_rate, ) out.last_purchase_rate = item_last_purchase_rate(args.name, args.conversion_rate, item.name, out.conversion_factor) # if default specified in item is for another company, fetch from company for d in [ ["Account", "income_account", "default_income_account"], ["Account", "expense_account", "default_expense_account"], ["Cost Center", "cost_center", "cost_center"], ["Warehouse", "warehouse", ""], ]: if not out[d[1]]: out[d[1]] = (frappe.get_cached_value("Company", args.company, d[2]) if d[2] else None) for fieldname in ("item_name", "item_group", "barcodes", "brand", "stock_uom"): out[fieldname] = item.get(fieldname) meta = frappe.get_meta(args.child_doctype) if meta.get_field("barcode"): update_barcode_value(out) return out