Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Shift Type): Auto update Last Sync of Checkin #2513

Merged
merged 5 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hrms/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
"hrms.hr.doctype.daily_work_summary_group.daily_work_summary_group.trigger_emails",
],
"hourly_long": [
"hrms.hr.doctype.shift_type.shift_type.update_last_sync_of_checkin",
"hrms.hr.doctype.shift_type.shift_type.process_auto_attendance_for_all_shifts",
"hrms.hr.doctype.shift_schedule_assignment.shift_schedule_assignment.process_auto_shift_creation",
],
Expand Down
6 changes: 6 additions & 0 deletions hrms/hr/doctype/shift_type/shift_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ frappe.ui.form.on("Shift Type", {
});
});
},

auto_update_last_sync: function (frm) {
if (frm.doc.auto_update_last_sync) {
frm.set_value("last_sync_of_checkin", "");
}
},
});
11 changes: 10 additions & 1 deletion hrms/hr/doctype/shift_type/shift_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"working_hours_threshold_for_absent",
"process_attendance_after",
"last_sync_of_checkin",
"auto_update_last_sync",
"grace_period_settings_auto_attendance_section",
"enable_late_entry_marking",
"late_entry_grace_period",
Expand Down Expand Up @@ -149,7 +150,8 @@
"description": "Last Known Successful Sync of Employee Checkin. Reset this only if you are sure that all Logs are synced from all the locations. Please don't modify this if you are unsure.",
"fieldname": "last_sync_of_checkin",
"fieldtype": "Datetime",
"label": "Last Sync of Checkin"
"label": "Last Sync of Checkin",
"read_only_depends_on": "auto_update_last_sync"
},
{
"default": "0",
Expand All @@ -176,6 +178,13 @@
"fieldtype": "Select",
"label": "Roster Color",
"options": "Blue\nCyan\nFuchsia\nGreen\nLime\nOrange\nPink\nRed\nViolet\nYellow"
},
{
"default": "0",
"description": "Recommended for a single biometric device / checkins via mobile app",
"fieldname": "auto_update_last_sync",
"fieldtype": "Check",
"label": "Automatically update Last Sync of Checkin"
}
],
"links": [],
Expand Down
19 changes: 19 additions & 0 deletions hrms/hr/doctype/shift_type/shift_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,26 @@ def should_mark_attendance(self, employee: str, attendance_date: str) -> bool:
return True


def update_last_sync_of_checkin():
"""Called from hooks"""
shifts = frappe.get_all(
"Shift Type",
filters={"enable_auto_attendance": 1, "auto_update_last_sync": 1},
fields=["name", "last_sync_of_checkin"],
)

for shift in shifts:
last_shift_sync = frappe.db.get_value(
"Employee Checkin", {"shift": shift.name}, "time", order_by="time desc"
)
if not shift.last_sync_of_checkin or get_datetime(last_shift_sync) > get_datetime(
shift.last_sync_of_checkin
):
frappe.db.set_value("Shift Type", shift.name, "last_sync_of_checkin", last_shift_sync)


def process_auto_attendance_for_all_shifts():
"""Called from hooks"""
shift_list = frappe.get_all("Shift Type", filters={"enable_auto_attendance": "1"}, pluck="name")
for shift in shift_list:
doc = frappe.get_cached_doc("Shift Type", shift)
Expand Down
24 changes: 24 additions & 0 deletions hrms/hr/doctype/shift_type/test_shift_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from erpnext.setup.doctype.holiday_list.test_holiday_list import set_holiday_list

from hrms.hr.doctype.leave_application.test_leave_application import get_first_sunday
from hrms.hr.doctype.shift_type.shift_type import update_last_sync_of_checkin
from hrms.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list
from hrms.tests.test_utils import add_date_to_holiday_list

Expand All @@ -25,6 +26,29 @@ def setUp(self):
to_date = get_year_ending(getdate())
self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date)

def test_auto_update_last_sync_of_checkin(self):
from hrms.hr.doctype.employee_checkin.test_employee_checkin import make_checkin

shift_type = setup_shift_type()
shift_type.last_sync_of_checkin = None
shift_type.auto_update_last_sync = 1
shift_type.save()

employee = make_employee("[email protected]", company="_Test Company")
date = getdate()
make_shift_assignment(shift_type.name, employee, date)

make_checkin(employee, datetime.combine(date, get_time("08:00:00")))
log_2 = make_checkin(employee, datetime.combine(date, get_time("08:45:53")))
update_last_sync_of_checkin()
shift_type.reload()
self.assertEqual(shift_type.last_sync_of_checkin, log_2.time)

log_3 = make_checkin(employee, datetime.combine(date, get_time("12:00:00")))
update_last_sync_of_checkin()
shift_type.reload()
self.assertEqual(shift_type.last_sync_of_checkin, log_3.time)

def test_mark_attendance(self):
from hrms.hr.doctype.employee_checkin.test_employee_checkin import make_checkin

Expand Down
Loading