}
def _altern_si_so(self, cr, uid, ids, context=None):
- current_attendance_data = self.browse(cr, uid, ids, context=context)[0]
- obj_attendance_ids = self.search(cr, uid, [('employee_id', '=', current_attendance_data.employee_id.id)], context=context)
- obj_attendance_ids.remove(ids[0])
- hr_attendance_data = self.browse(cr, uid, obj_attendance_ids, context=context)
-
- for old_attendance in hr_attendance_data:
- if old_attendance.employee_id.id == current_attendance_data['employee_id'].id:
- if old_attendance.action == current_attendance_data['action']:
- return False
- elif old_attendance.name >= current_attendance_data['name']:
- return False
- else:
- return True
- if current_attendance_data['action'] == 'sign_out':
- return False
+ """ Alternance sign_in/sign_out check.
+ Previous (if exists) must be of opposite action.
+ Next (if exists) must be of opposite action.
+ """
+ for att in self.browse(cr, uid, ids, context=context):
+ # search and browse for first previous and first next records
+ prev_att_ids = self.search(cr, uid, [('employee_id', '=', att.employee_id.id), ('name', '<', att.name), ('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='name DESC')
+ next_add_ids = self.search(cr, uid, [('employee_id', '=', att.employee_id.id), ('name', '>', att.name), ('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='name ASC')
+ prev_atts = self.browse(cr, uid, prev_att_ids, context=context)
+ next_atts = self.browse(cr, uid, next_add_ids, context=context)
+ # check for alternance, return False if at least one condition is not satisfied
+ if prev_atts and prev_atts[0].action == att.action: # previous exists and is same action
+ return False
+ if next_atts and next_atts[0].action == att.action: # next exists and is same action
+ return False
+ if (not prev_atts) and (not next_atts) and att.action != 'sign_in': # first attendance must be sign_in
+ return False
return True
_constraints = [(_altern_si_so, 'Error: Sign in (resp. Sign out) must follow Sign out (resp. Sign in)', ['action'])]