Merge branch 'develop' into work-order-partial-transfer
diff --git a/erpnext/manufacturing/doctype/work_order/test_work_order.py b/erpnext/manufacturing/doctype/work_order/test_work_order.py
index 8934f9c..2aba482 100644
--- a/erpnext/manufacturing/doctype/work_order/test_work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/test_work_order.py
@@ -1144,6 +1144,56 @@
 		for index, row in enumerate(ste_manu.get("items"), start=1):
 			self.assertEqual(index, row.idx)
 
+	@change_settings(
+		"Manufacturing Settings",
+		{"backflush_raw_materials_based_on": "Material Transferred for Manufacture"},
+	)
+	def test_work_order_multiple_material_transfer(self):
+		"""
+		Test transferring multiple RMs in separate Stock Entries.
+		"""
+		work_order = make_wo_order_test_record(planned_start_date=now(), qty=1)
+		test_stock_entry.make_stock_entry(  # stock up RM
+			item_code="_Test Item",
+			target="_Test Warehouse - _TC",
+			qty=1,
+			basic_rate=5000.0,
+		)
+		test_stock_entry.make_stock_entry(  # stock up RM
+			item_code="_Test Item Home Desktop 100",
+			target="_Test Warehouse - _TC",
+			qty=2,
+			basic_rate=1000.0,
+		)
+
+		transfer_entry = frappe.get_doc(
+			make_stock_entry(work_order.name, "Material Transfer for Manufacture", 1)
+		)
+		del transfer_entry.get("items")[0]  # transfer only one RM
+		transfer_entry.submit()
+
+		# WO's "Material Transferred for Mfg" shows all is transferred, one RM is pending
+		work_order.reload()
+		self.assertEqual(work_order.material_transferred_for_manufacturing, 1)
+		self.assertEqual(work_order.required_items[0].transferred_qty, 0)
+		self.assertEqual(work_order.required_items[1].transferred_qty, 2)
+
+		final_transfer_entry = frappe.get_doc(  # transfer last RM with For Quantity = 0
+			make_stock_entry(work_order.name, "Material Transfer for Manufacture", 0)
+		)
+		final_transfer_entry.save()
+
+		self.assertEqual(final_transfer_entry.fg_completed_qty, 0.0)
+		self.assertEqual(final_transfer_entry.items[0].qty, 1)
+
+		final_transfer_entry.submit()
+		work_order.reload()
+
+		# WO's "Material Transferred for Mfg" shows all is transferred, no RM is pending
+		self.assertEqual(work_order.material_transferred_for_manufacturing, 1)
+		self.assertEqual(work_order.required_items[0].transferred_qty, 1)
+		self.assertEqual(work_order.required_items[1].transferred_qty, 2)
+
 
 def update_job_card(job_card, jc_qty=None):
 	employee = frappe.db.get_value("Employee", {"status": "Active"}, "name")
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.js b/erpnext/manufacturing/doctype/work_order/work_order.js
index 6433a99..20f1503 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.js
+++ b/erpnext/manufacturing/doctype/work_order/work_order.js
@@ -540,8 +540,10 @@
 				|| frm.doc.transfer_material_against == 'Job Card') ? 0 : 1;
 
 			if (show_start_btn) {
-				if ((flt(doc.material_transferred_for_manufacturing) < flt(doc.qty))
-					&& frm.doc.status != 'Stopped') {
+				let pending_to_transfer = frm.doc.required_items.some(
+					item => flt(item.transferred_qty) < flt(item.required_qty)
+				);
+				if (pending_to_transfer && frm.doc.status != 'Stopped') {
 					frm.has_start_btn = true;
 					frm.add_custom_button(__('Create Pick List'), function() {
 						erpnext.work_order.create_pick_list(frm);
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py
index 2ee848c..2802310 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/work_order.py
@@ -1186,7 +1186,11 @@
 	stock_entry.from_bom = 1
 	stock_entry.bom_no = work_order.bom_no
 	stock_entry.use_multi_level_bom = work_order.use_multi_level_bom
-	stock_entry.fg_completed_qty = qty or (flt(work_order.qty) - flt(work_order.produced_qty))
+	# accept 0 qty as well
+	stock_entry.fg_completed_qty = (
+		qty if qty is not None else (flt(work_order.qty) - flt(work_order.produced_qty))
+	)
+
 	if work_order.bom_no:
 		stock_entry.inspection_required = frappe.db.get_value(
 			"BOM", work_order.bom_no, "inspection_required"
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 1e62471..c4aa8a4 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -1803,7 +1803,9 @@
 				or (desire_to_transfer > 0 and backflush_based_on == "Material Transferred for Manufacture")
 				or allow_overproduction
 			):
-				item_dict[item]["qty"] = desire_to_transfer
+				# "No need for transfer but qty still pending to transfer" case can occur
+				# when transferring multiple RM in different Stock Entries
+				item_dict[item]["qty"] = desire_to_transfer if (desire_to_transfer > 0) else pending_to_issue
 			elif pending_to_issue > 0:
 				item_dict[item]["qty"] = pending_to_issue
 			else: