'Verifies the validity of the attendance record compared to the others from the same employee.
For the same employee we must have :
* maximum 1 "open" attendance record (without check_out)
* no overlapping time slices with previous employee records'

@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_related, 'datetime': fields.Datetime.to_string(fields.Datetime.context_timestamp(self, fields.Datetime.from_string(attendance.check_in)))}))
        if (not attendance.check_out):
            no_check_out_attendances = self.env['hr.attendance'].search([('employee_id', '=', attendance.employee_id.id), ('check_out', '=', False), ('id', '!=', attendance.id)])
            if no_check_out_attendances:
                raise exceptions.ValidationError((_("Cannot    create    new    attendance    record    for    %(empl_name)s,    the    employee    hasn't    checked    out    since    %(datetime)s") % {'empl_name': attendance.employee_id.name_related, 'datetime': fields.Datetime.to_string(fields.Datetime.context_timestamp(self, fields.Datetime.from_string(no_check_out_attendances.check_in)))}))
        else:
            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_related, 'datetime': fields.Datetime.to_string(fields.Datetime.context_timestamp(self, fields.Datetime.from_string(last_attendance_before_check_out.check_in)))}))
