From c37dc7ca33ef87d8d50adcc4b56eb0909eeb8b22 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 24 Aug 2021 21:42:44 +0530 Subject: [PATCH] fix: Update Product Bundle price based on the rates of its child Items --- .../stock/doctype/packed_item/packed_item.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/erpnext/stock/doctype/packed_item/packed_item.py b/erpnext/stock/doctype/packed_item/packed_item.py index 4ab71bdf629..d2c07d28067 100644 --- a/erpnext/stock/doctype/packed_item/packed_item.py +++ b/erpnext/stock/doctype/packed_item/packed_item.py @@ -85,6 +85,7 @@ def make_packing_list(doc): 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 @@ def cleanup_packing_list(doc, parent_items): 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)