fix: Update Product Bundle price based on the rates of its child Items
diff --git a/erpnext/stock/doctype/packed_item/packed_item.py b/erpnext/stock/doctype/packed_item/packed_item.py
index 4ab71bd..d2c07d2 100644
--- a/erpnext/stock/doctype/packed_item/packed_item.py
+++ b/erpnext/stock/doctype/packed_item/packed_item.py
@@ -85,6 +85,7 @@
parent_items.append([d.item_code, d.name])
cleanup_packing_list(doc, parent_items)
+ update_product_bundle_price(doc, parent_items)
def cleanup_packing_list(doc, parent_items):
"""Remove all those child items which are no longer present in main item table"""
@@ -103,6 +104,40 @@
if d not in delete_list:
doc.append("packed_items", d)
+def update_product_bundle_price(doc, parent_items):
+ """Updates the prices of Product Bundles based on the rates of the Items in the bundle."""
+
+ parent_items_index = 0
+ bundle_price = 0
+ parent_items_doctype = doc.items[0].doctype
+
+ for bundle_item in doc.get("packed_items"):
+ if parent_items[parent_items_index][0] == bundle_item.parent_item:
+ bundle_price += bundle_item.qty * bundle_item.rate
+ else:
+ update_parent_item_price(doc, parent_items_doctype, parent_items[parent_items_index][0], bundle_price)
+
+ bundle_price = 0
+ parent_items_index += 1
+
+ # for the last product bundle
+ update_parent_item_price(doc, parent_items_doctype, parent_items[parent_items_index][0], bundle_price)
+ doc.reload()
+
+def update_parent_item_price(doc, parent_items_doctype, parent_item_code, bundle_price):
+ parent_item_doc_name = frappe.db.get_value(
+ parent_items_doctype,
+ {
+ 'parent': doc.name,
+ 'item_code': parent_item_code
+ },
+ 'name'
+ )
+
+ current_parent_item_price = frappe.db.get_value(parent_items_doctype, parent_item_doc_name, 'amount')
+ if current_parent_item_price != bundle_price:
+ frappe.db.set_value(parent_items_doctype, parent_item_doc_name, 'amount', bundle_price)
+
@frappe.whitelist()
def get_items_from_product_bundle(args):
args = json.loads(args)