From 646ab82880a5804528830fe0cd1954eadd68cd9c Mon Sep 17 00:00:00 2001 From: Laetitia Da Costa Date: Wed, 4 Nov 2026 11:02:37 +0100 Subject: [PATCH] [FIX]l10n_fr_hr_holidays:skip public holidays when looking for the return day of part-time employees When the next scheduled working day of a part-time employee after the leave is a public holiday, the leave end date was not postponed to the day before the actual return, so company working days in between were not counted (e.g. Wed PM-Fri employee, leave 23/12 -> 31/12 with 01/01 being a Friday: 6 days counted instead of 9). Co-Authored-By: Claude Opus 5.5 --- l10n_fr_hr_holidays/models/hr_leave.py | 48 +++++++++++++- .../tests/test_french_leaves.py | 64 +++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/l10n_fr_hr_holidays/models/hr_leave.py b/l10n_fr_hr_holidays/models/hr_leave.py index 442dfa5..b560f1a 100644 --- a/l10n_fr_hr_holidays/models/hr_leave.py +++ b/l10n_fr_hr_holidays/models/hr_leave.py @@ -1,7 +1,10 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from datetime import datetime, time + from dateutil.relativedelta import relativedelta +from pytz import timezone, UTC from odoo import fields, models, _, api from odoo.exceptions import UserError @@ -74,6 +77,44 @@ class HrLeave(models.Model): self.resource_calendar_id != self.employee_company_id.resource_calendar_id and \ self.holiday_status_id == self.employee_company_id._get_fr_reference_leave_type() + def _l10n_fr_is_public_holiday(self, date): + """Return True if `date` is fully covered by a global leave (public + holiday) registered on the employee calendar, the company calendar or + on no calendar at all.""" + self.ensure_one() + calendar = self.resource_calendar_id + company_calendar = self.employee_company_id.resource_calendar_id + tz = timezone(calendar.tz or company_calendar.tz or 'UTC') + day_start = tz.localize(datetime.combine(date, time.min)).astimezone(UTC).replace(tzinfo=None) + day_end = tz.localize(datetime.combine(date, time.max)).astimezone(UTC).replace(tzinfo=None) + calendar_ids = [False] + (calendar | company_calendar).ids + holidays = self.env['resource.calendar.leaves'].sudo().search([ + ('resource_id', '=', False), + ('time_type', '=', 'leave'), + ('calendar_id', 'in', calendar_ids), + ('date_from', '<=', day_end), + ('date_to', '>=', day_start), + ]) + if not holidays: + return False + # The holiday must cover all the employee attendances of that day + attendance_intervals = calendar._attendance_intervals_batch( + tz.localize(datetime.combine(date, time.min)), + tz.localize(datetime.combine(date, time.max)), + )[False] + for start, stop, _meta in attendance_intervals: + start_utc = start.astimezone(UTC).replace(tzinfo=None) + stop_utc = stop.astimezone(UTC).replace(tzinfo=None) + if not any(h.date_from <= start_utc and h.date_to >= stop_utc for h in holidays): + return False + return True + + def _l10n_fr_employee_works_on_date(self, date): + """Return True if the employee actually works on `date`: the day is a + working day in their calendar and is not a public holiday.""" + self.ensure_one() + return self.resource_calendar_id._works_on_date(date) and not self._l10n_fr_is_public_holiday(date) + def _get_fr_date_from_to(self, date_from, date_to): self.ensure_one() # What we need to compute is how much we will need to push date_to in order to account for the lost days @@ -103,7 +144,7 @@ class HrLeave(models.Model): if attendances_am and not attendances_pm: # If the employee does not work in the afternoon, postpone date_to to the next working day next_date = date_from + relativedelta(days=1) - while not self.resource_calendar_id._works_on_date(next_date): + while not self._l10n_fr_employee_works_on_date(next_date): next_date += relativedelta(days=1) return (date_from, next_date) elif attendances_am and attendances_pm: @@ -142,7 +183,10 @@ class HrLeave(models.Model): # account. while not self.resource_calendar_id._works_on_date(date_start): date_start += relativedelta(days=1) - while not self.resource_calendar_id._works_on_date(date_target + relativedelta(days=1)): + # Public holidays are skipped too: if the employee's next scheduled + # working day is a public holiday, they only come back on the following + # actual working day, so the leave must cover the days in between. + while not self._l10n_fr_employee_works_on_date(date_target + relativedelta(days=1)): date_target += relativedelta(days=1) # Undo the last day increment diff --git a/l10n_fr_hr_holidays/tests/test_french_leaves.py b/l10n_fr_hr_holidays/tests/test_french_leaves.py index 8d65cd6..9f56e6c 100644 --- a/l10n_fr_hr_holidays/tests/test_french_leaves.py +++ b/l10n_fr_hr_holidays/tests/test_french_leaves.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from datetime import date + from odoo.tests.common import TransactionCase, tagged @tagged('post_install_l10n', 'post_install', '-at_install', 'french_leaves') @@ -619,3 +621,65 @@ class TestFrenchLeaves(TransactionCase): self.assertEqual(leave.number_of_days, 5.0, 'The number of days should be 5 (14/07 excluded as public holiday).') leave.unlink() + + def test_public_holiday_postpones_return_day(self): + # Company works Mon-Sat. Employee works Wed afternoon, Thu and Fri. + # Public holidays: 25/12/2020 (Fri), 26/12/2020 (Sat), 01/01/2021 (Fri). + # Leave requested from Wed 23/12/2020 to Thu 31/12/2020. + # The next scheduled working day (Fri 01/01) is a public holiday, so the + # employee only comes back on Wed 06/01/2021: the leave must be extended + # to Tue 05/01/2021. + # Company working days excluding public holidays: + # 23, 24, 28, 29, 30, 31/12 and 02, 04, 05/01 = 9 days + company_calendar = self.env['resource.calendar'].create({ + 'name': 'Company Calendar 6d/week', + 'company_id': self.company.id, + 'tz': 'Europe/Paris', + 'attendance_ids': [ + (0, 0, {'name': 'Morning %s' % d, 'dayofweek': str(d), 'hour_from': 9, 'hour_to': 12.5, 'day_period': 'morning'}) + for d in range(6) + ] + [ + (0, 0, {'name': 'Afternoon %s' % d, 'dayofweek': str(d), 'hour_from': 13.5, 'hour_to': 17, 'day_period': 'afternoon'}) + for d in range(6) + ], + }) + employee_calendar = self.env['resource.calendar'].create({ + 'name': 'Employee Calendar Wed PM-Fri', + 'company_id': self.company.id, + 'tz': 'Europe/Paris', + 'attendance_ids': [ + (0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13.5, 'hour_to': 17, 'day_period': 'afternoon'}), + (0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 9, 'hour_to': 12.5, 'day_period': 'morning'}), + (0, 0, {'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13.5, 'hour_to': 17, 'day_period': 'afternoon'}), + (0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 9, 'hour_to': 12.5, 'day_period': 'morning'}), + (0, 0, {'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13.5, 'hour_to': 17, 'day_period': 'afternoon'}), + ], + }) + self.company.resource_calendar_id = company_calendar + self._set_employee_calendar(employee_calendar) + self.employee.tz = 'Europe/Paris' + + for name, day in [('Noel', '2020-12-25'), ('Saint Etienne', '2020-12-26'), ('Jour de l an', '2021-01-01')]: + self.env['resource.calendar.leaves'].create({ + 'name': name, + 'calendar_id': False, + 'company_id': self.company.id, + 'date_from': '%s 06:00:00' % day, + 'date_to': '%s 18:00:00' % day, + }) + + leave = self.env['hr.leave'].create({ + 'name': 'Test holiday before return', + 'holiday_status_id': self.time_off_type.id, + 'employee_id': self.employee.id, + 'request_date_from': '2020-12-23', + 'request_date_to': '2020-12-31', + 'employee_company_id': self.company.id, + 'resource_calendar_id': self.employee.resource_calendar_id.id, + }) + leave._compute_date_from_to() + self.assertEqual(leave.date_to.date(), date(2021, 1, 5), + 'The leave should be extended until the day before the return (05/01/2021).') + self.assertEqual(leave.number_of_days, 9.0, + 'The number of days should be 9.') + leave.unlink()