Purpose of the task is to improve the employee image and phone behaviour while changing the user. So in this commit, IF there is no pitcure on employee then apply the image of user while adding the user to employee from cretae, write and onchage. do not change the employee image if employee image is already set while creating employee or changing the user on employee. Also improve the phone behaviour when changing the company, - Never change the mobile_phone when switching the company on employee - Always change the work_phone when switching the company, except if empty on res.company. closes odoo/odoo#50115 Taskid: 2240167 Closes: #50115 Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
117 lines
5.7 KiB
Python
117 lines
5.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from ast import literal_eval
|
|
|
|
from odoo import api, fields, models
|
|
from pytz import timezone, UTC
|
|
from odoo.tools import format_time
|
|
|
|
|
|
class HrEmployeeBase(models.AbstractModel):
|
|
_name = "hr.employee.base"
|
|
_description = "Basic Employee"
|
|
_order = 'name'
|
|
|
|
name = fields.Char()
|
|
active = fields.Boolean("Active")
|
|
color = fields.Integer('Color Index', default=0)
|
|
department_id = fields.Many2one('hr.department', 'Department', domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
|
|
job_id = fields.Many2one('hr.job', 'Job Position', domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
|
|
job_title = fields.Char("Job Title", compute="_compute_job_title", store=True, readonly=False)
|
|
company_id = fields.Many2one('res.company', 'Company')
|
|
address_id = fields.Many2one('res.partner', 'Work Address', compute="_compute_address_id", store=True, readonly=False,
|
|
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
|
|
work_phone = fields.Char('Work Phone', compute="_compute_phones", store=True, readonly=False)
|
|
mobile_phone = fields.Char('Work Mobile', compute="_compute_phones", store=True, readonly=False)
|
|
work_email = fields.Char('Work Email')
|
|
work_location = fields.Char('Work Location')
|
|
user_id = fields.Many2one('res.users')
|
|
resource_id = fields.Many2one('resource.resource')
|
|
resource_calendar_id = fields.Many2one('resource.calendar', domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
|
|
parent_id = fields.Many2one('hr.employee', 'Manager', compute="_compute_parent_id", store=True, readonly=False,
|
|
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
|
|
coach_id = fields.Many2one(
|
|
'hr.employee', 'Coach', compute='_compute_coach', store=True, readonly=False,
|
|
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
|
|
help='Select the "Employee" who is the coach of this employee.\n'
|
|
'The "Coach" has no specific rights or responsibilities by default.')
|
|
tz = fields.Selection(
|
|
string='Timezone', related='resource_id.tz', readonly=False,
|
|
help="This field is used in order to define in which timezone the resources will work.")
|
|
hr_presence_state = fields.Selection([
|
|
('present', 'Present'),
|
|
('absent', 'Absent'),
|
|
('to_define', 'To Define')], compute='_compute_presence_state', default='to_define')
|
|
last_activity = fields.Date(compute="_compute_last_activity")
|
|
last_activity_time = fields.Char(compute="_compute_last_activity")
|
|
|
|
@api.depends('user_id.im_status')
|
|
def _compute_presence_state(self):
|
|
"""
|
|
This method is overritten in several other modules which add additional
|
|
presence criterions. e.g. hr_attendance, hr_holidays
|
|
"""
|
|
# Check on login
|
|
check_login = literal_eval(self.env['ir.config_parameter'].sudo().get_param('hr.hr_presence_control_login', 'False'))
|
|
for employee in self:
|
|
state = 'to_define'
|
|
if check_login:
|
|
if employee.user_id.im_status == 'online':
|
|
state = 'present'
|
|
elif employee.user_id.im_status == 'offline':
|
|
state = 'absent'
|
|
employee.hr_presence_state = state
|
|
|
|
@api.depends('user_id')
|
|
def _compute_last_activity(self):
|
|
presences = self.env['bus.presence'].search_read([('user_id', 'in', self.mapped('user_id').ids)], ['user_id', 'last_presence'])
|
|
# transform the result to a dict with this format {user.id: last_presence}
|
|
presences = {p['user_id'][0]: p['last_presence'] for p in presences}
|
|
|
|
for employee in self:
|
|
tz = employee.tz
|
|
last_presence = presences.get(employee.user_id.id, False)
|
|
if last_presence:
|
|
last_activity_datetime = last_presence.replace(tzinfo=UTC).astimezone(timezone(tz)).replace(tzinfo=None)
|
|
employee.last_activity = last_activity_datetime.date()
|
|
if employee.last_activity == fields.Date.today():
|
|
employee.last_activity_time = format_time(self.env, last_activity_datetime, time_format='short')
|
|
else:
|
|
employee.last_activity_time = False
|
|
else:
|
|
employee.last_activity = False
|
|
employee.last_activity_time = False
|
|
|
|
@api.depends('parent_id')
|
|
def _compute_coach(self):
|
|
for employee in self:
|
|
manager = employee.parent_id
|
|
previous_manager = employee._origin.parent_id
|
|
if manager and (employee.coach_id == previous_manager or not employee.coach_id):
|
|
employee.coach_id = manager
|
|
elif not employee.coach_id:
|
|
employee.coach_id = False
|
|
|
|
@api.depends('job_id')
|
|
def _compute_job_title(self):
|
|
for employee in self.filtered('job_id'):
|
|
employee.job_title = employee.job_id.name
|
|
|
|
@api.depends('address_id')
|
|
def _compute_phones(self):
|
|
for employee in self.filtered('address_id'):
|
|
if employee.address_id.phone:
|
|
employee.work_phone = employee.address_id.phone
|
|
|
|
@api.depends('company_id')
|
|
def _compute_address_id(self):
|
|
for employee in self:
|
|
address = employee.company_id.partner_id.address_get(['default'])
|
|
employee.address_id = address['default'] if address else False
|
|
|
|
@api.depends('department_id')
|
|
def _compute_parent_id(self):
|
|
for employee in self.filtered('department_id.manager_id'):
|
|
employee.parent_id = employee.department_id.manager_id
|