Merge pull request #32867 from s-aga-r/fix/scr/rejected-qty

fix: get `consumed_qty` based on `received_qty` in SCR
diff --git a/erpnext/controllers/subcontracting_controller.py b/erpnext/controllers/subcontracting_controller.py
index 8d67e30..7819fa5 100644
--- a/erpnext/controllers/subcontracting_controller.py
+++ b/erpnext/controllers/subcontracting_controller.py
@@ -100,7 +100,7 @@
 			and self._doc_before_save
 		):
 			for row in self._doc_before_save.get("items"):
-				item_dict[row.name] = (row.item_code, row.qty)
+				item_dict[row.name] = (row.item_code, row.received_qty or row.qty)
 
 		return item_dict
 
@@ -118,7 +118,9 @@
 
 		for row in self.items:
 			self.__reference_name.append(row.name)
-			if (row.name not in item_dict) or (row.item_code, row.qty) != item_dict[row.name]:
+			if (row.name not in item_dict) or (row.item_code, row.received_qty or row.qty) != item_dict[
+				row.name
+			]:
 				self.__changed_name.append(row.name)
 
 			if item_dict.get(row.name):
@@ -461,12 +463,13 @@
 
 	def __get_qty_based_on_material_transfer(self, item_row, transfer_item):
 		key = (item_row.item_code, item_row.get(self.subcontract_data.order_field))
+		item_qty = item_row.received_qty or item_row.qty
 
-		if self.qty_to_be_received == item_row.qty:
+		if self.qty_to_be_received.get(key) == item_qty:
 			return transfer_item.qty
 
 		if self.qty_to_be_received:
-			qty = (flt(item_row.qty) * flt(transfer_item.qty)) / flt(self.qty_to_be_received.get(key, 0))
+			qty = (flt(item_qty) * flt(transfer_item.qty)) / flt(self.qty_to_be_received.get(key, 0))
 			transfer_item.item_details.required_qty = transfer_item.qty
 
 			if transfer_item.serial_no or frappe.get_cached_value(
@@ -491,7 +494,11 @@
 				for bom_item in self.__get_materials_from_bom(
 					row.item_code, row.bom, row.get("include_exploded_items")
 				):
-					qty = flt(bom_item.qty_consumed_per_unit) * flt(row.qty) * row.conversion_factor
+					qty = (
+						flt(bom_item.qty_consumed_per_unit)
+						* flt(row.received_qty or row.qty)
+						* row.conversion_factor
+					)
 					bom_item.main_item_code = row.item_code
 					self.__update_reserve_warehouse(bom_item, row)
 					self.__set_alternative_item(bom_item)
diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py
index bce5360..c7f592b 100644
--- a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py
+++ b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py
@@ -58,6 +58,7 @@
 	def before_validate(self):
 		super(SubcontractingReceipt, self).before_validate()
 		self.set_items_bom()
+		self.set_received_qty()
 		self.set_items_cost_center()
 		self.set_items_expense_account()
 
@@ -212,6 +213,10 @@
 						"bom",
 					)
 
+	def set_received_qty(self):
+		for item in self.items:
+			item.received_qty = flt(item.qty) + flt(item.rejected_qty)
+
 	def set_items_cost_center(self):
 		if self.company:
 			cost_center = frappe.get_cached_value("Company", self.company, "cost_center")
diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt/test_subcontracting_receipt.py b/erpnext/subcontracting/doctype/subcontracting_receipt/test_subcontracting_receipt.py
index 34e7842..ca72ddf 100644
--- a/erpnext/subcontracting/doctype/subcontracting_receipt/test_subcontracting_receipt.py
+++ b/erpnext/subcontracting/doctype/subcontracting_receipt/test_subcontracting_receipt.py
@@ -468,6 +468,65 @@
 		scr.cancel()
 		self.assertTrue(get_gl_entries("Subcontracting Receipt", scr.name))
 
+	def test_supplied_items_consumed_qty(self):
+		# Set Backflush Based On as "Material Transferred for Subcontracting" to transfer RM's more than the required qty
+		set_backflush_based_on("Material Transferred for Subcontract")
+
+		# Create Material Receipt for RM's
+		make_stock_entry(
+			item_code="_Test Item", qty=100, target="_Test Warehouse 1 - _TC", basic_rate=100
+		)
+		make_stock_entry(
+			item_code="_Test Item Home Desktop 100",
+			qty=100,
+			target="_Test Warehouse 1 - _TC",
+			basic_rate=100,
+		)
+
+		service_items = [
+			{
+				"warehouse": "_Test Warehouse - _TC",
+				"item_code": "Subcontracted Service Item 1",
+				"qty": 10,
+				"rate": 100,
+				"fg_item": "_Test FG Item",
+				"fg_item_qty": 10,
+			},
+		]
+
+		# Create Subcontracting Order
+		sco = get_subcontracting_order(service_items=service_items)
+
+		# Transfer RM's
+		rm_items = get_rm_items(sco.supplied_items)
+		rm_items[0]["qty"] = 20  # Extra 10 Qty
+		itemwise_details = make_stock_in_entry(rm_items=rm_items)
+		make_stock_transfer_entry(
+			sco_no=sco.name,
+			rm_items=rm_items,
+			itemwise_details=copy.deepcopy(itemwise_details),
+		)
+
+		# Create Subcontracting Receipt
+		scr = make_subcontracting_receipt(sco.name)
+		scr.rejected_warehouse = "_Test Warehouse 1 - _TC"
+
+		scr.items[0].qty = 5  # Accepted Qty
+		scr.items[0].rejected_qty = 3
+		scr.save()
+
+		# consumed_qty should be ((received_qty) * (transfered_qty / qty)) = ((5 + 3) * (20 / 10)) = 16
+		self.assertEqual(scr.supplied_items[0].consumed_qty, 16)
+
+		# Set Backflush Based On as "BOM"
+		set_backflush_based_on("BOM")
+
+		scr.items[0].rejected_qty = 4
+		scr.save()
+
+		# consumed_qty should be ((received_qty) * (qty_consumed_per_unit)) = ((5 + 4) * (1)) = 9
+		self.assertEqual(scr.supplied_items[0].consumed_qty, 9)
+
 
 def make_return_subcontracting_receipt(**args):
 	args = frappe._dict(args)