From 23a2b655764f9311ad0ea87087994cca756ab79c Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Thu, 22 Feb 2018 11:32:09 +0530 Subject: [PATCH] [Fix] BOM Update Tool not update grandparent's exploded BOM (#13026) --- erpnext/manufacturing/doctype/bom/bom.py | 8 +++++++ .../bom_update_tool/bom_update_tool.py | 21 ++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 2e69475c9a..8cafb9105d 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -200,6 +200,14 @@ class BOM(WebsiteGenerator): if not from_child_bom: frappe.msgprint(_("Cost Updated")) + def update_parent_cost(self): + if self.total_cost: + cost = self.total_cost / self.quantity + + frappe.db.sql("""update `tabBOM Item` set rate=%s, amount=stock_qty*%s + where bom_no = %s and docstatus < 2 and parenttype='BOM'""", + (cost, cost, self.name)) + def get_bom_unitcost(self, bom_no): bom = frappe.db.sql("""select name, base_total_cost/quantity as unit_cost from `tabBOM` where is_active = 1 and name = %s""", bom_no, as_dict=1) diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py index 3b6c3a15ef..ec948eb5ed 100644 --- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py +++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py @@ -13,11 +13,14 @@ class BOMUpdateTool(Document): def replace_bom(self): self.validate_bom() self.update_new_bom() - bom_list = self.get_parent_boms() + bom_list = self.get_parent_boms(self.new_bom) updated_bom = [] for bom in bom_list: bom_obj = frappe.get_doc("BOM", bom) updated_bom = bom_obj.update_cost_and_exploded_items(updated_bom) + bom_obj.calculate_cost() + bom_obj.update_parent_cost() + bom_obj.db_update() frappe.msgprint(_("BOM replaced")) @@ -38,10 +41,18 @@ class BOMUpdateTool(Document): rate=%s, amount=stock_qty*%s where bom_no = %s and docstatus < 2 and parenttype='BOM'""", (self.new_bom, new_bom_unitcost, new_bom_unitcost, self.current_bom)) - def get_parent_boms(self): - return [d[0] for d in frappe.db.sql("""select distinct parent - from `tabBOM Item` where ifnull(bom_no, '') = %s and docstatus < 2 and parenttype='BOM'""", - self.new_bom)] + def get_parent_boms(self, bom, bom_list=None): + if not bom_list: + bom_list = [] + + data = frappe.db.sql(""" select distinct parent from `tabBOM Item` + where ifnull(bom_no, '') = %s and docstatus < 2 and parenttype='BOM'""", bom) + + for d in data: + bom_list.append(d[0]) + self.get_parent_boms(d[0], bom_list) + + return bom_list @frappe.whitelist() def enqueue_update_cost():