From b7013b06e4ceab02057cc113f6a227111f234977 Mon Sep 17 00:00:00 2001 From: krantheman Date: Thu, 27 Jun 2024 16:34:03 +0530 Subject: [PATCH] feat(Shift Assignment): add validations for checking linked employee checkins and attendance before cancelling --- .../shift_assignment/shift_assignment.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/hrms/hr/doctype/shift_assignment/shift_assignment.py b/hrms/hr/doctype/shift_assignment/shift_assignment.py index f5de8c5f91..c4c934894a 100644 --- a/hrms/hr/doctype/shift_assignment/shift_assignment.py +++ b/hrms/hr/doctype/shift_assignment/shift_assignment.py @@ -34,6 +34,44 @@ def on_update_after_submit(self): self.validate_from_to_dates("start_date", "end_date") self.validate_overlapping_shifts() + def on_cancel(self): + self.validate_employee_checkin() + self.validate_attendance() + + def validate_employee_checkin(self): + checkins = frappe.get_all( + "Employee Checkin", + filters={ + "employee": self.employee, + "shift": self.shift_type, + "time": ["between", [self.start_date, self.end_date]], + }, + pluck="name", + ) + if checkins: + frappe.throw( + _("Cannot cancel Shift Assignment: {0} as it is linked to Employee Checkin: {1}").format( + self.name, get_link_to_form("Employee Checkin", checkins[0]) + ) + ) + + def validate_attendance(self): + attendances = frappe.get_all( + "Attendance", + filters={ + "employee": self.employee, + "shift": self.shift_type, + "attendance_date": ["between", [self.start_date, self.end_date]], + }, + pluck="name", + ) + if attendances: + frappe.throw( + _("Cannot cancel Shift Assignment: {0} as it is linked to Attendance: {1}").format( + self.name, get_link_to_form("Attendance", attendances[0]) + ) + ) + def validate_overlapping_shifts(self): if self.status == "Inactive": return