Debian Codegraph @

addons/hr_holidays/tests/test_flexible_resource_calendar.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 import pytz
3 from datetime import datetime, date
4
5 from odoo.tests.common import TransactionCase
6
7 UTC = pytz.timezone('UTC')
8
9
10 class TestFlexibleResourceCalendar(TransactionCase):
11
12 @classmethod
13 def setUpClass(cls):
14 super().setUpClass()
15 cls.calendar_40h_flex = cls.env['resource.calendar'].create({
16 'name': 'Flexible 40h/week',
17 'tz': 'UTC',
18 'hours_per_day': 8.0,
19 'flexible_hours': True,
20 })
21 cls.flex_resource, cls.fully_flex_resource = cls.env['resource.resource'].create([{
22 'name': 'Flex',
23 'tz': 'UTC',
24 'calendar_id': cls.calendar_40h_flex.id,
25 }, {
26 'name': 'fully flex',
27 'tz': 'UTC',
28 'calendar_id': False,
29 }])
30
31 cls.flex_employee, cls.fully_flex_employee = cls.env['hr.employee'].create([{
32 'name': "flexible employee",
33 'date_version': date(2025, 1, 1),
34 'contract_date_start': date(2025, 1, 1),
35 'wage': 10,
36 'resource_calendar_id': cls.calendar_40h_flex.id,
37 'resource_id': cls.flex_resource.id,
38 }, {
39 'name': "fully flexible employee",
40 'date_version': date(2025, 1, 1),
41 'contract_date_start': date(2025, 1, 1),
42 'wage': 10,
43 'resource_calendar_id': False,
44 'resource_id': cls.fully_flex_resource.id,
45 }])
46
47 def test_flexible_resource_work_intervals_with_leaves(self):
48 self.env['resource.calendar.leaves'].create([{
49 'resource_id': self.flex_resource.id,
50 'date_from': datetime(2025, 7, 31, 8),
51 'date_to': datetime(2025, 8, 1, 17),
52 }, {
53 'resource_id': self.fully_flex_resource.id,
54 'date_from': datetime(2025, 7, 31, 8),
55 'date_to': datetime(2025, 8, 1, 17),
56 }])
57
58 custom_leave, half_day_leave = self.env['hr.leave.type'].create([{
59 'name': 'Custom Leave',
60 'requires_allocation': False,
61 'request_unit': 'hour',
62 }, {
63 'name': 'Half day',
64 'requires_allocation': False,
65 'request_unit': 'half_day',
66 }])
67
68 self.env['hr.leave'].with_context(mail_create_nolog=True, mail_notrack=True).create([{
69 'name': 'Half 1',
70 'holiday_status_id': half_day_leave.id,
71 'employee_id': self.flex_employee.id,
72 'request_date_from': date(2025, 7, 28),
73 'request_date_to': date(2025, 7, 28),
74 'request_date_from_period': 'am',
75 'request_date_to_period': 'am',
76 }, {
77 'name': 'Half 2',
78 'holiday_status_id': half_day_leave.id,
79 'employee_id': self.flex_employee.id,
80 'request_date_from': date(2025, 7, 30),
81 'request_date_to': date(2025, 7, 30),
82 'request_date_from_period': 'pm',
83 'request_date_to_period': 'pm',
84 }, {
85 'name': 'Custom',
86 'holiday_status_id': custom_leave.id,
87 'employee_id': self.flex_employee.id,
88 'request_date_from': date(2025, 7, 29),
89 'request_date_to': date(2025, 7, 29),
90 'request_hour_from': 11.0,
91 'request_hour_to': 16.0,
92 }, {
93 'name': 'Half 1',
94 'holiday_status_id': half_day_leave.id,
95 'employee_id': self.fully_flex_employee.id,
96 'request_date_from': date(2025, 7, 28),
97 'request_date_to': date(2025, 7, 28),
98 'request_date_from_period': 'am',
99 'request_date_to_period': 'am',
100 }, {
101 'name': 'Half 2',
102 'holiday_status_id': half_day_leave.id,
103 'employee_id': self.fully_flex_employee.id,
104 'request_date_from': date(2025, 7, 30),
105 'request_date_to': date(2025, 7, 30),
106 'request_date_from_period': 'pm',
107 'request_date_to_period': 'pm',
108 }, {
109 'name': 'Custom',
110 'holiday_status_id': custom_leave.id,
111 'employee_id': self.fully_flex_employee.id,
112 'request_date_from': date(2025, 7, 29),
113 'request_date_to': date(2025, 7, 29),
114 'request_hour_from': 11.0,
115 'request_hour_to': 16.0,
116 }]).action_approve()
117
118 start_dt = datetime(2025, 7, 28).astimezone(UTC)
119 end_dt = datetime(2025, 8, 3, 17).astimezone(UTC)
120
121 resources = self.flex_resource | self.fully_flex_resource
122 work_intervals, hours_per_day, hours_per_week = resources._get_flexible_resource_valid_work_intervals(start_dt, end_dt)
123
124 self.maxDiff = None
125 for resource in resources:
126 self.assertEqual(work_intervals[resource.id]._items, [
127 (datetime(2025, 7, 28, 12, 0, tzinfo=UTC), datetime(2025, 7, 28, 23, 59, 59, 999999, tzinfo=UTC), self.env['resource.calendar.attendance']),
128 (datetime(2025, 7, 29, 0, 0, tzinfo=UTC), datetime(2025, 7, 29, 11, 0, tzinfo=UTC), self.env['resource.calendar.attendance']),
129 (datetime(2025, 7, 29, 16, 0, tzinfo=UTC), datetime(2025, 7, 29, 23, 59, 59, 999999, tzinfo=UTC), self.env['resource.calendar.attendance']),
130 (datetime(2025, 7, 30, 0, 0, tzinfo=UTC), datetime(2025, 7, 30, 12, 0, tzinfo=UTC), self.env['resource.calendar.attendance']),
131 (datetime(2025, 8, 2, 0, 0, tzinfo=UTC), datetime(2025, 8, 2, 23, 59, 59, 999999, tzinfo=UTC), self.env['resource.calendar.attendance']),
132 (datetime(2025, 8, 3, 0, 0, tzinfo=UTC), datetime(2025, 8, 3, 17, 0, tzinfo=UTC), self.env['resource.calendar.attendance']),
133 ], "resource not available on 29, 31, 01, 28 morning and 30 afternoon. for other days, resource can do his hours at any moment of the day (from 00:00:00 to 23:59:59)")
134
135 self.assertDictEqual(hours_per_day[self.flex_resource.id], {
136 date(2025, 7, 28): 4.0,
137 date(2025, 7, 29): 3.0,
138 date(2025, 7, 30): 4.0,
139 date(2025, 7, 31): 0.0,
140 date(2025, 8, 1): 0.0,
141 date(2025, 8, 2): 8.0,
142 date(2025, 8, 3): 8.0,
143 }, "hours_per_day/2 for half days off, and hours_per_day - number_of_hoursfor custom time off")
144 self.assertTrue(self.fully_flex_resource.id not in hours_per_day)
145
146 self.assertDictEqual(hours_per_week[self.flex_resource.id], {
147 (2025, 31): 11.0,
148 (2025, 32): 40.0,
149 }, "week 31 (27/07 -> 02/08): 2 days off 31 & 01 (-16 hours), half day on 28 and 30 (-8 hours), 5 hours off on day 29 / hours = 40-(16+8+5) = 11 hours, no timeoff on week 32")
150 self.assertTrue(self.fully_flex_resource.id not in hours_per_week)