Skip to content

Instantly share code, notes, and snippets.

@nathando
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathando/4bd88afb66dccfb5b1ec to your computer and use it in GitHub Desktop.
Save nathando/4bd88afb66dccfb5b1ec to your computer and use it in GitHub Desktop.
Leave Application Email
set_notified = function(doc, dt, dn) {
frappe.call({
method:"frappe.client.set_value",
args: {
doctype: dt,
name: dn,
fieldname: 'notified_by_email',
value: 1
},
callback: function() {
console.log('Notified by email');
}
})
}
cur_frm.cscript.custom_refresh = function(doc, dt, dn) {
// New, not saved, set the default employer
if (doc.__islocal) {
frappe.call({
method: "frappe.client.get",
args: {
doctype: "Employee",
filters: {
user_id: cur_frm.doc.owner
}
},
callback: function(c) {
var emp = c.message;
if (emp) {
// Set default Leave Approver
//console.log(emp.employee_leave_approvers);
if (emp.employee_leave_approvers && emp.employee_leave_approvers.length > 0) {
cur_frm.set_value("leave_approver", emp.employee_leave_approvers[0].leave_approver);
}
// Set the only Employee
cur_frm.set_value("employee", emp.name);
cur_frm.set_value("employee_name", emp.employee_name);
}
}
});
}
// Saved but not notified to leave_approver
else if (doc.docstatus === 0 && doc.notified_by_email === 0 && user == doc.owner) {
// Get Leave approver's name
var la_field = cur_frm.get_field('leave_approver').df;
var la_name = la_field.options[la_field.idx].label || " [Leave Approver]";
// Template for leave message
var leave_message = "";
leave_message = 'Hi ' + la_name + ', </br></br>' + 'I would like to apply for leave during this period from ' + doc.from_date + ' to ' + doc.to_date + '</br>' + (doc.description ? 'Reason: ' + doc.description + '</br>' : '') + '</br>Regards, </br>' + doc.employee_name;
// Open Email
cur_frm.email_doc(leave_message);
// Set recipient
cur_dialog.set_value("recipients", cur_frm.doc.leave_approver);
// Set notified right away, no need to wait for email to be sent. If they cancel they will need to open email again
set_notified(doc, dt, dn);
}
// Saved but not notified to owner
else if (doc.docstatus === 1 && doc.notified_by_email === 0 && user == doc.leave_approver && doc.status != "Open") {
// Template for leave message
var leave_message = "Leave " + doc.status;
// Open Email
cur_frm.email_doc(leave_message);
// Set recipient
cur_dialog.set_value("recipients", doc.owner);
// Set notified right away, no need to wait for email to be sent. If they cancel they will need to open email again
set_notified(doc, dt, dn);
}
}
// Reset the notified flag if dirty
$(cur_frm.wrapper).on('dirty', function(e) {
cur_frm.set_value('notified_by_email', 0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment