From 3194807a41212c18d2b1578ad91143c54ba5b415 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Sat, 4 Jan 2025 17:22:16 +0530 Subject: [PATCH 1/2] 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 () { From 642b89782d5d1a2d53c12ed192e2e3dd62e65365 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Mon, 6 Jan 2025 11:22:06 +0530 Subject: [PATCH 2/2] fix: Alternative Item button dissapearing on Save event (cherry picked from commit b8838bd9b9845aa14b6cd756b1238ab6646bb3bd) --- .../manufacturing/doctype/work_order/work_order.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/erpnext/manufacturing/doctype/work_order/work_order.js b/erpnext/manufacturing/doctype/work_order/work_order.js index fe4768955ec..6da3d803358 100644 --- a/erpnext/manufacturing/doctype/work_order/work_order.js +++ b/erpnext/manufacturing/doctype/work_order/work_order.js @@ -1,12 +1,6 @@ // 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 = { @@ -142,7 +136,7 @@ frappe.ui.form.on("Work Order", { 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) { + 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, @@ -222,6 +216,7 @@ frappe.ui.form.on("Work Order", { } frm.trigger("add_custom_button_to_return_components"); + frm.trigger("allow_alternative_item"); }, add_custom_button_to_return_components: function (frm) { @@ -552,6 +547,9 @@ frappe.ui.form.on("Work Order", { }); frappe.ui.form.on("Work Order Item", { + allow_alternative_item(frm) { + frm.trigger("allow_alternative_item"); + }, source_warehouse: function (frm, cdt, cdn) { var row = locals[cdt][cdn]; if (!row.item_code) {