Fetch late_entry field as count in employee doctype

Hyy Community. Can anyone help me build logic.

We have late_entry field in Attendance Doctype, I want to capture this late entry as count field in custom_late_entry field I created in employee doctype which get auto filled based on no. of checked late_entry and on every 1st of new monthn it should go to 0 and again start counting late_entry if ther is for any employee. How can I do this?

Scenario:- let say for the month of august, an employee has total of late_entry checked as 5 so in the custom_late_entry field I want to capture 5 and on 1st of september it should go to zero and again start cunting late_entry if any.

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

@NCP
Eagerly waiting for your response. Thanks you very much. Keep up your contribution. Really Appreciated