From bbfc34f42733e669bd7aa11f8cbf598231db226e Mon Sep 17 00:00:00 2001 From: Ashna-v Date: Fri, 3 Nov 2023 17:29:09 +0530 Subject: [PATCH] fix: Edited Button in Design Analysis --- .../design_analysis/design_analysis.js | 298 +++++++++--------- .../design_analysis/design_analysis.json | 19 +- .../design_analysis/design_analysis.py | 18 +- 3 files changed, 172 insertions(+), 163 deletions(-) diff --git a/aumms/aumms/doctype/design_analysis/design_analysis.js b/aumms/aumms/doctype/design_analysis/design_analysis.js index 0274cca7..d8a7dfcc 100644 --- a/aumms/aumms/doctype/design_analysis/design_analysis.js +++ b/aumms/aumms/doctype/design_analysis/design_analysis.js @@ -48,150 +48,18 @@ frappe.ui.form.on('Design Analysis', { }); } }, - refresh: function(frm) { - frm.add_custom_button('Request For Approval', () => { - console.log("Refresh"); - let dia = new frappe.ui.Dialog({ - title: __('Request for Approval'), - fields: [ - { - fieldtype: 'Check', - label: 'Self Approval', - fieldname: 'self_approval', - default: 0, - onchange: function() { - dia.toggle_enable('assign_to', !this.value); - if (this.value) { - dia.set_value('assign_to', null); // Clear value when Self Assign is checked - } - } - }, - { - fieldtype: 'Link', - label: 'Assign To', - fieldname: 'assign_to', - options: 'User', - get_query: function () { - return { - query : 'aumms.aumms.doctype.design_analysis.design_analysis.supervisor_user_query', - }; - }, - depends_on: 'eval: !doc.self_approval' - } - ], - primary_action_label: __('Submit'), - //primary_action: function() { - // const values = dia.get_values(); - // console.log(values.self_approval); - //console.log(values.assign_to); - primary_action(values) { - let assign_to = ''; - if(values.self_assign){ - assign_to = frappe.session.user; - } - else{ - assign_to = values.assign_to; - } - frappe.call({ - method: 'frappe.desk.form.assign_to.add', - args: { - doctype: frm.doc.doctype, - name: frm.doc.name, - assign_to: [assign_to] - }, - freeze: true, - callback: (r) => { - console.log(r) - }, - }) - // Update the button text after successful assignment - let assignButton = document.getElementById('assignButton'); - assignButton.textContent = 'Assigned'; - // Optional: Show a message after the form fields are updated - frappe.msgprint(__('Design Analysis has been approved.')); - // Close the dialog after submitting - dia.hide(); - } - }); - - // Show the dialog - dia.show(); - }); - // Check if the logged-in user is a supervisor - const isSupervisor = frappe.user_roles.includes('Supervisor'); - if (isSupervisor) { - frm.add_custom_button(__('Approve'), () => { - const item_code = frm.doc.item_code; - const item_group = frm.doc.item_group; - const purity = frm.doc.purity; - const customer_expected_weight = frm.doc.customer_expected_weight; - - frappe.call({ - method: 'aumms.aumms.doctype.design_analysis.design_analysis.create_aumms_item_from_design_analysis', - args: { - item: item_code, - item_group: item_group, - purity: purity, - customer_expected_weight: customer_expected_weight, - }, - callback: (r) => { - if (r.message) { - frm.set_value('aumms_item', r.message); - frm.save(); - console.log('AuMMS Item Created:', r.message); - } else { - console.log('Failed to create AuMMS Item'); - } - } - }); - }); - } - frm.add_custom_button(__('Create BOM'), () => { - // Call a function to create the BOM - frm.trigger('create_bom'); - }); - frm.add_custom_button(__('Proceed'), () => { - frm.trigger('proceed_action'); - hide_proceed_button(frm) - }); - }, - create_bom: function(frm) { - frappe.call({ - method: 'aumms.aumms.doctype.design_analysis.design_analysis.create_bom_function', - args: { - design_analysis: frm.doc.name - }, - callback: (r) => { - if (r.message) { - frappe.msgprint(__('BOM created successfully.')); - frm.remove_custom_button('Create BOM') - } else { - frappe.throw(__('Failed to create BOM.')); - } - } - }); - }, - proceed_action: function(frm){ - if(frm.is_dirty()){ - frappe.throw('You have unsaved changed. Please save and continue.') - } - else{ - if(frm.doc.dr_required_check){ - frappe.call({ - method: 'aumms.aumms.doctype.design_analysis.design_analysis.create_design_request', - args: { - design_analysis: frm.doc.name - } - }) - } - else{ - frm.scroll_to_field('verified_item'); - if(frm.doc.verified_item.length<1){ - frappe.msgprint("Please fill the verified item table"); - } - } - } + refresh: function(frm){ + if(frm.doc.status == 'Draft'){ + create_custom_buttons(frm); + } + if(frm.doc.status == 'Request For Verification'){ + request_for_approval(frm); + } + if(frm.doc.status == 'Request For Approval'){ + approve_design_analysis(frm) + } }, + check_dr_required: function(frm){ let dr_required = 0; frm.doc.design_details.forEach(function(detail) { @@ -205,6 +73,136 @@ frappe.ui.form.on('Design Analysis', { frm.trigger('check_dr_required'); } }); + +let create_custom_buttons = function(frm){ + if(!frm.is_new()){ + frm.add_custom_button('Request For Verification',() =>{ + request_for_verification(frm); + }, 'Actions'); + } +} + +let request_for_verification = function(frm){ + if(frm.doc.dr_required_check){ + frappe.call({ + method: 'aumms.aumms.doctype.design_analysis.design_analysis.create_design_request', + args: { + design_analysis: frm.doc.name + }, + callback: (r) => { + frm.reload_doc() + }, + }) + } + else{ + frm.scroll_to_field('verified_item'); + if(frm.doc.verified_item.length<1){ + frappe.msgprint("Please fill the verified item table"); + } + } +} + +let request_for_approval = function(frm){ + if(frm.doc.status == "Request For Verification"){ + frm.add_custom_button('Request For Approval', () =>{ + make_request_for_approval(frm); + },'Actions'); + } +} + +let make_request_for_approval = function(frm){ + let d = new frappe.ui.Dialog({ + title: __('Request for Approval'), + fields: [ + { + fieldtype: 'Check', + label: 'Self Assign', + fieldname: 'self_assign', + default: 0, + onchange: function(e) { + d.toggle_enable('assign_to', !e.checked); + if (e.checked) { + d.set_value('assign_to', frappe.session.user); + } + } + }, + { + fieldtype: 'Link', + label: 'Assign To', + fieldname: 'assign_to', + options: 'User', + get_query: function () { + return { + query : 'aumms.aumms.doctype.design_analysis.design_analysis.supervisor_user_query', + }; + }, + depends_on: 'eval: !doc.self_assign' + } + ], + primary_action_label: __('Submit'), + primary_action(values) { + let assign_to = ''; + if(values.self_assign){ + assign_to = frappe.session.user; + } + else{ + assign_to = values.assign_to; + } + frappe.call({ + method: 'aumms.aumms.doctype.design_analysis.design_analysis.assign_design_analysis', + args: { + doctype: frm.doc.doctype, + docname: frm.doc.name, + assign_to: assign_to + }, + freeze: true, + callback: (r) => { + frm.reload_doc(); + d.hide() + } + }); + } + }); + d.show(); +} + +// Check if the logged-in user is a supervisor +const isSupervisor = frappe.user_roles.includes('Supervisor'); +let approve_design_analysis = function(frm) { + if (frm.doc.status === "Request For Approval") { + if (isSupervisor) { + frm.add_custom_button('Approve', () => { + const item_code = frm.doc.item_code; + const item_group = frm.doc.item_group; + const purity = frm.doc.purity; + const customer_expected_weight = frm.doc.customer_expected_weight; + + frappe.call({ + method: 'aumms.aumms.doctype.design_analysis.design_analysis.create_aumms_item_from_design_analysis', + args: { + item: item_code, + item_group: item_group, + purity: purity, + customer_expected_weight: customer_expected_weight, + }, + callback: (r) => { + if (r.message) { + frm.set_value('aumms_item', r.message); + frm.set_value("status","Approved") + frm.save(); + console.log('AuMMS Item Created:', r.message); + } else { + console.log('Failed to create AuMMS Item'); + } + } + }); + }, 'Actions'); + frm.add_custom_button('Reject', () =>{ + },'Actions'); + } + } +} + frappe.ui.form.on('Verified Item',{ item: function(frm,cdt,cdn){ let d = locals[cdt][cdn]; @@ -234,17 +232,3 @@ frappe.ui.form.on('Verified Item',{ frm.set_value('calculated_stone_weight',calculated_stone_weight) }, }); - -let hide_proceed_button = function(frm) { - frappe.call({ - method: 'aumms.aumms.doctype.design_analysis.design_analysis.hide_proceed_button', - args: { - 'customer' : frm.doc.customer_name - }, - callback: (r) => { - if(r.message) { - frm.remove_custom_button('Proceed') - } - } - }) -} diff --git a/aumms/aumms/doctype/design_analysis/design_analysis.json b/aumms/aumms/doctype/design_analysis/design_analysis.json index d1711cdd..9b125ab6 100644 --- a/aumms/aumms/doctype/design_analysis/design_analysis.json +++ b/aumms/aumms/doctype/design_analysis/design_analysis.json @@ -10,6 +10,7 @@ "aumms_item", "item", "customer_name", + "status", "approver_weight", "column_break_v9qzp", "item_code", @@ -29,7 +30,8 @@ "gold_weight", "calculated_stone_weight", "section_break_qpifr", - "expected_weight" + "expected_weight", + "assigned_person" ], "fields": [ { @@ -171,11 +173,24 @@ "fieldname": "calculated_stone_weight", "fieldtype": "Data", "label": "Calculated Stone Weight" + }, + { + "fieldname": "status", + "fieldtype": "Select", + "label": "Status", + "options": "Draft\nRequest For Verification\nRequest For Approval\nApproved\nHold\nRejected\nBOM Created\nWorkorder Created" + }, + { + "fieldname": "assigned_person", + "fieldtype": "Link", + "hidden": 1, + "label": "Assigned Person", + "options": "User" } ], "index_web_pages_for_search": 1, "links": [], - "modified": "2023-11-02 11:04:13.811167", + "modified": "2023-11-03 17:23:27.828034", "modified_by": "Administrator", "module": "AuMMS", "name": "Design Analysis", diff --git a/aumms/aumms/doctype/design_analysis/design_analysis.py b/aumms/aumms/doctype/design_analysis/design_analysis.py index 6682093c..6d1a1055 100644 --- a/aumms/aumms/doctype/design_analysis/design_analysis.py +++ b/aumms/aumms/doctype/design_analysis/design_analysis.py @@ -3,6 +3,7 @@ import frappe from frappe.model.document import Document +from frappe.desk.form.assign_to import add as add_assign class DesignAnalysis(Document): def autoname(self): @@ -87,10 +88,19 @@ def create_design_request(design_analysis): design_request.delivery_date = frappe.utils.today() design_request.flags.ignore_mandatory = True design_request.save(ignore_permissions=True) + if doc.status == 'Draft': + frappe.db.set_value("Design Analysis", design_analysis, 'status', 'Request For Verification') + doc.reload() frappe.msgprint("Design Request Created for the material {}".format(design_request.design_title), indicator="green", alert=1) @frappe.whitelist() -def hide_proceed_button(customer): - if frappe.db.exists('Design Request', {'customer' : customer}): - design_request_doc = frappe.get_doc('Design Request', {'customer' : customer}) - return 1 +def assign_design_analysis(doctype, docname, assign_to): + assign_to_list = [assign_to] + add_assign({ + "assign_to": assign_to_list, + "doctype": doctype, + "name": docname + }) + frappe.db.set_value(doctype, docname, 'assigned_person', assign_to) + frappe.db.set_value(doctype, docname, 'status','Request For Approval') + frappe.db.commit()