Debian Codegraph @

addons/l10n_fr_hr_holidays/tests/test_french_leaves.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 import logging
4 import time
5
6 from datetime import date
7 from odoo.tests.common import TransactionCase, tagged
8
9 from odoo import fields
10 import pytz
11
12 _logger = logging.getLogger(__name__)
13
14 @tagged('post_install_l10n', 'post_install', '-at_install', 'french_leaves')
15 class TestFrenchLeaves(TransactionCase):
16
17 @classmethod
18 def setUpClass(cls):
19 super().setUpClass()
20 country_fr = cls.env.ref('base.fr')
21 cls.company = cls.env['res.company'].create({
22 'name': 'French Company',
23 'country_id': country_fr.id,
24 })
25
26 cls.employee = cls.env['hr.employee'].create({
27 'name': 'Louis',
28 'sex': 'male',
29 'birthday': '1973-03-29',
30 'country_id': country_fr.id,
31 'company_id': cls.company.id,
32 })
33
34 cls.time_off_type = cls.env['hr.leave.type'].create({
35 'name': 'Time Off',
36 'requires_allocation': False,
37 'request_unit': 'half_day',
38 })
39 cls.company.write({
40 'l10n_fr_reference_leave_type': cls.time_off_type.id,
41 })
42
43 cls.base_calendar = cls.env['resource.calendar'].create({
44 'name': 'default calendar',
45 })
46
47 def test_no_differences(self):
48 # Base case that should not have a different behaviour
49 self.company.resource_calendar_id = self.base_calendar
50 self.employee.resource_calendar_id = self.base_calendar
51
52 leave = self.env['hr.leave'].create({
53 'name': 'Test',
54 'holiday_status_id': self.time_off_type.id,
55 'employee_id': self.employee.id,
56 'request_date_from': '2021-09-06',
57 'request_date_to': '2021-09-10',
58 })
59 self.assertEqual(leave.number_of_days, 5, 'The number of days should be equal to 5.')
60
61 def test_end_of_week(self):
62 employee_calendar = self.env['resource.calendar'].create({
63 'name': 'Employee Calendar',
64 'attendance_ids': [
65 (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
66 (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
67 (0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
68 (0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
69 (0, 0, {'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
70 (0, 0, {'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
71 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
72 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
73 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
74 ],
75 })
76 self.company.resource_calendar_id = self.base_calendar
77 self.employee.resource_calendar_id = employee_calendar
78
79 leave = self.env['hr.leave'].create({
80 'name': 'Test',
81 'holiday_status_id': self.time_off_type.id,
82 'employee_id': self.employee.id,
83 'request_date_from': '2021-09-06',
84 'request_date_to': '2021-09-08',
85 })
86 self.assertEqual(leave.number_of_days, 5, 'The number of days should be equal to 5.')
87
88 def test_start_of_week(self):
89 employee_calendar = self.env['resource.calendar'].create({
90 'name': 'Employee Calendar',
91 'attendance_ids': [
92 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
93 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
94 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
95 (0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
96 (0, 0, {'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
97 (0, 0, {'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
98 (0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
99 (0, 0, {'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
100 (0, 0, {'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
101 ],
102 })
103 self.company.resource_calendar_id = self.base_calendar
104 self.employee.resource_calendar_id = employee_calendar
105
106 leave = self.env['hr.leave'].create({
107 'name': 'Test',
108 'holiday_status_id': self.time_off_type.id,
109 'employee_id': self.employee.id,
110 'request_date_from': '2021-09-08',
111 'request_date_to': '2021-09-10',
112 })
113 self.assertEqual(leave.number_of_days, 5, 'The number of days should be equal to 5.')
114
115 def test_last_day_half(self):
116 employee_calendar = self.env['resource.calendar'].create({
117 'name': 'Employee Calendar',
118 'attendance_ids': [
119 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
120 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
121 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
122 (0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
123 (0, 0, {'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
124 (0, 0, {'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
125 (0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
126 (0, 0, {'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
127 (0, 0, {'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
128 ],
129 })
130 self.company.resource_calendar_id = self.base_calendar
131 self.employee.resource_calendar_id = employee_calendar
132
133 leave = self.env['hr.leave'].create({
134 'name': 'Test',
135 'holiday_status_id': self.time_off_type.id,
136 'employee_id': self.employee.id,
137 'request_date_from': '2021-09-10',
138 'request_date_to': '2021-09-10',
139 'request_date_from_period': 'am',
140 'request_date_to_period': 'am',
141 })
142 # Since the employee works on the afternoon, the date_to is not post-poned
143 self.assertEqual(leave.number_of_days, 0.5, 'The number of days should be equal to 0.5.')
144 leave.request_date_from_period = 'pm'
145 leave.request_date_to_period = 'pm'
146 # This however should push the date_to
147 self.assertEqual(leave.number_of_days, 2.5, 'The number of days should be equal to 2.5.')
148
149 def test_calendar_with_holes(self):
150 employee_calendar = self.env['resource.calendar'].create({
151 'name': 'Employee Calendar',
152 'attendance_ids': [
153 (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
154 (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
155 (0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
156 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
157 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
158 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
159 (0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
160 (0, 0, {'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
161 (0, 0, {'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
162 ],
163 })
164 self.company.resource_calendar_id = self.base_calendar
165 self.employee.resource_calendar_id = employee_calendar
166
167 leave = self.env['hr.leave'].create({
168 'name': 'Test',
169 'holiday_status_id': self.time_off_type.id,
170 'employee_id': self.employee.id,
171 'request_date_from': '2021-09-06',
172 'request_date_to': '2021-09-10',
173 })
174 self.assertEqual(leave.number_of_days, 5, 'The number of days should be equal to 5.')
175
176 def test_calendar_end_week_hole(self):
177 employee_calendar = self.env['resource.calendar'].create({
178 'name': 'Employee Calendar',
179 'attendance_ids': [
180 (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
181 (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
182 (0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
183 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
184 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
185 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
186 ],
187 })
188 self.company.resource_calendar_id = self.base_calendar
189 self.employee.resource_calendar_id = employee_calendar
190
191 leave = self.env['hr.leave'].create({
192 'name': 'Test',
193 'holiday_status_id': self.time_off_type.id,
194 'employee_id': self.employee.id,
195 'request_date_from': '2021-09-06',
196 'request_date_to': '2021-09-08',
197 })
198 self.assertEqual(leave.number_of_days, 5, 'The number of days should be equal to 5.')
199
200 def test_2_weeks_calendar(self):
201 company_calendar = self.env['resource.calendar'].create({
202 'name': 'Company Calendar',
203 'two_weeks_calendar': True,
204 'attendance_ids': [
205 (0, 0, {'week_type': '0', 'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
206 (0, 0, {'week_type': '0', 'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
207 (0, 0, {'week_type': '0', 'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
208 (0, 0, {'week_type': '0', 'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
209 (0, 0, {'week_type': '0', 'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
210 (0, 0, {'week_type': '0', 'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
211 (0, 0, {'week_type': '0', 'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
212 (0, 0, {'week_type': '0', 'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
213 (0, 0, {'week_type': '0', 'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
214 (0, 0, {'week_type': '0', 'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
215 (0, 0, {'week_type': '0', 'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
216 (0, 0, {'week_type': '0', 'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
217 (0, 0, {'week_type': '0', 'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
218 (0, 0, {'week_type': '0', 'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
219 (0, 0, {'week_type': '0', 'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
220
221 (0, 0, {'week_type': '1', 'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
222 (0, 0, {'week_type': '1', 'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
223 (0, 0, {'week_type': '1', 'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
224 (0, 0, {'week_type': '1', 'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
225 (0, 0, {'week_type': '1', 'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
226 (0, 0, {'week_type': '1', 'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
227 (0, 0, {'week_type': '1', 'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
228 (0, 0, {'week_type': '1', 'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
229 (0, 0, {'week_type': '1', 'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
230 ],
231 })
232 employee_calendar = self.env['resource.calendar'].create({
233 'name': 'Employee Calendar',
234 'attendance_ids': [
235 (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
236 (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
237 (0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
238 (0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
239 (0, 0, {'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
240 (0, 0, {'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
241 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
242 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
243 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
244 ],
245 })
246 self.company.resource_calendar_id = company_calendar
247 self.employee.resource_calendar_id = employee_calendar
248
249 # Week type 0
250 leave = self.env['hr.leave'].create({
251 'name': 'Test',
252 'holiday_status_id': self.time_off_type.id,
253 'employee_id': self.employee.id,
254 'request_date_from': '2021-09-06',
255 'request_date_to': '2021-09-08',
256 })
257 self.assertEqual(leave.number_of_days, 5, 'The number of days should be equal to 5.')
258 leave.unlink()
259
260 # Week type 1
261 leave = self.env['hr.leave'].create({
262 'name': 'Test',
263 'holiday_status_id': self.time_off_type.id,
264 'employee_id': self.employee.id,
265 'request_date_from': '2021-09-13',
266 'request_date_to': '2021-09-15',
267 })
268 self.assertEqual(leave.number_of_days, 3, 'The number of days should be equal to 3.')
269 leave.unlink()
270
271 # Both ending with week type 1
272 leave = self.env['hr.leave'].create({
273 'name': 'Test',
274 'holiday_status_id': self.time_off_type.id,
275 'employee_id': self.employee.id,
276 'request_date_from': '2021-09-06',
277 'request_date_to': '2021-09-15',
278 })
279 self.assertEqual(leave.number_of_days, 8, 'The number of days should be equal to 3.')
280 leave.unlink()
281
282 # Both ending with week type 0
283 with self.assertQueryCount(118):
284 start_time = time.time()
285 leave = self.env['hr.leave'].create({
286 'name': 'Test',
287 'holiday_status_id': self.time_off_type.id,
288 'employee_id': self.employee.id,
289 'request_date_from': '2021-09-13',
290 'request_date_to': '2021-09-22',
291 })
292 # --- 0.11486363410949707 seconds ---
293 _logger.info("French Leave Creation: --- %s seconds ---", time.time() - start_time)
294 self.assertEqual(leave.number_of_days, 8, 'The number of days should be equal to 3.')
295 leave.unlink()
296
297 def test_leave_type_half_day_different_working_hours(self):
298 """
299 Test Case:
300 ==========
301 - Employee works from 8 to 12 and 14 to 17 Monday to Wednesday -> 7h/d
302 - Company works from 9 to 12 and 13 to 18 Monday to Friday -> 8h/d
303 - Employee requests 1 day off on Monday -> duration should be 1.0
304 - Employee requests 0.5 day off on Monday morning or afternoon -> duration should be 0.5
305 """
306 employee_calendar = self.env['resource.calendar'].create({
307 'name': 'Employee Calendar',
308 'attendance_ids': [
309 (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
310 (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 14, 'day_period': 'lunch'}),
311 (0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 14, 'hour_to': 17, 'day_period': 'afternoon'}),
312 (0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
313 (0, 0, {'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 14, 'day_period': 'lunch'}),
314 (0, 0, {'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 14, 'hour_to': 17, 'day_period': 'afternoon'}),
315 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
316 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 14, 'day_period': 'lunch'}),
317 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 14, 'hour_to': 17, 'day_period': 'afternoon'}),
318 ],
319 })
320
321 company_calendar = self.env['resource.calendar'].create({
322 'name': 'Company Calendar',
323 'attendance_ids': [
324 (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 9, 'hour_to': 12, 'day_period': 'morning'}),
325 (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
326 (0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 18, 'day_period': 'afternoon'}),
327 (0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 9, 'hour_to': 12, 'day_period': 'morning'}),
328 (0, 0, {'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
329 (0, 0, {'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 18, 'day_period': 'afternoon'}),
330 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 9, 'hour_to': 12, 'day_period': 'morning'}),
331 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
332 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 18, 'day_period': 'afternoon'}),
333 (0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 9, 'hour_to': 12, 'day_period': 'morning'}),
334 (0, 0, {'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
335 (0, 0, {'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13, 'hour_to': 18, 'day_period': 'afternoon'}),
336 (0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 9, 'hour_to': 12, 'day_period': 'morning'}),
337 (0, 0, {'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
338 (0, 0, {'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13, 'hour_to': 18, 'day_period': 'afternoon'}),
339 ],
340 })
341
342 self.company.resource_calendar_id = company_calendar
343 self.employee.resource_calendar_id = employee_calendar
344
345 leave = self.env['hr.leave'].create({
346 'name': 'Test',
347 'holiday_status_id': self.time_off_type.id,
348 'employee_id': self.employee.id,
349 'request_date_from': '2024-07-29',
350 'request_date_to': '2024-07-29',
351 'request_date_from_period': 'am',
352 'request_date_to_period': 'am',
353 })
354 self.assertEqual(leave.number_of_days, 0.5, 'The duration should be 0.5 day.')
355 self.assertEqual(leave.date_from.date(), date(2024, 7, 29))
356 self.assertEqual(leave.date_to.date(), date(2024, 7, 29))
357 self.assertNotEqual(leave.number_of_hours, 8.0, 'Company and employee hours per day should not match in this case')
358
359 leave.request_date_to_period = 'pm'
360 leave.request_date_from_period = 'pm'
361 self.assertEqual(leave.number_of_days, 0.5, 'The duration should be 0.5 day.')
362 self.assertEqual(leave.date_from.date(), date(2024, 7, 29))
363 self.assertEqual(leave.date_to.date(), date(2024, 7, 29))
364 self.assertNotEqual(leave.number_of_hours, 8.0, 'Company and employee hours per day should not match in this case')
365
366 self.time_off_type.request_unit = "day"
367 leave = self.env['hr.leave'].create({
368 'name': 'Test',
369 'holiday_status_id': self.time_off_type.id,
370 'employee_id': self.employee.id,
371 'request_date_from': '2024-07-22',
372 'request_date_to': '2024-07-22',
373 })
374 self.assertEqual(leave.number_of_days, 1, 'The duration should be 1 day.')
375 self.assertNotEqual(leave.number_of_hours, 8.0, 'Company and employee hours per day should not match in this case')
376
377 def test_leave_full_day_different_working_hours(self):
378 """Check full days leave creation for an employee with different working hours than the 2 weeks company's calendar."""
379
380 self.company.resource_calendar_id = self.env['resource.calendar'].create({
381 'name': 'Company Calendar - 2 weeks with different working hours for each week',
382 'two_weeks_calendar': True,
383 'attendance_ids': [attendance for i in range(5) for attendance in [
384 (0, 0, {'name': f'Day {i} of first week', 'week_type': '0', 'dayofweek': str(i), 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
385 (0, 0, {'name': f'Day {i} of first week', 'week_type': '0', 'dayofweek': str(i), 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
386 (0, 0, {'name': f'Day {i} of first week', 'week_type': '0', 'dayofweek': str(i), 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
387 (0, 0, {'name': f'Day {i} of second week', 'week_type': '1', 'dayofweek': str(i), 'hour_from': 7, 'hour_to': 11, 'day_period': 'morning'}),
388 (0, 0, {'name': f'Day {i} of second week', 'week_type': '1', 'dayofweek': str(i), 'hour_from': 11, 'hour_to': 12, 'day_period': 'lunch'}),
389 (0, 0, {'name': f'Day {i} of second week', 'week_type': '1', 'dayofweek': str(i), 'hour_from': 12, 'hour_to': 16, 'day_period': 'afternoon'})]
390 ]
391 })
392 self.employee.resource_calendar_id = self.env['resource.calendar'].create({
393 'name': 'Employee Calendar',
394 'attendance_ids': [
395 (0, 0, {'name': 'Monday AM', 'dayofweek': '0', 'hour_from': 8.5, 'hour_to': 12.5, 'day_period': 'morning'}),
396 (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12.5, 'hour_to': 13.5, 'day_period': 'lunch'}),
397 (0, 0, {'name': 'Monday PM', 'dayofweek': '0', 'hour_from': 13.5, 'hour_to': 17.5, 'day_period': 'afternoon'}),
398 (0, 0, {'name': 'Tuesday AM', 'dayofweek': '1', 'hour_from': 8.5, 'hour_to': 12.5, 'day_period': 'morning'}),
399 ],
400 })
401
402 leave_1 = self.env['hr.leave'].create({
403 'name': 'Test leave',
404 'holiday_status_id': self.time_off_type.id,
405 'employee_id': self.employee.id,
406 'request_date_from': '2024-10-14',
407 'request_date_to': '2024-10-14',
408 })
409 self.assertEqual(leave_1.number_of_days, 1.0)
410 self.assertEqual(leave_1.date_from.date(), date(2024, 10, 14))
411 self.assertEqual(leave_1.date_to.date(), date(2024, 10, 14))
412
413 leave_2 = self.env['hr.leave'].create({
414 'name': 'Test leave',
415 'holiday_status_id': self.time_off_type.id,
416 'employee_id': self.employee.id,
417 'request_date_from': '2024-10-21',
418 'request_date_to': '2024-10-22',
419 })
420 self.assertEqual(leave_2.number_of_days, 5.0)
421 self.assertEqual(leave_2.date_from.date(), date(2024, 10, 21))
422 self.assertEqual(leave_2.date_to.date(), date(2024, 10, 27))
423
424 def test_leave_employee_different_schedule_from_company(self):
425 self.company.resource_calendar_id = self.env['resource.calendar'].create({
426 'name': 'Company Calendar',
427 'attendance_ids': [attendance for i in range(5) for attendance in [
428 (0, 0, {'name': f'Day {i} of week', 'dayofweek': str(i), 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
429 (0, 0, {'name': f'Day {i} of week', 'dayofweek': str(i), 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
430 (0, 0, {'name': f'Day {i} of week', 'dayofweek': str(i), 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'})]
431 ]
432 })
433 self.employee.resource_calendar_id = self.env['resource.calendar'].create({
434 'name': 'Employee Calendar',
435 'attendance_ids': [attendance for i in range(5) for attendance in [
436 (0, 0, {'name': f'Day {i} of week', 'dayofweek': str(i), 'hour_from': 9, 'hour_to': 12, 'day_period': 'morning'}),
437 (0, 0, {'name': f'Day {i} of week', 'dayofweek': str(i), 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
438 (0, 0, {'name': f'Day {i} of week', 'dayofweek': str(i), 'hour_from': 13, 'hour_to': 17.50, 'day_period': 'afternoon'})]
439 ]
440 })
441
442 leave_1 = self.env['hr.leave'].create({
443 'name': 'Test leave',
444 'holiday_status_id': self.time_off_type.id,
445 'employee_id': self.employee.id,
446 'request_date_from': '2025-08-04',
447 'request_date_to': '2025-08-04',
448 })
449
450 work_hours_data = leave_1.employee_id._list_work_time_per_day(
451 leave_1.date_from,
452 leave_1.date_to)
453
454 self.assertEqual(work_hours_data[leave_1.employee_id.id][0][1], 7.50)
455
456 def test_holiday_in_week(self):
457 """
458 Test Case:
459 ==========
460 - Employee works from Monday to Wednesday
461 - Company works from Monday to Friday
462 - Employee requests monday to wednesday off -> according to french law, he has to take all week (5 days)
463 - In a given week thursday is a holiday -> in that week a whole week is 4 days -> 4 days off
464 """
465 employee_calendar = self.env['resource.calendar'].create({
466 'name': 'Employee Calendar',
467 'attendance_ids': [
468 (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
469 (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
470 (0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
471 (0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
472 (0, 0, {'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
473 (0, 0, {'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
474 (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
475 (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
476 (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
477 ],
478 })
479
480 self.employee.resource_calendar_id = employee_calendar
481 self.company.resource_calendar_id = self.base_calendar
482
483 # Here we have to create a holiday with company, since the company is set based on the env
484 # We also need to take into account that in the frontend this is a one day leave from
485 # 00h00 to 23h59 , but in the server it is saved as utc, so we consider the current user tz
486 # and subtract that from the holiday. With this, wherever you may be running the tests, the
487 # result should be consistent
488 tz = pytz.timezone(self.env.user.tz)
489 self.env['resource.calendar.leaves'].with_company(self.company).create({
490 'name': 'Public Holiday',
491 'calendar_id': False,
492 'date_from': tz.localize(fields.Datetime.from_string("2024-12-26 00:00:00")).astimezone(pytz.UTC).replace(tzinfo=None),
493 'date_to': tz.localize(fields.Datetime.from_string("2024-12-26 23:59:59")).astimezone(pytz.UTC).replace(tzinfo=None),
494 'resource_id': False,
495 })
496
497 leave = self.env['hr.leave'].create({
498 'name': 'Test leave',
499 'holiday_status_id': self.time_off_type.id,
500 'employee_id': self.employee.id,
501 'request_date_from': '2024-12-23',
502 'request_date_to': '2024-12-25',
503 })
504 self.assertEqual(leave.number_of_days, 4.0, 'Public holidays for French part-time employees should be considered')
505
506 def test_employee_2_week_calendar(self):
507 """
508 Test Case:
509 ==========
510 - Employee has a two week calendar
511 - Company has a standard 40hr/week calendar
512 - Time off type is Paid Time Off
513 - Employee requests Monday off
514 """
515 self.time_off_type = self.env['hr.leave.type'].create({
516 'name': 'Paid Time Off',
517 'requires_allocation': 'yes',
518 'employee_requests': 'no',
519 'allocation_validation_type': 'hr',
520 'leave_validation_type': 'both',
521 'request_unit': 'day'
522 })
523 self.company.write({'l10n_fr_reference_leave_type': self.time_off_type.id})
524 self.employee.resource_calendar_id = self.env['resource.calendar'].create({
525 'name': 'Employee Calendar',
526 'two_weeks_calendar': True,
527 'attendance_ids': [
528 (0, 0, {'week_type': '0', 'name': 'First week', 'dayofweek': '0', 'hour_from': 0.0, 'hour_to': 0.0, 'day_period': 'morning', 'display_type': 'line_section'}),
529 (0, 0, {'week_type': '0', 'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
530 (0, 0, {'week_type': '0', 'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
531 (0, 0, {'week_type': '0', 'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
532 (0, 0, {'week_type': '0', 'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
533 (0, 0, {'week_type': '0', 'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
534 (0, 0, {'week_type': '0', 'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
535 (0, 0, {'week_type': '0', 'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
536 (0, 0, {'week_type': '0', 'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
537 (0, 0, {'week_type': '0', 'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
538 (0, 0, {'week_type': '0', 'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
539 (0, 0, {'week_type': '0', 'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
540 (0, 0, {'week_type': '0', 'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
541 (0, 0, {'week_type': '0', 'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
542 (0, 0, {'week_type': '0', 'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
543 (0, 0, {'week_type': '0', 'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
544
545 (0, 0, {'week_type': '1', 'name': 'Second week', 'dayofweek': '0', 'hour_from': 0.0, 'hour_to': 0.0, 'day_period': 'morning', 'display_type': 'line_section'}),
546 (0, 0, {'week_type': '1', 'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
547 (0, 0, {'week_type': '1', 'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
548 (0, 0, {'week_type': '1', 'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
549 (0, 0, {'week_type': '1', 'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
550 (0, 0, {'week_type': '1', 'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
551 (0, 0, {'week_type': '1', 'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
552 (0, 0, {'week_type': '1', 'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
553 (0, 0, {'week_type': '1', 'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
554 (0, 0, {'week_type': '1', 'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
555 (0, 0, {'week_type': '1', 'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
556 (0, 0, {'week_type': '1', 'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
557 (0, 0, {'week_type': '1', 'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
558 (0, 0, {'week_type': '1', 'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8.0, 'hour_to': 12.0, 'day_period': 'morning'}),
559 (0, 0, {'week_type': '1', 'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12.0, 'hour_to': 13.0, 'day_period': 'lunch'}),
560 (0, 0, {'week_type': '1', 'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13.0, 'hour_to': 17.0, 'day_period': 'afternoon'}),
561 ],
562 })
563 self.company.resource_calendar_id = self.base_calendar
564 allocation = self.env['hr.leave.allocation'].create({
565 'name': 'PTO allocation',
566 'holiday_status_id': self.time_off_type.id,
567 'employee_id': self.employee.id,
568 'date_from': '2025-01-01',
569 'number_of_days': 10.0,
570 })
571 allocation.action_approve()
572
573 leave = self.env['hr.leave'].create({
574 'name': 'Test',
575 'holiday_status_id': self.time_off_type.id,
576 'employee_id': self.employee.id,
577 'request_date_from': '2026-01-12',
578 'request_date_to': '2026-01-12',
579 })
580 self.assertEqual(leave.number_of_days, 1, 'The duration should be equal 1 day')
581 self.assertEqual(leave.date_from.date(), date(2026, 1, 12))
582 self.assertEqual(leave.date_to.date(), date(2026, 1, 12))
583 self.assertEqual(leave.number_of_hours, 8.0, 'Duration should be 8 hours (one working day)')