From a9fcd1e1cc104491f74ffce0277cec6afd574ca0 Mon Sep 17 00:00:00 2001 From: iphenelist Date: Mon, 13 Jul 2026 09:34:01 +0300 Subject: [PATCH] feat: employee check-in alert feature and integrate with daily scheduler --- .../av_tools_settings/av_tools_settings.json | 16 +++++- av_tools/av_tools_hooks/employee_checkins.py | 57 +++++++++++++++++++ av_tools/hooks.py | 1 + 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 av_tools/av_tools_hooks/employee_checkins.py diff --git a/av_tools/av_tools/doctype/av_tools_settings/av_tools_settings.json b/av_tools/av_tools/doctype/av_tools_settings/av_tools_settings.json index 5901ac7..9feab02 100644 --- a/av_tools/av_tools/doctype/av_tools_settings/av_tools_settings.json +++ b/av_tools/av_tools/doctype/av_tools_settings/av_tools_settings.json @@ -30,7 +30,9 @@ "enable_validate_item_remaining_qty", "parallel_approval_section", "enable_multi_approval_document", - "approval_doctype" + "approval_doctype", + "employee_checkins_section", + "enable_missing_employee_checkin_alert" ], "fields": [ { @@ -191,6 +193,18 @@ "fieldtype": "Table MultiSelect", "label": "Approval Doctype", "options": "Approval Doctypes" + }, + { + "fieldname": "employee_checkins_section", + "fieldtype": "Section Break", + "label": "Employee Checkins" + }, + { + "default": "0", + "description": "When enabled, a daily scheduled job emails all active System Managers if any active employee has no Employee Checkin recorded for the previous day.", + "fieldname": "enable_missing_employee_checkin_alert", + "fieldtype": "Check", + "label": "Enable Missing Employee Checkin Alert" } ], "index_web_pages_for_search": 1, diff --git a/av_tools/av_tools_hooks/employee_checkins.py b/av_tools/av_tools_hooks/employee_checkins.py new file mode 100644 index 0000000..6732415 --- /dev/null +++ b/av_tools/av_tools_hooks/employee_checkins.py @@ -0,0 +1,57 @@ +import frappe +from frappe.utils import add_days, today, format_date + +def check_missing_employee_checkins(): + """ + Checks if active employees missed checking in yesterday. + Sends an email alert to all active System Managers if any are missing. + """ + if not frappe.db.get_single_value("AV Tools Settings", "enable_missing_employee_checkin_alert"): + return + + # 1. Get yesterday's date + yesterday = add_days(today(), -1) + start_of_yesterday = f"{yesterday} 00:00:00" + end_of_yesterday = f"{yesterday} 23:59:59" + + # 2. Fetch all unique employee IDs who DID check in yesterday + checked_in_ids = frappe.get_all( + "Employee Checkin", + filters={"time": ["between", [start_of_yesterday, end_of_yesterday]]}, + pluck="employee" + ) + # Filter unique IDs + checked_in_ids = list(set(checked_in_ids)) + + # 3. Fetch all Active employees + active_employees = frappe.get_all( + "Employee", + filters={"status": "Active"}, + fields=["name", "employee_name"] + ) + + # 4. Filter out employees who have NO check-ins + missing_employees = [ + emp for emp in active_employees if emp.name not in checked_in_ids + ] + + # If everyone checked in, stop here + if not missing_employees: + return + + # 5. Compile a generic alert message (no employee names/IDs) + formatted_date = format_date(yesterday) + message = f"

Missing Employee Check-ins Alert

" + message += f"

Some active employees have no check-in records for {formatted_date}. Please check and verify.

" + + # 6. Fetch all enabled User emails assigned to the 'System Manager' role + system_managers = frappe.get_all("Has Role", filters={"role": "System Manager"}, pluck="parent") + recipients = frappe.get_all("User", filters={"name": ["in", system_managers], "enabled": 1}, pluck="email") + + # 7. Send the email notification + if recipients: + frappe.sendmail( + recipients=recipients, + subject=f"Notification: Missing Check-ins for {formatted_date}", + message=message, + ) \ No newline at end of file diff --git a/av_tools/hooks.py b/av_tools/hooks.py index c78f23c..3436e78 100644 --- a/av_tools/hooks.py +++ b/av_tools/hooks.py @@ -260,6 +260,7 @@ "daily": [ "av_tools.av_tools.doctype.visibility.visibility.trigger_daily_alerts", "av_tools.compliance.doctype.license_register.license_register.update_license_statuses", + "av_tools.av_tools_hooks.employee_checkins.check_missing_employee_checkins", ], }