diff --git a/yuthon_hr_attendance/models/hr_attendance.py b/yuthon_hr_attendance/models/hr_attendance.py index 3cb7aeded..b65bafed5 100644 --- a/yuthon_hr_attendance/models/hr_attendance.py +++ b/yuthon_hr_attendance/models/hr_attendance.py @@ -129,6 +129,35 @@ class HrAttendance(models.Model): print('------------------------------修改源码后期优化') # raise exceptions.ValidationError(_('"Check Out" time cannot be earlier than "Check In" time.')) + @api.constrains('check_in', 'check_out', 'employee_id') + def _check_validity(self): + """重写验证方法,移除未签退记录的检查""" + for attendance in self: + last_attendance_before_check_in = self.env['hr.attendance'].search([ + ('employee_id', '=', attendance.employee_id.id), + ('check_in', '<=', attendance.check_in), + ('id', '!=', attendance.id), + ], order='check_in desc', limit=1) + + if last_attendance_before_check_in and last_attendance_before_check_in.check_out and last_attendance_before_check_in.check_out > attendance.check_in: + raise exceptions.ValidationError(_( + "Cannot create new attendance record for %(empl_name)s, the employee was already checked in on %(datetime)s", + empl_name=attendance.employee_id.name, + datetime=format_datetime(self.env, attendance.check_in, dt_format=False) + )) + if attendance.check_out: + last_attendance_before_check_out = self.env['hr.attendance'].search([ + ('employee_id', '=', attendance.employee_id.id), + ('check_in', '<', attendance.check_out), + ('id', '!=', attendance.id), + ], order='check_in desc', limit=1) + if last_attendance_before_check_out and last_attendance_before_check_in != last_attendance_before_check_out: + raise exceptions.ValidationError(_( + "Cannot create new attendance record for %(empl_name)s, the employee was already checked in on %(datetime)s", + empl_name=attendance.employee_id.name, + datetime=format_datetime(self.env, last_attendance_before_check_out.check_in, dt_format=False) + )) + #考勤异常提醒 def card_remind(self): menu_id = self.env['ir.ui.menu'].search([('name', '=', '我的异常')])