diff --git a/planning/models/planning.py b/planning/models/planning.py index 6769cad..f590aa0 100755 --- a/planning/models/planning.py +++ b/planning/models/planning.py @@ -10,7 +10,7 @@ import uuid from math import ceil from odoo import api, fields, models, _ -from odoo.exceptions import UserError, AccessError +from odoo.exceptions import UserError, AccessError, ValidationError from odoo.osv import expression from odoo.tools.safe_eval import safe_eval from odoo.tools import format_time @@ -92,6 +92,25 @@ class Planning(models.Model): ('check_allocated_hours_positive', 'CHECK(allocated_hours >= 0)', 'You cannot have negative shift'), ] + @api.constrains('start_datetime', 'end_datetime', 'employee_id') + def _check_same_time_valid(self): + if self.ids: + self.flush(['start_datetime', 'end_datetime', 'employee_id']) + query = """ + SELECT S1.id,count(*) FROM + planning_slot S1, planning_slot S2 + WHERE + S1.start_datetime < S2.end_datetime and S1.end_datetime > S2.start_datetime and S1.id <> S2.id and S1.employee_id = S2.employee_id + GROUP BY S1.id; + """ + self.env.cr.execute(query, (tuple(self.ids),)) + overlap_mapping = dict(self.env.cr.fetchall()) + for slot in self: + if overlap_mapping.get(slot.id, 0) >= 1: + raise ValidationError(_('Planning already assigned for this employee at the same time.')) + + + @api.depends('employee_id') def _compute_planning_slot_company_id(self): if self.employee_id: