Merge pull request #30502 from dj12djdjs/fix-pe-ex-rate-precision

fix(accounts): payment entry exchange rate precision
diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py
index 150f68b..c71ea36 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -204,7 +204,9 @@
 		if not self.account_currency:
 			self.account_currency = frappe.get_cached_value("Company", self.company, "default_currency")
 
-		elif self.account_currency != frappe.db.get_value("Account", self.name, "account_currency"):
+		gl_currency = frappe.db.get_value("GL Entry", {"account": self.name}, "account_currency")
+
+		if gl_currency and self.account_currency != gl_currency:
 			if frappe.db.get_value("GL Entry", {"account": self.name}):
 				frappe.throw(_("Currency can not be changed after making entries using some other currency"))
 
diff --git a/erpnext/accounts/doctype/account/test_account.py b/erpnext/accounts/doctype/account/test_account.py
index efc063d..f9c9173 100644
--- a/erpnext/accounts/doctype/account/test_account.py
+++ b/erpnext/accounts/doctype/account/test_account.py
@@ -241,6 +241,28 @@
 		for doc in to_delete:
 			frappe.delete_doc("Account", doc)
 
+	def test_validate_account_currency(self):
+		from erpnext.accounts.doctype.journal_entry.test_journal_entry import make_journal_entry
+
+		if not frappe.db.get_value("Account", "Test Currency Account - _TC"):
+			acc = frappe.new_doc("Account")
+			acc.account_name = "Test Currency Account"
+			acc.parent_account = "Tax Assets - _TC"
+			acc.company = "_Test Company"
+			acc.insert()
+		else:
+			acc = frappe.get_doc("Account", "Test Currency Account - _TC")
+
+		self.assertEqual(acc.account_currency, "INR")
+
+		# Make a JV against this account
+		make_journal_entry(
+			"Test Currency Account - _TC", "Miscellaneous Expenses - _TC", 100, submit=True
+		)
+
+		acc.account_currency = "USD"
+		self.assertRaises(frappe.ValidationError, acc.save)
+
 
 def _make_test_records(verbose=None):
 	from frappe.test_runner import make_test_objects
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
index 1a398ab..5f6e610 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -276,6 +276,8 @@
 		if(this.frm.updating_party_details || this.frm.doc.inter_company_invoice_reference)
 			return;
 
+		if (this.frm.doc.__onload && this.frm.doc.__onload.load_after_mapping) return;
+
 		erpnext.utils.get_party_details(this.frm, "erpnext.accounts.party.get_party_details",
 			{
 				posting_date: this.frm.doc.posting_date,
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 57bc0a7..e6a46d0 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -249,8 +249,9 @@
 
 	def validate_warehouse(self, for_validate=True):
 		if self.update_stock and for_validate:
+			stock_items = self.get_stock_items()
 			for d in self.get("items"):
-				if not d.warehouse:
+				if not d.warehouse and d.item_code in stock_items:
 					frappe.throw(
 						_(
 							"Row No {0}: Warehouse is required. Please set a Default Warehouse for Item {1} and Company {2}"
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index af6a52a..6818955 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -280,6 +280,9 @@
 		}
 		var me = this;
 		if(this.frm.updating_party_details) return;
+
+		if (this.frm.doc.__onload && this.frm.doc.__onload.load_after_mapping) return;
+
 		erpnext.utils.get_party_details(this.frm,
 			"erpnext.accounts.party.get_party_details", {
 				posting_date: this.frm.doc.posting_date,
diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py
index f681b34..e759ad0 100644
--- a/erpnext/assets/doctype/asset/test_asset.py
+++ b/erpnext/assets/doctype/asset/test_asset.py
@@ -68,7 +68,7 @@
 	def test_item_exists(self):
 		asset = create_asset(item_code="MacBook", do_not_save=1)
 
-		self.assertRaises(frappe.DoesNotExistError, asset.save)
+		self.assertRaises(frappe.ValidationError, asset.save)
 
 	def test_validate_item(self):
 		asset = create_asset(item_code="MacBook Pro", do_not_save=1)
diff --git a/erpnext/buying/utils.py b/erpnext/buying/utils.py
index f97cd5e..e904af0 100644
--- a/erpnext/buying/utils.py
+++ b/erpnext/buying/utils.py
@@ -3,19 +3,19 @@
 
 
 import json
+from typing import Dict
 
 import frappe
 from frappe import _
-from frappe.utils import cint, cstr, flt
+from frappe.utils import cint, cstr, flt, getdate
 
 from erpnext.stock.doctype.item.item import get_last_purchase_details, validate_end_of_life
 
 
-def update_last_purchase_rate(doc, is_submit):
+def update_last_purchase_rate(doc, is_submit) -> None:
 	"""updates last_purchase_rate in item table for each item"""
-	import frappe.utils
 
-	this_purchase_date = frappe.utils.getdate(doc.get("posting_date") or doc.get("transaction_date"))
+	this_purchase_date = getdate(doc.get("posting_date") or doc.get("transaction_date"))
 
 	for d in doc.get("items"):
 		# get last purchase details
@@ -41,7 +41,7 @@
 		frappe.db.set_value("Item", d.item_code, "last_purchase_rate", flt(last_purchase_rate))
 
 
-def validate_for_items(doc):
+def validate_for_items(doc) -> None:
 	items = []
 	for d in doc.get("items"):
 		if not d.qty:
@@ -49,40 +49,11 @@
 				continue
 			frappe.throw(_("Please enter quantity for Item {0}").format(d.item_code))
 
-		# update with latest quantities
-		bin = frappe.db.sql(
-			"""select projected_qty from `tabBin` where
-			item_code = %s and warehouse = %s""",
-			(d.item_code, d.warehouse),
-			as_dict=1,
-		)
-
-		f_lst = {
-			"projected_qty": bin and flt(bin[0]["projected_qty"]) or 0,
-			"ordered_qty": 0,
-			"received_qty": 0,
-		}
-		if d.doctype in ("Purchase Receipt Item", "Purchase Invoice Item"):
-			f_lst.pop("received_qty")
-		for x in f_lst:
-			if d.meta.get_field(x):
-				d.set(x, f_lst[x])
-
-		item = frappe.db.sql(
-			"""select is_stock_item,
-			is_sub_contracted_item, end_of_life, disabled from `tabItem` where name=%s""",
-			d.item_code,
-			as_dict=1,
-		)[0]
-
+		set_stock_levels(row=d)  # update with latest quantities
+		item = validate_item_and_get_basic_data(row=d)
+		validate_stock_item_warehouse(row=d, item=item)
 		validate_end_of_life(d.item_code, item.end_of_life, item.disabled)
 
-		# validate stock item
-		if item.is_stock_item == 1 and d.qty and not d.warehouse and not d.get("delivered_by_supplier"):
-			frappe.throw(
-				_("Warehouse is mandatory for stock Item {0} in row {1}").format(d.item_code, d.idx)
-			)
-
 		items.append(cstr(d.item_code))
 
 	if (
@@ -93,7 +64,57 @@
 		frappe.throw(_("Same item cannot be entered multiple times."))
 
 
-def check_on_hold_or_closed_status(doctype, docname):
+def set_stock_levels(row) -> None:
+	projected_qty = frappe.db.get_value(
+		"Bin",
+		{
+			"item_code": row.item_code,
+			"warehouse": row.warehouse,
+		},
+		"projected_qty",
+	)
+
+	qty_data = {
+		"projected_qty": flt(projected_qty),
+		"ordered_qty": 0,
+		"received_qty": 0,
+	}
+	if row.doctype in ("Purchase Receipt Item", "Purchase Invoice Item"):
+		qty_data.pop("received_qty")
+
+	for field in qty_data:
+		if row.meta.get_field(field):
+			row.set(field, qty_data[field])
+
+
+def validate_item_and_get_basic_data(row) -> Dict:
+	item = frappe.db.get_values(
+		"Item",
+		filters={"name": row.item_code},
+		fieldname=["is_stock_item", "is_sub_contracted_item", "end_of_life", "disabled"],
+		as_dict=1,
+	)
+	if not item:
+		frappe.throw(_("Row #{0}: Item {1} does not exist").format(row.idx, frappe.bold(row.item_code)))
+
+	return item[0]
+
+
+def validate_stock_item_warehouse(row, item) -> None:
+	if (
+		item.is_stock_item == 1
+		and row.qty
+		and not row.warehouse
+		and not row.get("delivered_by_supplier")
+	):
+		frappe.throw(
+			_("Row #{1}: Warehouse is mandatory for stock Item {0}").format(
+				frappe.bold(row.item_code), row.idx
+			)
+		)
+
+
+def check_on_hold_or_closed_status(doctype, docname) -> None:
 	status = frappe.db.get_value(doctype, docname, "status")
 
 	if status in ("Closed", "On Hold"):
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 72ac1b3..3a20d3f 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -1267,17 +1267,9 @@
 		stock_items = []
 		item_codes = list(set(item.item_code for item in self.get("items")))
 		if item_codes:
-			stock_items = [
-				r[0]
-				for r in frappe.db.sql(
-					"""
-				select name from `tabItem`
-				where name in (%s) and is_stock_item=1
-			"""
-					% (", ".join(["%s"] * len(item_codes)),),
-					item_codes,
-				)
-			]
+			stock_items = frappe.db.get_values(
+				"Item", {"name": ["in", item_codes], "is_stock_item": 1}, pluck="name", cache=True
+			)
 
 		return stock_items
 
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index a2b1c41..1c009d3 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -469,7 +469,7 @@
 	],
 	"daily_long": [
 		"erpnext.setup.doctype.email_digest.email_digest.send",
-		"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.update_latest_price_in_all_boms",
+		"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.auto_update_latest_price_in_all_boms",
 		"erpnext.hr.doctype.leave_ledger_entry.leave_ledger_entry.process_expired_allocation",
 		"erpnext.hr.utils.generate_leave_encashment",
 		"erpnext.hr.utils.allocate_earned_leaves",
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index 9b6cf46..fefb2e5 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -697,15 +697,6 @@
 		self.scrap_material_cost = total_sm_cost
 		self.base_scrap_material_cost = base_total_sm_cost
 
-	def update_new_bom(self, old_bom, new_bom, rate):
-		for d in self.get("items"):
-			if d.bom_no != old_bom:
-				continue
-
-			d.bom_no = new_bom
-			d.rate = rate
-			d.amount = (d.stock_qty or d.qty) * rate
-
 	def update_exploded_items(self, save=True):
 		"""Update Flat BOM, following will be correct data"""
 		self.get_exploded_items()
diff --git a/erpnext/manufacturing/doctype/bom_update_log/__init__.py b/erpnext/manufacturing/doctype/bom_update_log/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/__init__.py
diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.js b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.js
new file mode 100644
index 0000000..6da808e
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.js
@@ -0,0 +1,8 @@
+// Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
+// For license information, please see license.txt
+
+frappe.ui.form.on('BOM Update Log', {
+	// refresh: function(frm) {
+
+	// }
+});
diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json
new file mode 100644
index 0000000..98c1acb
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json
@@ -0,0 +1,109 @@
+{
+ "actions": [],
+ "autoname": "BOM-UPDT-LOG-.#####",
+ "creation": "2022-03-16 14:23:35.210155",
+ "description": "BOM Update Tool Log with job status maintained",
+ "doctype": "DocType",
+ "editable_grid": 1,
+ "engine": "InnoDB",
+ "field_order": [
+  "current_bom",
+  "new_bom",
+  "column_break_3",
+  "update_type",
+  "status",
+  "error_log",
+  "amended_from"
+ ],
+ "fields": [
+  {
+   "fieldname": "current_bom",
+   "fieldtype": "Link",
+   "in_list_view": 1,
+   "in_standard_filter": 1,
+   "label": "Current BOM",
+   "options": "BOM"
+  },
+  {
+   "fieldname": "new_bom",
+   "fieldtype": "Link",
+   "in_list_view": 1,
+   "in_standard_filter": 1,
+   "label": "New BOM",
+   "options": "BOM"
+  },
+  {
+   "fieldname": "column_break_3",
+   "fieldtype": "Column Break"
+  },
+  {
+   "fieldname": "update_type",
+   "fieldtype": "Select",
+   "in_list_view": 1,
+   "label": "Update Type",
+   "options": "Replace BOM\nUpdate Cost"
+  },
+  {
+   "fieldname": "status",
+   "fieldtype": "Select",
+   "label": "Status",
+   "options": "Queued\nIn Progress\nCompleted\nFailed"
+  },
+  {
+   "fieldname": "amended_from",
+   "fieldtype": "Link",
+   "label": "Amended From",
+   "no_copy": 1,
+   "options": "BOM Update Log",
+   "print_hide": 1,
+   "read_only": 1
+  },
+  {
+   "fieldname": "error_log",
+   "fieldtype": "Link",
+   "label": "Error Log",
+   "options": "Error Log"
+  }
+ ],
+ "in_create": 1,
+ "index_web_pages_for_search": 1,
+ "is_submittable": 1,
+ "links": [],
+ "modified": "2022-03-31 12:51:44.885102",
+ "modified_by": "Administrator",
+ "module": "Manufacturing",
+ "name": "BOM Update Log",
+ "naming_rule": "Expression (old style)",
+ "owner": "Administrator",
+ "permissions": [
+  {
+   "create": 1,
+   "delete": 1,
+   "email": 1,
+   "export": 1,
+   "print": 1,
+   "read": 1,
+   "report": 1,
+   "role": "System Manager",
+   "share": 1,
+   "submit": 1,
+   "write": 1
+  },
+  {
+   "create": 1,
+   "email": 1,
+   "export": 1,
+   "print": 1,
+   "read": 1,
+   "report": 1,
+   "role": "Manufacturing Manager",
+   "share": 1,
+   "submit": 1,
+   "write": 1
+  }
+ ],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "states": [],
+ "track_changes": 1
+}
\ No newline at end of file
diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py
new file mode 100644
index 0000000..139dcbc
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py
@@ -0,0 +1,164 @@
+# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+from typing import Dict, List, Literal, Optional
+
+import frappe
+from frappe import _
+from frappe.model.document import Document
+from frappe.utils import cstr, flt
+
+from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import update_cost
+
+
+class BOMMissingError(frappe.ValidationError):
+	pass
+
+
+class BOMUpdateLog(Document):
+	def validate(self):
+		if self.update_type == "Replace BOM":
+			self.validate_boms_are_specified()
+			self.validate_same_bom()
+			self.validate_bom_items()
+
+		self.status = "Queued"
+
+	def validate_boms_are_specified(self):
+		if self.update_type == "Replace BOM" and not (self.current_bom and self.new_bom):
+			frappe.throw(
+				msg=_("Please mention the Current and New BOM for replacement."),
+				title=_("Mandatory"),
+				exc=BOMMissingError,
+			)
+
+	def validate_same_bom(self):
+		if cstr(self.current_bom) == cstr(self.new_bom):
+			frappe.throw(_("Current BOM and New BOM can not be same"))
+
+	def validate_bom_items(self):
+		current_bom_item = frappe.db.get_value("BOM", self.current_bom, "item")
+		new_bom_item = frappe.db.get_value("BOM", self.new_bom, "item")
+
+		if current_bom_item != new_bom_item:
+			frappe.throw(_("The selected BOMs are not for the same item"))
+
+	def on_submit(self):
+		if frappe.flags.in_test:
+			return
+
+		if self.update_type == "Replace BOM":
+			boms = {"current_bom": self.current_bom, "new_bom": self.new_bom}
+			frappe.enqueue(
+				method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.run_bom_job",
+				doc=self,
+				boms=boms,
+				timeout=40000,
+			)
+		else:
+			frappe.enqueue(
+				method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.run_bom_job",
+				doc=self,
+				update_type="Update Cost",
+				timeout=40000,
+			)
+
+
+def replace_bom(boms: Dict) -> None:
+	"""Replace current BOM with new BOM in parent BOMs."""
+	current_bom = boms.get("current_bom")
+	new_bom = boms.get("new_bom")
+
+	unit_cost = get_new_bom_unit_cost(new_bom)
+	update_new_bom_in_bom_items(unit_cost, current_bom, new_bom)
+
+	frappe.cache().delete_key("bom_children")
+	parent_boms = get_parent_boms(new_bom)
+
+	for bom in parent_boms:
+		bom_obj = frappe.get_doc("BOM", bom)
+		# this is only used for versioning and we do not want
+		# to make separate db calls by using load_doc_before_save
+		# which proves to be expensive while doing bulk replace
+		bom_obj._doc_before_save = bom_obj
+		bom_obj.update_exploded_items()
+		bom_obj.calculate_cost()
+		bom_obj.update_parent_cost()
+		bom_obj.db_update()
+		if bom_obj.meta.get("track_changes") and not bom_obj.flags.ignore_version:
+			bom_obj.save_version()
+
+
+def update_new_bom_in_bom_items(unit_cost: float, current_bom: str, new_bom: str) -> None:
+	bom_item = frappe.qb.DocType("BOM Item")
+	(
+		frappe.qb.update(bom_item)
+		.set(bom_item.bom_no, new_bom)
+		.set(bom_item.rate, unit_cost)
+		.set(bom_item.amount, (bom_item.stock_qty * unit_cost))
+		.where(
+			(bom_item.bom_no == current_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM")
+		)
+	).run()
+
+
+def get_parent_boms(new_bom: str, bom_list: Optional[List] = None) -> List:
+	bom_list = bom_list or []
+	bom_item = frappe.qb.DocType("BOM Item")
+
+	parents = (
+		frappe.qb.from_(bom_item)
+		.select(bom_item.parent)
+		.where((bom_item.bom_no == new_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM"))
+		.run(as_dict=True)
+	)
+
+	for d in parents:
+		if new_bom == d.parent:
+			frappe.throw(_("BOM recursion: {0} cannot be child of {1}").format(new_bom, d.parent))
+
+		bom_list.append(d.parent)
+		get_parent_boms(d.parent, bom_list)
+
+	return list(set(bom_list))
+
+
+def get_new_bom_unit_cost(new_bom: str) -> float:
+	bom = frappe.qb.DocType("BOM")
+	new_bom_unitcost = (
+		frappe.qb.from_(bom).select(bom.total_cost / bom.quantity).where(bom.name == new_bom).run()
+	)
+
+	return flt(new_bom_unitcost[0][0])
+
+
+def run_bom_job(
+	doc: "BOMUpdateLog",
+	boms: Optional[Dict[str, str]] = None,
+	update_type: Literal["Replace BOM", "Update Cost"] = "Replace BOM",
+) -> None:
+	try:
+		doc.db_set("status", "In Progress")
+		if not frappe.flags.in_test:
+			frappe.db.commit()
+
+		frappe.db.auto_commit_on_many_writes = 1
+
+		boms = frappe._dict(boms or {})
+
+		if update_type == "Replace BOM":
+			replace_bom(boms)
+		else:
+			update_cost()
+
+		doc.db_set("status", "Completed")
+
+	except Exception:
+		frappe.db.rollback()
+		error_log = frappe.log_error(message=frappe.get_traceback(), title=_("BOM Update Tool Error"))
+
+		doc.db_set("status", "Failed")
+		doc.db_set("error_log", error_log.name)
+
+	finally:
+		frappe.db.auto_commit_on_many_writes = 0
+		frappe.db.commit()  # nosemgrep
diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log_list.js b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log_list.js
new file mode 100644
index 0000000..e39b563
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log_list.js
@@ -0,0 +1,13 @@
+frappe.listview_settings['BOM Update Log'] = {
+	add_fields: ["status"],
+	get_indicator: function(doc) {
+		let status_map = {
+			"Queued": "orange",
+			"In Progress": "blue",
+			"Completed": "green",
+			"Failed": "red"
+		};
+
+		return [__(doc.status), status_map[doc.status], "status,=," + doc.status];
+	}
+};
\ No newline at end of file
diff --git a/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py
new file mode 100644
index 0000000..47efea9
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py
@@ -0,0 +1,96 @@
+# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
+# See license.txt
+
+import frappe
+from frappe.tests.utils import FrappeTestCase
+
+from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import (
+	BOMMissingError,
+	run_bom_job,
+)
+from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import enqueue_replace_bom
+
+test_records = frappe.get_test_records("BOM")
+
+
+class TestBOMUpdateLog(FrappeTestCase):
+	"Test BOM Update Tool Operations via BOM Update Log."
+
+	def setUp(self):
+		bom_doc = frappe.copy_doc(test_records[0])
+		bom_doc.items[1].item_code = "_Test Item"
+		bom_doc.insert()
+
+		self.boms = frappe._dict(
+			current_bom="BOM-_Test Item Home Desktop Manufactured-001",
+			new_bom=bom_doc.name,
+		)
+
+		self.new_bom_doc = bom_doc
+
+	def tearDown(self):
+		frappe.db.rollback()
+
+		if self._testMethodName == "test_bom_update_log_completion":
+			# clear logs and delete BOM created via setUp
+			frappe.db.delete("BOM Update Log")
+			self.new_bom_doc.cancel()
+			self.new_bom_doc.delete()
+
+			# explicitly commit and restore to original state
+			frappe.db.commit()  # nosemgrep
+
+	def test_bom_update_log_validate(self):
+		"Test if BOM presence is validated."
+
+		with self.assertRaises(BOMMissingError):
+			enqueue_replace_bom(boms={})
+
+		with self.assertRaises(frappe.ValidationError):
+			enqueue_replace_bom(boms=frappe._dict(current_bom=self.boms.new_bom, new_bom=self.boms.new_bom))
+
+		with self.assertRaises(frappe.ValidationError):
+			enqueue_replace_bom(boms=frappe._dict(current_bom=self.boms.new_bom, new_bom="Dummy BOM"))
+
+	def test_bom_update_log_queueing(self):
+		"Test if BOM Update Log is created and queued."
+
+		log = enqueue_replace_bom(
+			boms=self.boms,
+		)
+
+		self.assertEqual(log.docstatus, 1)
+		self.assertEqual(log.status, "Queued")
+
+	def test_bom_update_log_completion(self):
+		"Test if BOM Update Log handles job completion correctly."
+
+		log = enqueue_replace_bom(
+			boms=self.boms,
+		)
+
+		# Explicitly commits log, new bom (setUp) and replacement impact.
+		# Is run via background jobs IRL
+		run_bom_job(
+			doc=log,
+			boms=self.boms,
+			update_type="Replace BOM",
+		)
+		log.reload()
+
+		self.assertEqual(log.status, "Completed")
+
+		# teardown (undo replace impact) due to commit
+		boms = frappe._dict(
+			current_bom=self.boms.new_bom,
+			new_bom=self.boms.current_bom,
+		)
+		log2 = enqueue_replace_bom(
+			boms=self.boms,
+		)
+		run_bom_job(  # Explicitly commits
+			doc=log2,
+			boms=boms,
+			update_type="Replace BOM",
+		)
+		self.assertEqual(log2.status, "Completed")
diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.js b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.js
index bf5fe2e..7ba6517 100644
--- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.js
+++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.js
@@ -20,30 +20,67 @@
 
 	refresh: function(frm) {
 		frm.disable_save();
+		frm.events.disable_button(frm, "replace");
+
+		frm.add_custom_button(__("View BOM Update Log"), () => {
+			frappe.set_route("List", "BOM Update Log");
+		});
 	},
 
-	replace: function(frm) {
+	disable_button: (frm, field, disable=true) => {
+		frm.get_field(field).input.disabled = disable;
+	},
+
+	current_bom: (frm) => {
+		if (frm.doc.current_bom && frm.doc.new_bom) {
+			frm.events.disable_button(frm, "replace", false);
+		}
+	},
+
+	new_bom: (frm) => {
+		if (frm.doc.current_bom && frm.doc.new_bom) {
+			frm.events.disable_button(frm, "replace", false);
+		}
+	},
+
+	replace: (frm) => {
 		if (frm.doc.current_bom && frm.doc.new_bom) {
 			frappe.call({
 				method: "erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.enqueue_replace_bom",
 				freeze: true,
 				args: {
-					args: {
+					boms: {
 						"current_bom": frm.doc.current_bom,
 						"new_bom": frm.doc.new_bom
 					}
+				},
+				callback: result => {
+					if (result && result.message && !result.exc) {
+						frm.events.confirm_job_start(frm, result.message);
+					}
 				}
 			});
 		}
 	},
 
-	update_latest_price_in_all_boms: function() {
+	update_latest_price_in_all_boms: (frm) => {
 		frappe.call({
 			method: "erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.enqueue_update_cost",
 			freeze: true,
-			callback: function() {
-				frappe.msgprint(__("Latest price updated in all BOMs"));
+			callback: result => {
+				if (result && result.message && !result.exc) {
+					frm.events.confirm_job_start(frm, result.message);
+				}
 			}
 		});
+	},
+
+	confirm_job_start: (frm, log_data) => {
+		let log_link = frappe.utils.get_form_link("BOM Update Log", log_data.name, true);
+		frappe.msgprint({
+			"message": __("BOM Updation is queued and may take a few minutes. Check {0} for progress.", [log_link]),
+			"title": __("BOM Update Initiated"),
+			"indicator": "blue"
+		});
 	}
 });
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 00711ca..b0e7da1 100644
--- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
+++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
@@ -1,136 +1,69 @@
-# Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors
+# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
 # For license information, please see license.txt
 
-
 import json
+from typing import TYPE_CHECKING, Dict, Literal, Optional, Union
 
-import click
+if TYPE_CHECKING:
+	from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import BOMUpdateLog
+
 import frappe
-from frappe import _
 from frappe.model.document import Document
-from frappe.utils import cstr, flt
 
 from erpnext.manufacturing.doctype.bom.bom import get_boms_in_bottom_up_order
 
 
 class BOMUpdateTool(Document):
-	def replace_bom(self):
-		self.validate_bom()
-
-		unit_cost = get_new_bom_unit_cost(self.new_bom)
-		self.update_new_bom(unit_cost)
-
-		frappe.cache().delete_key("bom_children")
-		bom_list = self.get_parent_boms(self.new_bom)
-
-		with click.progressbar(bom_list) as bom_list:
-			pass
-		for bom in bom_list:
-			try:
-				bom_obj = frappe.get_cached_doc("BOM", bom)
-				# this is only used for versioning and we do not want
-				# to make separate db calls by using load_doc_before_save
-				# which proves to be expensive while doing bulk replace
-				bom_obj._doc_before_save = bom_obj
-				bom_obj.update_new_bom(self.current_bom, self.new_bom, unit_cost)
-				bom_obj.update_exploded_items()
-				bom_obj.calculate_cost()
-				bom_obj.update_parent_cost()
-				bom_obj.db_update()
-				if bom_obj.meta.get("track_changes") and not bom_obj.flags.ignore_version:
-					bom_obj.save_version()
-			except Exception:
-				frappe.log_error(frappe.get_traceback())
-
-	def validate_bom(self):
-		if cstr(self.current_bom) == cstr(self.new_bom):
-			frappe.throw(_("Current BOM and New BOM can not be same"))
-
-		if frappe.db.get_value("BOM", self.current_bom, "item") != frappe.db.get_value(
-			"BOM", self.new_bom, "item"
-		):
-			frappe.throw(_("The selected BOMs are not for the same item"))
-
-	def update_new_bom(self, unit_cost):
-		frappe.db.sql(
-			"""update `tabBOM Item` set bom_no=%s,
-			rate=%s, amount=stock_qty*%s where bom_no = %s and docstatus < 2 and parenttype='BOM'""",
-			(self.new_bom, unit_cost, unit_cost, self.current_bom),
-		)
-
-	def get_parent_boms(self, bom, bom_list=None):
-		if bom_list is None:
-			bom_list = []
-		data = frappe.db.sql(
-			"""SELECT DISTINCT parent FROM `tabBOM Item`
-			WHERE bom_no = %s AND docstatus < 2 AND parenttype='BOM'""",
-			bom,
-		)
-
-		for d in data:
-			if self.new_bom == d[0]:
-				frappe.throw(_("BOM recursion: {0} cannot be child of {1}").format(bom, self.new_bom))
-
-			bom_list.append(d[0])
-			self.get_parent_boms(d[0], bom_list)
-
-		return list(set(bom_list))
-
-
-def get_new_bom_unit_cost(bom):
-	new_bom_unitcost = frappe.db.sql(
-		"""SELECT `total_cost`/`quantity`
-		FROM `tabBOM` WHERE name = %s""",
-		bom,
-	)
-
-	return flt(new_bom_unitcost[0][0]) if new_bom_unitcost else 0
+	pass
 
 
 @frappe.whitelist()
-def enqueue_replace_bom(args):
-	if isinstance(args, str):
-		args = json.loads(args)
+def enqueue_replace_bom(
+	boms: Optional[Union[Dict, str]] = None, args: Optional[Union[Dict, str]] = None
+) -> "BOMUpdateLog":
+	"""Returns a BOM Update Log (that queues a job) for BOM Replacement."""
+	boms = boms or args
+	if isinstance(boms, str):
+		boms = json.loads(boms)
 
-	frappe.enqueue(
-		"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.replace_bom",
-		args=args,
-		timeout=40000,
-	)
-	frappe.msgprint(_("Queued for replacing the BOM. It may take a few minutes."))
+	update_log = create_bom_update_log(boms=boms)
+	return update_log
 
 
 @frappe.whitelist()
-def enqueue_update_cost():
-	frappe.enqueue(
-		"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.update_cost", timeout=40000
-	)
-	frappe.msgprint(
-		_("Queued for updating latest price in all Bill of Materials. It may take a few minutes.")
-	)
+def enqueue_update_cost() -> "BOMUpdateLog":
+	"""Returns a BOM Update Log (that queues a job) for BOM Cost Updation."""
+	update_log = create_bom_update_log(update_type="Update Cost")
+	return update_log
 
 
-def update_latest_price_in_all_boms():
+def auto_update_latest_price_in_all_boms() -> None:
+	"""Called via hooks.py."""
 	if frappe.db.get_single_value("Manufacturing Settings", "update_bom_costs_automatically"):
 		update_cost()
 
 
-def replace_bom(args):
-	frappe.db.auto_commit_on_many_writes = 1
-	args = frappe._dict(args)
-
-	doc = frappe.get_doc("BOM Update Tool")
-	doc.current_bom = args.current_bom
-	doc.new_bom = args.new_bom
-	doc.replace_bom()
-
-	frappe.db.auto_commit_on_many_writes = 0
-
-
-def update_cost():
-	frappe.db.auto_commit_on_many_writes = 1
+def update_cost() -> None:
+	"""Updates Cost for all BOMs from bottom to top."""
 	bom_list = get_boms_in_bottom_up_order()
 	for bom in bom_list:
 		frappe.get_doc("BOM", bom).update_cost(update_parent=False, from_child_bom=True)
 
-	frappe.db.auto_commit_on_many_writes = 0
+
+def create_bom_update_log(
+	boms: Optional[Dict[str, str]] = None,
+	update_type: Literal["Replace BOM", "Update Cost"] = "Replace BOM",
+) -> "BOMUpdateLog":
+	"""Creates a BOM Update Log that handles the background job."""
+
+	boms = boms or {}
+	current_bom = boms.get("current_bom")
+	new_bom = boms.get("new_bom")
+	return frappe.get_doc(
+		{
+			"doctype": "BOM Update Log",
+			"current_bom": current_bom,
+			"new_bom": new_bom,
+			"update_type": update_type,
+		}
+	).submit()
diff --git a/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py
index 57785e5..fae72a0 100644
--- a/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py
+++ b/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py
@@ -4,6 +4,7 @@
 import frappe
 from frappe.tests.utils import FrappeTestCase
 
+from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import replace_bom
 from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import update_cost
 from erpnext.manufacturing.doctype.production_plan.test_production_plan import make_bom
 from erpnext.stock.doctype.item.test_item import create_item
@@ -12,6 +13,8 @@
 
 
 class TestBOMUpdateTool(FrappeTestCase):
+	"Test major functions run via BOM Update Tool."
+
 	def test_replace_bom(self):
 		current_bom = "BOM-_Test Item Home Desktop Manufactured-001"
 
@@ -19,18 +22,16 @@
 		bom_doc.items[1].item_code = "_Test Item"
 		bom_doc.insert()
 
-		update_tool = frappe.get_doc("BOM Update Tool")
-		update_tool.current_bom = current_bom
-		update_tool.new_bom = bom_doc.name
-		update_tool.replace_bom()
+		boms = frappe._dict(current_bom=current_bom, new_bom=bom_doc.name)
+		replace_bom(boms)
 
 		self.assertFalse(frappe.db.sql("select name from `tabBOM Item` where bom_no=%s", current_bom))
 		self.assertTrue(frappe.db.sql("select name from `tabBOM Item` where bom_no=%s", bom_doc.name))
 
 		# reverse, as it affects other testcases
-		update_tool.current_bom = bom_doc.name
-		update_tool.new_bom = current_bom
-		update_tool.replace_bom()
+		boms.current_bom = bom_doc.name
+		boms.new_bom = current_bom
+		replace_bom(boms)
 
 	def test_bom_cost(self):
 		for item in ["BOM Cost Test Item 1", "BOM Cost Test Item 2", "BOM Cost Test Item 3"]:
diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py
index 47e6ae6..48e1751 100644
--- a/erpnext/regional/india/utils.py
+++ b/erpnext/regional/india/utils.py
@@ -268,6 +268,7 @@
 
 	if tax_template_by_category:
 		party_details["taxes_and_charges"] = tax_template_by_category
+		party_details["taxes"] = get_taxes_and_charges(master_doctype, tax_template_by_category)
 		return party_details
 
 	if not party_details.place_of_supply:
@@ -292,7 +293,7 @@
 		return party_details
 
 	party_details["taxes_and_charges"] = default_tax
-	party_details.taxes = get_taxes_and_charges(master_doctype, default_tax)
+	party_details["taxes"] = get_taxes_and_charges(master_doctype, default_tax)
 
 	return party_details
 
diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
index f1f5d96..e2eb2a4 100644
--- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
+++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
@@ -74,6 +74,7 @@
   "against_sales_invoice",
   "si_detail",
   "dn_detail",
+  "pick_list_item",
   "section_break_40",
   "batch_no",
   "serial_no",
@@ -762,13 +763,22 @@
    "fieldtype": "Check",
    "label": "Grant Commission",
    "read_only": 1
+  },
+  {
+   "fieldname": "pick_list_item",
+   "fieldtype": "Data",
+   "hidden": 1,
+   "label": "Pick List Item",
+   "no_copy": 1,
+   "print_hide": 1,
+   "read_only": 1
   }
  ],
  "idx": 1,
  "index_web_pages_for_search": 1,
  "istable": 1,
  "links": [],
- "modified": "2022-02-24 14:42:20.211085",
+ "modified": "2022-03-31 18:36:24.671913",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Delivery Note Item",
diff --git a/erpnext/stock/doctype/pick_list/pick_list.py b/erpnext/stock/doctype/pick_list/pick_list.py
index 7061ee1..d3476a8 100644
--- a/erpnext/stock/doctype/pick_list/pick_list.py
+++ b/erpnext/stock/doctype/pick_list/pick_list.py
@@ -534,6 +534,7 @@
 			dn_item = map_child_doc(source_doc, delivery_note, table_mapper)
 
 			if dn_item:
+				dn_item.pick_list_item = location.name
 				dn_item.warehouse = location.warehouse
 				dn_item.qty = flt(location.picked_qty) / (flt(location.conversion_factor) or 1)
 				dn_item.batch_no = location.batch_no
diff --git a/erpnext/stock/doctype/pick_list/test_pick_list.py b/erpnext/stock/doctype/pick_list/test_pick_list.py
index 7496b6b..ec5011b 100644
--- a/erpnext/stock/doctype/pick_list/test_pick_list.py
+++ b/erpnext/stock/doctype/pick_list/test_pick_list.py
@@ -521,6 +521,8 @@
 			for dn_item in frappe.get_doc("Delivery Note", dn.name).get("items"):
 				self.assertEqual(dn_item.item_code, "_Test Item")
 				self.assertEqual(dn_item.against_sales_order, sales_order_1.name)
+				self.assertEqual(dn_item.pick_list_item, pick_list.locations[dn_item.idx - 1].name)
+
 		for dn in frappe.get_all(
 			"Delivery Note",
 			filters={"pick_list": pick_list.name, "customer": "_Test Customer 1"},