automatic update repo with commit - 'fix: holydays duration' from project hr_holidays_ru

This commit is contained in:
odoo-robot 2025-01-23 07:51:07 +00:00
parent f6d0020515
commit b89b1fd6f2

View File

@ -1,11 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
from datetime import datetime from datetime import datetime, timedelta, time
from datetime import timedelta
from pytz import timezone, utc from pytz import timezone, utc
from datetime import datetime, time
from collections import defaultdict from collections import defaultdict
from dateutil.relativedelta import relativedelta
import pytz import pytz
from odoo import models, fields from odoo import models, fields
@ -335,56 +332,36 @@ class ResourceCalendar(models.Model):
def get_holidays_duration(self, start_dt, end_dt): def get_holidays_duration(self, start_dt, end_dt):
""" """
Returns timedelta object which is duration of all holidays included into Returns the number of holiday days in the specified datetime range.
specified period. Converts datetime objects to date and calculates overlapping holiday days.
""" """
start_date = start_dt.date()
end_date = end_dt.date()
days = set() days = set()
holidays_ids = self.env["holidays.calendar.leaves"].search(
# Find all holidays that are marked as "is_holiday"
holidays = self.env["holidays.calendar.leaves"].search(
[("type_transfer_day", "=", "is_holiday")] [("type_transfer_day", "=", "is_holiday")]
) )
for holiday in holidays_ids: for holiday in holidays:
# Transformation date to datetime holiday_start = holiday.date_from
date_from = datetime( holiday_end = holiday.date_to
year=holiday.date_from.year,
month=holiday.date_from.month, # Ensure both dates are valid
day=holiday.date_from.day, if not holiday_start or not holiday_end:
hour=0,
minute=0,
second=0,
)
date_to = datetime(
year=holiday.date_to.year,
month=holiday.date_to.month,
day=holiday.date_to.day,
hour=23,
minute=59,
second=59,
)
# Dates in database are without timezones, and time in fact for UTC timezone,
# so it should be added explicitly.
holiday_date_from = utc.localize(date_from) if date_from else None
holiday_date_to = utc.localize(date_to) if date_to else None
# Bring booth dates to the holiday timezone
if holiday_date_from and start_dt.tzinfo != holiday_date_from.tzinfo:
start_dt = start_dt.astimezone(holiday_date_from.tzinfo)
if holiday_date_to and end_dt.tzinfo != holiday_date_to.tzinfo:
end_dt = end_dt.astimezone(holiday_date_to.tzinfo)
# Check periods intersection
if (
holiday_date_from
and end_dt < holiday_date_from
or holiday_date_to
and start_dt > holiday_date_to
):
continue continue
# Handle this holiday
start = start_dt.date() # Check if the holiday range overlaps with the input range
if holiday_date_from: if holiday_end < start_date or holiday_start > end_date:
start = max(start, holiday_date_from.date()) continue
until = end_dt.date()
if holiday_date_to: overlap_start = max(start_date, holiday_start)
until = min(until, holiday_date_to.date()) overlap_end = min(end_date, holiday_end)
for i in range((until - start + timedelta(1)).days):
days.add(start + timedelta(i)) for i in range((overlap_end - overlap_start).days + 1):
days.add(overlap_start + timedelta(days=i))
return timedelta(len(days)) return timedelta(len(days))