[Fix] BOM Update Tool not update grandparent's exploded BOM (#13026)

diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index 2e69475..8cafb91 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -200,6 +200,14 @@
 		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 3b6c3a1..ec948eb 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 @@
 	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 @@
 			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():