-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
hooks.py
65 lines (62 loc) · 2.59 KB
/
hooks.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
# Copyright 2019 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from collections import defaultdict
def post_init_hook(env, employees=None):
"""Split current calendars by date ranges and assign new ones for
having proper initial data.
"""
if not employees:
employees = env["hr.employee"].search([])
calendars = employees.mapped("resource_calendar_id")
calendar_obj = env["resource.calendar"]
line_obj = env["resource.calendar.attendance"]
groups = line_obj.read_group(
[("calendar_id", "in", calendars.ids)],
["calendar_id", "date_from", "date_to"],
["calendar_id", "date_from:day", "date_to:day"],
lazy=False,
)
calendar_mapping = defaultdict(list)
for group in groups:
calendar = calendar_obj.browse(group["calendar_id"][0])
lines = line_obj.search(group["__domain"])
if len(calendar.attendance_ids) == len(lines):
# Don't alter calendar, as it's the same
new_calendar = calendar
else:
name = calendar.name + f" {lines[0].date_from}-{lines[0].date_to}"
attendances = []
for line in lines:
data = line.copy_data({"date_from": False, "date_to": False})[0]
data.pop("calendar_id")
attendances.append((0, 0, data))
new_calendar = calendar_obj.create(
{"name": name, "attendance_ids": attendances}
)
calendar_mapping[calendar].append(
(lines[0].date_from, lines[0].date_to, new_calendar),
)
for employee in employees.filtered("resource_calendar_id"):
calendar_lines = []
for data in calendar_mapping[employee.resource_calendar_id]:
calendar_lines.append(
(
0,
0,
{
"date_start": data[0],
"date_end": data[1],
"calendar_id": data[2].id,
},
)
)
# Extract employee's existing leaves so they are passed to the new
# automatic calendar.
leaves = employee.resource_calendar_id.leave_ids.filtered(
lambda x, e=employee: x.resource_id == e.resource_id
)
employee.calendar_ids = calendar_lines
employee.resource_calendar_id.active = False
# Now the automatic calendar has been created, so we link the
# leaves to that one so they count correctly.
leaves.write({"calendar_id": employee.resource_calendar_id.id})