From d4405c5e5288f3caed530e24c62471af38059571 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Fri, 27 Aug 2021 01:02:34 +0530 Subject: [PATCH] fix: Calculate Product Bundle price based on the prices of its child items --- .../stock/doctype/packed_item/packed_item.py | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/erpnext/stock/doctype/packed_item/packed_item.py b/erpnext/stock/doctype/packed_item/packed_item.py index d8a8959a4d..4f1a67597d 100644 --- a/erpnext/stock/doctype/packed_item/packed_item.py +++ b/erpnext/stock/doctype/packed_item/packed_item.py @@ -114,39 +114,31 @@ def update_product_bundle_price(doc, parent_items): 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 + bundle_item_rate = bundle_item.rate if bundle_item.rate else 0 + 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) + update_parent_item_price(doc, 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) - update_parent_item_rate(parent_items_doctype, parent_item_doc_name, bundle_price) + if doc.get("packed_items"): + update_parent_item_price(doc, parent_items[parent_items_index][0], bundle_price) + +def update_parent_item_price(doc, parent_item_code, bundle_price): + parent_item_doc = doc.get('items', {'item_code': parent_item_code})[0] -def update_parent_item_rate(parent_items_doctype, parent_item_doc_name, bundle_price): - parent_item_qty = frappe.db.get_value(parent_items_doctype, parent_item_doc_name, 'qty') - frappe.db.set_value(parent_items_doctype, parent_item_doc_name, 'rate', (bundle_price/parent_item_qty)) + current_parent_item_price = parent_item_doc.amount + if current_parent_item_price != bundle_price: + parent_item_doc.amount = bundle_price + update_parent_item_rate(parent_item_doc, bundle_price) + +def update_parent_item_rate(parent_item_doc, bundle_price): + parent_item_doc.rate = bundle_price/parent_item_doc.qty @frappe.whitelist() def get_items_from_product_bundle(args):