Debian Codegraph @

addons/hr_holidays_attendance/tests/test_accrual_allocations.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 import datetime
4 from freezegun import freeze_time
5 from dateutil.relativedelta import relativedelta
6
7 from odoo.tests import Form, tagged
8
9 from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon
10
11
12 @tagged('post_install', '-at_install', 'accruals')
13 class TestAccrualAllocationsAttendance(TestHrHolidaysCommon):
14
15 @classmethod
16 def setUpClass(cls):
17 super(TestAccrualAllocationsAttendance, cls).setUpClass()
18 cls.leave_type = cls.env['hr.leave.type'].create({
19 'name': 'Paid Time Off',
20 'time_type': 'leave',
21 'requires_allocation': True,
22 'allocation_validation_type': 'hr',
23 })
24
25 def test_frequency_hourly_attendance(self):
26 with freeze_time("2017-12-05"):
27 accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).create({
28 'is_based_on_worked_time': True,
29 'can_be_carryover': True,
30 'level_ids': [(0, 0, {
31 'milestone_date': 'after',
32 'start_count': 1,
33 'start_type': 'day',
34 'added_value': 1,
35 'added_value_type': 'day',
36 'frequency': 'worked_hours',
37 'cap_accrued_time': True,
38 'maximum_leave': 10000,
39 'action_with_unused_accruals': 'all',
40 })],
41 })
42 allocation = self.env['hr.leave.allocation'].with_user(self.user_hrmanager_id).with_context(tracking_disable=True).create({
43 'name': 'Accrual allocation for employee',
44 'accrual_plan_id': accrual_plan.id,
45 'employee_id': self.employee_emp.id,
46 'holiday_status_id': self.leave_type.id,
47 'number_of_days': 0,
48 'allocation_type': 'accrual',
49 })
50 allocation.action_approve()
51 self.assertFalse(allocation.nextcall, 'There should be no nextcall set on the allocation.')
52 self.assertEqual(allocation.number_of_days, 0, 'There should be no days allocated yet.')
53 allocation._update_accrual()
54 tomorrow = datetime.date.today() + relativedelta(days=2)
55 self.assertEqual(allocation.number_of_days, 0, 'There should be no days allocated yet. The accrual starts tomorrow.')
56
57 self.env['hr.attendance'].create({
58 'employee_id': self.employee_emp.id,
59 'check_in': datetime.datetime(2017, 12, 6, 8, 0, 0),
60 'check_out': datetime.datetime(2017, 12, 6, 13, 22, 0),
61 })
62
63 with freeze_time(tomorrow):
64 allocation._update_accrual()
65 nextcall = datetime.date.today() + relativedelta(days=1)
66 self.assertAlmostEqual(allocation.number_of_days, 4.37, places=2)
67 self.assertEqual(allocation.nextcall, nextcall, 'The next call date of the cron should be in 2 days.')
68 allocation._update_accrual()
69 self.assertAlmostEqual(allocation.number_of_days, 4.37, places=2)
70
71 def test_accrual_allocation_based_on_attendance(self):
72 accrual_plan = self.env['hr.leave.accrual.plan'].create({
73 'name': 'Accrual Plan For Test',
74 'is_based_on_worked_time': False,
75 'accrued_gain_time': 'end',
76 'can_be_carryover': True,
77 'carryover_date': 'year_start',
78 'level_ids': [(0, 0, {
79 'milestone_date': 'after',
80 'start_count': 1,
81 'added_value': 1,
82 'added_value_type': 'hour',
83 'frequency': 'worked_hours',
84 'cap_accrued_time': True,
85 'maximum_leave': 100,
86 'action_with_unused_accruals': 'all',
87 })],
88 })
89 self.env['hr.attendance'].create({
90 'employee_id': self.employee_emp.id,
91 'check_in': datetime.datetime(2024, 4, 1, 8, 0, 0),
92 'check_out': datetime.datetime(2024, 4, 1, 17, 0, 0),
93 })
94 with Form(self.env['hr.leave.allocation'].with_user(self.user_hrmanager)) as allocation_form:
95 allocation_form.allocation_type = 'accrual'
96 allocation_form.employee_id = self.employee_emp
97 allocation_form.accrual_plan_id = accrual_plan
98 allocation_form.holiday_status_id = self.leave_type
99 allocation_form.date_from = datetime.date(2024, 3, 20)
100 allocation_form.name = 'Accrual allocation for employee'
101 self.assertEqual(allocation_form.number_of_hours_display, 8.0)
102 allocation_form.date_from = datetime.date(2024, 3, 25)
103 allocation_form.name = 'Accrual allocation for employee'
104 self.assertEqual(allocation_form.number_of_hours_display, 8.0)
105
106 def test_accrual_allocation_with_overlapping_attendance(self):
107 accrual_plan = self.env['hr.leave.accrual.plan'].create({
108 'name': 'Accrual Plan For Test',
109 'is_based_on_worked_time': True,
110 'accrued_gain_time': 'end',
111 'carryover_date': 'year_start',
112 'level_ids': [(0, 0, {
113 'start_count': 0,
114 'added_value': 1,
115 'added_value_type': 'hour',
116 'cap_accrued_time': True,
117 'maximum_leave': 100,
118 'frequency': 'worked_hours'
119 })],
120 })
121 with freeze_time("2024-4-1"):
122 allocation = self.env['hr.leave.allocation'].create({
123 'name': 'Accrual allocation for employee',
124 'accrual_plan_id': accrual_plan.id,
125 'employee_id': self.employee_emp.id,
126 'holiday_status_id': self.leave_type.id,
127 'number_of_days': 0,
128 'allocation_type': 'accrual',
129 })
130 allocation.action_approve()
131
132 self.env['hr.attendance'].create({
133 'employee_id': self.employee_emp.id,
134 'check_in': datetime.datetime(2024, 4, 1, 22, 0, 0),
135 'check_out': datetime.datetime(2024, 4, 2, 7, 0, 0),
136 })
137
138 with freeze_time(datetime.datetime(2024, 4, 2, 20, 0, 0)):
139 # Only counts the part of the attendance on the 01/04/2024: 2 hours
140 allocation._update_accrual()
141 self.assertEqual(allocation.number_of_days, 0.25) # 2 / 8 = 0.25
142
143 with freeze_time(datetime.datetime(2024, 4, 3, 20, 0, 0)):
144 # Counts the whole attendance: 9 hours
145 allocation._update_accrual()
146 self.assertEqual(allocation.number_of_days, 1.125) # 9 / 8 = 1.125
147
148 def test_accrual_allocation_with_overlapping_attendance_timezone(self):
149 self.employee_emp.tz = 'Asia/Tokyo'
150 self.employee_emp.resource_calendar_id.tz = 'Asia/Tokyo'
151 accrual_plan = self.env['hr.leave.accrual.plan'].create({
152 'name': 'Accrual Plan For Test',
153 'is_based_on_worked_time': True,
154 'accrued_gain_time': 'end',
155 'carryover_date': 'year_start',
156 'level_ids': [(0, 0, {
157 'start_count': 0,
158 'added_value': 1,
159 'added_value_type': 'hour',
160 'cap_accrued_time': True,
161 'maximum_leave': 100,
162 'frequency': 'worked_hours'
163 })],
164 })
165 with freeze_time("2024-4-1"):
166 allocation = self.env['hr.leave.allocation'].create({
167 'name': 'Accrual allocation for employee',
168 'accrual_plan_id': accrual_plan.id,
169 'employee_id': self.employee_emp.id,
170 'holiday_status_id': self.leave_type.id,
171 'number_of_days': 0,
172 'allocation_type': 'accrual',
173 })
174 allocation.action_approve()
175
176 self.env['hr.attendance'].create({
177 'employee_id': self.employee_emp.id,
178 'check_in': datetime.datetime(2024, 4, 1, 22, 0, 0), # In Tokyo: 2024/04/02, 7h
179 'check_out': datetime.datetime(2024, 4, 2, 7, 0, 0), # In Tokyo: 2024/04/02, 16h
180 })
181
182 with freeze_time(datetime.datetime(2024, 4, 2, 20, 0, 0)):
183 # Only counts the part of the attendance on the 01/04/2024 UTC: 2 hours
184 allocation._update_accrual()
185 self.assertEqual(allocation.number_of_days, 0.25) # 2 / 8 = 0.25
186
187 with freeze_time(datetime.datetime(2024, 4, 3, 20, 0, 0)):
188 # Counts the whole attendance: 9 hours - 1h of lunchtime = 8h
189 allocation._update_accrual()
190 self.assertEqual(allocation.number_of_days, 1.0) # 8 / 8 = 1.0