From 1763824e5f7ccaefb83aea84db47c92f9e4c9417 Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Tue, 28 Nov 2023 17:19:21 +0530 Subject: [PATCH 1/4] refactor: use arrow function --- .../doctype/warranty_claim/warranty_claim.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/erpnext/support/doctype/warranty_claim/warranty_claim.js b/erpnext/support/doctype/warranty_claim/warranty_claim.js index 358768eb46..6888f61ae8 100644 --- a/erpnext/support/doctype/warranty_claim/warranty_claim.js +++ b/erpnext/support/doctype/warranty_claim/warranty_claim.js @@ -4,7 +4,7 @@ frappe.provide("erpnext.support"); frappe.ui.form.on("Warranty Claim", { - setup: function(frm) { + setup: (frm) => { frm.set_query('contact_person', erpnext.queries.contact_query); frm.set_query('customer_address', erpnext.queries.address_query); frm.set_query('customer', erpnext.queries.customer); @@ -20,18 +20,22 @@ frappe.ui.form.on("Warranty Claim", { frm.add_fetch('item_code', 'item_name', 'item_name'); frm.add_fetch('item_code', 'description', 'description'); }, - onload: function(frm) { + + onload: (frm) => { if(!frm.doc.status) { frm.set_value('status', 'Open'); } }, - customer: function(frm) { + + customer: (frm) => { erpnext.utils.get_party_details(frm); }, - customer_address: function(frm) { + + customer_address: (frm) => { erpnext.utils.get_address_display(frm); }, - contact_person: function(frm) { + + contact_person: (frm) => { erpnext.utils.get_contact_details(frm); } }); @@ -57,7 +61,7 @@ erpnext.support.WarrantyClaim = class WarrantyClaim extends frappe.ui.form.Contr extend_cscript(cur_frm.cscript, new erpnext.support.WarrantyClaim({frm: cur_frm})); -cur_frm.fields_dict['serial_no'].get_query = function(doc, cdt, cdn) { +cur_frm.fields_dict['serial_no'].get_query = (doc, cdt, cdn) => { var cond = []; var filter = [ ['Serial No', 'docstatus', '!=', 2] @@ -75,7 +79,7 @@ cur_frm.fields_dict['serial_no'].get_query = function(doc, cdt, cdn) { } } -cur_frm.fields_dict['item_code'].get_query = function(doc, cdt, cdn) { +cur_frm.fields_dict['item_code'].get_query = (doc, cdt, cdn) => { if(doc.serial_no) { return{ doctype: "Serial No", From 01044ca8e95ec4e84e63fcfe4928ca111cf3a75b Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Tue, 28 Nov 2023 17:33:27 +0530 Subject: [PATCH 2/4] refactor: use DocType `Fetch From` instead of `frm.add_fetch` --- .../doctype/warranty_claim/warranty_claim.js | 11 ----------- .../warranty_claim/warranty_claim.json | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/erpnext/support/doctype/warranty_claim/warranty_claim.js b/erpnext/support/doctype/warranty_claim/warranty_claim.js index 6888f61ae8..a653f840d8 100644 --- a/erpnext/support/doctype/warranty_claim/warranty_claim.js +++ b/erpnext/support/doctype/warranty_claim/warranty_claim.js @@ -8,17 +8,6 @@ frappe.ui.form.on("Warranty Claim", { frm.set_query('contact_person', erpnext.queries.contact_query); frm.set_query('customer_address', erpnext.queries.address_query); frm.set_query('customer', erpnext.queries.customer); - - frm.add_fetch('serial_no', 'item_code', 'item_code'); - frm.add_fetch('serial_no', 'item_name', 'item_name'); - frm.add_fetch('serial_no', 'description', 'description'); - frm.add_fetch('serial_no', 'maintenance_status', 'warranty_amc_status'); - frm.add_fetch('serial_no', 'warranty_expiry_date', 'warranty_expiry_date'); - frm.add_fetch('serial_no', 'amc_expiry_date', 'amc_expiry_date'); - frm.add_fetch('serial_no', 'customer', 'customer'); - frm.add_fetch('serial_no', 'customer_name', 'customer_name'); - frm.add_fetch('item_code', 'item_name', 'item_name'); - frm.add_fetch('item_code', 'description', 'description'); }, onload: (frm) => { diff --git a/erpnext/support/doctype/warranty_claim/warranty_claim.json b/erpnext/support/doctype/warranty_claim/warranty_claim.json index 01d9b01390..9af2b4606c 100644 --- a/erpnext/support/doctype/warranty_claim/warranty_claim.json +++ b/erpnext/support/doctype/warranty_claim/warranty_claim.json @@ -92,7 +92,8 @@ "fieldname": "serial_no", "fieldtype": "Link", "label": "Serial No", - "options": "Serial No" + "options": "Serial No", + "search_index": 1 }, { "fieldname": "customer", @@ -128,6 +129,8 @@ "options": "fa fa-ticket" }, { + "fetch_from": "serial_no.item_code", + "fetch_if_empty": 1, "fieldname": "item_code", "fieldtype": "Link", "in_list_view": 1, @@ -140,6 +143,7 @@ }, { "depends_on": "eval:doc.item_code", + "fetch_from": "item_code.item_name", "fieldname": "item_name", "fieldtype": "Data", "label": "Item Name", @@ -149,6 +153,7 @@ }, { "depends_on": "eval:doc.item_code", + "fetch_from": "item_code.description", "fieldname": "description", "fieldtype": "Small Text", "label": "Description", @@ -164,17 +169,24 @@ "width": "50%" }, { + "fetch_from": "serial_no.maintenance_status", + "fetch_if_empty": 1, "fieldname": "warranty_amc_status", "fieldtype": "Select", "label": "Warranty / AMC Status", - "options": "\nUnder Warranty\nOut of Warranty\nUnder AMC\nOut of AMC" + "options": "\nUnder Warranty\nOut of Warranty\nUnder AMC\nOut of AMC", + "search_index": 1 }, { + "fetch_from": "serial_no.warranty_expiry_date", + "fetch_if_empty": 1, "fieldname": "warranty_expiry_date", "fieldtype": "Date", "label": "Warranty Expiry Date" }, { + "fetch_from": "serial_no.amc_expiry_date", + "fetch_if_empty": 1, "fieldname": "amc_expiry_date", "fieldtype": "Date", "label": "AMC Expiry Date" @@ -225,6 +237,7 @@ { "bold": 1, "depends_on": "customer", + "fetch_from": "customer.customer_name", "fieldname": "customer_name", "fieldtype": "Data", "in_global_search": 1, @@ -366,7 +379,7 @@ "icon": "fa fa-bug", "idx": 1, "links": [], - "modified": "2023-06-03 16:17:07.694449", + "modified": "2023-11-28 17:30:35.676410", "modified_by": "Administrator", "module": "Support", "name": "Warranty Claim", From 640dfab827f2e83b9c3ae7ff839c6f94b63b71b2 Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Tue, 28 Nov 2023 17:45:17 +0530 Subject: [PATCH 3/4] refactor: use `frm.set_query` to add filters --- .../doctype/warranty_claim/warranty_claim.js | 102 +++++++++--------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/erpnext/support/doctype/warranty_claim/warranty_claim.js b/erpnext/support/doctype/warranty_claim/warranty_claim.js index a653f840d8..32494efe53 100644 --- a/erpnext/support/doctype/warranty_claim/warranty_claim.js +++ b/erpnext/support/doctype/warranty_claim/warranty_claim.js @@ -5,14 +5,34 @@ frappe.provide("erpnext.support"); frappe.ui.form.on("Warranty Claim", { setup: (frm) => { - frm.set_query('contact_person', erpnext.queries.contact_query); - frm.set_query('customer_address', erpnext.queries.address_query); - frm.set_query('customer', erpnext.queries.customer); + frm.set_query("contact_person", erpnext.queries.contact_query); + frm.set_query("customer_address", erpnext.queries.address_query); + frm.set_query("customer", erpnext.queries.customer); + + frm.set_query("serial_no", () => { + let filters = { + company: frm.doc.company, + }; + + if (frm.doc.item_code) { + filters["item_code"] = frm.doc.item_code; + } + + return { filters: filters }; + }); + + frm.set_query("item_code", () => { + return { + filters: { + disabled: 0, + }, + }; + }); }, onload: (frm) => { - if(!frm.doc.status) { - frm.set_value('status', 'Open'); + if (!frm.doc.status) { + frm.set_value("status", "Open"); } }, @@ -26,64 +46,40 @@ frappe.ui.form.on("Warranty Claim", { contact_person: (frm) => { erpnext.utils.get_contact_details(frm); - } + }, }); -erpnext.support.WarrantyClaim = class WarrantyClaim extends frappe.ui.form.Controller { +erpnext.support.WarrantyClaim = class WarrantyClaim extends ( + frappe.ui.form.Controller +) { refresh() { - frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} + frappe.dynamic_link = { + doc: this.frm.doc, + fieldname: "customer", + doctype: "Customer", + }; - if(!cur_frm.doc.__islocal && - (cur_frm.doc.status=='Open' || cur_frm.doc.status == 'Work In Progress')) { - cur_frm.add_custom_button(__('Maintenance Visit'), - this.make_maintenance_visit); + if ( + !cur_frm.doc.__islocal && + (cur_frm.doc.status == "Open" || + cur_frm.doc.status == "Work In Progress") + ) { + cur_frm.add_custom_button( + __("Maintenance Visit"), + this.make_maintenance_visit + ); } } make_maintenance_visit() { frappe.model.open_mapped_doc({ method: "erpnext.support.doctype.warranty_claim.warranty_claim.make_maintenance_visit", - frm: cur_frm - }) + frm: cur_frm, + }); } }; -extend_cscript(cur_frm.cscript, new erpnext.support.WarrantyClaim({frm: cur_frm})); - -cur_frm.fields_dict['serial_no'].get_query = (doc, cdt, cdn) => { - var cond = []; - var filter = [ - ['Serial No', 'docstatus', '!=', 2] - ]; - if(doc.item_code) { - cond = ['Serial No', 'item_code', '=', doc.item_code]; - filter.push(cond); - } - if(doc.customer) { - cond = ['Serial No', 'customer', '=', doc.customer]; - filter.push(cond); - } - return{ - filters:filter - } -} - -cur_frm.fields_dict['item_code'].get_query = (doc, cdt, cdn) => { - if(doc.serial_no) { - return{ - doctype: "Serial No", - fields: "item_code", - filters:{ - name: doc.serial_no - } - } - } - else{ - return{ - filters:[ - ['Item', 'docstatus', '!=', 2], - ['Item', 'disabled', '=', 0] - ] - } - } -}; +extend_cscript( + cur_frm.cscript, + new erpnext.support.WarrantyClaim({ frm: cur_frm }) +); From 9fadf5f42678736567160bb2b06619383146d4ca Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Tue, 28 Nov 2023 21:34:07 +0530 Subject: [PATCH 4/4] refactor: don't use `cur_frm` --- .../doctype/warranty_claim/warranty_claim.js | 55 +++++++------------ 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/erpnext/support/doctype/warranty_claim/warranty_claim.js b/erpnext/support/doctype/warranty_claim/warranty_claim.js index 32494efe53..10cb37f512 100644 --- a/erpnext/support/doctype/warranty_claim/warranty_claim.js +++ b/erpnext/support/doctype/warranty_claim/warranty_claim.js @@ -36,6 +36,26 @@ frappe.ui.form.on("Warranty Claim", { } }, + refresh: (frm) => { + frappe.dynamic_link = { + doc: frm.doc, + fieldname: "customer", + doctype: "Customer", + }; + + if ( + !frm.doc.__islocal && + ["Open", "Work In Progress"].includes(frm.doc.status) + ) { + frm.add_custom_button(__("Maintenance Visit"), () => { + frappe.model.open_mapped_doc({ + method: "erpnext.support.doctype.warranty_claim.warranty_claim.make_maintenance_visit", + frm: frm, + }); + }); + } + }, + customer: (frm) => { erpnext.utils.get_party_details(frm); }, @@ -48,38 +68,3 @@ frappe.ui.form.on("Warranty Claim", { erpnext.utils.get_contact_details(frm); }, }); - -erpnext.support.WarrantyClaim = class WarrantyClaim extends ( - frappe.ui.form.Controller -) { - refresh() { - frappe.dynamic_link = { - doc: this.frm.doc, - fieldname: "customer", - doctype: "Customer", - }; - - if ( - !cur_frm.doc.__islocal && - (cur_frm.doc.status == "Open" || - cur_frm.doc.status == "Work In Progress") - ) { - cur_frm.add_custom_button( - __("Maintenance Visit"), - this.make_maintenance_visit - ); - } - } - - make_maintenance_visit() { - frappe.model.open_mapped_doc({ - method: "erpnext.support.doctype.warranty_claim.warranty_claim.make_maintenance_visit", - frm: cur_frm, - }); - } -}; - -extend_cscript( - cur_frm.cscript, - new erpnext.support.WarrantyClaim({ frm: cur_frm }) -);