Debian Codegraph @

addons/hr_attendance/tests/test_hr_attendance_rulesets.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 from datetime import date, datetime
3 from freezegun import freeze_time
4
5 from odoo import Command
6 from odoo.tests import new_test_user, Form
7 from odoo.tests.common import tagged, TransactionCase
8
9
10 @tagged('hr_attendance_overtime_ruleset')
11 class TestHrAttendanceOvertime(TransactionCase):
12 """ Tests for overtime """
13
14 @classmethod
15 def setUpClass(cls):
16 super().setUpClass()
17 cls.ruleset = cls.env['hr.attendance.overtime.ruleset'].create({
18 'name': 'Ruleset schedule quantity',
19 'rule_ids': [
20 Command.create({
21 'name': 'Rule schedule quantity',
22 'base_off': 'quantity',
23 'quantity_period': 'day',
24 'expected_hours': 8,
25 'paid': True,
26 'amount_rate': 150,
27 }),
28 Command.create({
29 'name': 'Rule schedule quantity',
30 'base_off': 'quantity',
31 'quantity_period': 'day',
32 'expected_hours': 10,
33 'paid': True,
34 'amount_rate': 200,
35 }),
36 Command.create({
37 'name': 'Rule schedule quantity',
38 'base_off': 'quantity',
39 'quantity_period': 'week',
40 'expected_hours': 40,
41 'paid': True,
42 'amount_rate': 150,
43 }),
44 ],
45 })
46
47 cls.company = cls.env['res.company'].create({
48 'name': 'SweatChipChop Inc.',
49 'attendance_overtime_validation': 'no_validation',
50 })
51 cls.company.resource_calendar_id = cls.env.company.resource_calendar_id = cls.env['resource.calendar'].create({
52 'name': 'Standard 40 hours/week (No Lunch)',
53 'company_id': cls.env.company.id,
54 'hours_per_day': 7.6,
55 'full_time_required_hours': 38,
56 'attendance_ids': [
57 (5, 0, 0), # Clear existing attendances
58 (0, 0, {'name': 'Monday', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 16, 'day_period': 'morning'}),
59 (0, 0, {'name': 'Tuesday', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 16, 'day_period': 'morning'}),
60 (0, 0, {'name': 'Wednesday', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 16, 'day_period': 'morning'}),
61 (0, 0, {'name': 'Thursday', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 16, 'day_period': 'morning'}),
62 (0, 0, {'name': 'Friday', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 16, 'day_period': 'morning'}),
63 ],
64 })
65
66 cls.user = new_test_user(cls.env, login='fru', groups='base.group_user,hr_attendance.group_hr_attendance_manager', company_id=cls.company.id).with_company(cls.company)
67 cls.employee = cls.env['hr.employee'].create({
68 'name': "Marie-Edouard De La Court",
69 'user_id': cls.user.id,
70 'company_id': cls.company.id,
71 'tz': 'UTC',
72 'date_version': date(2020, 1, 1),
73 'contract_date_start': date(2020, 1, 1),
74 'resource_calendar_id': cls.company.resource_calendar_id.id,
75 'ruleset_id': cls.ruleset.id
76 })
77
78 def test_daily_overtime_8_hours_rule(self):
79 with freeze_time("2021-01-04"):
80 # Attendance: 10 hours (8 expected + 2 overtime at 150%)
81 attendance = self.env['hr.attendance'].create({
82 'employee_id': self.employee.id,
83 'check_in': datetime(2021, 1, 4, 8, 0),
84 'check_out': datetime(2021, 1, 4, 18, 0)
85 })
86
87 self.assertAlmostEqual(attendance.employee_id.total_overtime, 2, 2, msg="2 hours overtime at 150% should yield 2 hours total overtime")
88
89 def test_daily_overtime_10_hours_rule(self):
90 """ Test daily overtime for the 10-hour rule """
91 with freeze_time("2021-01-04"):
92 # Attendance: 12 hours (10 expected + 2 overtime at 200%)
93 attendance = self.env['hr.attendance'].create({
94 'employee_id': self.employee.id,
95 'check_in': datetime(2021, 1, 4, 8, 0),
96 'check_out': datetime(2021, 1, 4, 20, 0)
97 })
98
99 self.assertAlmostEqual(attendance.employee_id.total_overtime, 4.0, 2, msg="2 hours overtime at 200% should yield 4 hours total overtime")
100
101 def test_no_overtime(self):
102 """ Test no overtime when working expected hours or less """
103 with freeze_time("2021-01-04"):
104 # Attendance: 8 hours (exactly 8 expected, no overtime)
105 attendance = self.env['hr.attendance'].create({
106 'employee_id': self.employee.id,
107 'check_in': datetime(2021, 1, 4, 8, 0),
108 'check_out': datetime(2021, 1, 4, 16, 0)
109 })
110 self.assertAlmostEqual(attendance.employee_id.total_overtime, 0.0, 2, msg="No overtime should be recorded for 8 hours or less")
111
112 def test_weekly_overtime(self):
113 """ Test weekly overtime for the 40-hour rule """
114 with freeze_time("2021-01-04"):
115 # Week: Mon-Fri, 10 hours/day = 50 hours total (40 expected + 10 overtime at 200%)
116 [
117 self.env['hr.attendance'].create({
118 'employee_id': self.employee.id, # This employee have a calendar with no lunch
119 'check_in': datetime(2021, 1, day, 8, 0),
120 'check_out': datetime(2021, 1, day, 18, 0)
121 }) for day in range(4, 9) # Monday to Friday
122 ]
123 self.assertAlmostEqual(self.employee.total_overtime, 10, 2, msg="He should work from 8-16h so each day he did 2 hours of overtime")
124
125 def test_multiple_attendances_same_day(self):
126 """ Test multiple attendances in one day """
127 with freeze_time("2021-01-04"):
128 # Two attendances: 6 hours + 6 hours = 12 hours (10 expected + 2 overtime at 200%)
129 self.env['hr.attendance'].create([
130 {
131 'employee_id': self.employee.id,
132 'check_in': datetime(2021, 1, 4, 8, 0),
133 'check_out': datetime(2021, 1, 4, 14, 0)
134 },
135 {
136 'employee_id': self.employee.id,
137 'check_in': datetime(2021, 1, 4, 14, 0),
138 'check_out': datetime(2021, 1, 4, 20, 0)
139 }
140 ])
141
142 self.assertAlmostEqual(self.employee.total_overtime, 4.0, 2, msg="2 hours overtime at 200% should yield 4 hours total overtime")
143
144 def test_partial_week(self):
145 """ Test partial week with overtime """
146 with freeze_time("2021-01-04"):
147 # Week: Mon-Wed, 12 hours/day = 36 hours total (no weekly overtime, daily overtime applies)
148 [
149 self.env['hr.attendance'].create({
150 'employee_id': self.employee.id,
151 'check_in': datetime(2021, 1, day, 8, 0),
152 'check_out': datetime(2021, 1, day, 20, 0)
153 }) for day in range(4, 7) # Monday to Wednesday
154 ]
155
156 self.assertAlmostEqual(self.employee.total_overtime, 12.0, 2, msg="3 days of 2 hours overtime at 200% should yield 12 hours total overtime")
157
158 def test_access_ruleset_on_employee(self):
159 """
160 Test the access rights of the ruleset on the employee
161 Only the employee admin should be able to see and change the ruleset on the employee
162 """
163 user = new_test_user(self.env, login='usr', groups='hr.group_hr_user', company_id=self.company.id).with_company(self.company)
164 employee = self.env['hr.employee'].with_company(self.company).create({'name': "Employee Test"})
165 with Form(employee.with_user(user)) as employee_form:
166 self.assertFalse("ruleset_id" in employee_form._view['fields'])
167
168 # HR Mangers should be able to see the ruleset on the employee
169 user.group_ids |= self.env.ref('hr.group_hr_manager')
170 # fix le truc chelou de pas pouvoir ecrire la surrement les access rule
171 with Form(employee.with_user(user)) as employee_form:
172 self.assertTrue("ruleset_id" in employee_form._view['fields'])
173 employee_form.record.ruleset_id = self.ruleset.id
174
175 def test_is_manager_with_overtime(self):
176 """ Test the computation of is_manager with overtime """
177 user = new_test_user(self.env, login='usr', groups='hr_attendance.group_hr_attendance_officer', company_id=self.company.id).with_company(self.company)
178 self.employee.attendance_manager_id = user.id
179 attendance = self.env['hr.attendance'].with_company(self.company).create({
180 'employee_id': self.employee.id,
181 'check_in': datetime(2021, 1, 4, 8, 0),
182 'check_out': datetime(2021, 1, 4, 20, 0)
183 })
184 self.assertTrue(attendance.with_user(user).linked_overtime_ids.is_manager)