[REF]l10n_fr_hr_holidays:clean up leave days computation

- remove unused _round_to_nearest_half
- evaluate _l10n_fr_leave_applies once in _get_number_of_days_batch and
  count working dates without a day-by-day loop
- compute the calendar working hours once per date lookup loop
- move pytz import to the top of resource_calendar.py

No functional change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-04 11:03:52 +01:00
committed by laetitiadacosta
parent 646ab82880
commit ef3c9143dd
2 changed files with 26 additions and 23 deletions

View File

@@ -109,11 +109,12 @@ class HrLeave(models.Model):
return False return False
return True return True
def _l10n_fr_employee_works_on_date(self, date): def _l10n_fr_employee_works_on_date(self, date, working_days=None):
"""Return True if the employee actually works on `date`: the day is a """Return True if the employee actually works on `date`: the day is a
working day in their calendar and is not a public holiday.""" working day in their calendar and is not a public holiday."""
self.ensure_one() self.ensure_one()
return self.resource_calendar_id._works_on_date(date) and not self._l10n_fr_is_public_holiday(date) return (self.resource_calendar_id._works_on_date(date, working_days)
and not self._l10n_fr_is_public_holiday(date))
def _get_fr_date_from_to(self, date_from, date_to): def _get_fr_date_from_to(self, date_from, date_to):
self.ensure_one() self.ensure_one()
@@ -124,6 +125,8 @@ class HrLeave(models.Model):
# which the employee works zero hours. # which the employee works zero hours.
if not (self.resource_calendar_id.attendance_ids): if not (self.resource_calendar_id.attendance_ids):
raise UserError(_("An employee cannot take a paid time off in a period they work no hours.")) raise UserError(_("An employee cannot take a paid time off in a period they work no hours."))
calendar = self.resource_calendar_id
working_days = calendar._get_working_hours()
if self.request_unit_half and self.request_date_from_period == 'am': if self.request_unit_half and self.request_date_from_period == 'am':
# In normal workflows request_unit_half implies that date_from and date_to are the same # In normal workflows request_unit_half implies that date_from and date_to are the same
@@ -144,7 +147,7 @@ class HrLeave(models.Model):
if attendances_am and not attendances_pm: if attendances_am and not attendances_pm:
# If the employee does not work in the afternoon, postpone date_to to the next working day # If the employee does not work in the afternoon, postpone date_to to the next working day
next_date = date_from + relativedelta(days=1) next_date = date_from + relativedelta(days=1)
while not self._l10n_fr_employee_works_on_date(next_date): while not self._l10n_fr_employee_works_on_date(next_date, working_days):
next_date += relativedelta(days=1) next_date += relativedelta(days=1)
return (date_from, next_date) return (date_from, next_date)
elif attendances_am and attendances_pm: elif attendances_am and attendances_pm:
@@ -157,7 +160,7 @@ class HrLeave(models.Model):
current_date = date_from current_date = date_from
days_count = 0 days_count = 0
while current_date <= date_to: while current_date <= date_to:
if self.resource_calendar_id._works_on_date(current_date): if calendar._works_on_date(current_date, working_days):
days_count += 1 days_count += 1
current_date += relativedelta(days=1) current_date += relativedelta(days=1)
# Adjust date_to so it matches the expected number of days # Adjust date_to so it matches the expected number of days
@@ -167,7 +170,7 @@ class HrLeave(models.Model):
current_date = date_from current_date = date_from
counted = 0 counted = 0
while counted < days_count: while counted < days_count:
if self.resource_calendar_id._works_on_date(current_date): if calendar._works_on_date(current_date, working_days):
counted += 1 counted += 1
if counted == days_count: if counted == days_count:
break break
@@ -181,12 +184,12 @@ class HrLeave(models.Model):
# the employee calendar as otherwise days worked on by the company # the employee calendar as otherwise days worked on by the company
# calendar before the actual start of the leave would be taken into # calendar before the actual start of the leave would be taken into
# account. # account.
while not self.resource_calendar_id._works_on_date(date_start): while not calendar._works_on_date(date_start, working_days):
date_start += relativedelta(days=1) date_start += relativedelta(days=1)
# Public holidays are skipped too: if the employee's next scheduled # Public holidays are skipped too: if the employee's next scheduled
# working day is a public holiday, they only come back on the following # 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. # 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)): while not self._l10n_fr_employee_works_on_date(date_target + relativedelta(days=1), working_days):
date_target += relativedelta(days=1) date_target += relativedelta(days=1)
# Undo the last day increment # Undo the last day increment
@@ -236,8 +239,9 @@ class HrLeave(models.Model):
] ]
calendar = self._get_calendar() calendar = self._get_calendar()
result = employee._get_work_days_data_batch(date_from, date_to, calendar=calendar, domain=domain) result = employee._get_work_days_data_batch(date_from, date_to, calendar=calendar, domain=domain)
l10n_fr_applies = self._l10n_fr_leave_applies()
for employee_id in result: for employee_id in result:
if self._l10n_fr_leave_applies(): if l10n_fr_applies:
company_calendar = calendar company_calendar = calendar
employee_calendar = self.resource_calendar_id employee_calendar = self.resource_calendar_id
first_day = date_from.date() first_day = date_from.date()
@@ -270,24 +274,18 @@ class HrLeave(models.Model):
other_half = employee_calendar.attendance_ids.filtered( other_half = employee_calendar.attendance_ids.filtered(
lambda a: a.dayofweek == dayofweek and a.day_period == other_period lambda a: a.dayofweek == dayofweek and a.day_period == other_period
) )
first_day_count = 0.5 if (other_half and first_day in working_dates) else (1.0 if first_day in working_dates else 0.0) if first_day in working_dates:
days_count = first_day_count days_count = 0.5 if other_half else 1.0
else:
days_count = 0.0
current_date = first_day + relativedelta(days=1) current_date = first_day + relativedelta(days=1)
else: else:
days_count = 0 days_count = 0.0
current_date = first_day current_date = first_day
while current_date <= last_date: days_count += sum(1 for d in working_dates if current_date <= d <= last_date)
if current_date in working_dates:
days_count += 1
current_date += relativedelta(days=1)
result[employee_id]['days'] = float(days_count) result[employee_id]['days'] = float(days_count)
elif self.request_unit_half and result[employee_id]['hours'] > 0: elif self.request_unit_half and result[employee_id]['hours'] > 0:
# Non-French context: a half-day leave always counts as 0.5 day # Non-French context: a half-day leave always counts as 0.5 day
result[employee_id]['days'] = 0.5 result[employee_id]['days'] = 0.5
return result return result
def _round_to_nearest_half(self, x):
"""Round a float to the nearest 0.5."""
return round(x * 2) / 2

View File

@@ -4,16 +4,22 @@
from collections import defaultdict from collections import defaultdict
from datetime import datetime, time from datetime import datetime, time
from pytz import timezone
from odoo import models from odoo import models
class ResourceCalendar(models.Model): class ResourceCalendar(models.Model):
_inherit = 'resource.calendar' _inherit = 'resource.calendar'
def _works_on_date(self, date): def _works_on_date(self, date, working_days=None):
"""Return True if the calendar has attendances on the weekday of `date`.
`working_days` can be given (result of `_get_working_hours`) to avoid
recomputing it when called in a loop."""
self.ensure_one() self.ensure_one()
working_days = self._get_working_hours() if working_days is None:
working_days = self._get_working_hours()
dayofweek = str(date.weekday()) dayofweek = str(date.weekday())
if self.two_weeks_calendar: if self.two_weeks_calendar:
weektype = str(self.env['resource.calendar.attendance'].get_week_type(date)) weektype = str(self.env['resource.calendar.attendance'].get_week_type(date))
@@ -34,7 +40,6 @@ class ResourceCalendar(models.Model):
and other global leaves registered on the calendar. and other global leaves registered on the calendar.
""" """
self.ensure_one() self.ensure_one()
from pytz import timezone
tz = timezone(self.tz or 'UTC') tz = timezone(self.tz or 'UTC')
from_dt = tz.localize(datetime.combine(from_date, time.min)) from_dt = tz.localize(datetime.combine(from_date, time.min))
to_dt = tz.localize(datetime.combine(to_date, time(23, 59, 59))) to_dt = tz.localize(datetime.combine(to_date, time(23, 59, 59)))