# -*- coding: utf-8 -*- ################################################################################### # A part of OpenHrms Project # # Cybrosys Technologies Pvt. Ltd. # Copyright (C) 2018-TODAY Cybrosys Technologies (). # Author: Saritha Sahadevan () # # This program is free software: you can modify # it under the terms of the GNU Affero General Public License (AGPL) as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # ################################################################################### from odoo.exceptions import Warning from odoo import models, fields, api, _ class HrEmployeeContract(models.Model): _inherit = 'hr.contract' shift_schedule = fields.One2many('hr.shift.schedule', 'rel_hr_schedule', string="Shift Schedule") working_hours = fields.Many2one('resource.calendar', string='Working Schedule') department_id = fields.Many2one('hr.department', string="Department", required=True) class HrSchedule(models.Model): _name = 'hr.shift.schedule' start_date = fields.Date(string="Date From", required=True) end_date = fields.Date(string="Date To", required=True) rel_hr_schedule = fields.Many2one('hr.contract') hr_shift = fields.Many2one('resource.calendar', string="Shift", required=True) @api.onchange('start_date', 'end_date') def get_department(self): """Adding domain to the hr_shift field""" hr_department = None if self.start_date: hr_department = self.rel_hr_schedule.department_id.id return { 'domain': { 'hr_shift': [('hr_department', '=', hr_department)] } } @api.multi def write(self, vals): self._check_overlap(vals) return super(HrSchedule, self).write(vals) @api.model def create(self, vals): self._check_overlap(vals) return super(HrSchedule, self).create(vals) def _check_overlap(self, vals): if vals.get('start_date', False) and vals.get('end_date', False): shifts = self.env['hr.shift.schedule'].search([('rel_hr_schedule', '=', vals.get('rel_hr_schedule'))]) for each in shifts: if each != shifts[-1]: if each.end_date >= vals.get('start_date') or each.start_date >= vals.get('start_date'): raise Warning(_('The dates may not overlap with one another.')) if vals.get('start_date') > vals.get('end_date'): raise Warning(_('Start date should be less than end date.')) return True