From c4967ddf5dc60db87779e1574a3317a3e887ee20 Mon Sep 17 00:00:00 2001 From: Augustin Borsu Date: Sat, 8 Jun 2019 18:03:17 +0200 Subject: [PATCH] [ADD] beesdoo_shift: warn when assigning a second shift Warn when adding a shift to a worker who is already subscribed to one. --- beesdoo_shift/models/planning.py | 20 ++++++++++++++++++++ beesdoo_shift/wizard/subscribe.py | 12 +++++++++++- beesdoo_shift/wizard/subscribe.xml | 4 ++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/beesdoo_shift/models/planning.py b/beesdoo_shift/models/planning.py index 5b6c47b..d7ea850 100644 --- a/beesdoo_shift/models/planning.py +++ b/beesdoo_shift/models/planning.py @@ -178,3 +178,23 @@ class TaskTemplate(models.Model): }) return tasks + + @api.onchange('worker_ids') + def check_for_multiple_shifts(self): + original_ids = {worker.id for worker in self._origin.worker_ids} + + warnings = [] + for worker in self.worker_ids: + if worker.id not in original_ids: + shifts = [shift.name for shift in worker.subscribed_shift_ids if shift.id != self.id] + if shifts: + warnings.append( + worker.name + _(' is already assigned to ') + ", ".join(shifts)) + + if warnings: + return { + 'warning': { + 'title': _("Warning"), + 'message': "\n".join(warnings) + } + } diff --git a/beesdoo_shift/wizard/subscribe.py b/beesdoo_shift/wizard/subscribe.py index dc80758..38053f6 100644 --- a/beesdoo_shift/wizard/subscribe.py +++ b/beesdoo_shift/wizard/subscribe.py @@ -41,6 +41,15 @@ class Subscribe(models.TransientModel): .info_session) return session_followed + def _get_shift(self): + shifts = self.env['res.partner'].browse(self._context.get('active_id')).subscribed_shift_ids + if shifts: + return shifts[0] + return + + def _get_nb_shifts(self): + return len(self.env['res.partner'].browse(self._context.get('active_id')).subscribed_shift_ids) + def _get_super(self): return self.env['res.partner'].browse(self._context.get('active_id')).super @@ -62,7 +71,8 @@ class Subscribe(models.TransientModel): ], default=_get_mode ) exempt_reason_id = fields.Many2one('cooperative.exempt.reason', 'Exempt Reason') - shift_id = fields.Many2one('beesdoo.shift.template') + shift_id = fields.Many2one('beesdoo.shift.template', default=_get_shift) + nb_shifts = fields.Integer(string='Number of shifts', default=_get_nb_shifts) reset_counter = fields.Boolean(default=_get_reset_counter_default) reset_compensation_counter = fields.Boolean(default=False) unsubscribed = fields.Boolean(default=False, string="Are you sure to unsubscribe this cooperator") diff --git a/beesdoo_shift/wizard/subscribe.xml b/beesdoo_shift/wizard/subscribe.xml index 3a10cb7..97b0333 100644 --- a/beesdoo_shift/wizard/subscribe.xml +++ b/beesdoo_shift/wizard/subscribe.xml @@ -16,12 +16,16 @@ +