feat: Service Item and Finished Good Map (#36647)

* feat: new DocType `Service Item and Finished Good Map`

* fix(ux): filters for Service Item and Finished Good

* fix: validations for Service Item and Finished Good

* feat: set FG Item on Service Item selection in PO

* refactor: one-to-many mapping between service item and finished goods

* feat: auto set Service Item for finished good in PO created from PP

* feat: auto set Service Item on Finished Good selection in PO

* test: add test case for service item and finished goods map

* feat: `BOM` field in `Finished Good Detail`

* feat: new DocType `Subcontracting BOM`

* fix: filters and validations for Subcontracting BOM

* feat: auto select Service Item in PO created from PP

* test: add test case for PO service item auto pick

* feat: pick BOM from Subcontracting BOM in SCO

* feat: auto pick `Service Item` on FG select

* refactor: remove DocType `Service Item and Finished Goods Map` and `Finished Good Detail`

* feat: fetch FG for Service Item

* chore: `linter`

* refactor: update `Auto Name` expression for Subcontracting BOM
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js
index f6a1951..88faeee 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.js
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.js
@@ -118,6 +118,24 @@
 			frm.set_value("tax_withholding_category", frm.supplier_tds);
 		}
 	},
+
+	get_subcontracting_boms_for_finished_goods: function(fg_item) {
+		return frappe.call({
+			method:"erpnext.subcontracting.doctype.subcontracting_bom.subcontracting_bom.get_subcontracting_boms_for_finished_goods",
+			args: {
+				fg_items: fg_item
+			},
+		});
+	},
+
+	get_subcontracting_boms_for_service_item: function(service_item) {
+		return frappe.call({
+			method:"erpnext.subcontracting.doctype.subcontracting_bom.subcontracting_bom.get_subcontracting_boms_for_service_item",
+			args: {
+				service_item: service_item
+			},
+		});
+	},
 });
 
 frappe.ui.form.on("Purchase Order Item", {
@@ -132,15 +150,83 @@
 		}
 	},
 
-	qty: function(frm, cdt, cdn) {
+	item_code: async function(frm, cdt, cdn) {
 		if (frm.doc.is_subcontracted && !frm.doc.is_old_subcontracting_flow) {
 			var row = locals[cdt][cdn];
 
-			if (row.qty) {
-				row.fg_item_qty = row.qty;
+			if (row.item_code && !row.fg_item) {
+				var result = await frm.events.get_subcontracting_boms_for_service_item(row.item_code)
+
+				if (result.message && Object.keys(result.message).length) {
+					var finished_goods = Object.keys(result.message);
+
+					// Set FG if only one active Subcontracting BOM is found
+					if (finished_goods.length === 1) {
+						row.fg_item = result.message[finished_goods[0]].finished_good;
+						row.uom = result.message[finished_goods[0]].finished_good_uom;
+						refresh_field("items");
+					} else {
+						const dialog = new frappe.ui.Dialog({
+							title: __("Select Finished Good"),
+							size: "small",
+							fields: [
+								{
+									fieldname: "finished_good",
+									fieldtype: "Autocomplete",
+									label: __("Finished Good"),
+									options: finished_goods,
+								}
+							],
+							primary_action_label: __("Select"),
+							primary_action: () => {
+								var subcontracting_bom = result.message[dialog.get_value("finished_good")];
+
+								if (subcontracting_bom) {
+									row.fg_item = subcontracting_bom.finished_good;
+									row.uom = subcontracting_bom.finished_good_uom;
+									refresh_field("items");
+								}
+
+								dialog.hide();
+							},
+						});
+
+						dialog.show();
+					}
+				}
 			}
 		}
-	}
+	},
+
+	fg_item: async function(frm, cdt, cdn) {
+		if (frm.doc.is_subcontracted && !frm.doc.is_old_subcontracting_flow) {
+			var row = locals[cdt][cdn];
+
+			if (row.fg_item) {
+				var result = await frm.events.get_subcontracting_boms_for_finished_goods(row.fg_item)
+
+				if (result.message && Object.keys(result.message).length) {
+					frappe.model.set_value(cdt, cdn, "item_code", result.message.service_item);
+					frappe.model.set_value(cdt, cdn, "qty", flt(row.fg_item_qty) * flt(result.message.conversion_factor));
+					frappe.model.set_value(cdt, cdn, "uom", result.message.service_item_uom);
+				}
+			}
+		}
+	},
+
+	fg_item_qty: async function(frm, cdt, cdn) {
+		if (frm.doc.is_subcontracted && !frm.doc.is_old_subcontracting_flow) {
+			var row = locals[cdt][cdn];
+
+			if (row.fg_item) {
+				var result = await frm.events.get_subcontracting_boms_for_finished_goods(row.fg_item)
+
+				if (result.message && row.item_code == result.message.service_item && row.uom == result.message.service_item_uom) {
+					frappe.model.set_value(cdt, cdn, "qty", flt(row.fg_item_qty) * flt(result.message.conversion_factor));
+				}
+			}
+		}
+	},
 });
 
 erpnext.buying.PurchaseOrderController = class PurchaseOrderController extends erpnext.buying.BuyingController {
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 3576cd4..465fe96 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -28,6 +28,9 @@
 from erpnext.stock.doctype.item.item import get_item_defaults, get_last_purchase_details
 from erpnext.stock.stock_balance import get_ordered_qty, update_bin_qty
 from erpnext.stock.utils import get_bin
+from erpnext.subcontracting.doctype.subcontracting_bom.subcontracting_bom import (
+	get_subcontracting_boms_for_finished_goods,
+)
 
 form_grid_templates = {"items": "templates/form_grid/item_grid.html"}
 
@@ -451,6 +454,25 @@
 		else:
 			self.db_set("per_received", 0, update_modified=False)
 
+	def set_service_items_for_finished_goods(self):
+		if not self.is_subcontracted or self.is_old_subcontracting_flow:
+			return
+
+		finished_goods_without_service_item = {
+			d.fg_item for d in self.items if (not d.item_code and d.fg_item)
+		}
+
+		if subcontracting_boms := get_subcontracting_boms_for_finished_goods(
+			finished_goods_without_service_item
+		):
+			for item in self.items:
+				if not item.item_code and item.fg_item in subcontracting_boms:
+					subcontracting_bom = subcontracting_boms[item.fg_item]
+
+					item.item_code = subcontracting_bom.service_item
+					item.qty = flt(item.fg_item_qty) * flt(subcontracting_bom.conversion_factor)
+					item.uom = subcontracting_bom.service_item_uom
+
 	def can_update_items(self) -> bool:
 		result = True
 
diff --git a/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json b/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
index c645b04..414f086 100644
--- a/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
+++ b/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
@@ -7,13 +7,13 @@
  "editable_grid": 1,
  "engine": "InnoDB",
  "field_order": [
+  "fg_item",
+  "fg_item_qty",
   "item_code",
   "supplier_part_no",
   "item_name",
   "brand",
   "product_bundle",
-  "fg_item",
-  "fg_item_qty",
   "column_break_4",
   "schedule_date",
   "expected_delivery_date",
@@ -862,7 +862,7 @@
    "depends_on": "eval:parent.is_subcontracted && !parent.is_old_subcontracting_flow",
    "fieldname": "fg_item",
    "fieldtype": "Link",
-   "label": "Finished Good Item",
+   "label": "Finished Good",
    "mandatory_depends_on": "eval:parent.is_subcontracted && !parent.is_old_subcontracting_flow",
    "options": "Item"
   },
@@ -871,7 +871,7 @@
    "depends_on": "eval:parent.is_subcontracted && !parent.is_old_subcontracting_flow",
    "fieldname": "fg_item_qty",
    "fieldtype": "Float",
-   "label": "Finished Good Item Qty",
+   "label": "Finished Good Qty",
    "mandatory_depends_on": "eval:parent.is_subcontracted && !parent.is_old_subcontracting_flow"
   },
   {
@@ -902,7 +902,7 @@
  "index_web_pages_for_search": 1,
  "istable": 1,
  "links": [],
- "modified": "2022-11-29 16:47:41.364387",
+ "modified": "2023-08-17 10:17:40.893393",
  "modified_by": "Administrator",
  "module": "Buying",
  "name": "Purchase Order Item",
diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.py b/erpnext/manufacturing/doctype/production_plan/production_plan.py
index 34e9423..b7a2489 100644
--- a/erpnext/manufacturing/doctype/production_plan/production_plan.py
+++ b/erpnext/manufacturing/doctype/production_plan/production_plan.py
@@ -673,6 +673,7 @@
 
 				po.append("items", po_data)
 
+			po.set_service_items_for_finished_goods()
 			po.set_missing_values()
 			po.flags.ignore_mandatory = True
 			po.flags.ignore_validate = True
diff --git a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py
index 2871a29..dccb903 100644
--- a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py
+++ b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py
@@ -412,11 +412,15 @@
 
 	def test_production_plan_for_subcontracting_po(self):
 		from erpnext.manufacturing.doctype.bom.test_bom import create_nested_bom
+		from erpnext.subcontracting.doctype.subcontracting_bom.test_subcontracting_bom import (
+			create_subcontracting_bom,
+		)
 
-		bom_tree_1 = {"Test Laptop 1": {"Test Motherboard 1": {"Test Motherboard Wires 1": {}}}}
+		fg_item = "Test Motherboard 1"
+		bom_tree_1 = {"Test Laptop 1": {fg_item: {"Test Motherboard Wires 1": {}}}}
 		create_nested_bom(bom_tree_1, prefix="")
 
-		item_doc = frappe.get_doc("Item", "Test Motherboard 1")
+		item_doc = frappe.get_doc("Item", fg_item)
 		company = "_Test Company"
 
 		item_doc.is_sub_contracted_item = 1
@@ -429,6 +433,12 @@
 
 		item_doc.save()
 
+		service_item = make_item(properties={"is_stock_item": 0}).name
+		create_subcontracting_bom(
+			finished_good=fg_item,
+			service_item=service_item,
+		)
+
 		plan = create_production_plan(
 			item_code="Test Laptop 1", planned_qty=10, use_multi_level_bom=1, do_not_submit=True
 		)
@@ -445,7 +455,8 @@
 		self.assertEqual(po_doc.items[0].qty, 10.0)
 		self.assertEqual(po_doc.items[0].fg_item_qty, 10.0)
 		self.assertEqual(po_doc.items[0].fg_item_qty, 10.0)
-		self.assertEqual(po_doc.items[0].fg_item, "Test Motherboard 1")
+		self.assertEqual(po_doc.items[0].fg_item, fg_item)
+		self.assertEqual(po_doc.items[0].item_code, service_item)
 
 	def test_production_plan_combine_subassembly(self):
 		"""
diff --git a/erpnext/subcontracting/doctype/subcontracting_bom/__init__.py b/erpnext/subcontracting/doctype/subcontracting_bom/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/subcontracting/doctype/subcontracting_bom/__init__.py
diff --git a/erpnext/subcontracting/doctype/subcontracting_bom/subcontracting_bom.js b/erpnext/subcontracting/doctype/subcontracting_bom/subcontracting_bom.js
new file mode 100644
index 0000000..a7f0d7a
--- /dev/null
+++ b/erpnext/subcontracting/doctype/subcontracting_bom/subcontracting_bom.js
@@ -0,0 +1,40 @@
+// Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
+// For license information, please see license.txt
+
+frappe.ui.form.on('Subcontracting BOM', {
+    setup: (frm) => {
+		frm.trigger('set_queries');
+	},
+
+    set_queries: (frm) => {
+        frm.set_query('finished_good', () => {
+            return {
+                filters: {
+                    disabled: 0,
+                    is_stock_item: 1,
+                    default_bom: ['!=', ''],
+                    is_sub_contracted_item: 1,
+                }
+            }
+        });
+
+        frm.set_query('finished_good_bom', () => {
+            return {
+                filters: {
+                    docstatus: 1,
+                    is_active: 1,
+                    item: frm.doc.finished_good,
+                }
+            }
+        });
+
+        frm.set_query('service_item', () => {
+            return {
+                filters: {
+                    disabled: 0,
+                    is_stock_item: 0,
+                }
+            }
+        });
+    }
+});
diff --git a/erpnext/subcontracting/doctype/subcontracting_bom/subcontracting_bom.json b/erpnext/subcontracting/doctype/subcontracting_bom/subcontracting_bom.json
new file mode 100644
index 0000000..b258afc
--- /dev/null
+++ b/erpnext/subcontracting/doctype/subcontracting_bom/subcontracting_bom.json
@@ -0,0 +1,155 @@
+{
+ "actions": [],
+ "allow_rename": 1,
+ "autoname": "format:SB-{####}",
+ "creation": "2023-08-29 12:43:20.417184",
+ "default_view": "List",
+ "doctype": "DocType",
+ "editable_grid": 1,
+ "engine": "InnoDB",
+ "field_order": [
+  "is_active",
+  "section_break_dsjm",
+  "finished_good",
+  "finished_good_qty",
+  "column_break_quoy",
+  "finished_good_uom",
+  "finished_good_bom",
+  "section_break_qdw9",
+  "service_item",
+  "service_item_qty",
+  "column_break_uzmw",
+  "service_item_uom",
+  "conversion_factor"
+ ],
+ "fields": [
+  {
+   "default": "1",
+   "fieldname": "is_active",
+   "fieldtype": "Check",
+   "in_list_view": 1,
+   "in_preview": 1,
+   "in_standard_filter": 1,
+   "label": "Is Active",
+   "print_hide": 1
+  },
+  {
+   "fieldname": "section_break_dsjm",
+   "fieldtype": "Section Break"
+  },
+  {
+   "fieldname": "finished_good",
+   "fieldtype": "Link",
+   "in_list_view": 1,
+   "in_preview": 1,
+   "in_standard_filter": 1,
+   "label": "Finished Good",
+   "options": "Item",
+   "reqd": 1,
+   "search_index": 1,
+   "set_only_once": 1
+  },
+  {
+   "default": "1",
+   "fieldname": "finished_good_qty",
+   "fieldtype": "Float",
+   "label": "Finished Good Qty",
+   "non_negative": 1,
+   "reqd": 1
+  },
+  {
+   "fetch_from": "finished_good.default_bom",
+   "fetch_if_empty": 1,
+   "fieldname": "finished_good_bom",
+   "fieldtype": "Link",
+   "in_list_view": 1,
+   "in_preview": 1,
+   "in_standard_filter": 1,
+   "label": "Finished Good BOM",
+   "options": "BOM",
+   "reqd": 1,
+   "search_index": 1
+  },
+  {
+   "fieldname": "section_break_qdw9",
+   "fieldtype": "Section Break"
+  },
+  {
+   "fieldname": "service_item",
+   "fieldtype": "Link",
+   "in_list_view": 1,
+   "in_preview": 1,
+   "in_standard_filter": 1,
+   "label": "Service Item",
+   "options": "Item",
+   "reqd": 1,
+   "search_index": 1
+  },
+  {
+   "default": "1",
+   "fieldname": "service_item_qty",
+   "fieldtype": "Float",
+   "label": "Service Item Qty",
+   "non_negative": 1,
+   "reqd": 1
+  },
+  {
+   "fetch_from": "service_item.stock_uom",
+   "fetch_if_empty": 1,
+   "fieldname": "service_item_uom",
+   "fieldtype": "Link",
+   "label": "Service Item UOM",
+   "options": "UOM",
+   "reqd": 1
+  },
+  {
+   "description": "Service Item Qty / Finished Good Qty",
+   "fieldname": "conversion_factor",
+   "fieldtype": "Float",
+   "label": "Conversion Factor",
+   "read_only": 1
+  },
+  {
+   "fieldname": "column_break_quoy",
+   "fieldtype": "Column Break"
+  },
+  {
+   "fetch_from": "finished_good.stock_uom",
+   "fieldname": "finished_good_uom",
+   "fieldtype": "Link",
+   "label": "Finished Good UOM",
+   "options": "UOM",
+   "read_only": 1
+  },
+  {
+   "fieldname": "column_break_uzmw",
+   "fieldtype": "Column Break"
+  }
+ ],
+ "index_web_pages_for_search": 1,
+ "links": [],
+ "modified": "2023-09-03 16:51:43.558295",
+ "modified_by": "Administrator",
+ "module": "Subcontracting",
+ "name": "Subcontracting BOM",
+ "naming_rule": "Expression",
+ "owner": "Administrator",
+ "permissions": [
+  {
+   "create": 1,
+   "delete": 1,
+   "email": 1,
+   "export": 1,
+   "print": 1,
+   "read": 1,
+   "report": 1,
+   "role": "System Manager",
+   "share": 1,
+   "write": 1
+  }
+ ],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "states": [],
+ "track_changes": 1
+}
\ No newline at end of file
diff --git a/erpnext/subcontracting/doctype/subcontracting_bom/subcontracting_bom.py b/erpnext/subcontracting/doctype/subcontracting_bom/subcontracting_bom.py
new file mode 100644
index 0000000..49ba986
--- /dev/null
+++ b/erpnext/subcontracting/doctype/subcontracting_bom/subcontracting_bom.py
@@ -0,0 +1,97 @@
+# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+import frappe
+from frappe import _
+from frappe.model.document import Document
+from frappe.utils import flt
+
+
+class SubcontractingBOM(Document):
+	def validate(self):
+		self.validate_finished_good()
+		self.validate_service_item()
+		self.validate_is_active()
+
+	def before_save(self):
+		self.set_conversion_factor()
+
+	def validate_finished_good(self):
+		disabled, is_stock_item, default_bom, is_sub_contracted_item = frappe.db.get_value(
+			"Item",
+			self.finished_good,
+			["disabled", "is_stock_item", "default_bom", "is_sub_contracted_item"],
+		)
+
+		if disabled:
+			frappe.throw(_("Finished Good {0} is disabled.").format(frappe.bold(self.finished_good)))
+		if not is_stock_item:
+			frappe.throw(
+				_("Finished Good {0} must be a stock item.").format(frappe.bold(self.finished_good))
+			)
+		if not default_bom:
+			frappe.throw(
+				_("Finished Good {0} does not have a default BOM.").format(frappe.bold(self.finished_good))
+			)
+		if not is_sub_contracted_item:
+			frappe.throw(
+				_("Finished Good {0} must be a sub-contracted item.").format(frappe.bold(self.finished_good))
+			)
+
+	def validate_service_item(self):
+		disabled, is_stock_item = frappe.db.get_value(
+			"Item", self.service_item, ["disabled", "is_stock_item"]
+		)
+
+		if disabled:
+			frappe.throw(_("Service Item {0} is disabled.").format(frappe.bold(self.service_item)))
+		if is_stock_item:
+			frappe.throw(
+				_("Service Item {0} must be a non-stock item.").format(frappe.bold(self.service_item))
+			)
+
+	def validate_is_active(self):
+		if self.is_active:
+			if sb := frappe.db.exists(
+				"Subcontracting BOM",
+				{"finished_good": self.finished_good, "is_active": 1, "name": ["!=", self.name]},
+			):
+				frappe.throw(
+					_("There is already an active Subcontracting BOM {0} for the Finished Good {1}.").format(
+						frappe.bold(sb), frappe.bold(self.finished_good)
+					)
+				)
+
+	def set_conversion_factor(self):
+		self.conversion_factor = flt(self.service_item_qty) / flt(self.finished_good_qty)
+
+
+@frappe.whitelist()
+def get_subcontracting_boms_for_finished_goods(fg_items: str | list) -> dict:
+	if fg_items:
+		filters = {"is_active": 1}
+
+		if isinstance(fg_items, list):
+			filters["finished_good"] = ["in", fg_items]
+		else:
+			filters["finished_good"] = fg_items
+
+		if subcontracting_boms := frappe.get_all("Subcontracting BOM", filters=filters, fields=["*"]):
+			if isinstance(fg_items, list):
+				return {d.finished_good: d for d in subcontracting_boms}
+			else:
+				return subcontracting_boms[0]
+
+	return {}
+
+
+@frappe.whitelist()
+def get_subcontracting_boms_for_service_item(service_item: str) -> dict:
+	if service_item:
+		filters = {"is_active": 1, "service_item": service_item}
+		Subcontracting_boms = frappe.db.get_all("Subcontracting BOM", filters=filters, fields=["*"])
+
+		if Subcontracting_boms:
+			return {d.finished_good: d for d in Subcontracting_boms}
+
+	return {}
diff --git a/erpnext/subcontracting/doctype/subcontracting_bom/test_subcontracting_bom.py b/erpnext/subcontracting/doctype/subcontracting_bom/test_subcontracting_bom.py
new file mode 100644
index 0000000..9335ac8
--- /dev/null
+++ b/erpnext/subcontracting/doctype/subcontracting_bom/test_subcontracting_bom.py
@@ -0,0 +1,26 @@
+# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and Contributors
+# See license.txt
+
+import frappe
+from frappe.tests.utils import FrappeTestCase
+
+
+class TestSubcontractingBOM(FrappeTestCase):
+	pass
+
+
+def create_subcontracting_bom(**kwargs):
+	kwargs = frappe._dict(kwargs)
+
+	doc = frappe.new_doc("Subcontracting BOM")
+	doc.is_active = kwargs.is_active or 1
+	doc.finished_good = kwargs.finished_good
+	doc.finished_good_uom = kwargs.finished_good_uom
+	doc.finished_good_qty = kwargs.finished_good_qty or 1
+	doc.finished_good_bom = kwargs.finished_good_bom
+	doc.service_item = kwargs.service_item
+	doc.service_item_uom = kwargs.service_item_uom
+	doc.service_item_qty = kwargs.service_item_qty or 1
+	doc.save()
+
+	return doc
diff --git a/erpnext/subcontracting/doctype/subcontracting_order/subcontracting_order.py b/erpnext/subcontracting/doctype/subcontracting_order/subcontracting_order.py
index b7b3445..faf0cad 100644
--- a/erpnext/subcontracting/doctype/subcontracting_order/subcontracting_order.py
+++ b/erpnext/subcontracting/doctype/subcontracting_order/subcontracting_order.py
@@ -128,8 +128,12 @@
 		for si in self.service_items:
 			if si.fg_item:
 				item = frappe.get_doc("Item", si.fg_item)
-				bom = frappe.db.get_value("BOM", {"item": item.item_code, "is_active": 1, "is_default": 1})
-
+				bom = (
+					frappe.db.get_value(
+						"Subcontracting BOM", {"finished_good": item.item_code, "is_active": 1}, "finished_good_bom"
+					)
+					or item.default_bom
+				)
 				items.append(
 					{
 						"item_code": item.item_code,