Fetch late_entry field as count in employee doctype

Hi @jaissunny009,

That for, you have to use the Scheduler Events but you can also use it in server script doctype.

Script:

current_date = frappe.utils.getdate(frappe.utils.today())
first_day_of_month = current_date.replace(day=1)

# Reset custom_late_entry on the 1st of each month
if current_date == first_day_of_month:
    frappe.db.sql("""UPDATE `tabEmployee` SET custom_late_entry = 0""")
    frappe.db.commit()

# Count late entries for the current month
attendance_records = frappe.db.sql("""
    SELECT employee, COUNT(*) as late_count
    FROM `tabAttendance`
    WHERE late_entry = 1 
    AND attendance_date BETWEEN %s AND %s
    GROUP BY employee
""", (first_day_of_month, current_date), as_dict=True)

for record in attendance_records:
    frappe.db.set_value('Employee', record.employee, 'custom_late_entry', record.late_count)

The above script will run daily and check. then update the count in the employee doctype.

If the server script is not enabled then please enable it using the below command:

bench --site your-sitename.com set-config -g server_script_enabled 1
1 Like