Skip to content

Commit

Permalink
feat: auto create SCO on PO submission
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Nov 23, 2023
1 parent 628ea42 commit 9ec6f1e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
8 changes: 4 additions & 4 deletions erpnext/buying/doctype/buying_settings/buying_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"column_break_11",
"over_transfer_allowance",
"section_break_xcug",
"purchase_order_submit_action"
"action_on_purchase_order_submission"
],
"fields": [
{
Expand Down Expand Up @@ -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"
}
],
Expand All @@ -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",
Expand Down
26 changes: 24 additions & 2 deletions erpnext/buying/doctype/purchase_order/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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") + "<br><br>" + str(e))

return target_doc


def get_mapped_subcontracting_order(source_name, target_doc=None):
Expand Down

0 comments on commit 9ec6f1e

Please sign in to comment.