Debian Codegraph @

addons/hr_holidays/tests/test_expiring_leaves.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from datetime import date
4 from dateutil.relativedelta import relativedelta
5 from freezegun import freeze_time
6
7 from odoo.addons.base.tests.common import HttpCase
8 from odoo.tests.common import tagged
9 from odoo.tests.common import users
10
11 from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon
12
13
14 @tagged('post_install', '-at_install', 'carryover_expiring_leaves')
15 class TestExpiringLeaves(HttpCase, TestHrHolidaysCommon):
16
17 @classmethod
18 def setUpClass(cls):
19 super().setUpClass()
20 cls.leave_type = cls.env['hr.leave.type'].create({
21 'name': 'Test',
22 'time_type': 'leave',
23 'requires_allocation': True,
24 'allocation_validation_type': 'no_validation',
25 })
26 cls.accrual_plan_with_accrual_validity = cls.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
27 'name': 'Test Accrual Plan With Accrual Validity',
28 'carryover_date': 'other',
29 'carryover_day': 1,
30 'carryover_month': '4',
31 'level_ids': [
32 (0, 0, {
33 'start_count': 0,
34 'start_type': 'day',
35 'added_value': 3,
36 'added_value_type': 'day',
37 'frequency': 'yearly',
38 'yearly_day': 1,
39 'yearly_month': '1',
40 'cap_accrued_time': False,
41 'action_with_unused_accruals': 'all',
42 'carryover_options': 'limited',
43 'postpone_max_days': 5,
44 'accrual_validity': True,
45 'accrual_validity_count': 3,
46 'accrual_validity_type': 'month',
47 })
48 ],
49 })
50
51 @users('enguerran')
52 def test_no_carried_over_leaves(self):
53 """
54 The accrual plan:
55 - Accrue at the end of period.
56 - Carryover date : 31/12 (end of the year).
57 Milestones:
58 Milestone 1:
59 - Start immediately.
60 - Accrue 10 days.
61 - Accrue days on 01/01 (start of the year).
62 - Unused accruals are lost (no leaves are carried over).
63
64 Create an accrual allocation with this plan and allocate it to the logged-in user.
65 The employee will be accrued 10 days. The employee will use some of them. The carryover policy is set
66 to None, so no leaves will be carriedover. The remaining days of the allocation will expire.
67 """
68 number_of_accrued_days = 10
69 accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
70 'name': 'Test Accrual Plan',
71 'can_be_carryover': True,
72 'carryover_date': 'other',
73 'carryover_day': 31,
74 'carryover_month': '12',
75 'level_ids': [
76 (0, 0, {
77 'milestone_date': 'creation',
78 'start_type': 'day',
79 'added_value': number_of_accrued_days,
80 'added_value_type': 'day',
81 'frequency': 'yearly',
82 'yearly_day': 1,
83 'yearly_month': '1',
84 'cap_accrued_time': False,
85 'action_with_unused_accruals': 'lost'
86 })
87 ],
88 })
89
90 logged_in_emp = self.env.user.employee_id
91 allocation = self.env['hr.leave.allocation'].sudo().create({
92 'date_from': date(date.today().year, 1, 1),
93 'allocation_type': 'accrual',
94 'accrual_plan_id': accrual_plan.id,
95 'holiday_status_id': self.leave_type.id,
96 'employee_id': logged_in_emp.id,
97 'number_of_days': 0,
98 })
99
100 target_date = date(date.today().year + 1, 12, 30)
101 leave = self.env['hr.leave'].create({
102 'employee_id': logged_in_emp.id,
103 'holiday_status_id': self.leave_type.id,
104 'request_date_from': target_date + relativedelta(month=12, day=1),
105 'request_date_to': target_date + relativedelta(month=12, day=7)
106 })
107
108 allocation_data = self.leave_type.get_allocation_data(
109 allocation.employee_id, target_date)
110
111 # Assert the date of expiration
112 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
113 allocation._get_carryover_date(target_date).strftime('%m/%d/%Y'),
114 "The expiration date should match the carryover date")
115
116 # Assert the number of expiring leaves
117 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_remaining'],
118 number_of_accrued_days - leave.number_of_days,
119 "All the remaining days of the allocation will expire")
120
121 @users('enguerran')
122 def test_carried_over_leaves_with_maximum(self):
123 """
124 The accrual plan:
125 - Accrue at the end of period.
126 - Carryover date : 31/12 (end of the year).
127 Milestones:
128 Milestone 1:
129 - Start immediately.
130 - Accrue 20 days.
131 - Accrue days on 01/01 (start of the year).
132 - Unused accruals are carried over with a maximum of 10.
133
134 Create an accrual allocation with this plan and allocate it to the logged-in user.
135 The employee will be accrued 20 days. The employee will use some of them. The carryover
136 policy is set to carryover with a maximum of 10, so at max 10 leaves will be carriedover.
137 The remaining days of the allocation will expire.
138 """
139
140 number_of_accrued_days = 20
141 carryover_limit = 10
142 accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
143 'name': 'Test Accrual Plan',
144 'can_be_carryover': True,
145 'carryover_date': 'other',
146 'carryover_day': 31,
147 'carryover_month': '12',
148 'level_ids': [
149 (0, 0, {
150 'milestone_date': 'creation',
151 'start_type': 'day',
152 'added_value': number_of_accrued_days,
153 'added_value_type': 'day',
154 'frequency': 'yearly',
155 'yearly_day': 1,
156 'yearly_month': '1',
157 'cap_accrued_time': False,
158 'action_with_unused_accruals': 'all',
159 'carryover_options': 'limited',
160 'postpone_max_days': carryover_limit,
161 })
162 ],
163 })
164
165 logged_in_emp = self.env.user.employee_id
166 allocation = self.env['hr.leave.allocation'].sudo().create({
167 'date_from': date(date.today().year, 1, 1),
168 'allocation_type': 'accrual',
169 'accrual_plan_id': accrual_plan.id,
170 'holiday_status_id': self.leave_type.id,
171 'employee_id': logged_in_emp.id,
172 'number_of_days': 0,
173 })
174
175 target_date = date(date.today().year + 1, 12, 30)
176 leave = self.env['hr.leave'].create({
177 'employee_id': logged_in_emp.id,
178 'holiday_status_id': self.leave_type.id,
179 'request_date_from': target_date + relativedelta(month=12, day=1),
180 'request_date_to': target_date + relativedelta(month=12, day=7)
181 })
182 allocation_data = self.leave_type.get_allocation_data(
183 allocation.employee_id, target_date)
184
185 # Assert the date of expiration
186 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
187 allocation._get_carryover_date(target_date).strftime('%m/%d/%Y'),
188 "The expiration date should match the carryover date")
189
190 # Assert the number of expiring leaves
191 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_remaining'],
192 number_of_accrued_days - leave.number_of_days - carryover_limit,
193 "All the remaining days of the allocation will expire")
194
195 @users('enguerran')
196 def test_allocation_with_max_carryover_and_expiring_allocation(self):
197 """
198 The accrual plan:
199 - Accrue at the end of period.
200 - Carryover date : 31/12 (end of the year).
201 Milestones:
202 Milestone 1:
203 - Start immediately.
204 - Accrue 20 days.
205 - Accrue days on 01/01 (start of the year).
206 - Unused accruals are carried over with a maximum of 10.
207
208 Create an accrual allocation with this plan:
209 - Employee: logged-in user.
210 - Start date: 01/01/2024
211 - Carryover Policy: carryover with a maximum of 10
212
213 On 01/01/2025 The employee will be accrued 20 days.
214 The employee will take 5 leaves using this allocation.
215 - The carryover policy is set to carryover with a maximum of 10, so at max 10 days will be carriedover.
216 - 5 days will expire.
217
218 Create a second accrual allocation:
219 - The configuration is the same as the previous one except that carryover date is the start of the year
220 and all unused accruals will be carriedover.
221 - This allocation will expire on 31/12 next year. 20 days will expire when the allocation expires.
222 Number of expiring leaves = number of not carriedover days from the first allocation +
223 number of expiring leaves due to expiration of the second allocation
224 = 5 + 20 = 25 days
225 """
226
227 number_of_accrued_days = 20
228 carryover_limit = 10
229 accrual_plan_1 = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
230 'name': 'Test Accrual Plan',
231 'can_be_carryover': True,
232 'carryover_date': 'other',
233 'carryover_day': 31,
234 'carryover_month': '12',
235 'level_ids': [
236 (0, 0, {
237 'milestone_date': 'creation',
238 'start_type': 'day',
239 'added_value': number_of_accrued_days,
240 'added_value_type': 'day',
241 'frequency': 'yearly',
242 'yearly_day': 1,
243 'yearly_month': '1',
244 'cap_accrued_time': False,
245 'action_with_unused_accruals': 'all',
246 'carryover_options': 'limited',
247 'postpone_max_days': carryover_limit,
248 })
249 ],
250 })
251
252 accrual_plan_2 = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
253 'name': 'Test Accrual Plan With All Leaves Carried Over',
254 'can_be_carryover': True,
255 'level_ids': [
256 (0, 0, {
257 'milestone_date': 'creation',
258 'start_type': 'day',
259 'added_value': number_of_accrued_days,
260 'added_value_type': 'day',
261 'frequency': 'yearly',
262 'yearly_day': 1,
263 'yearly_month': '1',
264 'cap_accrued_time': False,
265 'action_with_unused_accruals': 'all',
266 })
267 ],
268 })
269
270 logged_in_emp = self.env.user.employee_id
271 with freeze_time("2024-01-01"):
272 allocation_with_carryover = self.env['hr.leave.allocation'].sudo().create({
273 'date_from': '2024-01-01',
274 'allocation_type': 'accrual',
275 'accrual_plan_id': accrual_plan_1.id,
276 'holiday_status_id': self.leave_type.id,
277 'employee_id': logged_in_emp.id,
278 'number_of_days': 0,
279 })
280 leave = self.env['hr.leave'].create({
281 'employee_id': logged_in_emp.id,
282 'holiday_status_id': self.leave_type.id,
283 'request_date_from': '2025-12-01',
284 'request_date_to': '2025-12-05'
285 })
286 # The expiring allocation
287 self.env['hr.leave.allocation'].sudo().create({
288 'date_from': '2024-01-01',
289 'date_to': '2025-12-31',
290 'allocation_type': 'accrual',
291 'accrual_plan_id': accrual_plan_2.id,
292 'holiday_status_id': self.leave_type.id,
293 'employee_id': logged_in_emp.id,
294 'number_of_days': 0,
295 })
296
297 target_date = date(2025, 12, 30)
298 allocation_data = self.leave_type.get_allocation_data(logged_in_emp, target_date)
299
300 # Assert the date of expiration
301 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
302 allocation_with_carryover._get_carryover_date(target_date).strftime('%m/%d/%Y'),
303 "The expiration date should match the carryover date")
304
305 # Assert the number of expiring leaves
306 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_remaining'],
307 (number_of_accrued_days - leave.number_of_days - carryover_limit) + number_of_accrued_days,
308 "All the remaining days of the allocation will expire")
309
310 @users('enguerran')
311 def test_expiring_allocation_without_carried_over_leaves(self):
312 """
313 The accrual plan:
314 - Accrue at the end of period.
315 - Carryover date : 31/12 (end of the year).
316 Milestones:
317 Milestone 1:
318 - Start immediately.
319 - Accrue 20 days.
320 - Accrue days on 01/01 (start of the year).
321 - Unused accruals are lost (no leaves are carried over).
322
323 Create an accrual allocation with this plan and allocate it to the logged-in user. This
324 allocation will expire on 31/12 next year.
325 The employee will be accrued 10 days. The carryover policy is set to None, so no leaves
326 will be carriedover. The remaining days of the allocation will expire.
327
328 The number of expiring leaves should be 10 and not 20 (10 for the expiration of the allocation
329 and 10 for the leaves being not carried over).
330 """
331
332 number_of_accrued_days = 10
333 accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
334 'name': 'Test Accrual Plan',
335 'can_be_carryover': True,
336 'carryover_date': 'other',
337 'carryover_day': 31,
338 'carryover_month': '12',
339 'level_ids': [
340 (0, 0, {
341 'milestone_date': 'creation',
342 'start_type': 'day',
343 'added_value': number_of_accrued_days,
344 'added_value_type': 'day',
345 'frequency': 'yearly',
346 'yearly_day': 1,
347 'yearly_month': '1',
348 'cap_accrued_time': False,
349 'action_with_unused_accruals': 'lost',
350 })
351 ],
352 })
353
354 logged_in_emp = self.env.user.employee_id
355 allocation = self.env['hr.leave.allocation'].sudo().create({
356 'date_from': date(date.today().year, 1, 1),
357 'date_to': date(date.today().year + 1, 12, 31),
358 'allocation_type': 'accrual',
359 'accrual_plan_id': accrual_plan.id,
360 'holiday_status_id': self.leave_type.id,
361 'employee_id': logged_in_emp.id,
362 'number_of_days': 0,
363 })
364
365 target_date = date(date.today().year + 1, 12, 30)
366 allocation_data = self.leave_type.get_allocation_data(
367 allocation.employee_id, target_date)
368
369 # Assert the date of expiration
370 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
371 allocation._get_carryover_date(target_date).strftime('%m/%d/%Y'),
372 "The expiration date should match the carryover date")
373
374 # Assert the number of expiring leaves
375 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_remaining'],
376 number_of_accrued_days,
377 "All the remaining days of the allocation will expire")
378
379 @users('enguerran')
380 def test_expiration_date(self):
381 """
382 The accrual plan:
383 - Accrue at the end of period.
384 - Carryover date : 01/01.
385 Milestones:
386 Milestone 1:
387 - Start immediately.
388 - Accrue 10 days.
389 - Accrue days on 01/01 (start of the year).
390 - Unused accruals are carried over with a maximum of 5.
391
392 Create an accrual allocation with this plan and allocate it to the logged-in user.
393 The employee will be accrued 10 days. The carryover policy is set to carryover with
394 a maximum of 5, so only 5 leaves will be carriedover. The remaining days of the allocation
395 will expire.
396
397 If the target date is 01/01/2025, then the expiration date should be 01/01/2026 because 5 of the days
398 accrued on 01/01/2025 will expire on 01/01/2026.
399 """
400 with freeze_time('2024-01-01'):
401 accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
402 'name': 'Test Accrual Plan',
403 'can_be_carryover': True,
404 'carryover_date': 'year_start',
405 'level_ids': [
406 (0, 0, {
407 'milestone_date': 'creation',
408 'start_type': 'day',
409 'added_value': 10,
410 'added_value_type': 'day',
411 'frequency': 'yearly',
412 'yearly_day': 1,
413 'yearly_month': '1',
414 'cap_accrued_time': False,
415 'action_with_unused_accruals': 'all',
416 'carryover_options': 'limited',
417 'postpone_max_days': 5,
418 })
419 ],
420 })
421
422 logged_in_emp = self.env.user.employee_id
423 allocation = self.env['hr.leave.allocation'].sudo().create({
424 'date_from': date(2024, 1, 1),
425 'allocation_type': 'accrual',
426 'accrual_plan_id': accrual_plan.id,
427 'holiday_status_id': self.leave_type.id,
428 'employee_id': logged_in_emp.id,
429 'number_of_days': 0,
430 })
431
432 target_date = date(2025, 1, 1)
433 allocation_data = self.leave_type.get_allocation_data(allocation.employee_id, target_date)
434 # Assert the date of expiration
435 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
436 (target_date + relativedelta(years=1)).strftime('%m/%d/%Y'),
437 "The expiration date should be the carryover date of the year that follows the target date's year")
438
439 # Assert the number of expiring leaves
440 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_remaining'], 5)
441
442 @users('enguerran')
443 def test_expiration_date_2(self):
444 """
445 - Define an accrual plan:
446 - Carryover date : 1st of September.
447 - Carryover with a maximum of 5 days.
448 - Define 2 allocations:
449 - Both allocations will start on 01/01/2023.
450 - Both allocations use the accrual plan defined above.
451 - Both allocations accrue 3 days yearly.
452 - The first allocation expires on the 1st of October.
453 - The second allocation doesn't expire.
454
455 - On 01/01/2024, both allocations will accrue 3 days for the employee.
456 - The expiration date should be 01/10/2024 because on 01/09/2024, 3 days will carryover for both allocations
457 and no days will expire.
458 """
459
460 accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
461 'name': 'Test Accrual Plan',
462 'can_be_carryover': True,
463 'carryover_date': 'other',
464 'carryover_day': 1,
465 'carryover_month': '9',
466 'level_ids': [
467 (0, 0, {
468 'milestone_date': 'creation',
469 'start_type': 'day',
470 'added_value': 3,
471 'added_value_type': 'day',
472 'frequency': 'yearly',
473 'yearly_day': 1,
474 'yearly_month': '1',
475 'cap_accrued_time': False,
476 'action_with_unused_accruals': 'all',
477 'carryover_options': 'limited',
478 'postpone_max_days': 5,
479 })
480 ],
481 })
482
483 logged_in_emp = self.env.user.employee_id
484 with freeze_time("2023-01-01"):
485 # Allocation 1
486 self.env['hr.leave.allocation'].sudo().create({
487 'date_from': '2023-01-01',
488 'allocation_type': 'accrual',
489 'accrual_plan_id': accrual_plan.id,
490 'holiday_status_id': self.leave_type.id,
491 'employee_id': logged_in_emp.id,
492 'number_of_days': 0,
493 })
494 # Allocation 2
495 self.env['hr.leave.allocation'].sudo().create({
496 'date_from': '2023-01-01',
497 'date_to': '2024-10-01',
498 'allocation_type': 'accrual',
499 'accrual_plan_id': accrual_plan.id,
500 'holiday_status_id': self.leave_type.id,
501 'employee_id': logged_in_emp.id,
502 'number_of_days': 0,
503 })
504
505 with freeze_time("2024-01-01"):
506 self.env['hr.leave.allocation'].with_user(self.user_hruser)._update_accrual()
507
508 target_date = date(2024, 1, 1)
509 allocation_data = self.leave_type.get_allocation_data(logged_in_emp, target_date)
510 # Assert the date of expiration
511 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
512 (target_date + relativedelta(month=10)).strftime('%m/%d/%Y'),
513 "The expiration date should be the expiration date of the second allocation because no days will expire on carryover date")
514
515 @users('enguerran')
516 def test_no_carried_over_leaves_for_flexible_resource(self):
517 """
518 Identical test to test_no_carried_over_leaves but with a flexible resource calendar. The test aims to verify that
519 the expiration date is correctly calculated even if attendance is not taken into account for the flexible resource.
520 The accrual plan:
521 - Accrue at the end of period.
522 - Carryover date : 31/12 (end of the year).
523 Milestones:
524 Milestone 1:
525 - Start immediately.
526 - Accrue 10 days.
527 - Accrue days on 01/01 (start of the year).
528 - Unused accruals are lost (no leaves are carried over).
529
530 Create an accrual allocation with this plan and allocate it to the logged-in user.
531 The employee will be accrued 10 days. The employee will use some of them. The carryover policy is set
532 to None, so no leaves will be carriedover. The remaining days of the allocation will expire.
533 """
534 number_of_accrued_days = 10
535 accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
536 'name': 'Test Accrual Plan',
537 'can_be_carryover': True,
538 'carryover_date': 'other',
539 'carryover_day': 31,
540 'carryover_month': '12',
541 'level_ids': [
542 (0, 0, {
543 'milestone_date': 'creation',
544 'start_type': 'day',
545 'added_value': number_of_accrued_days,
546 'added_value_type': 'day',
547 'frequency': 'yearly',
548 'yearly_day': 1,
549 'yearly_month': '1',
550 'cap_accrued_time': False,
551 'action_with_unused_accruals': 'lost'
552 })
553 ],
554 })
555
556 self.flex_40h_calendar = self.env['resource.calendar'].sudo().create({
557 'name': 'Flexible 40h/week',
558 'tz': 'UTC',
559 'hours_per_day': 8.0,
560 'full_time_required_hours': 80.0,
561 'flexible_hours': True,
562 })
563 logged_in_emp = self.env.user.employee_id
564 logged_in_emp.resource_calendar_id = self.flex_40h_calendar
565
566 allocation = self.env['hr.leave.allocation'].sudo().create({
567 'date_from': date(date.today().year, 1, 1),
568 'allocation_type': 'accrual',
569 'accrual_plan_id': accrual_plan.id,
570 'holiday_status_id': self.leave_type.id,
571 'employee_id': logged_in_emp.id,
572 'number_of_days': 0,
573 })
574
575 target_date = date(date.today().year + 1, 12, 30)
576 leave = self.env['hr.leave'].create({
577 'employee_id': logged_in_emp.id,
578 'holiday_status_id': self.leave_type.id,
579 'request_date_from': target_date + relativedelta(month=12, day=1),
580 'request_date_to': target_date + relativedelta(month=12, day=7)
581 })
582
583 allocation_data = self.leave_type.get_allocation_data(
584 allocation.employee_id, target_date)
585
586 # Assert the date of expiration
587 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
588 allocation._get_carryover_date(target_date).strftime('%m/%d/%Y'),
589 "The expiration date should match the carryover date")
590
591 # Assert the number of expiring leaves
592 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_remaining'],
593 number_of_accrued_days - leave.number_of_days,
594 "All the remaining days of the allocation will expire")
595
596 @users('enguerran')
597 def test_no_carried_over_leaves_for_fully_flexible_resource(self):
598 """
599 /!\\ Fully Flexible Resource should not take leaves. However the test aims to verify that the expiration date
600 is correctly calculated for the fully flexible resource.
601
602 The accrual plan:
603 - Accrue at the end of period.
604 - Carryover date : 31/12 (end of the year).
605 Milestones:
606 Milestone 1:
607 - Start immediately.
608 - Accrue 10 days.
609 - Accrue days on 01/01 (start of the year).
610 - Unused accruals are lost (no leaves are carried over).
611
612 Create an accrual allocation with this plan and allocate it to the logged-in user.
613 The employee will be accrued 10 days. The employee will use some of them. The carryover policy is set
614 to None, so no leaves will be carriedover. The remaining days of the allocation will expire.
615 """
616 number_of_accrued_days = 10
617 accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
618 'name': 'Test Accrual Plan',
619 'can_be_carryover': True,
620 'carryover_date': 'other',
621 'carryover_day': 31,
622 'carryover_month': '12',
623 'level_ids': [
624 (0, 0, {
625 'milestone_date': 'creation',
626 'start_type': 'day',
627 'added_value': number_of_accrued_days,
628 'added_value_type': 'day',
629 'frequency': 'yearly',
630 'yearly_day': 1,
631 'yearly_month': '1',
632 'cap_accrued_time': False,
633 'action_with_unused_accruals': 'lost'
634 })
635 ],
636 })
637
638 logged_in_emp = self.env.user.employee_id
639 logged_in_emp.resource_calendar_id = None # Set as Fully flexible resource
640
641 allocation = self.env['hr.leave.allocation'].sudo().create({
642 'date_from': date(date.today().year, 1, 1),
643 'allocation_type': 'accrual',
644 'accrual_plan_id': accrual_plan.id,
645 'holiday_status_id': self.leave_type.id,
646 'employee_id': logged_in_emp.id,
647 'number_of_days': 0,
648 })
649
650 target_date = date(date.today().year + 1, 12, 30)
651 leave = self.env['hr.leave'].create({
652 'employee_id': logged_in_emp.id,
653 'holiday_status_id': self.leave_type.id,
654 'request_date_from': target_date + relativedelta(month=12, day=1),
655 'request_date_to': target_date + relativedelta(month=12, day=7)
656 })
657
658 allocation_data = self.leave_type.get_allocation_data(
659 allocation.employee_id, target_date)
660
661 # Assert the date of expiration
662 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
663 allocation._get_carryover_date(target_date).strftime('%m/%d/%Y'),
664 "The expiration date should match the carryover date")
665
666 # Assert the number of expiring leaves
667 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_remaining'],
668 number_of_accrued_days - leave.number_of_days,
669 "All the remaining days of the allocation will expire")
670
671 # Days between the target date and the expiration date (accrual_plan's carryover date)
672 working_days_equivalent_needed = (allocation._get_carryover_date(target_date) - target_date).days + 1
673
674 # Assert the closest allocation duration (number of working days equivalent (8 hours/day) remaining before the allocation expires)
675 self.assertEqual(round(allocation_data[logged_in_emp][0][1]['closest_allocation_duration']), working_days_equivalent_needed,
676 "The closest allocation duration should be the number of working days equivalent (24 hours/day) remaining before the allocation expires")
677
678 @users('enguerran')
679 def test_carried_over_days_expiration_date(self):
680 """
681 This test case aims to assert that carried_over_days_expiration_date is taken into account when the
682 expiration date is computed.
683 - First accrual plan:
684 - Carryover date : 1st of April.
685 - Has 1 level:
686 - Accrues 3 days yearly on the 1st of January.
687 - Carryover with a maximum of 5 days.
688 - Second accrual plan:
689 Has the same definition as the one above except that carried over days are valid for 3 months.
690 - Note: the following dates are in format dd/mm/YYYY
691 - Define 2 allocations:
692 - Both allocations will start on 01/01/2023.
693 - One allocation uses the first accrual plan and the other uses the second accrual plan.
694 - Both allocations accrue 3 days yearly.
695 - The first allocation expires on the 1st of October.
696 - The second allocation doesn't expire.
697 - On 01/01/2024, both allocations will accrue 3 days for the employee.
698 - If target date is 01/04/2024, then the expiration date should be 01/7/2024 because on 01/04/2024, 3 days will carryover for
699 the second allocation and these 3 days will expire in 3 months.
700 """
701 accrual_plan_without_accrual_validity = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).sudo().create({
702 'name': 'Test Accrual Plan',
703 'carryover_date': 'other',
704 'carryover_day': 1,
705 'carryover_month': '4',
706 'level_ids': [
707 (0, 0, {
708 'start_count': 0,
709 'start_type': 'day',
710 'added_value': 3,
711 'added_value_type': 'day',
712 'frequency': 'yearly',
713 'yearly_day': 1,
714 'yearly_month': '1',
715 'cap_accrued_time': False,
716 'action_with_unused_accruals': 'all',
717 'carryover_options': 'limited',
718 'postpone_max_days': 5,
719 })
720 ],
721 })
722
723 logged_in_emp = self.env.user.employee_id
724 with freeze_time("2023-01-01"):
725 # Allocation 1
726 self.env['hr.leave.allocation'].sudo().create({
727 'date_from': '2023-01-01',
728 'date_to': '2024-10-01',
729 'allocation_type': 'accrual',
730 'accrual_plan_id': accrual_plan_without_accrual_validity.id,
731 'holiday_status_id': self.leave_type.id,
732 'employee_id': logged_in_emp.id,
733 'number_of_days': 0,
734 })
735 # Allocation 2
736 self.env['hr.leave.allocation'].sudo().create({
737 'date_from': '2023-01-01',
738 'allocation_type': 'accrual',
739 'accrual_plan_id': self.accrual_plan_with_accrual_validity.id,
740 'holiday_status_id': self.leave_type.id,
741 'employee_id': logged_in_emp.id,
742 'number_of_days': 0,
743 })
744
745 with freeze_time("2024-04-01"):
746 self.env['hr.leave.allocation'].with_user(self.user_hruser)._update_accrual()
747
748 target_date = date(2024, 4, 1)
749 allocation_data = self.leave_type.get_allocation_data(logged_in_emp, target_date)
750 # Assert the date of expiration
751 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
752 (target_date + relativedelta(month=7)).strftime('%m/%d/%Y'),
753 "The expiration date should be the carried over days expiration date of allocation 3")
754
755 # Assert the number of expiring leaves
756 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_remaining'], 3)
757
758 @users('enguerran')
759 def test_carried_over_days_expiration_date_2(self):
760 """
761 This tess case aims to assert that the number of expiring leaves on carried_over_days_expiration_date is
762 computed properly
763 - Define an accrual plan:
764 - Carryover date : 1st of April.
765 - Has 1 level:
766 - Accrues 3 days yearly on the 1st of January.
767 - Carryover with a maximum of 5 days.
768 - Carried over days are valid for 3 months.
769 - Note: the following dates are in format dd/mm/YYYY
770 - Define an allocation:
771 - The allocation will start on 01/01/2023.
772 - The allocation uses the accrual plan defined above.
773 - The allocation expires on the 1st of October.
774 - On 01/01/2024, 3 days are accrued.
775 - If target date is 01/05/2024, then the expiration date should be 01/7/2024.
776 - On 01/04/2024, 3 days will carryover.
777 - The employee taked 2 days as time off.
778 - The number of expiring days on 01/07/2024 is 1 day.
779 """
780
781 logged_in_emp = self.env.user.employee_id
782 with freeze_time("2023-01-01"):
783 self.env['hr.leave.allocation'].sudo().create({
784 'date_from': '2023-01-01',
785 'allocation_type': 'accrual',
786 'accrual_plan_id': self.accrual_plan_with_accrual_validity.id,
787 'holiday_status_id': self.leave_type.id,
788 'employee_id': logged_in_emp.id,
789 'number_of_days': 0,
790 })
791
792 with freeze_time("2024-04-01"):
793 self.env['hr.leave.allocation'].with_user(self.user_hruser)._update_accrual()
794 leave = self.env['hr.leave'].create({
795 'name': 'leave',
796 'employee_id': logged_in_emp.id,
797 'holiday_status_id': self.leave_type.id,
798 'request_date_from': '2024-04-03',
799 'request_date_to': '2024-04-04',
800 })
801 leave.sudo().action_approve()
802
803 target_date = date(2024, 5, 1)
804 allocation_data = self.leave_type.get_allocation_data(logged_in_emp, target_date)
805 # Assert the date of expiration
806 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_expire'],
807 (target_date + relativedelta(month=7)).strftime('%m/%d/%Y'),
808 "The expiration date should be the carried over days expiration date of allocation 3")
809
810 # Assert the number of expiring leaves
811 self.assertEqual(allocation_data[logged_in_emp][0][1]['closest_allocation_remaining'], 1)