Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 1 | frappe.ui.form.on(cur_frm.doctype, { |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 2 | setup: function(frm) { |
| 3 | frm.set_query("employee", function() { |
| 4 | return { |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 5 | filters: { |
| 6 | "status": "Active" |
| 7 | } |
| 8 | }; |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 9 | }); |
| 10 | }, |
| 11 | onload: function(frm){ |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 12 | if(frm.doc.__islocal){ |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 13 | if(frm.doctype == "Employee Promotion"){ |
| 14 | frm.doc.promotion_details = []; |
| 15 | }else if (frm.doctype == "Employee Transfer") { |
| 16 | frm.doc.transfer_details = []; |
| 17 | } |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 18 | } |
| 19 | }, |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 20 | employee: function(frm) { |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 21 | frm.add_fetch("employee", "company", "company"); |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 22 | }, |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 23 | refresh: function(frm) { |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 24 | var table; |
| 25 | if(frm.doctype == "Employee Promotion"){ |
| 26 | table = "promotion_details"; |
| 27 | }else if (frm.doctype == "Employee Transfer") { |
Ranjith | 86b81e9 | 2018-05-05 16:49:51 +0530 | [diff] [blame] | 28 | table = "transfer_details"; |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 29 | } |
Ranjith | 86b81e9 | 2018-05-05 16:49:51 +0530 | [diff] [blame] | 30 | if(!table){return;} |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 31 | cur_frm.fields_dict[table].grid.wrapper.find('.grid-add-row').hide(); |
| 32 | cur_frm.fields_dict[table].grid.add_custom_button(__('Add Row'), () => { |
| 33 | if(!frm.doc.employee){ |
| 34 | frappe.msgprint(__("Please select Employee")); |
| 35 | return; |
| 36 | } |
| 37 | frappe.call({ |
| 38 | method: 'erpnext.hr.utils.get_employee_fields_label', |
| 39 | callback: function(r) { |
| 40 | if(r.message){ |
| 41 | show_dialog(frm, table, r.message); |
| 42 | } |
| 43 | } |
| 44 | }); |
| 45 | }); |
| 46 | } |
| 47 | }); |
| 48 | |
| 49 | var show_dialog = function(frm, table, field_labels) { |
| 50 | var d = new frappe.ui.Dialog({ |
| 51 | title: "Update Property", |
| 52 | fields: [ |
| 53 | {fieldname: "property", label: __('Select Property'), fieldtype:"Select", options: field_labels}, |
| 54 | {fieldname: "current", fieldtype: "Data", label:__('Current'), read_only: true}, |
| 55 | {fieldname: "field_html", fieldtype: "HTML"} |
| 56 | ], |
| 57 | primary_action_label: __('Add to Details'), |
| 58 | primary_action: () => { |
| 59 | d.get_primary_btn().attr('disabled', true); |
Shreya | 14069c9 | 2018-05-15 15:19:24 +0530 | [diff] [blame] | 60 | if(d.data) { |
| 61 | var input = $('[data-fieldname="field_html"] input'); |
| 62 | d.data.new = input.val(); |
| 63 | $(input).remove(); |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 64 | add_to_details(frm, d, table); |
| 65 | } |
| 66 | } |
| 67 | }); |
| 68 | d.fields_dict["property"].df.onchange = () => { |
| 69 | let property = d.get_values().property; |
| 70 | d.data.fieldname = property; |
| 71 | if(!property){return;} |
| 72 | frappe.call({ |
| 73 | method: 'erpnext.hr.utils.get_employee_field_property', |
| 74 | args: {employee: frm.doc.employee, fieldname: property}, |
| 75 | callback: function(r) { |
| 76 | if(r.message){ |
| 77 | d.data.current = r.message.value; |
| 78 | d.data.property = r.message.label; |
| 79 | d.fields_dict.field_html.$wrapper.html(""); |
| 80 | d.set_value('current', r.message.value); |
| 81 | render_dynamic_field(d, r.message.datatype, r.message.options, property); |
| 82 | d.get_primary_btn().attr('disabled', false); |
| 83 | } |
| 84 | } |
| 85 | }); |
| 86 | }; |
| 87 | d.get_primary_btn().attr('disabled', true); |
| 88 | d.data = {}; |
| 89 | d.show(); |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 90 | }; |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 91 | |
| 92 | var render_dynamic_field = function(d, fieldtype, options, fieldname) { |
| 93 | d.data.new = null; |
| 94 | var dynamic_field = frappe.ui.form.make_control({ |
| 95 | df: { |
| 96 | "fieldtype": fieldtype, |
| 97 | "fieldname": fieldname, |
| 98 | "options": options || '' |
| 99 | }, |
| 100 | parent: d.fields_dict.field_html.wrapper, |
| 101 | only_input: false |
| 102 | }); |
| 103 | dynamic_field.make_input(); |
| 104 | $(dynamic_field.label_area).text(__("New")); |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 105 | }; |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 106 | |
| 107 | var add_to_details = function(frm, d, table) { |
| 108 | let data = d.data; |
| 109 | if(data.fieldname){ |
| 110 | if(validate_duplicate(frm, table, data.fieldname)){ |
| 111 | frappe.show_alert({message:__("Property already added"), indicator:'orange'}); |
| 112 | return false; |
| 113 | } |
| 114 | if(data.current == data.new){ |
| 115 | frappe.show_alert({message:__("Nothing to change"), indicator:'orange'}); |
| 116 | d.get_primary_btn().attr('disabled', false); |
| 117 | return false; |
| 118 | } |
| 119 | frm.add_child(table, { |
| 120 | fieldname: data.fieldname, |
| 121 | property: data.property, |
| 122 | current: data.current, |
| 123 | new: data.new |
| 124 | }); |
| 125 | frm.refresh_field(table); |
| 126 | d.fields_dict.field_html.$wrapper.html(""); |
| 127 | d.set_value("property", ""); |
| 128 | d.set_value('current', ""); |
| 129 | frappe.show_alert({message:__("Added to details"),indicator:'green'}); |
| 130 | d.data = {}; |
| 131 | }else { |
| 132 | frappe.show_alert({message:__("Value missing"),indicator:'red'}); |
| 133 | } |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 134 | }; |
Ranjith | fddfffd | 2018-05-05 13:27:26 +0530 | [diff] [blame] | 135 | |
| 136 | var validate_duplicate = function(frm, table, fieldname){ |
| 137 | let duplicate = false; |
| 138 | $.each(frm.doc[table], function(i, detail) { |
| 139 | if(detail.fieldname === fieldname){ |
| 140 | duplicate = true; |
| 141 | return; |
| 142 | } |
| 143 | }); |
| 144 | return duplicate; |
Ranjith | 9505ea9 | 2018-05-05 14:32:42 +0530 | [diff] [blame] | 145 | }; |