[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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user