Merge pull request #40783 from rohitwaghchaure/fixed-provision-to-add-serial-batch-bundle-in-subcontracting-receipt

fix: button to select serial / batch bundle in the subcontracting receipt
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index 89e47b4..51d2cb8 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -748,12 +748,12 @@
 			"item_code": child.item_code,
 			"warehouse": child.warehouse,
 			"voucher_type": parent.doctype,
-			"voucher_no": parent.name,
+			"voucher_no": parent.name if parent.docstatus < 2 else None,
 			"voucher_detail_no": child.name,
 			"posting_date": parent.posting_date,
 			"posting_time": parent.posting_time,
 			"qty": child.qty,
-			"type_of_transaction": "Outward" if child.qty > 0 else "Inward",
+			"type_of_transaction": "Outward" if child.qty > 0 and parent.docstatus < 2 else "Inward",
 			"company": parent.company,
 			"do_not_submit": "True",
 		}
diff --git a/erpnext/public/js/controllers/buying.js b/erpnext/public/js/controllers/buying.js
index 1c49366..b5a8b75 100644
--- a/erpnext/public/js/controllers/buying.js
+++ b/erpnext/public/js/controllers/buying.js
@@ -386,7 +386,7 @@
 						if (r.message && (r.message.has_batch_no || r.message.has_serial_no)) {
 							item.has_serial_no = r.message.has_serial_no;
 							item.has_batch_no = r.message.has_batch_no;
-							item.type_of_transaction = item.qty > 0 ? "Inward" : "Outward";
+							item.type_of_transaction = item.rejected_qty > 0 ? "Inward" : "Outward";
 							item.is_rejected = true;
 
 							new erpnext.SerialBatchPackageSelector(
diff --git a/erpnext/stock/serial_batch_bundle.py b/erpnext/stock/serial_batch_bundle.py
index d9f3473..72945e9 100644
--- a/erpnext/stock/serial_batch_bundle.py
+++ b/erpnext/stock/serial_batch_bundle.py
@@ -844,6 +844,9 @@
 		if not doc.get("entries"):
 			return frappe._dict({})
 
+		if doc.voucher_no and frappe.get_cached_value(doc.voucher_type, doc.voucher_no, "docstatus") == 2:
+			doc.voucher_no = ""
+
 		doc.save()
 		self.validate_qty(doc)
 
diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js
index aeff2f6..d407d9c 100644
--- a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js
+++ b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js
@@ -335,12 +335,115 @@
 	items_remove: (frm) => {
 		set_missing_values(frm);
 	},
+
+	add_serial_batch_bundle(frm, cdt, cdn) {
+		let item = locals[cdt][cdn];
+
+		frappe.db.get_value("Item", item.item_code, ["has_batch_no", "has_serial_no"]).then((r) => {
+			if (r.message && (r.message.has_batch_no || r.message.has_serial_no)) {
+				item.has_serial_no = r.message.has_serial_no;
+				item.has_batch_no = r.message.has_batch_no;
+				item.type_of_transaction = item.qty > 0 ? "Inward" : "Outward";
+				item.is_rejected = false;
+
+				new erpnext.SerialBatchPackageSelector(frm, item, (r) => {
+					if (r) {
+						let qty = Math.abs(r.total_qty);
+						if (frm.doc.is_return) {
+							qty = qty * -1;
+						}
+
+						let update_values = {
+							serial_and_batch_bundle: r.name,
+							use_serial_batch_fields: 0,
+							qty: qty / flt(item.conversion_factor || 1, precision("conversion_factor", item)),
+						};
+
+						if (r.warehouse) {
+							update_values["warehouse"] = r.warehouse;
+						}
+
+						frappe.model.set_value(item.doctype, item.name, update_values);
+					}
+				});
+			}
+		});
+	},
+
+	add_serial_batch_for_rejected_qty(frm, cdt, cdn) {
+		let item = locals[cdt][cdn];
+
+		frappe.db.get_value("Item", item.item_code, ["has_batch_no", "has_serial_no"]).then((r) => {
+			if (r.message && (r.message.has_batch_no || r.message.has_serial_no)) {
+				item.has_serial_no = r.message.has_serial_no;
+				item.has_batch_no = r.message.has_batch_no;
+				item.type_of_transaction = item.rejected_qty > 0 ? "Inward" : "Outward";
+				item.is_rejected = true;
+
+				new erpnext.SerialBatchPackageSelector(frm, item, (r) => {
+					if (r) {
+						let qty = Math.abs(r.total_qty);
+						if (frm.doc.is_return) {
+							qty = qty * -1;
+						}
+
+						let update_values = {
+							serial_and_batch_bundle: r.name,
+							use_serial_batch_fields: 0,
+							rejected_qty:
+								qty / flt(item.conversion_factor || 1, precision("conversion_factor", item)),
+						};
+
+						if (r.warehouse) {
+							update_values["rejected_warehouse"] = r.warehouse;
+						}
+
+						frappe.model.set_value(item.doctype, item.name, update_values);
+					}
+				});
+			}
+		});
+	},
 });
 
 frappe.ui.form.on("Subcontracting Receipt Supplied Item", {
 	consumed_qty(frm) {
 		set_missing_values(frm);
 	},
+
+	add_serial_batch_bundle(frm, cdt, cdn) {
+		let item = locals[cdt][cdn];
+
+		item.item_code = item.rm_item_code;
+		item.qty = item.consumed_qty;
+		item.warehouse = frm.doc.supplier_warehouse;
+		frappe.db.get_value("Item", item.item_code, ["has_batch_no", "has_serial_no"]).then((r) => {
+			if (r.message && (r.message.has_batch_no || r.message.has_serial_no)) {
+				item.has_serial_no = r.message.has_serial_no;
+				item.has_batch_no = r.message.has_batch_no;
+				item.type_of_transaction = item.qty > 0 ? "Outward" : "Inward";
+				item.is_rejected = false;
+
+				new erpnext.SerialBatchPackageSelector(frm, item, (r) => {
+					if (r) {
+						let qty = Math.abs(r.total_qty);
+						if (frm.doc.is_return) {
+							qty = qty * -1;
+						}
+
+						let update_values = {
+							serial_and_batch_bundle: r.name,
+							use_serial_batch_fields: 0,
+							consumed_qty:
+								qty / flt(item.conversion_factor || 1, precision("conversion_factor", item)),
+						};
+
+						frappe.model.set_value(item.doctype, item.name, update_values);
+					}
+				});
+			}
+		});
+	},
 });
 
 let set_warehouse_in_children = (child_table, warehouse_field, warehouse) => {
diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt_item/subcontracting_receipt_item.json b/erpnext/subcontracting/doctype/subcontracting_receipt_item/subcontracting_receipt_item.json
index 7ad364a..a0be6c3 100644
--- a/erpnext/subcontracting/doctype/subcontracting_receipt_item/subcontracting_receipt_item.json
+++ b/erpnext/subcontracting/doctype/subcontracting_receipt_item/subcontracting_receipt_item.json
@@ -47,9 +47,11 @@
   "schedule_date",
   "reference_name",
   "section_break_45",
+  "add_serial_batch_bundle",
   "serial_and_batch_bundle",
   "use_serial_batch_fields",
   "col_break5",
+  "add_serial_batch_for_rejected_qty",
   "rejected_serial_and_batch_bundle",
   "section_break_jshh",
   "serial_no",
@@ -563,12 +565,24 @@
   {
    "fieldname": "column_break_henr",
    "fieldtype": "Column Break"
+  },
+  {
+   "depends_on": "eval:doc.use_serial_batch_fields === 0",
+   "fieldname": "add_serial_batch_bundle",
+   "fieldtype": "Button",
+   "label": "Add Serial / Batch Bundle"
+  },
+  {
+   "depends_on": "eval:doc.use_serial_batch_fields === 0",
+   "fieldname": "add_serial_batch_for_rejected_qty",
+   "fieldtype": "Button",
+   "label": "Add Serial / Batch No (Rejected Qty)"
   }
  ],
  "idx": 1,
  "istable": 1,
  "links": [],
- "modified": "2024-03-27 13:10:47.165943",
+ "modified": "2024-03-29 15:42:43.425544",
  "modified_by": "Administrator",
  "module": "Subcontracting",
  "name": "Subcontracting Receipt Item",
@@ -576,7 +590,7 @@
  "owner": "Administrator",
  "permissions": [],
  "quick_entry": 1,
- "sort_field": "creation",
+ "sort_field": "modified",
  "sort_order": "DESC",
  "states": []
 }
\ No newline at end of file
diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt_supplied_item/subcontracting_receipt_supplied_item.json b/erpnext/subcontracting/doctype/subcontracting_receipt_supplied_item/subcontracting_receipt_supplied_item.json
index 12f67ad..3bc7217 100644
--- a/erpnext/subcontracting/doctype/subcontracting_receipt_supplied_item/subcontracting_receipt_supplied_item.json
+++ b/erpnext/subcontracting/doctype/subcontracting_receipt_supplied_item/subcontracting_receipt_supplied_item.json
@@ -25,6 +25,7 @@
   "consumed_qty",
   "current_stock",
   "secbreak_3",
+  "add_serial_batch_bundle",
   "serial_and_batch_bundle",
   "use_serial_batch_fields",
   "col_break4",
@@ -224,19 +225,25 @@
   {
    "fieldname": "column_break_qibi",
    "fieldtype": "Column Break"
+  },
+  {
+   "depends_on": "eval:doc.use_serial_batch_fields === 0",
+   "fieldname": "add_serial_batch_bundle",
+   "fieldtype": "Button",
+   "label": "Add Serial / Batch Bundle"
   }
  ],
  "idx": 1,
  "istable": 1,
  "links": [],
- "modified": "2024-03-27 13:10:47.405161",
+ "modified": "2024-03-30 10:26:27.237371",
  "modified_by": "Administrator",
  "module": "Subcontracting",
  "name": "Subcontracting Receipt Supplied Item",
  "naming_rule": "Autoincrement",
  "owner": "Administrator",
  "permissions": [],
- "sort_field": "creation",
+ "sort_field": "modified",
  "sort_order": "DESC",
  "states": [],
  "track_changes": 1