From faf9f13215c834ef951fc26303ff596727cb3e67 Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Mon, 21 Aug 2023 15:51:57 +0530 Subject: [PATCH] feat: allow `Update Items` for Subcontracted PO not having SCO created --- .../doctype/purchase_order/purchase_order.js | 3 +- .../doctype/purchase_order/purchase_order.py | 13 ++++++++ erpnext/public/js/utils.js | 31 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js index 7c33056a91..f6a1951439 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.js +++ b/erpnext/buying/doctype/purchase_order/purchase_order.js @@ -185,8 +185,7 @@ erpnext.buying.PurchaseOrderController = class PurchaseOrderController extends e if(!in_list(["Closed", "Delivered"], doc.status)) { if(this.frm.doc.status !== 'Closed' && flt(this.frm.doc.per_received, 2) < 100 && flt(this.frm.doc.per_billed, 2) < 100) { - // Don't add Update Items button if the PO is following the new subcontracting flow. - if (!(this.frm.doc.is_subcontracted && !this.frm.doc.is_old_subcontracting_flow)) { + if (!this.frm.doc.__onload || this.frm.doc.__onload.can_update_items) { this.frm.add_custom_button(__('Update Items'), () => { erpnext.utils.update_child_items({ frm: this.frm, diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py index 06b9d29e69..4da1e76c2d 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/purchase_order.py @@ -49,9 +49,22 @@ class PurchaseOrder(BuyingController): } ] + def can_update_items(self) -> bool: + result = True + + if self.is_subcontracted and not self.is_old_subcontracting_flow: + # Check - 1: NOT ALLOWED if non-cancelled Subcontracting Order exists for this Purchase Order + if frappe.db.exists( + "Subcontracting Order", {"purchase_order": self.name, "docstatus": ["!=", 2]} + ): + return False + + return result + def onload(self): supplier_tds = frappe.db.get_value("Supplier", self.supplier, "tax_withholding_category") self.set_onload("supplier_tds", supplier_tds) + self.set_onload("can_update_items", self.can_update_items()) def validate(self): super(PurchaseOrder, self).validate() diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js index f456e5e500..9116fd1f03 100755 --- a/erpnext/public/js/utils.js +++ b/erpnext/public/js/utils.js @@ -678,6 +678,37 @@ erpnext.utils.update_child_items = function(opts) { }) } + if (frm.doc.doctype == 'Purchase Order' && frm.doc.is_subcontracted && !frm.doc.is_old_subcontracting_flow) { + fields.push({ + fieldtype:'Link', + fieldname:'fg_item', + options: 'Item', + reqd: 1, + in_list_view: 0, + read_only: 0, + disabled: 0, + label: __('Finished Good Item'), + get_query: () => { + return { + filters: { + 'is_stock_item': 1, + 'is_sub_contracted_item': 1, + 'default_bom': ['!=', ''] + } + } + }, + }, { + fieldtype:'Float', + fieldname:'fg_item_qty', + reqd: 1, + default: 0, + read_only: 0, + in_list_view: 0, + label: __('Finished Good Item Qty'), + precision: get_precision('fg_item_qty') + }) + } + let dialog = new frappe.ui.Dialog({ title: __("Update Items"), size: "extra-large",