diff --git a/beesdoo_shift/__openerp__.py b/beesdoo_shift/__openerp__.py index baa4ec3..b1cf8e3 100644 --- a/beesdoo_shift/__openerp__.py +++ b/beesdoo_shift/__openerp__.py @@ -13,7 +13,7 @@ 'website': "https://github.com/beescoop/Obeesdoo", 'category': 'Cooperative management', - 'version': '9.0.1.0.1', + 'version': '9.0.1.0.2', 'depends': ['beesdoo_base'], diff --git a/beesdoo_shift/models/task.py b/beesdoo_shift/models/task.py index 5ce5175..ac81bfa 100644 --- a/beesdoo_shift/models/task.py +++ b/beesdoo_shift/models/task.py @@ -47,23 +47,41 @@ class Task(models.Model): revert_info = fields.Text(copy=False) working_mode = fields.Selection(related='worker_id.working_mode') - @api.constrains('is_regular', 'is_compensation', 'worker_id') + def _compensation_validation(self, task): + """ + Raise a validation error if the fields is_regular and + is_compensation are not properly set. + """ + if (task.is_regular == task.is_compensation + or not (task.is_regular or task.is_compensation)): + raise ValidationError( + "You must choose between Regular Shift or " + "Compensation Shift." + ) + + @api.constrains('is_regular', 'is_compensation') def _check_compensation(self): for task in self: if task.working_mode == 'regular': - if (task.is_regular == task.is_compensation - or not (task.is_regular or task.is_compensation)): - raise ValidationError( - "You must choose between Regular Shift or " - "Compensation Shift." - ) - - @api.onchange('worker_id') - def _onchange_worker_id(self): + self._compensation_validation(task) + + @api.constrains('worker_id') + def _check_worker_id(self): + """ + When worker_id changes we need to check whether is_regular + and is_compensation are set correctly. + When worker_id is set to a worker that doesn't need field + is_regular and is_compensation, these two fields are set to + False. + """ for task in self: - if task.working_mode != 'regular': - task.is_regular = False - task.is_compensation = False + if task.working_mode == 'regular': + self._compensation_validation(task) + else: + task.write({ + 'is_regular': False, + 'is_compensation': False, + }) def message_auto_subscribe(self, updated_fields, values=None): self._add_follower(values) @@ -97,11 +115,11 @@ class Task(models.Model): date_domain.append(('end_time', '<=', end_date)) to_unsubscribe = self.search([('worker_id', 'in', worker_ids)] + date_domain) - to_unsubscribe.write({'worker_id': False, 'is_regular': False}) + to_unsubscribe.write({'worker_id': False}) # What about replacement ? # Remove worker, replaced_id and regular to_unsubscribe_replace = self.search([('replaced_id', 'in', worker_ids)] + date_domain) - to_unsubscribe_replace.write({'worker_id': False, 'is_regular': False, 'replaced_id': False}) + to_unsubscribe_replace.write({'worker_id': False, 'replaced_id': False}) # If worker is Super cooperator, remove it from planning super_coop_ids = self.env['res.users'].search( @@ -110,8 +128,7 @@ class Task(models.Model): if super_coop_ids: to_unsubscribe_super_coop = self.search( [('super_coop_id', 'in', super_coop_ids)] + date_domain) - to_unsubscribe_super_coop.write({'super_coop_id': False, - 'is_regular': False}) + to_unsubscribe_super_coop.write({'super_coop_id': False}) @api.multi def write(self, vals):