Files
hr-tools/l10n_fr_hr_holidays/models/resource_calendar.py
Laetitia Da Costa ef3c9143dd [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>
2026-11-04 10:46:57 +00:00

48 lines
1.8 KiB
Python

# -*- coding:utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from collections import defaultdict
from datetime import datetime, time
from pytz import timezone
from odoo import models
class ResourceCalendar(models.Model):
_inherit = 'resource.calendar'
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()
if working_days is None:
working_days = self._get_working_hours()
dayofweek = str(date.weekday())
if self.two_weeks_calendar:
weektype = str(self.env['resource.calendar.attendance'].get_week_type(date))
return working_days[weektype][dayofweek]
return working_days[False][dayofweek]
def _get_working_hours(self):
self.ensure_one()
working_days = defaultdict(lambda: defaultdict(lambda: False))
for attendance in self.attendance_ids:
working_days[attendance.week_type][attendance.dayofweek] = True
return working_days
def _get_working_dates(self, from_date, to_date, domain=None):
"""Return a set of dates where the company actually works between
from_date and to_date (inclusive), excluding public holidays
and other global leaves registered on the calendar.
"""
self.ensure_one()
tz = timezone(self.tz or 'UTC')
from_dt = tz.localize(datetime.combine(from_date, time.min))
to_dt = tz.localize(datetime.combine(to_date, time(23, 59, 59)))
intervals = self._work_intervals_batch(from_dt, to_dt, domain=domain)[False]
return {start.astimezone(tz).date() for start, stop, meta in intervals}