Files
odoo_source/addons/hr_holidays/models/res_users.py
T
Abdelouahab (abla) 8293ae5d37 [FIX] hr_holidays : ignore inactive leaves for IM status
To reproduce
============

- In a belgian company, set legal time type as no need approval

- as admin, take a day in the future with this time off

- in the time off form, a cancel appears (near refuse)

- Cancel the time off

The day of the time off, the flight icon is set on the employee

Purpose
=======

When cancelling a time off, we set it's `active` field to False, but its state remains `validate`,
but when fetching for users on leave, there is no check on the `active` field.

Specification
=============

To solve the issue a check on `active` field was added to the query.

opw-2784173

closes odoo/odoo#87183

X-original-commit: 5d80d7c91fe05716602e2182515a4e26ea957e41
Signed-off-by: Kevin Baptiste <kba@odoo.com>
2022-03-24 17:12:34 +01:00

79 lines
3.3 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class User(models.Model):
_inherit = "res.users"
leave_manager_id = fields.Many2one(related='employee_id.leave_manager_id')
show_leaves = fields.Boolean(related='employee_id.show_leaves')
allocation_count = fields.Float(related='employee_id.allocation_count')
leave_date_to = fields.Date(related='employee_id.leave_date_to')
is_absent = fields.Boolean(related='employee_id.is_absent')
allocation_remaining_display = fields.Char(related='employee_id.allocation_remaining_display')
allocation_display = fields.Char(related='employee_id.allocation_display')
hr_icon_display = fields.Selection(related='employee_id.hr_icon_display')
@property
def SELF_READABLE_FIELDS(self):
return super().SELF_READABLE_FIELDS + [
'leave_manager_id',
'show_leaves',
'allocation_count',
'leave_date_to',
'is_absent',
'allocation_remaining_display',
'allocation_display',
'hr_icon_display',
]
def _compute_im_status(self):
super(User, self)._compute_im_status()
on_leave_user_ids = self._get_on_leave_ids()
for user in self:
if user.id in on_leave_user_ids:
if user.im_status == 'online':
user.im_status = 'leave_online'
elif user.im_status == 'away':
user.im_status = 'leave_away'
else:
user.im_status = 'leave_offline'
@api.model
def _get_on_leave_ids(self, partner=False):
now = fields.Datetime.now()
field = 'partner_id' if partner else 'id'
self.env['res.users'].flush(fnames=['active'])
self.env['hr.leave'].flush(fnames=['user_id', 'state', 'date_from', 'date_to'])
self.env.cr.execute('''SELECT res_users.%s FROM res_users
JOIN hr_leave ON hr_leave.user_id = res_users.id
AND state not in ('cancel', 'refuse')
AND hr_leave.active = 't'
AND res_users.active = 't'
AND date_from <= %%s AND date_to >= %%s''' % field, (now, now))
return [r[0] for r in self.env.cr.fetchall()]
def _clean_leave_responsible_users(self):
# self = old bunch of leave responsibles
# This method compares the current leave managers
# and remove the access rights to those who don't
# need them anymore
approver_group = self.env.ref('hr_holidays.group_hr_holidays_responsible', raise_if_not_found=False)
if not self or not approver_group:
return
res = self.env['hr.employee'].read_group(
[('leave_manager_id', 'in', self.ids)],
['leave_manager_id'],
['leave_manager_id'])
responsibles_to_remove_ids = set(self.ids) - {x['leave_manager_id'][0] for x in res}
approver_group.sudo().write({
'users': [(3, manager_id) for manager_id in responsibles_to_remove_ids]})
@api.model_create_multi
def create(self, vals_list):
users = super().create(vals_list)
users._clean_leave_responsible_users()
return users