I want to detect the value of a One2many field for employees, ie there is no overlap for employees in a period, it's like for holidays but here for multi employee , I tried this code but it does not work :

thank you in advance.

In python file :

class HrMission(models.Model):
    _name = 'hr.employee.mission'
    _description = 'Mission object'
    _inherit = 'mail.thread'

    line_mission = fields.One2many('line.employee.mission','line_mission', string='Employee', help="Employee sent on mission", 
                            copy=True, auto_join=True)

class LineEmployee(models.Model):
    _name = 'line.employee.mission'
    _description = 'Lignes des employés pour les mission'

    line_mission = fields.Many2one('hr.employee.mission', string='Line mission Reference', required=True, ondelete='cascade', index=True, copy=False)
    employee_id = fields.Many2one('hr.employee', string='Employee', help="Employee sent on mission",
                                  required=True)

My function for check date :

@api.constrains('mission_start_date', 'mission_end_date')
    def _check_date(self):
        for mission in self:
            employee = self.env['line.employee.mission'].search_count([('employee_id', '=', self.ids)])
            domain = [
                ('mission_start_date', '<=', mission.mission_end_date),
                ('mission_end_date', '>=', mission.mission_start_date),
                ('id', '!=', mission.id),
            ]
            nmissions = self.search_count(domain)
            if nmissions and employee:
                raise exceptions.ValidationError('Vous ne pouvez pas avoir 2 missions qui se chevauchent le même jour!')

0 Answers