Hi Community !!
In Employee Master, I have added 2 fields “Years in Service” and “Probation Status”.
Normally, the Years in Service calculating automatically based on DOJ and The Probation Status having 2 option Done & Not Done. When the Probation End Date meet the Current Date, then it automatically change into Done. My Problem is, I have added two Charts based on Years in Service & Probation end in future, So this bar generated by taking the updated employee master data’s. But when the Years in service & probation status trigger, then the employee master going to “Not Saved” Status, once saved then only the bar chart generate by updated value. So i need a solution that Y in service & Probation status trigger without change the Status of Employee master. I have added client script & Image for reference.
frappe.ui.form.on('Employee', {
onload: function(frm) {
// Trigger the calculation when the form loads
calculateYearOfService(frm);
},
date_of_joining: function(frm) {
// Trigger the calculation when the "Date of Joining" field changes
calculateYearOfService(frm);
},
date: function(frm) {
// Trigger the calculation when the "Date" field changes
calculateYearOfService(frm);
}
});
function calculateYearOfService(frm) {
// Get the values of "Date of Joining" and "Date" fields
var dateOfJoining = frm.doc.date_of_joining;
var currentDate = frm.doc.date;
// Calculate the difference in days
var daysOfService = frappe.datetime.get_diff(dateOfJoining, currentDate);
// Convert days to fraction of a year (considering a year as 365.25 days)
var yearOfService = Math.abs((daysOfService / 365.25).toFixed(2));
// Set the calculated value in the "Year of Service" field
frm.set_value('custom_years_of_service_', parseFloat(yearOfService));
}
frappe.ui.form.on('Employee', {
// Trigger when the form is loaded
onload: function(frm) {
// Call the functions to calculate probation end date and update status on form load
calculate_probation_end_date(frm);
update_probation_status(frm);
},
// Trigger when custom_probation_months or date_of_joining field changes
custom_probation_months: function(frm) {
calculate_probation_end_date(frm);
update_probation_status(frm); // Update status after calculation
},
date_of_joining: function(frm) {
calculate_probation_end_date(frm);
update_probation_status(frm); // Update status after calculation
},
custom_probation_end_date: function(frm) {
update_probation_status(frm); // Update status if the end date is changed manually
}
});
// Function to calculate the probation end date
function calculate_probation_end_date(frm) {
if (frm.doc.date_of_joining && frm.doc.custom_probation_months) {
// Get the date of joining and probation months
var date_of_joining = frm.doc.date_of_joining;
var probation_months = frm.doc.custom_probation_months;
// Create a date object from date_of_joining
var doj = new Date(date_of_joining);
// Add probation months to the date
doj.setMonth(doj.getMonth() + probation_months);
// Ensure to set the date to the start of the day (midnight) to avoid time issues
doj.setHours(0, 0, 0, 0);
// Set the custom_probation_end_date field to the new date
frm.set_value('custom_probation_end_date', doj);
}
}
// Function to update the probation status based on the current date and custom_probation_end_date
function update_probation_status(frm) {
if (frm.doc.custom_probation_end_date) {
var probation_end_date = new Date(frm.doc.custom_probation_end_date);
var current_date = new Date(); // Get the current date from the system
// Ensure both dates are set to midnight (00:00:00) to compare only the date part
probation_end_date.setHours(0, 0, 0, 0);
current_date.setHours(0, 0, 0, 0);
// Compare current date with the probation end date
if (current_date < probation_end_date) {
frm.set_value('custom_probation_status', 'Not Done');
} else {
frm.set_value('custom_probation_status', 'Done');
}
}
}