From 3194807a41212c18d2b1578ad91143c54ba5b415 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Sat, 4 Jan 2025 17:22:16 +0530 Subject: [PATCH] fix: Alternative Items button in Work Order (cherry picked from commit e28382afc1c7799918de79a5806b0cacea37ef5c) --- .../doctype/work_order/work_order.js | 54 +++++++++++-------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/erpnext/manufacturing/doctype/work_order/work_order.js b/erpnext/manufacturing/doctype/work_order/work_order.js index df72b1e6b51..fe4768955ec 100644 --- a/erpnext/manufacturing/doctype/work_order/work_order.js +++ b/erpnext/manufacturing/doctype/work_order/work_order.js @@ -1,6 +1,12 @@ // Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors // For license information, please see license.txt +frappe.ui.form.on("Work Order Item", { + allow_alternative_item(frm, cdt, cdn) { + frm.trigger("allow_alternative_item"); + }, +}); + frappe.ui.form.on("Work Order", { setup: function (frm) { frm.custom_make_buttons = { @@ -130,6 +136,32 @@ frappe.ui.form.on("Work Order", { ); }, + allow_alternative_item: function (frm) { + let has_alternative = false; + if (frm.doc.required_items) { + has_alternative = frm.doc.required_items.find((i) => i.allow_alternative_item === 1); + } + + if (frm.doc.allow_alternative_item && frm.doc.docstatus == 0 && has_alternative) { + frm.add_custom_button(__("Alternate Item"), () => { + erpnext.utils.select_alternate_items({ + frm: frm, + child_docname: "required_items", + warehouse_field: "source_warehouse", + child_doctype: "Work Order Item", + original_item_field: "original_item", + condition: (d) => { + if (d.allow_alternative_item) { + return true; + } + }, + }); + }); + } else { + frm.remove_custom_button(__("Alternate Item")); + } + }, + refresh: function (frm) { erpnext.toggle_naming_series(); erpnext.work_order.set_custom_buttons(frm); @@ -163,26 +195,6 @@ frappe.ui.form.on("Work Order", { } } - if (frm.doc.required_items && frm.doc.allow_alternative_item) { - const has_alternative = frm.doc.required_items.find((i) => i.allow_alternative_item === 1); - if (frm.doc.docstatus == 0 && has_alternative) { - frm.add_custom_button(__("Alternate Item"), () => { - erpnext.utils.select_alternate_items({ - frm: frm, - child_docname: "required_items", - warehouse_field: "source_warehouse", - child_doctype: "Work Order Item", - original_item_field: "original_item", - condition: (d) => { - if (d.allow_alternative_item) { - return true; - } - }, - }); - }); - } - } - if (frm.doc.status == "Completed") { if (frm.doc.__onload.backflush_raw_materials_based_on == "Material Transferred for Manufacture") { frm.add_custom_button( @@ -618,7 +630,7 @@ erpnext.work_order = { set_custom_buttons: function (frm) { var doc = frm.doc; - if (doc.status !== "Closed") { + if (doc.docstatus === 1 && doc.status !== "Closed") { frm.add_custom_button( __("Close"), function () {