From 81c0fef91f6e6ca6f978c32e7ae99336394fcab4 Mon Sep 17 00:00:00 2001 From: krantheman Date: Mon, 12 Aug 2024 14:43:31 +0530 Subject: [PATCH 1/4] feat: add Fetch Shift buttons from Employee Checkin (cherry picked from commit 88c9078e7f28f02897a8328df7a45a2c0c598ff4) --- .../employee_checkin/employee_checkin.js | 19 +++++- .../employee_checkin/employee_checkin.py | 58 ++++++++++++------- .../employee_checkin/employee_checkin_list.js | 14 +++++ .../employee_checkin/test_employee_checkin.py | 35 +++++++++++ 4 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 hrms/hr/doctype/employee_checkin/employee_checkin_list.js diff --git a/hrms/hr/doctype/employee_checkin/employee_checkin.js b/hrms/hr/doctype/employee_checkin/employee_checkin.js index a57232706e..c3f276aa08 100644 --- a/hrms/hr/doctype/employee_checkin/employee_checkin.js +++ b/hrms/hr/doctype/employee_checkin/employee_checkin.js @@ -2,7 +2,9 @@ // For license information, please see license.txt frappe.ui.form.on("Employee Checkin", { - refresh: async (_frm) => { + refresh: async (frm) => { + if (!frm.doc.__islocal) frm.trigger("add_fetch_shift_button"); + const allow_geolocation_tracking = await frappe.db.get_single_value( "HR Settings", "allow_geolocation_tracking", @@ -14,6 +16,21 @@ frappe.ui.form.on("Employee Checkin", { } }, + add_fetch_shift_button(frm) { + frm.add_custom_button(__("Fetch Shift"), function () { + frappe.call({ + method: "fetch_shift", + doc: frm.doc, + freeze: true, + freeze_message: __("Fetching Shift"), + callback: function () { + frm.dirty(); + frm.save(); + }, + }); + }); + }, + fetch_geolocation: async (frm) => { if (!navigator.geolocation) { frappe.msgprint({ diff --git a/hrms/hr/doctype/employee_checkin/employee_checkin.py b/hrms/hr/doctype/employee_checkin/employee_checkin.py index aca800de41..3b1993ebd1 100644 --- a/hrms/hr/doctype/employee_checkin/employee_checkin.py +++ b/hrms/hr/doctype/employee_checkin/employee_checkin.py @@ -36,30 +36,33 @@ def validate_duplicate_log(self): _("This employee already has a log with the same timestamp.{0}").format("
" + doc_link) ) + @frappe.whitelist() def fetch_shift(self): - shift_actual_timings = get_actual_start_end_datetime_of_shift( - self.employee, get_datetime(self.time), True - ) - if shift_actual_timings: - if ( - shift_actual_timings.shift_type.determine_check_in_and_check_out - == "Strictly based on Log Type in Employee Checkin" - and not self.log_type - and not self.skip_auto_attendance - ): - frappe.throw( - _("Log Type is required for check-ins falling in the shift: {0}.").format( - shift_actual_timings.shift_type.name - ) - ) - if not self.attendance: - self.shift = shift_actual_timings.shift_type.name - self.shift_actual_start = shift_actual_timings.actual_start - self.shift_actual_end = shift_actual_timings.actual_end - self.shift_start = shift_actual_timings.start_datetime - self.shift_end = shift_actual_timings.end_datetime - else: + if not ( + shift_actual_timings := get_actual_start_end_datetime_of_shift( + self.employee, get_datetime(self.time), True + ) + ): self.shift = None + return + + if ( + shift_actual_timings.shift_type.determine_check_in_and_check_out + == "Strictly based on Log Type in Employee Checkin" + and not self.log_type + and not self.skip_auto_attendance + ): + frappe.throw( + _("Log Type is required for check-ins falling in the shift: {0}.").format( + shift_actual_timings.shift_type.name + ) + ) + if not self.attendance: + self.shift = shift_actual_timings.shift_type.name + self.shift_actual_start = shift_actual_timings.actual_start + self.shift_actual_end = shift_actual_timings.actual_end + self.shift_start = shift_actual_timings.start_datetime + self.shift_end = shift_actual_timings.end_datetime @frappe.whitelist() def set_geolocation_from_coordinates(self): @@ -134,6 +137,17 @@ def add_log_based_on_employee_field( return doc +@frappe.whitelist() +def bulk_fetch_shift(checkins: list[str] | str) -> None: + if isinstance(checkins, str): + checkins = frappe.json.loads(checkins) + for d in checkins: + doc = frappe.get_doc("Employee Checkin", d) + doc.fetch_shift() + doc.flags.ignore_validate = True + doc.save() + + def mark_attendance_and_link_log( logs, attendance_status, diff --git a/hrms/hr/doctype/employee_checkin/employee_checkin_list.js b/hrms/hr/doctype/employee_checkin/employee_checkin_list.js new file mode 100644 index 0000000000..da89813f80 --- /dev/null +++ b/hrms/hr/doctype/employee_checkin/employee_checkin_list.js @@ -0,0 +1,14 @@ +frappe.listview_settings["Employee Checkin"] = { + onload: function (listview) { + listview.page.add_action_item(__("Fetch Shifts"), () => { + const checkins = listview.get_checked_items().map((checkin) => checkin.name); + frappe.call({ + method: "hrms.hr.doctype.employee_checkin.employee_checkin.bulk_fetch_shift", + freeze: true, + args: { + checkins, + }, + }); + }); + }, +}; diff --git a/hrms/hr/doctype/employee_checkin/test_employee_checkin.py b/hrms/hr/doctype/employee_checkin/test_employee_checkin.py index a5cf25fabe..ab866c9ce0 100644 --- a/hrms/hr/doctype/employee_checkin/test_employee_checkin.py +++ b/hrms/hr/doctype/employee_checkin/test_employee_checkin.py @@ -20,6 +20,7 @@ from hrms.hr.doctype.employee_checkin.employee_checkin import ( add_log_based_on_employee_field, + bulk_fetch_shift, calculate_working_hours, mark_attendance_and_link_log, ) @@ -490,6 +491,40 @@ def test_consecutive_shift_assignments_overlapping_within_grace_period(self): log = make_checkin(employee, timestamp) self.assertEqual(log.shift, shift2.name) + def test_bulk_fetch_shift(self): + emp1 = make_employee("emp1@example.com", company="_Test Company") + emp2 = make_employee("emp2@example.com", company="_Test Company") + + # 8 - 12 + shift1 = setup_shift_type(shift_type="Shift 1") + # 12:30 - 16:30 + shift2 = setup_shift_type(shift_type="Shift 2", start_time="12:30:00", end_time="16:30:00") + + frappe.db.set_value("Employee", emp1, "default_shift", shift1.name) + frappe.db.set_value("Employee", emp2, "default_shift", shift1.name) + + date = getdate() + timestamp = datetime.combine(date, get_time("12:30:00")) + + log1 = make_checkin(emp1, timestamp) + self.assertEqual(log1.shift, shift1.name) + log2 = make_checkin(emp2, timestamp) + self.assertEqual(log2.shift, shift1.name) + + mark_attendance_and_link_log([log2], "Present", date) + + make_shift_assignment(shift2.name, emp1, date) + make_shift_assignment(shift2.name, emp2, date) + + bulk_fetch_shift([log1.name, log2.name]) + + log1.reload() + # shift changes according to the new assignment + self.assertEqual(log1.shift, shift2.name) + log2.reload() + # shift does not change since attendance is already marked + self.assertEqual(log1.shift, shift1.name) + def make_n_checkins(employee, n, hours_to_reverse=1): logs = [make_checkin(employee, now_datetime() - timedelta(hours=hours_to_reverse, minutes=n + 1))] From 70b8a2144c27fa582d108234a8235dd511ef3605 Mon Sep 17 00:00:00 2001 From: krantheman Date: Tue, 13 Aug 2024 11:20:23 +0530 Subject: [PATCH 2/4] test: fix typo in variable name (cherry picked from commit 95786ce204558382132cb900e6a6cfe865ac6783) --- hrms/hr/doctype/employee_checkin/test_employee_checkin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hrms/hr/doctype/employee_checkin/test_employee_checkin.py b/hrms/hr/doctype/employee_checkin/test_employee_checkin.py index ab866c9ce0..f3a7fea6d9 100644 --- a/hrms/hr/doctype/employee_checkin/test_employee_checkin.py +++ b/hrms/hr/doctype/employee_checkin/test_employee_checkin.py @@ -523,7 +523,7 @@ def test_bulk_fetch_shift(self): self.assertEqual(log1.shift, shift2.name) log2.reload() # shift does not change since attendance is already marked - self.assertEqual(log1.shift, shift1.name) + self.assertEqual(log2.shift, shift1.name) def make_n_checkins(employee, n, hours_to_reverse=1): From d187297232c371e72156d8607afa30aae79aaea3 Mon Sep 17 00:00:00 2001 From: krantheman Date: Wed, 14 Aug 2024 15:59:59 +0530 Subject: [PATCH 3/4] feat: show alert when shift is updated (cherry picked from commit e78e86b1f7be986d6295d26fd41a38128e7f387d) --- hrms/hr/doctype/employee_checkin/employee_checkin.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/hrms/hr/doctype/employee_checkin/employee_checkin.js b/hrms/hr/doctype/employee_checkin/employee_checkin.js index c3f276aa08..8f64bae7f9 100644 --- a/hrms/hr/doctype/employee_checkin/employee_checkin.js +++ b/hrms/hr/doctype/employee_checkin/employee_checkin.js @@ -18,14 +18,22 @@ frappe.ui.form.on("Employee Checkin", { add_fetch_shift_button(frm) { frm.add_custom_button(__("Fetch Shift"), function () { + const previous_shift = frm.doc.shift; frappe.call({ method: "fetch_shift", doc: frm.doc, freeze: true, freeze_message: __("Fetching Shift"), callback: function () { + if (previous_shift === frm.doc.shift) return; frm.dirty(); frm.save(); + frappe.show_alert({ + message: __("Shift has been successfully updated to {0}.", [ + frm.doc.shift, + ]), + indicator: "green", + }); }, }); }); From 1982d818b7afc948e4c1d8bd95a77580f23e0cd9 Mon Sep 17 00:00:00 2001 From: krantheman Date: Thu, 15 Aug 2024 15:15:24 +0530 Subject: [PATCH 4/4] fix: don't show button when attendance has been marked (cherry picked from commit e75af94abb0840a5dc20c117d266a26749194484) --- hrms/hr/doctype/employee_checkin/employee_checkin.js | 1 + 1 file changed, 1 insertion(+) diff --git a/hrms/hr/doctype/employee_checkin/employee_checkin.js b/hrms/hr/doctype/employee_checkin/employee_checkin.js index 8f64bae7f9..2f209e3fee 100644 --- a/hrms/hr/doctype/employee_checkin/employee_checkin.js +++ b/hrms/hr/doctype/employee_checkin/employee_checkin.js @@ -17,6 +17,7 @@ frappe.ui.form.on("Employee Checkin", { }, add_fetch_shift_button(frm) { + if (frm.doc.attendace) return; frm.add_custom_button(__("Fetch Shift"), function () { const previous_shift = frm.doc.shift; frappe.call({