From d92ca20629d813d3de3540bed6993b42ba9160d7 Mon Sep 17 00:00:00 2001 From: Elouan Le Bars Date: Tue, 7 Jan 2020 13:49:23 +0100 Subject: [PATCH] [FIX] b_shift : automatic unsubscribe from next shifts Doesn't unsubscribe from present shift anymore when worker is changing to "unsubscribed" status. You cannot change future shifts status anymore. --- beesdoo_shift/models/cooperative_status.py | 6 +-- beesdoo_shift/models/task.py | 45 ++++++++++++++++------ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/beesdoo_shift/models/cooperative_status.py b/beesdoo_shift/models/cooperative_status.py index f77912b..37186df 100644 --- a/beesdoo_shift/models/cooperative_status.py +++ b/beesdoo_shift/models/cooperative_status.py @@ -321,10 +321,10 @@ class CooperativeStatus(models.Model): [('super_coop_id', 'in', self.cooperator_id.user_ids.ids)] ) task_tpls.write({'super_coop_id': False}) - # Remove worker for future task (remove also supercoop) - # TODO: Add one day otherwise unsubscribed from the shift you were absent + # Remove worker for future tasks (remove also supercoop) self.env['beesdoo.shift.shift'].sudo().unsubscribe_from_today( - [self.cooperator_id.id], today=fields.Date.today()) + [self.cooperator_id.id], now=fields.Datetime.now() + ) def _change_counter(self, data): self.sc += data.get('sc', 0) diff --git a/beesdoo_shift/models/task.py b/beesdoo_shift/models/task.py index 9e85456..253279d 100644 --- a/beesdoo_shift/models/task.py +++ b/beesdoo_shift/models/task.py @@ -81,6 +81,16 @@ class Task(models.Model): "Compensation Shift." ) + @api.constrains("state") + def _lock_future_task(self): + start_time_dt = fields.Datetime.from_string(self.start_time) + if datetime.now() < start_time_dt: + if self.state in ["done", "absent_2", "absent_1", "absent_0"]: + raise UserError(_( + "You cannot set shift state to 'present' " + "or 'absent' for a future shift." + )) + @api.constrains('is_regular', 'is_compensation') def _check_compensation(self): for task in self: @@ -119,19 +129,32 @@ class Task(models.Model): #TODO button to replaced someone @api.model - def unsubscribe_from_today(self, worker_ids, today=None, end_date=None): - today = today or fields.Date.today() - today += ' 00:00:00' - if end_date: - end_date += ' 23:59:59' - # date_domain = [('worker_id', 'in', worker_ids), ('start_time', '>=', today)] - date_domain = [('start_time', '>=', today)] - if end_date: - date_domain.append(('end_time', '<=', end_date)) - to_unsubscribe = self.search([('worker_id', 'in', worker_ids)] + date_domain) + def unsubscribe_from_today(self, worker_ids, today=None, end_date=None, now=None): + """ + Unsubscribe workers from *worker_ids* from all shift that start *today* and later. + If *end_date* is given, unsubscribe workers from shift between *today* and *end_date*. + If *now* is given workers are unsubscribed from all shifts starting *now* and later. + If *now* is given, *end_date* is not taken into account. + + :type today: fields.Date + :type end_date: fields.Date + :type now: fields.Datetime + """ + if now: + if len(now) != 19: + raise UserError (_("'Now' must be a datetime.")) + date_domain = [('start_time', '>', now)] + else: + today = today or fields.Date.today() + today += ' 00:00:00' + date_domain = [('start_time', '>', today)] + if end_date: + end_date += ' 23:59:59' + date_domain.append(('end_time', '<=', end_date)) + to_unsubscribe = self.search([('worker_id', 'in', worker_ids)] + date_domain) 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, 'replaced_id': False})