-
-
Notifications
You must be signed in to change notification settings - Fork 726
Expand file tree
/
Copy pathhooks.py
More file actions
70 lines (64 loc) · 2.81 KB
/
hooks.py
File metadata and controls
70 lines (64 loc) · 2.81 KB
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
import logging
_logger = logging.getLogger(__name__)
def post_init_hook(env, employees=None):
"""Migrate calendars from contracts to calendar_ids
to have consistent work schedule history"""
if not employees:
employees = env["hr.employee"].with_context(active_test=False).search([])
for employee in employees.filtered("contract_ids"):
contract_calendar_lines = []
for contract in employee.contract_ids.sorted("date_start"):
date_start = contract.date_start
date_end = contract.date_end
# filter calendar_ids to check for overlaps with contracts
# with the same work schedule
cal_ids = employee.calendar_ids.filtered(
lambda x, contract=contract: x.calendar_id
== contract.resource_calendar_id
and (
not x.date_start
or not contract.date_end
or x.date_start < contract.date_end
)
and (
not x.date_end
or not contract.date_start
or x.date_end > contract.date_start
)
)
if cal_ids:
_logger.info(f"{contract} is overlapping with {cal_ids}")
for calendar in cal_ids.sorted("date_start"):
if date_start and calendar.date_start != date_start:
_logger.info(
f"changing date_start of {calendar} "
f"from {calendar.date_start} to {date_start}"
)
calendar.date_start = date_start
if date_end and calendar.date_end != date_end:
_logger.info(
f"changing date_end of {calendar} "
f"from {calendar.date_end} to {date_end}"
)
calendar.date_end = date_end
break
else:
_logger.info(
f"adding new calendar_id for {contract.employee_id.name}: "
f"{contract.resource_calendar_id.name} "
f"from {date_start} to {date_end}"
)
contract_calendar_lines.append(
(
0,
0,
{
"date_start": date_start,
"date_end": date_end,
"calendar_id": contract.resource_calendar_id.id,
},
)
)
employee.calendar_ids = contract_calendar_lines
# set correct calendar in contract
employee.contract_id.resource_calendar_id = employee.resource_calendar_id