fix: multiple SCO against a PO
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 6f960a2..cd58d25 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -691,3 +691,12 @@
item.warehouse = source_doc.items[idx].warehouse
return target_doc
+
+
+@frappe.whitelist()
+def is_subcontracting_order_created(po_name) -> bool:
+ count = frappe.db.count(
+ "Subcontracting Order", {"purchase_order": po_name, "status": ["not in", ["Draft", "Cancelled"]]}
+ )
+
+ return True if count else False
diff --git a/erpnext/subcontracting/doctype/subcontracting_order/subcontracting_order.py b/erpnext/subcontracting/doctype/subcontracting_order/subcontracting_order.py
index 3655910..73ab434 100644
--- a/erpnext/subcontracting/doctype/subcontracting_order/subcontracting_order.py
+++ b/erpnext/subcontracting/doctype/subcontracting_order/subcontracting_order.py
@@ -6,6 +6,7 @@
from frappe.model.mapper import get_mapped_doc
from frappe.utils import flt
+from erpnext.buying.doctype.purchase_order.purchase_order import is_subcontracting_order_created
from erpnext.controllers.subcontracting_controller import SubcontractingController
from erpnext.stock.stock_balance import get_ordered_qty, update_bin_qty
from erpnext.stock.utils import get_bin
@@ -36,7 +37,15 @@
def validate_purchase_order_for_subcontracting(self):
if self.purchase_order:
+ if is_subcontracting_order_created(self.purchase_order):
+ frappe.throw(
+ _(
+ "Only one Subcontracting Order can be created against a Purchase Order, cancel the existing Subcontracting Order to create a new one."
+ )
+ )
+
po = frappe.get_doc("Purchase Order", self.purchase_order)
+
if not po.is_subcontracted:
frappe.throw(_("Please select a valid Purchase Order that is configured for Subcontracting."))