Skip to content

Commit

Permalink
fix: unlink Attendance from Employee Checkins on cancellation (backport
Browse files Browse the repository at this point in the history
#31045) (#31049)

* fix: unlink Attendance from Employee Checkins on cancellation (#31045)

(cherry picked from commit 28fe4f3)

* fix: import missing function

Co-authored-by: Rucha Mahabal <ruchamahabal2@gmail.com>
  • Loading branch information
mergify[bot] and ruchamahabal authored May 17, 2022
1 parent c3417e4 commit e03fe97
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
32 changes: 32 additions & 0 deletions erpnext/hr/doctype/attendance/attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def validate(self):
self.validate_employee_status()
self.check_leave_record()

def on_cancel(self):
self.unlink_attendance_from_checkins()

def validate_attendance_date(self):
date_of_joining = frappe.db.get_value("Employee", self.employee, "date_of_joining")

Expand Down Expand Up @@ -102,6 +105,35 @@ def validate_employee(self):
if not emp:
frappe.throw(_("Employee {0} is not active or does not exist").format(self.employee))

def unlink_attendance_from_checkins(self):
from frappe.utils import get_link_to_form

EmployeeCheckin = frappe.qb.DocType("Employee Checkin")
linked_logs = (
frappe.qb.from_(EmployeeCheckin)
.select(EmployeeCheckin.name)
.where(EmployeeCheckin.attendance == self.name)
.for_update()
.run(as_dict=True)
)

if linked_logs:
(
frappe.qb.update(EmployeeCheckin)
.set("attendance", "")
.where(EmployeeCheckin.attendance == self.name)
).run()

frappe.msgprint(
msg=_("Unlinked Attendance record from Employee Checkins: {}").format(
", ".join(get_link_to_form("Employee Checkin", log.name) for log in linked_logs)
),
title=_("Unlinked logs"),
indicator="blue",
is_minimizable=True,
wide=True,
)


@frappe.whitelist()
def get_events(start, end, filters=None):
Expand Down
11 changes: 11 additions & 0 deletions erpnext/hr/doctype/employee_checkin/test_employee_checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ def test_mark_attendance_and_link_log(self):
)
self.assertEqual(attendance_count, 1)

def test_unlink_attendance_on_cancellation(self):
employee = make_employee("test_mark_attendance_and_link_log@example.com")
logs = make_n_checkins(employee, 3)

frappe.db.delete("Attendance", {"employee": employee})
attendance = mark_attendance_and_link_log(logs, "Present", nowdate(), 8.2)
attendance.cancel()

linked_logs = frappe.db.get_all("Employee Checkin", {"attendance": attendance.name})
self.assertEquals(len(linked_logs), 0)

def test_calculate_working_hours(self):
check_in_out_type = [
"Alternating entries as IN and OUT during the same shift",
Expand Down

0 comments on commit e03fe97

Please sign in to comment.