From eb24e87637c0e0d3e77b6943239d8c6ef4d8bee5 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 31 Aug 2021 18:44:29 +0530 Subject: [PATCH] fix: Replace Product Bundles in the Items table with their child items --- .../doctype/sales_order/sales_order.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index 5d44582aab..ff459cb13f 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -950,8 +950,56 @@ def make_purchase_order(source_name, selected_items=None, target_doc=None): "condition": lambda doc: doc.ordered_qty < doc.stock_qty and doc.item_code in items_to_map } }, target_doc, set_missing_values) + + doc.items = replace_product_bundles_with_bundle_items(doc.items, source_name) + return doc +def replace_product_bundles_with_bundle_items(items, sales_order_name): + updated_items = [] + + for item in items: + if is_product_bundle(item.item_code): + bundle_items = get_bundle_items(item.item_code, sales_order_name) + insert_bundle_items(updated_items, bundle_items, item, sales_order_name) + else: + updated_items.append(item) + + items = updated_items + + return items + +def is_product_bundle(item_code): + return frappe.db.exists('Product Bundle', item_code) + +def get_bundle_items(item_code, so_name): + return frappe.get_all( + 'Packed Item', + filters = { + 'parent': so_name, + 'parent_item': item_code + }, + fields = ['item_code', 'item_name', 'qty', 'rate', 'uom'] + ) + +def insert_bundle_items(updated_items, bundle_items, item, sales_order): + for bundle_item in bundle_items: + new_item = frappe.get_doc({ + 'doctype': 'Purchase Order Item', + 'item_code': bundle_item.item_code, + 'item_name': bundle_item.item_name, + 'product_bundle': item.item_code, + 'qty': bundle_item.qty, + 'rate': bundle_item.rate, + 'uom': bundle_item.uom, + 'schedule_date': item.schedule_date, + 'sales_order': sales_order, + 'expense_account': item.expense_account, + 'cost_center': item.cost_center + }) + + updated_items.append(new_item) + @frappe.whitelist() def make_work_orders(items, sales_order, company, project=None): '''Make Work Orders against the given Sales Order for the given `items`'''