Debian Codegraph @

addons/hr_holidays_attendance/tests/test_holidays_overtime.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from datetime import datetime
4
5 from odoo import Command
6 from odoo.tests import new_test_user
7 from odoo.tests.common import TransactionCase, tagged
8
9 from odoo.exceptions import ValidationError
10
11 from freezegun import freeze_time
12 import time
13
14 @tagged('post_install', '-at_install', 'holidays_attendance')
15 class TestHolidaysOvertime(TransactionCase):
16
17 @classmethod
18 def setUpClass(cls):
19 super().setUpClass()
20 cls.company = cls.env['res.company'].create({
21 'name': 'SweatChipChop Inc.',
22 })
23 cls.user = new_test_user(cls.env, login='user', groups='base.group_user', company_id=cls.company.id).with_company(cls.company)
24 cls.user_manager = new_test_user(cls.env, login='manager', groups='base.group_user,hr_holidays.group_hr_holidays_user,hr_attendance.group_hr_attendance_manager', company_id=cls.company.id).with_company(cls.company)
25
26 cls.manager = cls.env['hr.employee'].create({
27 'name': 'Dominique',
28 'user_id': cls.user_manager.id,
29 'company_id': cls.company.id,
30 })
31 cls.employee = cls.env['hr.employee'].create({
32 'name': 'Barnabé',
33 'user_id': cls.user.id,
34 'parent_id': cls.manager.id,
35 'company_id': cls.company.id,
36 })
37
38 cls.leave_type_no_alloc = cls.env['hr.leave.type'].create({
39 'name': 'Overtime Compensation No Allocation',
40 'company_id': cls.company.id,
41 'requires_allocation': False,
42 'overtime_deductible': True,
43 })
44 cls.leave_type_employee_allocation = cls.env['hr.leave.type'].create({
45 'name': 'Overtime Compensation Employee Allocation',
46 'company_id': cls.company.id,
47 'requires_allocation': True,
48 'employee_requests': True,
49 'allocation_validation_type': 'hr',
50 'overtime_deductible': True,
51 })
52
53 cls.ruleset = cls.env['hr.attendance.overtime.ruleset'].create({
54 'name': 'Ruleset schedule quantity',
55 'rule_ids': [
56 Command.create({
57 'name': 'Rule schedule quantity',
58 'base_off': 'quantity',
59 'expected_hours_from_contract': True,
60 'quantity_period': 'day',
61 'compensable_as_leave': True,
62 }),
63 ],
64 })
65
66 cls.employee.ruleset_id = cls.ruleset
67 cls.employee.version_ids.sorted('date_version')[0].date_version = datetime(2020, 1, 1).date()
68
69 cls.manager.ruleset_id = cls.ruleset
70 cls.manager.version_ids.sorted('date_version')[0].date_version = datetime(2020, 1, 1).date()
71
72 def new_attendance(self, check_in, check_out=False):
73 return self.env['hr.attendance'].sudo().create({
74 'employee_id': self.employee.id,
75 'check_in': check_in,
76 'check_out': check_out,
77 })
78
79 def _check_deductible(self, expected_hours):
80 ded = self.employee._get_deductible_employee_overtime()
81 self.assertAlmostEqual(ded[self.employee], expected_hours, 5)
82
83 def test_check_overtime(self):
84 with self.with_user('user'):
85 self.assertEqual(self.employee.total_overtime, 0, 'No overtime')
86
87 with self.assertRaises(ValidationError):
88 self.env['hr.leave'].create({
89 'name': 'no overtime',
90 'employee_id': self.employee.id,
91 'holiday_status_id': self.leave_type_no_alloc.id,
92 'request_date_from': datetime(2021, 1, 4),
93 'request_date_to': datetime(2021, 1, 4),
94 'state': 'confirm',
95 })
96
97 self.new_attendance(check_in=datetime(2021, 1, 2, 8), check_out=datetime(2021, 1, 2, 16))
98 self.assertEqual(self.employee.total_overtime, 8, 'Should have 8 hours of overtime')
99
100 overtime_leave_data = self.leave_type_no_alloc.get_allocation_data(self.employee)
101 self.assertEqual(overtime_leave_data[self.employee][0][1]['virtual_remaining_leaves'], 8.0)
102 # `employee_company` must be present to avoid traceback when opening the Time Off Type
103 self.assertTrue(overtime_leave_data[self.employee][0][1].get('employee_company'))
104
105 def test_leave_adjust_overtime(self):
106 self.new_attendance(check_in=datetime(2021, 1, 2, 8), check_out=datetime(2021, 1, 2, 16))
107 self.assertEqual(self.employee.total_overtime, 8, 'Should have 8 hours of overtime')
108
109 leave = self.env['hr.leave'].create({
110 'name': 'no overtime',
111 'employee_id': self.employee.id,
112 'holiday_status_id': self.leave_type_no_alloc.id,
113 'request_date_from': datetime(2021, 1, 4),
114 'request_date_to': datetime(2021, 1, 4),
115 })
116
117 self._check_deductible(0)
118 leave.action_refuse()
119 self._check_deductible(8)
120
121 def test_leave_check_overtime_write(self):
122 self.new_attendance(check_in=datetime(2021, 1, 2, 8), check_out=datetime(2021, 1, 2, 16))
123 self.new_attendance(check_in=datetime(2021, 1, 3, 8), check_out=datetime(2021, 1, 3, 16))
124 self.assertEqual(self.employee.total_overtime, 16)
125
126 leave = self.env['hr.leave'].create({
127 'name': 'no overtime',
128 'employee_id': self.employee.id,
129 'holiday_status_id': self.leave_type_no_alloc.id,
130 'request_date_from': '2021-01-04',
131 'request_date_to': '2021-01-04',
132 })
133 self._check_deductible(8)
134
135 leave.date_to = datetime(2021, 1, 5)
136 self._check_deductible(0)
137 with self.assertRaises(ValidationError):
138 leave.date_to = datetime(2021, 1, 6)
139
140 leave.date_to = datetime(2021, 1, 4)
141 self._check_deductible(8)
142
143 def test_employee_create_allocation(self):
144 with self.with_user('user'):
145 self.assertEqual(self.employee.total_overtime, 0)
146 with self.assertRaises(ValidationError):
147 self.env['hr.leave.allocation'].create({
148 'name': 'test allocation',
149 'holiday_status_id': self.leave_type_employee_allocation.id,
150 'employee_id': self.employee.id,
151 'number_of_days': 1,
152 'state': 'confirm',
153 'date_from': time.strftime('%Y-01-01'),
154 'date_to': time.strftime('%Y-12-31'),
155 })
156
157 self.new_attendance(check_in=datetime(2021, 1, 2, 8), check_out=datetime(2021, 1, 2, 16))
158 self.assertAlmostEqual(self.employee.total_overtime, 8, 'Should have 8 hours of overtime')
159
160 self.env['hr.leave.allocation'].sudo().create({
161 'name': 'test allocation',
162 'holiday_status_id': self.leave_type_employee_allocation.id,
163 'employee_id': self.employee.id,
164 'number_of_days': 1,
165 'state': 'confirm',
166 'date_from': time.strftime('%Y-01-01'),
167 'date_to': time.strftime('%Y-12-31'),
168 })
169 self._check_deductible(0)
170
171 leave_type = self.env['hr.leave.type'].sudo().create({
172 'name': 'Overtime Compensation Employee Allocation',
173 'company_id': self.company.id,
174 'requires_allocation': True,
175 'employee_requests': True,
176 'allocation_validation_type': 'hr',
177 'overtime_deductible': False,
178 })
179
180 # User can request another allocation even without overtime
181 self.env['hr.leave.allocation'].create({
182 'name': 'test allocation',
183 'holiday_status_id': leave_type.id,
184 'employee_id': self.employee.id,
185 'number_of_days': 1,
186 'state': 'confirm',
187 'date_from': time.strftime('%Y-01-01'),
188 'date_to': time.strftime('%Y-12-31'),
189 })
190
191 def test_allocation_check_overtime_write(self):
192 self.new_attendance(check_in=datetime(2021, 1, 2, 8), check_out=datetime(2021, 1, 2, 16))
193 self.new_attendance(check_in=datetime(2021, 1, 3, 8), check_out=datetime(2021, 1, 3, 16))
194 self.assertEqual(self.employee.total_overtime, 16, 'Should have 16 hours of overtime')
195 self._check_deductible(16)
196
197 alloc = self.env['hr.leave.allocation'].create({
198 'name': 'test allocation',
199 'holiday_status_id': self.leave_type_employee_allocation.id,
200 'employee_id': self.employee.id,
201 'number_of_days': 1,
202 'state': 'confirm',
203 'date_from': time.strftime('%Y-01-01'),
204 'date_to': time.strftime('%Y-12-31'),
205 })
206 self._check_deductible(8)
207
208 with self.assertRaises(ValidationError):
209 alloc.number_of_days = 3
210
211 alloc.number_of_days = 2
212 self._check_deductible(0)
213
214 @freeze_time('2022-01-01')
215 def test_leave_check_cancel(self):
216 self.new_attendance(check_in=datetime(2021, 1, 2, 8), check_out=datetime(2021, 1, 2, 16))
217 self.new_attendance(check_in=datetime(2021, 1, 3, 8), check_out=datetime(2021, 1, 3, 16))
218 self.assertEqual(self.employee.total_overtime, 16)
219
220 leave = self.env['hr.leave'].create({
221 'name': 'no overtime',
222 'employee_id': self.employee.id,
223 'holiday_status_id': self.leave_type_no_alloc.id,
224 'request_date_from': '2022-01-06',
225 'request_date_to': '2022-01-06',
226 })
227 leave.with_user(self.user_manager).action_approve()
228 self._check_deductible(8)
229
230 self.assertTrue(leave.with_user(self.user).can_cancel)
231 self.env['hr.holidays.cancel.leave'].with_user(self.user).with_context(default_leave_id=leave.id) \
232 .new({'reason': 'Test remove holiday'}) \
233 .action_cancel_leave()
234 self._check_deductible(16)
235
236 def test_public_leave_overtime_with_timing_rule(self):
237 ruleset_with_timing_rule = self.env['hr.attendance.overtime.ruleset'].create({
238 'name': 'Ruleset schedule quantity',
239 'rule_ids': [
240 Command.create({
241 'name': 'Rule schedule quantity',
242 'base_off': 'quantity',
243 'expected_hours_from_contract': True,
244 'quantity_period': 'day',
245 'compensable_as_leave': True,
246 }),
247 Command.create({
248 'name': 'Rule employee is off',
249 'base_off': 'timing',
250 'timing_type': 'leave',
251 }),
252 ],
253 })
254 (self.employee.version_ids + self.manager.version_ids).ruleset_id = ruleset_with_timing_rule
255 self.manager.company_id = self.env.company
256 leave = self.env['resource.calendar.leaves'].with_company(self.manager.company_id).create([{
257 'name': 'Public Holiday',
258 'date_from': datetime(2022, 5, 5, 6),
259 'date_to': datetime(2022, 5, 5, 18),
260 }])
261
262 leave.company_id.write({
263 'attendance_overtime_validation': 'no_validation',
264 })
265 for emp in [self.employee, self.manager]:
266 self.env['hr.attendance'].create({
267 'employee_id': emp.id,
268 'check_in': datetime(2022, 5, 5, 8),
269 'check_out': datetime(2022, 5, 5, 17),
270 })
271
272 self.assertEqual(self.employee.total_overtime, 0, 'Should have 0 hours of overtime')
273 self.assertEqual(self.manager.total_overtime, 9, "Should have 9 hours of overtime")
274
275 def test_public_leave_overtime_without_timing_rule(self):
276 self.manager.company_id = self.env.company
277 leave = self.env['resource.calendar.leaves'].with_company(self.manager.company_id).create([{
278 'name': 'Public Holiday',
279 'date_from': datetime(2022, 5, 5, 6),
280 'date_to': datetime(2022, 5, 5, 18),
281 }])
282
283 leave.company_id.write({
284 'attendance_overtime_validation': 'no_validation',
285 })
286 for emp in [self.employee, self.manager]:
287 self.env['hr.attendance'].create({
288 'employee_id': emp.id,
289 'check_in': datetime(2022, 5, 5, 8),
290 'check_out': datetime(2022, 5, 5, 17),
291 })
292
293 self.assertEqual(self.employee.total_overtime, 0, 'Should have 0 hours of overtime')
294 self.assertEqual(self.manager.total_overtime, 9, "Should have 9 hours of overtime (because of the quantity rule)")
295
296 def test_worked_leave_type_overtime(self):
297 """ Test that an attendance during a worked time off doesn't count as overtime. """
298 calendar = self.env['resource.calendar'].create({'name': 'Calendar'})
299 self.env['hr.version'].create({
300 'date_version': datetime(2021, 1, 1),
301 'contract_date_start': datetime(2021, 1, 1),
302 'contract_date_end': datetime(2021, 12, 31),
303 'name': 'Contract 2021',
304 'resource_calendar_id': calendar.id,
305 'wage': 5000.0,
306 'employee_id': self.employee.id,
307 })
308
309 leave_type_worked = self.env['hr.leave.type'].create({
310 'name': 'Worked Leave Type',
311 'company_id': self.company.id,
312 'requires_allocation': False,
313 'overtime_deductible': False,
314 'time_type': 'other',
315 })
316
317 leave = self.env['hr.leave'].create({
318 'name': 'no overtime',
319 'employee_id': self.employee.id,
320 'holiday_status_id': leave_type_worked.id,
321 'request_date_from': datetime(2021, 1, 5),
322 'request_date_to': datetime(2021, 1, 5),
323 })
324 leave._action_validate()
325
326 att = self.env['hr.attendance'].create({
327 'employee_id': self.employee.id,
328 'check_in': datetime(2021, 1, 5, 8),
329 'check_out': datetime(2021, 1, 5, 16),
330 })
331
332 self.assertEqual(att.overtime_hours, 0)
333 self.assertEqual(att.worked_hours, 7)
334
335 self.assertEqual(self.employee.total_overtime, 0, 'Should have 0 hours of overtime')
336
337 def test_overtime_approval_after_refusal(self):
338 self.new_attendance(check_in=datetime(2021, 1, 2, 8), check_out=datetime(2021, 1, 2, 16))
339 self.new_attendance(check_in=datetime(2021, 1, 3, 8), check_out=datetime(2021, 1, 3, 16))
340 self.assertEqual(self.employee.total_overtime, 16)
341
342 leave = self.env['hr.leave'].create({
343 'name': 'no overtime',
344 'employee_id': self.employee.id,
345 'holiday_status_id': self.leave_type_no_alloc.id,
346 'request_date_from': '2022-1-6',
347 'request_date_to': '2022-1-6',
348 })
349 leave.with_user(self.user_manager).action_approve()
350 self._check_deductible(8)
351
352 leave.with_user(self.user_manager).action_refuse()
353 self._check_deductible(16)
354
355 leave.with_user(self.user_manager).action_approve(check_state=False)
356 self._check_deductible(8)
357
358 def test_get_overtime_data_by_employee(self):
359 # Even if employee has not overtime, it should still appear in return
360 # value
361 expected_overtime_data = {
362 'compensable_overtime': 0,
363 'not_compensable_overtime': 0,
364 'unspent_compensable_overtime': 0,
365 }
366 overtime_data = self.employee.get_overtime_data_by_employee()
367 self.assertEqual(
368 overtime_data[self.employee.id],
369 expected_overtime_data,
370 "get_overtime_data_by_employee() did not return an empty overtime_data",
371 )
372
373 # These attendances will create some extra hours that is deductible as
374 # time off
375 self.new_attendance(
376 check_in=datetime(2021, 1, 1, 8), check_out=datetime(2021, 1, 1, 20)
377 )
378 self.new_attendance(
379 check_in=datetime(2021, 1, 2, 4), check_out=datetime(2021, 1, 2, 20)
380 )
381 self.new_attendance(
382 check_in=datetime(2021, 2, 2, 4), check_out=datetime(2021, 2, 2, 18)
383 )
384
385 # The extra hours from the next attendances will not be deductible as
386 # time off. Affects compensable_overtime's value.
387 not_compensable_ruleset = self.env[
388 'hr.attendance.overtime.ruleset'
389 ].create(
390 {
391 'name': 'Ruleset schedule quantity',
392 'rule_ids': [
393 Command.create(
394 {
395 'name': 'Extra Mile',
396 'base_off': 'quantity',
397 'expected_hours_from_contract': True,
398 'quantity_period': 'day',
399 'compensable_as_leave': False,
400 }
401 ),
402 ],
403 }
404 )
405 self.employee.ruleset_id = not_compensable_ruleset
406
407 # Creates extra hours, but won't be usable as time off
408 # Affects not_compensable_overtime's value.
409 self.new_attendance(
410 check_in=datetime(2021, 3, 3, 5), check_out=datetime(2021, 3, 3, 20)
411 )
412
413 # Use some of the overtime as a day off (8 hours)
414 # Affects unspent_compensable_time's value
415 leave = self.env['hr.leave'].create(
416 {
417 'name': 'no overtime',
418 'employee_id': self.employee.id,
419 'holiday_status_id': self.leave_type_no_alloc.id,
420 'request_date_from': '2022-1-6',
421 'request_date_to': '2022-1-6',
422 }
423 )
424 leave.with_user(self.user_manager).action_approve()
425
426 expected_overtime_data = {
427 'compensable_overtime': 24.0,
428 'not_compensable_overtime': 6.0,
429 'unspent_compensable_overtime': 16.0,
430 }
431 overtime_data = self.employee.get_overtime_data_by_employee()
432 self.assertEqual(
433 overtime_data[self.employee.id],
434 expected_overtime_data,
435 "get_overtime_data_by_employee() did not return the expected values",
436 )