Debian Codegraph @

addons/hr_holidays/tests/test_global_leaves.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from datetime import date, datetime, timedelta
5 from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon
6 from odoo.addons.mail.tests.common import mail_new_test_user
7 from odoo.exceptions import ValidationError
8 from freezegun import freeze_time
9
10 from odoo.tests import tagged
11
12 @tagged('global_leaves')
13 class TestGlobalLeaves(TestHrHolidaysCommon):
14 """ Test global leaves for a whole company, conflict resolutions """
15
16 @classmethod
17 def setUpClass(cls):
18 super().setUpClass()
19 cls.calendar_1 = cls.env['resource.calendar'].create({
20 'name': 'Classic 40h/week',
21 'tz': 'UTC',
22 'hours_per_day': 8.0,
23 'attendance_ids': [
24 (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
25 (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
26 (0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
27 (0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
28 (0, 0, {'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
29 (0, 0, {'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
30 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
31 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
32 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
33 (0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
34 (0, 0, {'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
35 (0, 0, {'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
36 (0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
37 (0, 0, {'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
38 (0, 0, {'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'})
39 ]
40 })
41
42 cls.calendar_2 = cls.env['resource.calendar'].create({
43 'name': 'Classic 20h/week',
44 'tz': 'UTC',
45 'hours_per_day': 4.0,
46 'attendance_ids': [
47 (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
48 (0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
49 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
50 (0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
51 (0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
52 ]
53 })
54
55 cls.global_leave = cls.env['resource.calendar.leaves'].create({
56 'name': 'Global Time Off',
57 'date_from': date(2022, 3, 7),
58 'date_to': date(2022, 3, 7),
59 })
60
61 cls.calendar_leave = cls.env['resource.calendar.leaves'].create({
62 'name': 'Global Time Off',
63 'date_from': date(2022, 3, 8),
64 'date_to': date(2022, 3, 8),
65 'calendar_id': cls.calendar_1.id,
66 })
67
68 def test_leave_on_global_leave(self):
69 with self.assertRaises(ValidationError):
70 self.env['resource.calendar.leaves'].create({
71 'name': 'Wrong Time Off',
72 'date_from': date(2022, 3, 7),
73 'date_to': date(2022, 3, 7),
74 'calendar_id': self.calendar_1.id,
75 })
76
77 with self.assertRaises(ValidationError):
78 self.env['resource.calendar.leaves'].create({
79 'name': 'Wrong Time Off',
80 'date_from': date(2022, 3, 7),
81 'date_to': date(2022, 3, 7),
82 })
83
84 def test_leave_on_calendar_leave(self):
85 self.env['resource.calendar.leaves'].create({
86 'name': 'Correct Time Off',
87 'date_from': date(2022, 3, 8),
88 'date_to': date(2022, 3, 8),
89 'calendar_id': self.calendar_2.id,
90 })
91
92 with self.assertRaises(ValidationError):
93 self.env['resource.calendar.leaves'].create({
94 'name': 'Wrong Time Off',
95 'date_from': date(2022, 3, 8),
96 'date_to': date(2022, 3, 8),
97 })
98
99 with self.assertRaises(ValidationError):
100 self.env['resource.calendar.leaves'].create({
101 'name': 'Wrong Time Off',
102 'date_from': date(2022, 3, 8),
103 'date_to': date(2022, 3, 8),
104 'calendar_id': self.calendar_1.id,
105 })
106
107 @freeze_time('2023-05-12')
108 def test_global_leave_timezone(self):
109 """
110 It is necessary to use the timezone of the calendar
111 for the global leaves (without resource).
112 """
113 calendar_asia = self.env['resource.calendar'].create({
114 'name': 'Asia calendar',
115 'tz': 'Asia/Kolkata', # UTC +05:30
116 'hours_per_day': 8.0,
117 'attendance_ids': []
118 })
119 self.env.user.tz = 'Europe/Brussels'
120 global_leave = self.env['resource.calendar.leaves'].with_user(self.env.user).create({
121 'name': 'Public holiday',
122 'date_from': "2023-05-15 06:00:00", # utc from 8:00:00 for Europe/Brussels (UTC +02:00)
123 'date_to': "2023-05-15 15:00:00", # utc from 17:00:00 for Europe/Brussels (UTC +02:00)
124 'calendar_id': calendar_asia.id,
125 })
126 # Expectation:
127 # 6:00:00 in UTC (data from the browser) --> 8:00:00 for Europe/Brussel (UTC +02:00)
128 # 8:00:00 for Asia/Kolkata (UTC +05:30) --> 2:30:00 in UTC
129 self.assertEqual(global_leave.date_from, datetime(2023, 5, 15, 2, 30))
130 self.assertEqual(global_leave.date_to, datetime(2023, 5, 15, 11, 30))
131 # Note:
132 # The user in Europe/Brussels timezone see 4:30 and not 2:30 because he is in UTC +02:00.
133 # The user in Asia/Kolkata timezone (determined via the browser) see 8:00 because he is in UTC +05:30
134
135 def test_global_leave_working_schedule_without_company(self):
136 """
137 Check public holidays for a company apply to employees of this company
138 when using a working schedule without a company.
139 """
140 calendar_no_company = self.env['resource.calendar'].create({
141 'name': 'Schedule without company',
142 'company_id': False,
143 })
144 self.employee_emp.resource_calendar_id = calendar_no_company
145
146 self.env['resource.calendar.leaves'].create({
147 'name': 'Public Holiday',
148 'date_from': datetime(2024, 1, 3, 0, 0),
149 'date_to': datetime(2024, 1, 3, 23, 59),
150 'calendar_id': calendar_no_company.id,
151 'company_id': self.employee_emp.company_id.id,
152 })
153 leave_type = self.env['hr.leave.type'].create({
154 'name': 'Paid Time Off',
155 'time_type': 'leave',
156 'requires_allocation': False,
157 })
158 leave = self.env['hr.leave'].create({
159 'name': 'Time Off',
160 'employee_id': self.employee_emp.id,
161 'holiday_status_id': leave_type.id,
162 'request_date_from': date(2024, 1, 2),
163 'request_date_to': date(2024, 1, 4),
164 })
165
166 self.assertEqual(leave.number_of_days, 2, "Public holiday duration should not be included")
167
168 def test_global_leave_number_of_days_with_new(self):
169 """
170 Check that leaves stored in memory (and not in the database)
171 take into account global leaves.
172 """
173 global_leave = self.env['resource.calendar.leaves'].create({
174 'name': 'Global Time Off',
175 'date_from': datetime(2024, 1, 3, 6, 0, 0),
176 'date_to': datetime(2024, 1, 3, 19, 0, 0),
177 'calendar_id': self.calendar_1.id,
178 })
179 leave_type = self.env['hr.leave.type'].create({
180 'name': 'Paid Time Off',
181 'time_type': 'leave',
182 'requires_allocation': False,
183 })
184 self.employee_emp.resource_calendar_id = self.calendar_1.id
185
186 leave = self.env['hr.leave'].create({
187 'name': 'Test new leave',
188 'employee_id': self.employee_emp.id,
189 'holiday_status_id': leave_type.id,
190 'request_date_from': global_leave.date_from,
191 'request_date_to': global_leave.date_to,
192 })
193 self.assertEqual(leave.number_of_days, 0, 'It is a global leave')
194
195 leave = self.env['hr.leave'].new({
196 'name': 'Test new leave',
197 'employee_id': self.employee_emp.id,
198 'holiday_status_id': leave_type.id,
199 'request_date_from': global_leave.date_from,
200 'request_date_to': global_leave.date_to,
201 })
202 self.assertEqual(leave.number_of_days, 0, 'It is a global leave')
203
204 leave = self.env['hr.leave'].new({
205 'name': 'Test new leave',
206 'employee_id': self.employee_emp.id,
207 'holiday_status_id': leave_type.id,
208 'request_date_from': global_leave.date_from - timedelta(days=1),
209 'request_date_to': global_leave.date_to + timedelta(days=1),
210 })
211 self.assertEqual(leave.number_of_days, 2, 'There is a global leave')
212
213 @freeze_time('2024-12-01')
214 def test_global_leave_keeps_employee_resource_leave(self):
215 """
216 When a global leave is created, and it happens during a leave period of an employee,
217 if the employee's leave is not fully covered by the global leave, the employee's leave
218 should still have resource leaves linked to it.
219 """
220 employee = self.employee_emp
221 leave_type = self.env['hr.leave.type'].create({
222 'name': 'Paid Time Off',
223 'request_unit': 'hour',
224 'leave_validation_type': 'both',
225 })
226 self.env['hr.leave.allocation'].create({
227 'name': '20 days allocation',
228 'holiday_status_id': leave_type.id,
229 'number_of_days': 20,
230 'employee_id': employee.id,
231 'state': 'confirm',
232 'date_from': date(2024, 12, 1),
233 'date_to': date(2024, 12, 30),
234 }).action_approve()
235
236 partially_covered_leave = self.env['hr.leave'].create({
237 'name': 'Holiday 1 week',
238 'employee_id': employee.id,
239 'holiday_status_id': leave_type.id,
240 'request_date_from': datetime(2024, 12, 3, 7, 0),
241 'request_date_to': datetime(2024, 12, 5, 18, 0),
242 })
243 partially_covered_leave.action_approve()
244
245 global_leave = self.env['resource.calendar.leaves'].with_user(self.env.user).create({
246 'name': 'Public holiday',
247 'date_from': "2024-12-04 06:00:00",
248 'date_to': "2024-12-04 23:00:00",
249 'calendar_id': self.calendar_1.id,
250 })
251
252 # retrieve resource leaves linked to the employee's leave
253 resource_leaves = self.env['resource.calendar.leaves'].search([
254 ('holiday_id', '=', partially_covered_leave.id)
255 ])
256 self.assertTrue(resource_leaves, 'Resource leaves linked to the employee leave should exist.')
257
258 @freeze_time('2025-05-11')
259 def test_employee_leave_with_global_leave(self):
260 """
261 When an employee's leave is created, if there are any public holidays within the leave period,
262 the number of leave days is reduced accordingly.
263 eg,.
264 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
265 | Leave Requested | Leave State | Public Holiday days | # days leave remains |
266 |---------------------------------------------------------------------------------|
267 | 5 Days | confirm | 1 Days | 4 Days |
268 |---------------------------------------------------------------------------------|
269 | 4 Days | validate1 | 1 Days | 3 Days |
270 |---------------------------------------------------------------------------------|
271 | 3 Days | validate | 1 Days | 2 Days |
272 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
273
274 """
275 user_david = mail_new_test_user(self.env, login='david', groups='base.group_user')
276 user_timeoff_officer_david = mail_new_test_user(self.env, login='timeoff_officer', groups='base.group_user')
277
278 employee_david = self.env['hr.employee'].create({
279 'name': 'David Employee',
280 'user_id': user_david.id,
281 'leave_manager_id': user_timeoff_officer_david.id,
282 'parent_id': self.employee_hruser.id,
283 'department_id': self.rd_dept.id,
284 'resource_calendar_id': self.calendar_1.id,
285 })
286 leave_type = self.env['hr.leave.type'].create({
287 'name': 'Sick Time Off',
288 'time_type': 'leave',
289 'requires_allocation': False,
290 'leave_validation_type': 'both',
291 })
292
293 employee_leave = self.env['hr.leave'].create({
294 'name': 'Holiday 5 days',
295 'employee_id': employee_david.id,
296 'holiday_status_id': leave_type.id,
297 'request_date_from': datetime(2025, 5, 12),
298 'request_date_to': datetime(2025, 5, 16),
299 })
300
301 self.env['resource.calendar.leaves'].with_user(self.user_hrmanager).create({
302 'name': 'Public holiday day 1',
303 'date_from': datetime(2025, 5, 13),
304 'date_to': datetime(2025, 5, 13, 23, 59),
305 'calendar_id': employee_david.resource_calendar_id.id,
306 })
307
308 self.assertEqual(employee_leave.number_of_days, 4, 'Leave duration should be reduced because of public holiday day 1')
309
310 employee_leave.with_user(user_timeoff_officer_david).action_approve()
311 self.env['resource.calendar.leaves'].with_user(self.user_hrmanager).create({
312 'name': 'Public holiday day 2',
313 'date_from': datetime(2025, 5, 14),
314 'date_to': datetime(2025, 5, 14, 23, 59),
315 'calendar_id': employee_david.resource_calendar_id.id,
316 })
317 self.assertEqual(employee_leave.number_of_days, 3, 'Leave duration should be reduced because of public holiday day 2')
318
319 employee_leave.with_user(self.user_hruser).action_approve()
320 self.env['resource.calendar.leaves'].with_user(self.user_hrmanager).create({
321 'name': 'Public holiday day 3',
322 'date_from': datetime(2025, 5, 15),
323 'date_to': datetime(2025, 5, 15, 23, 59),
324 'calendar_id': employee_david.resource_calendar_id.id,
325 })
326 self.assertEqual(employee_leave.number_of_days, 2, 'Leave duration should be reduced because of public holiday day 3')
327
328 def test_multi_day_public_holidays_for_flexible_schedule(self):
329 """
330 Test that _get_unusual_days return correct value for
331 multi-day holidays in flexible schedules
332 """
333
334 flex_cal = self.env['resource.calendar'].create({
335 'name': 'Flexible', 'tz': 'UTC', 'flexible_hours': True, 'hours_per_day': 8.0
336 })
337
338 # tuesday to thursday
339 self.env['resource.calendar.leaves'].create({
340 'name': '3 day holiday', 'calendar_id': flex_cal.id,
341 'date_from': datetime(2024, 3, 5), 'date_to': date(2024, 3, 7)
342 })
343
344 # monday to saturday
345 start = datetime(2024, 3, 4)
346 end = datetime(2024, 3, 10)
347
348 flex_days = flex_cal._get_unusual_days(start, end)
349
350 expected = {
351 '2024-03-04': False,
352 '2024-03-05': True,
353 '2024-03-06': True,
354 '2024-03-07': True,
355 '2024-03-08': False,
356 '2024-03-09': False,
357 '2024-03-10': False,
358 }
359 for day, value in expected.items():
360 self.assertEqual(flex_days.get(day), value, f"Day {day} should be {'unusual' if value else 'normal'}")
361
362 def test_public_holidays_for_consecutive_allocations(self):
363 employee = self.employee_emp
364 leave_type = self.env['hr.leave.type'].create({
365 'name': 'Paid Time Off',
366 'time_type': 'leave',
367 'requires_allocation': 'yes',
368 })
369 self.env['hr.leave.allocation'].create([
370 {
371 'name': '2025 allocation',
372 'holiday_status_id': leave_type.id,
373 'number_of_days': 20,
374 'employee_id': employee.id,
375 'state': 'confirm',
376 'date_from': date(2025, 1, 1),
377 'date_to': date(2025, 12, 31),
378 },
379 {
380 'name': '2026 allocation',
381 'holiday_status_id': leave_type.id,
382 'number_of_days': 20,
383 'employee_id': employee.id,
384 'state': 'confirm',
385 'date_from': date(2026, 1, 1),
386 'date_to': date(2026, 12, 31),
387 }
388 ]).action_approve()
389
390 leave = self.env['hr.leave'].create({
391 'name': 'Holiday 1 week',
392 'employee_id': employee.id,
393 'holiday_status_id': leave_type.id,
394 'request_date_from': datetime(2025, 12, 8, 7, 0),
395 'request_date_to': datetime(2026, 1, 3, 18, 0),
396 })
397 leave.action_approve()
398
399 self.assertEqual(leave.number_of_days, 20, "Number of days should be 20")
400
401 public_holiday = self.env['resource.calendar.leaves'].create({
402 'name': 'Global Time Off',
403 'date_from': datetime(2025, 12, 31, 23, 0, 0),
404 'date_to': datetime(2026, 1, 1, 22, 59, 59),
405 })
406
407 self.assertTrue(public_holiday)
408 self.assertEqual(leave.number_of_days, 19, "Number of days should be 19 as one day has been granted back to the"
409 "the employee for the public holiday")