fix: Partial Trabsfers against JC

- Fixed transferred qty not back updating on JC if partial transfer
- Partial transfer not mapping pending qty from JC correctly in SE
- tests for above cases
- minor code cleanup
diff --git a/erpnext/manufacturing/doctype/job_card/job_card.py b/erpnext/manufacturing/doctype/job_card/job_card.py
index ff4feaa..55197f1 100644
--- a/erpnext/manufacturing/doctype/job_card/job_card.py
+++ b/erpnext/manufacturing/doctype/job_card/job_card.py
@@ -615,17 +615,22 @@
 
 @frappe.whitelist()
 def make_stock_entry(source_name, target_doc=None):
-	def update_item(obj, target, source_parent):
+	def update_item(source, target, source_parent):
 		target.t_warehouse = source_parent.wip_warehouse
+
 		if not target.conversion_factor:
 			target.conversion_factor = 1
 
+		pending_rm_qty = flt(source.required_qty) - flt(source.transferred_qty)
+		if pending_rm_qty > 0:
+			target.qty = pending_rm_qty
+
 	def set_missing_values(source, target):
 		target.purpose = "Material Transfer for Manufacture"
 		target.from_bom = 1
 
 		# avoid negative 'For Quantity'
-		pending_fg_qty = source.get('for_quantity', 0) - source.get('transferred_qty', 0)
+		pending_fg_qty = flt(source.get('for_quantity', 0)) - flt(source.get('transferred_qty', 0))
 		target.fg_completed_qty = pending_fg_qty if pending_fg_qty > 0 else 0
 
 		target.set_transfer_qty()
diff --git a/erpnext/manufacturing/doctype/job_card/test_job_card.py b/erpnext/manufacturing/doctype/job_card/test_job_card.py
index daf6d4a..e16c480 100644
--- a/erpnext/manufacturing/doctype/job_card/test_job_card.py
+++ b/erpnext/manufacturing/doctype/job_card/test_job_card.py
@@ -24,7 +24,8 @@
 		)
 		tests_that_transfer_against_jc = (
 			"test_job_card_multiple_materials_transfer",
-			"test_job_card_excess_material_transfer"
+			"test_job_card_excess_material_transfer",
+			"test_job_card_partial_material_transfer"
 		)
 
 		if self._testMethodName in tests_that_skip_setup:
@@ -201,6 +202,42 @@
 		# JC is Completed with excess transfer
 		self.assertEqual(job_card.status, "Completed")
 
+	def test_job_card_partial_material_transfer(self):
+		"Test partial material transfer against Job Card"
+
+		make_stock_entry(item_code="_Test Item", target="Stores - _TC",
+			qty=25, basic_rate=100)
+		make_stock_entry(item_code="_Test Item Home Desktop Manufactured",
+			target="Stores - _TC", qty=15, basic_rate=100)
+
+		job_card_name = frappe.db.get_value("Job Card", {'work_order': self.work_order.name})
+		job_card = frappe.get_doc("Job Card", job_card_name)
+
+		# partially transfer
+		transfer_entry = make_stock_entry_from_jc(job_card_name)
+		transfer_entry.fg_completed_qty = 1
+		transfer_entry.get_items()
+		transfer_entry.insert()
+		transfer_entry.submit()
+
+		job_card.reload()
+		self.assertEqual(job_card.transferred_qty, 1)
+		self.assertEqual(transfer_entry.items[0].qty, 5)
+		self.assertEqual(transfer_entry.items[1].qty, 3)
+
+		# transfer remaining
+		transfer_entry_2 = make_stock_entry_from_jc(job_card_name)
+
+		self.assertEqual(transfer_entry_2.fg_completed_qty, 1)
+		self.assertEqual(transfer_entry_2.items[0].qty, 5)
+		self.assertEqual(transfer_entry_2.items[1].qty, 3)
+
+		transfer_entry_2.insert()
+		transfer_entry_2.submit()
+
+		job_card.reload()
+		self.assertEqual(job_card.transferred_qty, 2)
+
 	def test_job_card_material_transfer_correctness(self):
 		"""
 			1. Test if only current Job Card Items are pulled in a Stock Entry against a Job Card
@@ -248,12 +285,14 @@
 	test_record = frappe.get_test_records("BOM")[2]
 	bom_doc = frappe.get_doc(test_record)
 
-	make_operation({
+	row = {
 		"operation": "Test Operation A",
 		"workstation": "_Test Workstation A",
 		"hour_rate_rent": 300,
 		"time_in_mins": 60
-	})
+	}
+	make_workstation(row)
+	make_operation(row)
 
 	bom_doc.append("operations", {
 		"operation": "Test Operation A",
diff --git a/erpnext/manufacturing/doctype/workstation/test_workstation.py b/erpnext/manufacturing/doctype/workstation/test_workstation.py
index c77cef2..5ed5153 100644
--- a/erpnext/manufacturing/doctype/workstation/test_workstation.py
+++ b/erpnext/manufacturing/doctype/workstation/test_workstation.py
@@ -89,7 +89,7 @@
 	args = frappe._dict(args)
 
 	workstation_name = args.workstation_name or args.workstation
-	try:
+	if not frappe.db.exists("Workstation", workstation_name):
 		doc = frappe.get_doc({
 			"doctype": "Workstation",
 			"workstation_name": workstation_name
@@ -99,5 +99,5 @@
 		doc.insert()
 
 		return doc
-	except frappe.DuplicateEntryError:
-		return frappe.get_doc("Workstation", workstation_name)
+
+	return frappe.get_doc("Workstation", workstation_name)
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 67a700c..70df82b 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -1491,6 +1491,16 @@
 				item_row = d.as_dict()
 				item_row["idx"] = len(item_dict) + 1
 
+				if consider_job_card:
+					job_card_item = frappe.db.get_value(
+						"Job Card Item",
+						{
+							"item_code": d.item_code,
+							"parent": self.get("job_card")
+						}
+					)
+					item_row["job_card_item"] = job_card_item or None
+
 				if d.source_warehouse and not frappe.db.get_value("Warehouse", d.source_warehouse, "is_group"):
 					item_row["from_warehouse"] = d.source_warehouse
 
@@ -1518,27 +1528,28 @@
 
 	def add_to_stock_entry_detail(self, item_dict, bom_no=None):
 		for d in item_dict:
-			stock_uom = item_dict[d].get("stock_uom") or frappe.db.get_value("Item", d, "stock_uom")
+			item_row = item_dict[d]
+			stock_uom = item_row.get("stock_uom") or frappe.db.get_value("Item", d, "stock_uom")
 
 			se_child = self.append('items')
-			se_child.s_warehouse = item_dict[d].get("from_warehouse")
-			se_child.t_warehouse = item_dict[d].get("to_warehouse")
-			se_child.item_code = item_dict[d].get('item_code') or cstr(d)
-			se_child.uom = item_dict[d]["uom"] if item_dict[d].get("uom") else stock_uom
+			se_child.s_warehouse = item_row.get("from_warehouse")
+			se_child.t_warehouse = item_row.get("to_warehouse")
+			se_child.item_code = item_row.get('item_code') or cstr(d)
+			se_child.uom = item_row["uom"] if item_row.get("uom") else stock_uom
 			se_child.stock_uom = stock_uom
-			se_child.qty = flt(item_dict[d]["qty"], se_child.precision("qty"))
-			se_child.allow_alternative_item = item_dict[d].get("allow_alternative_item", 0)
-			se_child.subcontracted_item = item_dict[d].get("main_item_code")
-			se_child.cost_center = (item_dict[d].get("cost_center") or
-				get_default_cost_center(item_dict[d], company = self.company))
-			se_child.is_finished_item = item_dict[d].get("is_finished_item", 0)
-			se_child.is_scrap_item = item_dict[d].get("is_scrap_item", 0)
-			se_child.is_process_loss = item_dict[d].get("is_process_loss", 0)
+			se_child.qty = flt(item_row["qty"], se_child.precision("qty"))
+			se_child.allow_alternative_item = item_row.get("allow_alternative_item", 0)
+			se_child.subcontracted_item = item_row.get("main_item_code")
+			se_child.cost_center = (item_row.get("cost_center") or
+				get_default_cost_center(item_row, company = self.company))
+			se_child.is_finished_item = item_row.get("is_finished_item", 0)
+			se_child.is_scrap_item = item_row.get("is_scrap_item", 0)
+			se_child.is_process_loss = item_row.get("is_process_loss", 0)
 
 			for field in ["idx", "po_detail", "original_item", "expense_account",
 				"description", "item_name", "serial_no", "batch_no", "allow_zero_valuation_rate"]:
-				if item_dict[d].get(field):
-					se_child.set(field, item_dict[d].get(field))
+				if item_row.get(field):
+					se_child.set(field, item_row.get(field))
 
 			if se_child.s_warehouse==None:
 				se_child.s_warehouse = self.from_warehouse
@@ -1546,12 +1557,11 @@
 				se_child.t_warehouse = self.to_warehouse
 
 			# in stock uom
-			se_child.conversion_factor = flt(item_dict[d].get("conversion_factor")) or 1
-			se_child.transfer_qty = flt(item_dict[d]["qty"]*se_child.conversion_factor, se_child.precision("qty"))
+			se_child.conversion_factor = flt(item_row.get("conversion_factor")) or 1
+			se_child.transfer_qty = flt(item_row["qty"]*se_child.conversion_factor, se_child.precision("qty"))
 
-
-			# to be assigned for finished item
-			se_child.bom_no = bom_no
+			se_child.bom_no = bom_no # to be assigned for finished item
+			se_child.job_card_item = item_row.get("job_card_item") if self.get("job_card") else None
 
 	def validate_with_material_request(self):
 		for item in self.get("items"):