# -*- 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 odoo import models class ResourceCalendar(models.Model): _inherit = 'resource.calendar' def _works_on_date(self, date): self.ensure_one() 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() from pytz import timezone 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}