diff --git a/addons/hr_homeworking/models/hr_employee.py b/addons/hr_homeworking/models/hr_employee.py index 74ec77f073d..3812464e6bd 100644 --- a/addons/hr_homeworking/models/hr_employee.py +++ b/addons/hr_homeworking/models/hr_employee.py @@ -19,6 +19,10 @@ class HrEmployeeBase(models.AbstractModel): friday_location_id = fields.Many2one('hr.work.location', string='Friday') saturday_location_id = fields.Many2one('hr.work.location', string='Saturday') sunday_location_id = fields.Many2one('hr.work.location', string='Sunday') + exceptional_location_id = fields.Many2one( + 'hr.work.location', string='Current', + compute='_compute_exceptional_location_id', + help='This is the exceptional, non-weekly, location set for today.') hr_icon_display = fields.Selection(selection_add=[('presence_home', 'At Home'), ('presence_office', 'At Office'), ('presence_other', 'At Other')]) @@ -41,18 +45,32 @@ class HrEmployeeBase(models.AbstractModel): res['views']['list']['arch'] = res['views']['list']['arch'].replace('name_work_location_display', dayfield) return res + @api.depends('exceptional_location_id') def _compute_name_work_location_display(self): dayfield = self._get_current_day_location_field() unspecified = _('Unspecified') for employee in self: - employee.name_work_location_display = employee[dayfield].name if employee[dayfield] else unspecified + current_location_id = employee.exceptional_location_id or employee[dayfield] + employee.name_work_location_display = current_location_id.name if current_location_id else unspecified - @api.depends(*DAYS) + def _compute_exceptional_location_id(self): + today = fields.Date.today() + current_employee_locations = self.env['hr.employee.location'].search([ + ('employee_id', 'in', self.ids), + ('start_date', '=', today), + ('weekly', '=', False) + ]) + employee_work_locations = {l.employee_id.id: l.work_location_id for l in current_employee_locations} + + for employee in self: + employee.exceptional_location_id = employee_work_locations.get(employee.id, False) + + @api.depends(*DAYS, 'exceptional_location_id') def _compute_presence_icon(self): super()._compute_presence_icon() dayfield = self._get_current_day_location_field() for employee in self: - today_employee_location_id = employee[dayfield] + today_employee_location_id = employee.exceptional_location_id or employee[dayfield] if not today_employee_location_id or employee.hr_icon_display.startswith('presence_holiday'): continue employee.hr_icon_display = f'presence_{today_employee_location_id.location_type}'