fix: Replace Product Bundles in the Items table with their child items
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index 5d44582..ff459cb 100755
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -950,8 +950,56 @@
"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`'''