How to ensure no two leave types are clubbed

Hi, how can I stop employees using two leave types to take consecutive leaves.

For example, they shouldn’t use Casual Leave and Sick leave to request continuous leaves.

@anilpoda Write client script for that if before one day of start date and after one day of end date any other type of leave are exists then throw error message on validate event write your query

Example :

frappe.ui.form.on('Leave Application', {
    validate: function(frm) {
        // Define start and end dates with a buffer of one day
        let start_date = frappe.datetime.add_days(frm.doc.from_date, -1);
        let end_date = frappe.datetime.add_days(frm.doc.to_date, 1);

        frappe.call({
            method: 'frappe.client.get_list',
            args: {
                doctype: 'Leave Application',
                filters: [
                    ['employee', '=', frm.doc.employee],
                    ['leave_type', '!=', frm.doc.leave_type],  // Ensure leave type is different
                    ['from_date', '<=', end_date],
                    ['to_date', '>=', start_date],
                    ['docstatus', '<', 2]  // Exclude cancelled leaves
                ],
                fields: ['name', 'leave_type', 'from_date', 'to_date']
            },
            callback: function(r) {
                if (r.message.length > 0) {
                    // Ensure the leave type is not the same as the current application
                    r.message.forEach(function(leave) {
                        if (leave.leave_type !== frm.doc.leave_type) {
                            frappe.msgprint(__('You cannot apply for this leave as another type of leave exists one day before the start date or one day after the end date.'));
                            frappe.validated = false;
                        }
                    });
                }
            }
        });
    }
});
1 Like

@Meet But they can apply consecutive days with two different types right?

Like 1st 2 days casual, next 2 days sick. May be we cant avoid this situation!

@anilpoda This code ensures that an employee cannot apply for two different types of leave consecutively.

@Meet , Thanks, can you modify this code, to allow Sick and Earned leaves combination and throw error for any other combination?