-
Notifications
You must be signed in to change notification settings - Fork 23.2k
/
hr_employee_base.py
193 lines (175 loc) · 9.89 KB
/
hr_employee_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- 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, utc
from datetime import timedelta
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')
work_email = fields.Char('Work Email')
work_location_id = fields.Many2one('hr.work.location', 'Work Location', compute="_compute_work_location_id", store=True, readonly=False,
domain="[('address_id', '=', address_id), '|', ('company_id', '=', False), ('company_id', '=', company_id)]")
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")
hr_icon_display = fields.Selection([
('presence_present', 'Present'),
('presence_absent_active', 'Present but not active'),
('presence_absent', 'Absent'),
('presence_to_define', 'To define'),
('presence_undetermined', 'Undetermined')], compute='_compute_presence_icon')
employee_type = fields.Selection([
('employee', 'Employee'),
('student', 'Student'),
('trainee', 'Trainee'),
('contractor', 'Contractor'),
('freelance', 'Freelancer'),
], string='Employee Type', default='employee', required=True,
help="The employee type. Although the primary purpose may seem to categorize employees, this field has also an impact in the Contract History. Only Employee type is supposed to be under contract and will have a Contract History.")
@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'))
employee_to_check_working = self.filtered(lambda e: e.user_id.im_status == 'offline')
working_now_list = employee_to_check_working._get_employee_working_now()
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' and employee.id not in working_now_list:
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_presence, 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:
if employee.address_id and employee.address_id.phone:
employee.work_phone = employee.address_id.phone
else:
employee.work_phone = False
@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
@api.depends('resource_calendar_id', 'hr_presence_state')
def _compute_presence_icon(self):
"""
This method compute the state defining the display icon in the kanban view.
It can be overriden to add other possibilities, like time off or attendances recordings.
"""
working_now_list = self.filtered(lambda e: e.hr_presence_state == 'present')._get_employee_working_now()
for employee in self:
if employee.hr_presence_state == 'present':
if employee.id in working_now_list:
icon = 'presence_present'
else:
icon = 'presence_absent_active'
elif employee.hr_presence_state == 'absent':
# employee is not in the working_now_list and he has a user_id
icon = 'presence_absent'
else:
# without attendance, default employee state is 'to_define' without confirmed presence/absence
# we need to check why they are not there
if employee.user_id:
# Display an orange icon on internal users.
icon = 'presence_to_define'
else:
# We don't want non-user employee to have icon.
icon = 'presence_undetermined'
employee.hr_icon_display = icon
@api.depends('address_id')
def _compute_work_location_id(self):
to_reset = self.filtered(lambda e: e.address_id != e.work_location_id.address_id)
to_reset.work_location_id = False
@api.model
def _get_employee_working_now(self):
working_now = []
# We loop over all the employee tz and the resource calendar_id to detect working hours in batch.
all_employee_tz = set(self.mapped('tz'))
for tz in all_employee_tz:
employee_ids = self.filtered(lambda e: e.tz == tz)
resource_calendar_ids = employee_ids.mapped('resource_calendar_id')
for calendar_id in resource_calendar_ids:
res_employee_ids = employee_ids.filtered(lambda e: e.resource_calendar_id.id == calendar_id.id)
start_dt = fields.Datetime.now()
stop_dt = start_dt + timedelta(hours=1)
from_datetime = utc.localize(start_dt).astimezone(timezone(tz or 'UTC'))
to_datetime = utc.localize(stop_dt).astimezone(timezone(tz or 'UTC'))
# Getting work interval of the first is working. Functions called on resource_calendar_id
# are waiting for singleton
work_interval = res_employee_ids[0].resource_calendar_id._work_intervals_batch(from_datetime, to_datetime)[False]
# Employee that is not supposed to work have empty items.
if len(work_interval._items) > 0:
# The employees should be working now according to their work schedule
working_now += res_employee_ids.ids
return working_now