From 9ec6f1e1d63ccb8087cb3f95c6a100b02d22ba79 Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Wed, 22 Nov 2023 17:06:19 +0530 Subject: [PATCH] feat: auto create SCO on PO submission --- .../buying_settings/buying_settings.json | 8 +++--- .../doctype/purchase_order/purchase_order.py | 26 +++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/erpnext/buying/doctype/buying_settings/buying_settings.json b/erpnext/buying/doctype/buying_settings/buying_settings.json index b20257cfb715..2791e327309b 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 961697c0ac23..86a652b4876c 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/purchase_order.py @@ -357,6 +357,8 @@ def on_submit(self): 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 @@ def can_update_items(self) -> bool: 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 @@ def make_inter_company_sales_order(source_name, target_doc=None): @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") + "

" + str(e)) + + return target_doc def get_mapped_subcontracting_order(source_name, target_doc=None):