brotherton-erpnext/erpnext/hr/employee_property_update.js

146 lines
3.9 KiB
JavaScript
Raw Normal View History

frappe.ui.form.on(cur_frm.doctype, {
2018-05-05 09:02:42 +00:00
setup: function(frm) {
frm.set_query("employee", function() {
return {
filters: {
"status": "Active"
}
};
2018-05-05 09:02:42 +00:00
});
},
onload: function(frm){
if(frm.doc.__islocal){
2018-05-05 09:02:42 +00:00
if(frm.doctype == "Employee Promotion"){
frm.doc.promotion_details = [];
}else if (frm.doctype == "Employee Transfer") {
frm.doc.transfer_details = [];
}
}
},
2018-05-05 09:02:42 +00:00
employee: function(frm) {
frm.add_fetch("employee", "company", "company");
2018-05-05 09:02:42 +00:00
},
refresh: function(frm) {
2018-05-05 09:02:42 +00:00
var table;
if(frm.doctype == "Employee Promotion"){
table = "promotion_details";
}else if (frm.doctype == "Employee Transfer") {
2018-05-05 11:19:51 +00:00
table = "transfer_details";
2018-05-05 09:02:42 +00:00
}
2018-05-05 11:19:51 +00:00
if(!table){return;}
cur_frm.fields_dict[table].grid.wrapper.find('.grid-add-row').hide();
cur_frm.fields_dict[table].grid.add_custom_button(__('Add Row'), () => {
if(!frm.doc.employee){
frappe.msgprint(__("Please select Employee"));
return;
}
frappe.call({
method: 'erpnext.hr.utils.get_employee_fields_label',
callback: function(r) {
if(r.message){
show_dialog(frm, table, r.message);
}
}
});
});
}
});
var show_dialog = function(frm, table, field_labels) {
var d = new frappe.ui.Dialog({
title: "Update Property",
fields: [
{fieldname: "property", label: __('Select Property'), fieldtype:"Select", options: field_labels},
{fieldname: "current", fieldtype: "Data", label:__('Current'), read_only: true},
{fieldname: "field_html", fieldtype: "HTML"}
],
primary_action_label: __('Add to Details'),
primary_action: () => {
d.get_primary_btn().attr('disabled', true);
if(d.data) {
var input = $('[data-fieldname="field_html"] input');
d.data.new = input.val();
$(input).remove();
add_to_details(frm, d, table);
}
}
});
d.fields_dict["property"].df.onchange = () => {
let property = d.get_values().property;
d.data.fieldname = property;
if(!property){return;}
frappe.call({
method: 'erpnext.hr.utils.get_employee_field_property',
args: {employee: frm.doc.employee, fieldname: property},
callback: function(r) {
if(r.message){
d.data.current = r.message.value;
d.data.property = r.message.label;
d.fields_dict.field_html.$wrapper.html("");
d.set_value('current', r.message.value);
render_dynamic_field(d, r.message.datatype, r.message.options, property);
d.get_primary_btn().attr('disabled', false);
}
}
});
};
d.get_primary_btn().attr('disabled', true);
d.data = {};
d.show();
2018-05-05 09:02:42 +00:00
};
var render_dynamic_field = function(d, fieldtype, options, fieldname) {
d.data.new = null;
var dynamic_field = frappe.ui.form.make_control({
df: {
"fieldtype": fieldtype,
"fieldname": fieldname,
"options": options || ''
},
parent: d.fields_dict.field_html.wrapper,
only_input: false
});
dynamic_field.make_input();
$(dynamic_field.label_area).text(__("New"));
2018-05-05 09:02:42 +00:00
};
var add_to_details = function(frm, d, table) {
let data = d.data;
if(data.fieldname){
if(validate_duplicate(frm, table, data.fieldname)){
frappe.show_alert({message:__("Property already added"), indicator:'orange'});
return false;
}
if(data.current == data.new){
frappe.show_alert({message:__("Nothing to change"), indicator:'orange'});
d.get_primary_btn().attr('disabled', false);
return false;
}
frm.add_child(table, {
fieldname: data.fieldname,
property: data.property,
current: data.current,
new: data.new
});
frm.refresh_field(table);
d.fields_dict.field_html.$wrapper.html("");
d.set_value("property", "");
d.set_value('current', "");
frappe.show_alert({message:__("Added to details"),indicator:'green'});
d.data = {};
}else {
frappe.show_alert({message:__("Value missing"),indicator:'red'});
}
2018-05-05 09:02:42 +00:00
};
var validate_duplicate = function(frm, table, fieldname){
let duplicate = false;
$.each(frm.doc[table], function(i, detail) {
if(detail.fieldname === fieldname){
duplicate = true;
return;
}
});
return duplicate;
2018-05-05 09:02:42 +00:00
};