Hello community, in some countries, vacation laws are as follows:
After completing your first year, you earn 12 vacation days.
After completing your second year, you earn 2 additional days.
You earn 2 days for each year completed until you reach 20. Once you get this number of days, you earn 2 days every 5 years.
Is there a way to configure these leave days by milestone?
Solution: Automatically Update Leave Allocation Based on Seniority
This solution uses a Server Script to check every employee’s joining date daily and automatically updates their leave allocation based on their years of service.
Steps to Implement:
- Search for Server Script in the Awesomebar.
- Create a New Server Script.
- Fill in the details as follows:
- Name:
Auto Update Leave Allocation
- Script Type:
Scheduler Event
- Event Frequency:
Daily
- Paste the following code into the Script field:
import frappe
from frappe.utils import date_diff, today, flt
# ================= CONFIGURATION =================
# Exact name of the Leave Type as in your system
LEAVE_TYPE_NAME = "Annual Leave"
# ===============================================
def get_entitled_days(years):
# Define your logic here:
if years >= 2:
return 16 # Employees with 2+ years of service
elif years >= 1:
return 14 # Employees with 1+ year of service
else:
return 12 # New employees (less than 1 year)
# 1. Fetch all active employees
employees = frappe.get_all("Employee",
filters={"status": "Active", "date_of_joining": ["is", "set"]},
fields=["name", "date_of_joining", "employee_name"]
)
for emp in employees:
# 2. Calculate exact years of service
years_worked = date_diff(today(), emp.date_of_joining) / 365.25
# 3. Determine entitlement for today
new_entitlement = get_entitled_days(years_worked)
# 4. Find the currently active Leave Allocation for this employee
# (Matches allocation where today falls between from_date and to_date)
allocations = frappe.get_all("Leave Allocation",
filters={
"employee": emp.name,
"leave_type": LEAVE_TYPE_NAME,
"docstatus": 1, # Must be Submitted
"from_date": ["<=", today()],
"to_date": [">=", today()]
},
fields=["name", "total_leaves_allocated"],
limit=1
)
if allocations:
alloc = allocations[0]
# 5. Update only if the current allocation is different
if flt(alloc.total_leaves_allocated) != flt(new_entitlement):
# Update the value in the database
frappe.db.set_value("Leave Allocation", alloc.name, "total_leaves_allocated", new_entitlement)
# Log the change for reference
frappe.log_error("Auto Leave Update", f"Updated {emp.employee_name} to {new_entitlement} days")
5. Click Save and ensure the script is Enabled.
How it works:
Every night, the system will run this script. It calculates the tenure for every employee. If an employee has crossed a seniority threshold (e.g., completed their 1st or 2nd year) on that day, their current leave allocation will be updated instantly to reflected the new balance.