Merge pull request #35700 from s-aga-r/FIX-SR-DIFF-QTY

fix: update `Stock Reconciliation` diff qty while reposting
diff --git a/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.js b/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.js
index 733a761..f51b90d 100644
--- a/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.js
+++ b/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.js
@@ -36,7 +36,7 @@
 	},
 
 	validate_rounding_loss: function(frm) {
-		allowance = frm.doc.rounding_loss_allowance;
+		let allowance = frm.doc.rounding_loss_allowance;
 		if (!(allowance > 0 && allowance < 1)) {
 			frappe.throw(__("Rounding Loss Allowance should be between 0 and 1"));
 		}
diff --git a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.js b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.js
index 5a37685..96f4438 100644
--- a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.js
+++ b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.js
@@ -65,6 +65,18 @@
 			};
 		});
 
+		me.frm.set_query("serial_and_batch_bundle", "stock_items", (doc, cdt, cdn) => {
+			let row = locals[cdt][cdn];
+			return {
+				filters: {
+					'item_code': row.item_code,
+					'voucher_type': doc.doctype,
+					'voucher_no': ["in", [doc.name, ""]],
+					'is_cancelled': 0,
+				}
+			}
+		});
+
 		me.frm.set_query("item_code", "stock_items", function() {
 			return erpnext.queries.item({"is_stock_item": 1});
 		});
diff --git a/erpnext/assets/doctype/asset_repair/asset_repair.js b/erpnext/assets/doctype/asset_repair/asset_repair.js
index f9ed2cc..b2ab82c 100644
--- a/erpnext/assets/doctype/asset_repair/asset_repair.js
+++ b/erpnext/assets/doctype/asset_repair/asset_repair.js
@@ -28,6 +28,18 @@
 				}
 			};
 		};
+
+		frm.set_query("serial_and_batch_bundle", "stock_items", (doc, cdt, cdn) => {
+			let row = locals[cdt][cdn];
+			return {
+				filters: {
+					'item_code': row.item_code,
+					'voucher_type': doc.doctype,
+					'voucher_no': ["in", [doc.name, ""]],
+					'is_cancelled': 0,
+				}
+			}
+		});
 	},
 
 	refresh: function(frm) {
diff --git a/erpnext/manufacturing/doctype/job_card/job_card.js b/erpnext/manufacturing/doctype/job_card/job_card.js
index 4a46d57..7d08aca 100644
--- a/erpnext/manufacturing/doctype/job_card/job_card.js
+++ b/erpnext/manufacturing/doctype/job_card/job_card.js
@@ -12,6 +12,17 @@
 			};
 		});
 
+		frm.set_query("serial_and_batch_bundle", () => {
+			return {
+				filters: {
+					'item_code': frm.doc.production_item,
+					'voucher_type': frm.doc.doctype,
+					'voucher_no': ["in", [frm.doc.name, ""]],
+					'is_cancelled': 0,
+				}
+			}
+		});
+
 		frm.set_indicator_formatter('sub_operation',
 			function(doc) {
 				if (doc.status == "Pending") {
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index a47d131..953a893 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -130,6 +130,7 @@
 						'item_code': item_row.item_code,
 						'voucher_type': doc.doctype,
 						'voucher_no': ["in", [doc.name, ""]],
+						'is_cancelled': 0,
 					}
 				}
 			});
diff --git a/erpnext/public/js/telephony.js b/erpnext/public/js/telephony.js
index 1c3e314..f4b0b18 100644
--- a/erpnext/public/js/telephony.js
+++ b/erpnext/public/js/telephony.js
@@ -8,7 +8,7 @@
 			Object.values(this.frm.fields_dict).forEach(function(field) {
 				if (field.df.read_only === 1 && field.df.options === 'Phone'
 					&& field.disp_area.style[0] != 'display' && !field.has_icon) {
-					field.setup_phone();
+					field.setup_phone && field.setup_phone();
 					field.has_icon = true;
 				}
 			});
diff --git a/erpnext/selling/doctype/installation_note/installation_note.js b/erpnext/selling/doctype/installation_note/installation_note.js
index 27a3b35..d63060e 100644
--- a/erpnext/selling/doctype/installation_note/installation_note.js
+++ b/erpnext/selling/doctype/installation_note/installation_note.js
@@ -7,6 +7,17 @@
 		frm.set_query('customer_address', erpnext.queries.address_query);
 		frm.set_query('contact_person', erpnext.queries.contact_query);
 		frm.set_query('customer', erpnext.queries.customer);
+		frm.set_query("serial_and_batch_bundle", "items", (doc, cdt, cdn) => {
+			let row = locals[cdt][cdn];
+			return {
+				filters: {
+					'item_code': row.item_code,
+					'voucher_type': doc.doctype,
+					'voucher_no': ["in", [doc.name, ""]],
+					'is_cancelled': 0,
+				}
+			}
+		});
 	},
 	onload: function(frm) {
 		if(!frm.doc.status) {
diff --git a/erpnext/selling/doctype/quotation/quotation.js b/erpnext/selling/doctype/quotation/quotation.js
index 2d5c3fa..280485a 100644
--- a/erpnext/selling/doctype/quotation/quotation.js
+++ b/erpnext/selling/doctype/quotation/quotation.js
@@ -34,6 +34,18 @@
 				}
 			};
 		});
+
+		frm.set_query("serial_and_batch_bundle", "packed_items", (doc, cdt, cdn) => {
+			let row = locals[cdt][cdn];
+			return {
+				filters: {
+					'item_code': row.item_code,
+					'voucher_type': doc.doctype,
+					'voucher_no': ["in", [doc.name, ""]],
+					'is_cancelled': 0,
+				}
+			}
+		});
 	},
 
 	refresh: function(frm) {
diff --git a/erpnext/stock/doctype/pick_list/pick_list.js b/erpnext/stock/doctype/pick_list/pick_list.js
index 54e2631..acbb62d 100644
--- a/erpnext/stock/doctype/pick_list/pick_list.js
+++ b/erpnext/stock/doctype/pick_list/pick_list.js
@@ -12,6 +12,7 @@
 			'Delivery Note': 'Delivery Note',
 			'Stock Entry': 'Stock Entry',
 		};
+
 		frm.set_query('parent_warehouse', () => {
 			return {
 				filters: {
@@ -20,6 +21,7 @@
 				}
 			};
 		});
+
 		frm.set_query('work_order', () => {
 			return {
 				query: 'erpnext.stock.doctype.pick_list.pick_list.get_pending_work_orders',
@@ -28,6 +30,7 @@
 				}
 			};
 		});
+
 		frm.set_query('material_request', () => {
 			return {
 				filters: {
@@ -35,9 +38,11 @@
 				}
 			};
 		});
+
 		frm.set_query('item_code', 'locations', () => {
 			return erpnext.queries.item({ "is_stock_item": 1 });
 		});
+
 		frm.set_query('batch_no', 'locations', (frm, cdt, cdn) => {
 			const row = locals[cdt][cdn];
 			return {
@@ -48,6 +53,18 @@
 				},
 			};
 		});
+
+		frm.set_query("serial_and_batch_bundle", "locations", (doc, cdt, cdn) => {
+			let row = locals[cdt][cdn];
+			return {
+				filters: {
+					'item_code': row.item_code,
+					'voucher_type': doc.doctype,
+					'voucher_no': ["in", [doc.name, ""]],
+					'is_cancelled': 0,
+				}
+			}
+		});
 	},
 	set_item_locations:(frm, save) => {
 		if (!(frm.doc.locations && frm.doc.locations.length)) {
diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.js b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.js
index d50bdba..cda4445 100644
--- a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.js
+++ b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.js
@@ -161,6 +161,23 @@
 					'istable': 0,
 					'issingle': 0,
 					'is_submittable': 1,
+					'name': ['in', [
+						"Asset Capitalization",
+						"Asset Repair",
+						"Delivery Note",
+						"Installation Note",
+						"Job Card",
+						"Maintenance Schedule",
+						"POS Invoice",
+						"Pick List",
+						"Purchase Invoice",
+						"Purchase Receipt",
+						"Quotation",
+						"Sales Invoice",
+						"Stock Entry",
+						"Stock Reconciliation",
+						"Subcontracting Receipt",
+					]],
 				}
 			};
 		});
@@ -194,6 +211,7 @@
 			return {
 				filters: {
 					item: frm.doc.item_code,
+					disabled: 0,
 				}
 			};
 		});
diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py
index cc55bd6..57bb71e 100644
--- a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py
+++ b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py
@@ -122,7 +122,12 @@
 		frappe.throw(_(message), exception, title=_("Error"))
 
 	def set_incoming_rate(self, row=None, save=False):
-		if self.type_of_transaction not in ["Inward", "Outward"]:
+		if self.type_of_transaction not in ["Inward", "Outward"] or self.voucher_type in [
+			"Installation Note",
+			"Job Card",
+			"Maintenance Schedule",
+			"Pick List",
+		]:
 			return
 
 		if self.type_of_transaction == "Outward":
@@ -220,7 +225,7 @@
 
 	def set_incoming_rate_for_inward_transaction(self, row=None, save=False):
 		valuation_field = "valuation_rate"
-		if self.voucher_type in ["Sales Invoice", "Delivery Note"]:
+		if self.voucher_type in ["Sales Invoice", "Delivery Note", "Quotation"]:
 			valuation_field = "incoming_rate"
 
 		if self.voucher_type == "POS Invoice":
@@ -229,8 +234,10 @@
 		rate = row.get(valuation_field) if row else 0.0
 		child_table = self.child_table
 
-		if self.voucher_type == "Subcontracting Receipt" and self.voucher_detail_no:
-			if frappe.db.exists("Subcontracting Receipt Supplied Item", self.voucher_detail_no):
+		if self.voucher_type == "Subcontracting Receipt":
+			if not self.voucher_detail_no:
+				return
+			elif frappe.db.exists("Subcontracting Receipt Supplied Item", self.voucher_detail_no):
 				valuation_field = "rate"
 				child_table = "Subcontracting Receipt Supplied Item"
 			else:
@@ -563,11 +570,21 @@
 
 	@property
 	def child_table(self):
-		table = f"{self.voucher_type} Item"
-		if self.voucher_type == "Stock Entry":
-			table = f"{self.voucher_type} Detail"
+		if self.voucher_type == "Job Card":
+			return
 
-		return table
+		parent_child_map = {
+			"Asset Capitalization": "Asset Capitalization Stock Item",
+			"Asset Repair": "Asset Repair Consumed Item",
+			"Quotation": "Packed Item",
+			"Stock Entry": "Stock Entry Detail",
+		}
+
+		return (
+			parent_child_map[self.voucher_type]
+			if self.voucher_type in parent_child_map
+			else f"{self.voucher_type} Item"
+		)
 
 	def delink_refernce_from_voucher(self):
 		or_filters = {"serial_and_batch_bundle": self.name}
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js
index 00b4471..f7fb633 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.js
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.js
@@ -103,6 +103,18 @@
 			}
 		});
 
+		frm.set_query("serial_and_batch_bundle", "items", (doc, cdt, cdn) => {
+			let row = locals[cdt][cdn];
+			return {
+				filters: {
+					'item_code': row.item_code,
+					'voucher_type': doc.doctype,
+					'voucher_no': ["in", [doc.name, ""]],
+					'is_cancelled': 0,
+				}
+			}
+		});
+
 
 		frm.add_fetch("bom_no", "inspection_required", "inspection_required");
 		erpnext.accounts.dimensions.setup_dimension_filters(frm, frm.doctype);
diff --git a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.js b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.js
index d584858..56cc21c 100644
--- a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.js
+++ b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.js
@@ -30,6 +30,18 @@
 			};
 		});
 
+		frm.set_query("serial_and_batch_bundle", "items", (doc, cdt, cdn) => {
+			let row = locals[cdt][cdn];
+			return {
+				filters: {
+					'item_code': row.item_code,
+					'voucher_type': doc.doctype,
+					'voucher_no': ["in", [doc.name, ""]],
+					'is_cancelled': 0,
+				}
+			}
+		});
+
 		if (frm.doc.company) {
 			erpnext.queries.setup_queries(frm, "Warehouse", function() {
 				return erpnext.queries.warehouse(frm.doc);
diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js
index 78572a6..bd1512b 100644
--- a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js
+++ b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js
@@ -77,6 +77,18 @@
 			}
 		});
 
+		frm.set_query("serial_and_batch_bundle", "supplied_items", (doc, cdt, cdn) => {
+			let row = locals[cdt][cdn];
+			return {
+				filters: {
+					'item_code': row.rm_item_code,
+					'voucher_type': doc.doctype,
+					'voucher_no': ["in", [doc.name, ""]],
+					'is_cancelled': 0,
+				}
+			}
+		});
+
 		let batch_no_field = frm.get_docfield('items', 'batch_no');
 		if (batch_no_field) {
 			batch_no_field.get_route_options_for_new_doc = function(row) {