feat: auto create SCO on PO submission
diff --git a/erpnext/buying/doctype/buying_settings/buying_settings.json b/erpnext/buying/doctype/buying_settings/buying_settings.json
index b20257c..2791e32 100644
--- a/erpnext/buying/doctype/buying_settings/buying_settings.json
+++ b/erpnext/buying/doctype/buying_settings/buying_settings.json
@@ -30,7 +30,7 @@
   "column_break_11",
   "over_transfer_allowance",
   "section_break_xcug",
-  "purchase_order_submit_action"
+  "action_on_purchase_order_submission"
  ],
  "fields": [
   {
@@ -180,9 +180,9 @@
    "fieldtype": "Section Break"
   },
   {
-   "fieldname": "purchase_order_submit_action",
+   "fieldname": "action_on_purchase_order_submission",
    "fieldtype": "Select",
-   "label": "Purchase Order Submit Action",
+   "label": "Action on Purchase Order Submission",
    "options": "\nCreate Subcontracting Order\nCreate and Submit Subcontracting Order"
   }
  ],
@@ -191,7 +191,7 @@
  "index_web_pages_for_search": 1,
  "issingle": 1,
  "links": [],
- "modified": "2023-11-22 16:05:33.262940",
+ "modified": "2023-11-22 17:42:28.978582",
  "modified_by": "Administrator",
  "module": "Buying",
  "name": "Buying Settings",
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 961697c..86a652b 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -357,6 +357,8 @@
 
 		update_linked_doc(self.doctype, self.name, self.inter_company_order_reference)
 
+		self.auto_create_subcontracting_order()
+
 	def on_cancel(self):
 		self.ignore_linked_doctypes = ("GL Entry", "Payment Ledger Entry")
 		super(PurchaseOrder, self).on_cancel()
@@ -484,6 +486,16 @@
 
 		return result
 
+	def auto_create_subcontracting_order(self):
+		if self.is_subcontracted and not self.is_old_subcontracting_flow:
+			if action := frappe.db.get_single_value(
+				"Buying Settings", "action_on_purchase_order_submission"
+			):
+				if action == "Create Subcontracting Order":
+					make_subcontracting_order(self.name, save=True)
+				elif action == "Create and Submit Subcontracting Order":
+					make_subcontracting_order(self.name, submit=True)
+
 
 def item_last_purchase_rate(name, conversion_rate, item_code, conversion_factor=1.0):
 	"""get last purchase rate for an item"""
@@ -686,8 +698,18 @@
 
 
 @frappe.whitelist()
-def make_subcontracting_order(source_name, target_doc=None):
-	return get_mapped_subcontracting_order(source_name, target_doc)
+def make_subcontracting_order(source_name, target_doc=None, save=False, submit=False):
+	target_doc = get_mapped_subcontracting_order(source_name, target_doc)
+
+	if (save or submit) and frappe.has_permission(target_doc.doctype, "create"):
+		target_doc.save()
+		if submit and frappe.has_permission(target_doc.doctype, "submit", target_doc):
+			try:
+				target_doc.submit()
+			except Exception as e:
+				target_doc.add_comment("Comment", _("Submit Action Failed") + "<br><br>" + str(e))
+
+	return target_doc
 
 
 def get_mapped_subcontracting_order(source_name, target_doc=None):