diff --git a/resource_calendar_flexible_exclude_weekend/README.rst b/resource_calendar_flexible_exclude_weekend/README.rst new file mode 100644 index 000000000..44ebc544b --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/README.rst @@ -0,0 +1,76 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +================================================ +Resource Calendar Flexible Hours Exclude Weekend +================================================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:61e58a28c612d99b70b031c84cd27a2ac0274bf9281c8257e64871b66c31d9c0 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fhr--holidays-lightgray.png?logo=github + :target: https://github.com/OCA/hr-holidays/tree/18.0/resource_calendar_flexible_exclude_weekend + :alt: OCA/hr-holidays +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/hr-holidays-18-0/hr-holidays-18-0-resource_calendar_flexible_exclude_weekend + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/hr-holidays&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to compute the resource calendar as flexible, without +taking the week-end days on the computation. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Camptocamp SA + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/hr-holidays `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/resource_calendar_flexible_exclude_weekend/__init__.py b/resource_calendar_flexible_exclude_weekend/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/resource_calendar_flexible_exclude_weekend/__manifest__.py b/resource_calendar_flexible_exclude_weekend/__manifest__.py new file mode 100644 index 000000000..12d922093 --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2025 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Resource Calendar Flexible Hours Exclude Weekend", + "version": "19.0.1.0.0", + "category": "Tools", + "website": "https://github.com/OCA/hr-holidays", + "author": "Camptocamp SA, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["resource", "hr_work_entry"], + "data": [ + "views/resource_calendar_view.xml", + ], + "installable": True, +} diff --git a/resource_calendar_flexible_exclude_weekend/i18n/it.po b/resource_calendar_flexible_exclude_weekend/i18n/it.po new file mode 100644 index 000000000..87f0eb9d5 --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/i18n/it.po @@ -0,0 +1,27 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * resource_calendar_flexible_exclude_weekend +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2026-03-16 10:45+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.15.2\n" + +#. module: resource_calendar_flexible_exclude_weekend +#: model:ir.model.fields,field_description:resource_calendar_flexible_exclude_weekend.field_resource_calendar__exclude_weekends +msgid "Exclude Weekends" +msgstr "Escludi fine settimana" + +#. module: resource_calendar_flexible_exclude_weekend +#: model:ir.model,name:resource_calendar_flexible_exclude_weekend.model_resource_calendar +msgid "Resource Working Time" +msgstr "Orario lavoro risorsa" diff --git a/resource_calendar_flexible_exclude_weekend/i18n/resource_calendar_flexible_exclude_weekend.pot b/resource_calendar_flexible_exclude_weekend/i18n/resource_calendar_flexible_exclude_weekend.pot new file mode 100644 index 000000000..2b163747f --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/i18n/resource_calendar_flexible_exclude_weekend.pot @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * resource_calendar_flexible_exclude_weekend +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: resource_calendar_flexible_exclude_weekend +#: model:ir.model.fields,field_description:resource_calendar_flexible_exclude_weekend.field_resource_calendar__exclude_weekends +msgid "Exclude Weekends" +msgstr "" + +#. module: resource_calendar_flexible_exclude_weekend +#: model:ir.model,name:resource_calendar_flexible_exclude_weekend.model_resource_calendar +msgid "Resource Working Time" +msgstr "" diff --git a/resource_calendar_flexible_exclude_weekend/i18n/sl.po b/resource_calendar_flexible_exclude_weekend/i18n/sl.po new file mode 100644 index 000000000..ae9ca0875 --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/i18n/sl.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * resource_calendar_flexible_exclude_weekend +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2026-06-16 19:46+0000\n" +"Last-Translator: Matjaz Mozetic \n" +"Language-Team: none\n" +"Language: sl\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=4; plural=n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || " +"n%100==4 ? 2 : 3;\n" +"X-Generator: Weblate 5.15.2\n" + +#. module: resource_calendar_flexible_exclude_weekend +#: model:ir.model.fields,field_description:resource_calendar_flexible_exclude_weekend.field_resource_calendar__exclude_weekends +msgid "Exclude Weekends" +msgstr "Izključi vikende" + +#. module: resource_calendar_flexible_exclude_weekend +#: model:ir.model,name:resource_calendar_flexible_exclude_weekend.model_resource_calendar +msgid "Resource Working Time" +msgstr "Delovni čas zaposlenega" diff --git a/resource_calendar_flexible_exclude_weekend/models/__init__.py b/resource_calendar_flexible_exclude_weekend/models/__init__.py new file mode 100644 index 000000000..81793ddba --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/models/__init__.py @@ -0,0 +1 @@ +from . import resource_calendar diff --git a/resource_calendar_flexible_exclude_weekend/models/resource_calendar.py b/resource_calendar_flexible_exclude_weekend/models/resource_calendar.py new file mode 100644 index 000000000..27376dd0c --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/models/resource_calendar.py @@ -0,0 +1,161 @@ +# Copyright 2025 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import logging +from collections import defaultdict +from datetime import timedelta + +from dateutil.rrule import DAILY, rrule +from pytz import timezone + +from odoo import fields, models +from odoo.fields import Domain +from odoo.tools.intervals import Intervals + +_logger = logging.getLogger(__name__) + + +class ResourceCalendar(models.Model): + _inherit = "resource.calendar" + + exclude_weekends = fields.Boolean() + + # Override to return weekends as special days if exclude_weekends is set + def _attendance_intervals_batch( # noqa: C901 + self, start_dt, end_dt, resources=None, domain=None, tz=None, lunch=False + ): + """ + Override to adjust start_dt if it falls on a weekend. + - Convert start_dt to the relevant timezone + - If Saturday or Sunday → move to next Monday + - Then call super() with the adjusted start_dt + """ + # Ensure timezone awareness + assert start_dt.tzinfo and end_dt.tzinfo, "Datetimes must be timezone-aware" + self.ensure_one() + + # shortcut: the method can be called with and end_dt before start_dt, + # in this case the expected value is a dictionary with empty intervals. + if end_dt < start_dt: + return super()._attendance_intervals_batch( + start_dt, end_dt, resources, domain, tz, lunch + ) + if not resources: + resources = self.env["resource.resource"] + resources_list = [resources] + else: + resources_list = list(resources) + [self.env["resource.resource"]] + + resources_with_flex_no_weekend = [] + other_resources = [] + for resource in resources_list: + if ( + resource + and resource.calendar_id.flexible_hours + and resource.calendar_id.exclude_weekends + ) or (self.flexible_hours and self.exclude_weekends): + resources_with_flex_no_weekend.append(resource) + else: + other_resources.append(resource) + if other_resources: + res_others = super()._attendance_intervals_batch( + start_dt, + end_dt, + sum(other_resources, self.env["resource.resource"]), + domain, + tz, + lunch, + ) + else: + # containing resources (empty or not) + res_others = {} + if resources_with_flex_no_weekend: + skipping_res = defaultdict(dict) + resources_per_tz = defaultdict(list) + for resource in resources_with_flex_no_weekend: + resources_per_tz[tz or timezone((resource or self).tz)].append(resource) + for tz, tz_resources in resources_per_tz.items(): + skipping_start_dt = start_dt.astimezone(tz) + # 0=Monday, 6=Sunday + weekday = skipping_start_dt.weekday() + if weekday in (5, 6): # Saturday or Sunday + # Move to next Monday, midnight in the considered TZ + days_to_monday = 7 - weekday + skipping_start_dt += timedelta(days=days_to_monday, hours=1) + # reconvert to get Daylight saving time applied if needed + skipping_start_dt = skipping_start_dt.astimezone(tz) + skipping_start_dt -= timedelta( + hours=skipping_start_dt.hour, + minutes=skipping_start_dt.minute, + seconds=skipping_start_dt.second, + ) + # check if we moved past the end date, in which case we want to move + # it to ensure we enter the while loop below + if end_dt < skipping_start_dt: + end_dt = skipping_start_dt + + # for resources which should skip weekend we have to iterate by week + # I thought that maybe we can be smarter and only do 1 or 2 calls: + # 1 call if the first day is a monday, and a second if we need to + # check the following week, but this does not work when there are + # holidays in the period + while skipping_start_dt <= end_dt: + # find the end of the current week or the end of the period + skipping_end_dt = ( + skipping_start_dt + + timedelta(days=7 - skipping_start_dt.weekday(), hours=1) + ).astimezone(tz) + skipping_end_dt -= timedelta( + hours=skipping_end_dt.hour, + minutes=skipping_end_dt.minute, + seconds=skipping_end_dt.second, + ) + skipping_end_dt = min(skipping_end_dt, end_dt) + res_skip = super()._attendance_intervals_batch( + skipping_start_dt, + skipping_end_dt, + sum(tz_resources, self.env["resource.resource"]), + domain, + tz, + lunch, + ) + for resource_id, work_intervals in res_skip.items(): + new_intervals = skipping_res[resource_id] + for start, end, attendance in work_intervals: + if start.weekday() not in (5, 6): + new_intervals[(start, end)] = (start, end, attendance) + if skipping_end_dt == end_dt: + break + else: + # go to next monday + skipping_start_dt = skipping_end_dt + # merge both result set + for resource_id, intervals in skipping_res.items(): + res_others[resource_id] = Intervals( + intervals.values(), keep_distinct=True + ) + return res_others + + def _get_unusual_days(self, start_dt, end_dt, company_id=False): + if self.flexible_hours and self.exclude_weekends: + # when we exclude weekend, use the same implementation as + # for non flexible hours + utc = timezone("UTC") + if not start_dt.tzinfo: + start_dt = start_dt.replace(tzinfo=utc) + if not end_dt.tzinfo: + end_dt = end_dt.replace(tzinfo=utc) + domain = Domain(True) + if company_id: + domain = Domain("company_id", "in", (company_id.id, False)) + works = { + d[0].date() + for d in self._work_intervals_batch(start_dt, end_dt, domain=domain)[ + False + ] + } + return { + fields.Date.to_string(day.date()): (day.date() not in works) + for day in rrule(DAILY, start_dt, until=end_dt) + } + else: + return super()._get_unusual_days(start_dt, end_dt, company_id) diff --git a/resource_calendar_flexible_exclude_weekend/pyproject.toml b/resource_calendar_flexible_exclude_weekend/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/resource_calendar_flexible_exclude_weekend/readme/DESCRIPTION.md b/resource_calendar_flexible_exclude_weekend/readme/DESCRIPTION.md new file mode 100644 index 000000000..f6b428644 --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module allows to compute the resource calendar as flexible, without taking the week-end days on the computation. diff --git a/resource_calendar_flexible_exclude_weekend/static/description/icon.png b/resource_calendar_flexible_exclude_weekend/static/description/icon.png new file mode 100644 index 000000000..1dcc49c24 Binary files /dev/null and b/resource_calendar_flexible_exclude_weekend/static/description/icon.png differ diff --git a/resource_calendar_flexible_exclude_weekend/static/description/index.html b/resource_calendar_flexible_exclude_weekend/static/description/index.html new file mode 100644 index 000000000..962aed6f7 --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/static/description/index.html @@ -0,0 +1,423 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Resource Calendar Flexible Hours Exclude Weekend

+ +

Beta License: AGPL-3 OCA/hr-holidays Translate me on Weblate Try me on Runboat

+

This module allows to compute the resource calendar as flexible, without +taking the week-end days on the computation.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp SA
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/hr-holidays project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/resource_calendar_flexible_exclude_weekend/tests/__init__.py b/resource_calendar_flexible_exclude_weekend/tests/__init__.py new file mode 100644 index 000000000..452cd2469 --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/tests/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import test_resource_calendar diff --git a/resource_calendar_flexible_exclude_weekend/tests/test_resource_calendar.py b/resource_calendar_flexible_exclude_weekend/tests/test_resource_calendar.py new file mode 100644 index 000000000..ea4112bfc --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/tests/test_resource_calendar.py @@ -0,0 +1,304 @@ +from datetime import datetime + +import pytz + +from odoo.tests.common import TransactionCase + + +class TestResourceCalendar(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.calendar_flex_with_weekend = cls.env["resource.calendar"].create( + { + "name": "Flexible Calendar (std implementation)", + "hours_per_day": 8.0, + "hours_per_week": 40.0, + "flexible_hours": True, + "exclude_weekends": False, + "tz": "UTC", + } + ) + cls.calendar_flex_without_weekend = cls.env["resource.calendar"].create( + { + "name": "Flexible Calendar (exclude weekends)", + "hours_per_day": 8.0, + "hours_per_week": 40.0, + "flexible_hours": True, + "exclude_weekends": True, + "tz": "UTC", + } + ) + cls.UTC = pytz.timezone("UTC") + cls.tz_FR = pytz.timezone("Europe/Paris") + + def _check(self, calendar, start_dt, end_dt, expected_duration, message): + result_per_resource_id = calendar._attendance_intervals_batch(start_dt, end_dt) + + actual_duration = 0 + for _res_id, intervals in result_per_resource_id.items(): + for start, end, _ in intervals: + actual_duration += (end - start).seconds + self.assertEqual( + actual_duration / 3600, + expected_duration, + message, + ) + + def test_flexible_calendar_without_weekend_starting_sat(self): + calendar = self.calendar_flex_without_weekend + # start on saturday + start_dt = datetime(2025, 11, 1, 0, 0, 0).astimezone(self.UTC) + # end on friday midnight + end_dt = datetime(2025, 11, 8, 0, 0, 0).astimezone(self.UTC) + self._check( + calendar, + start_dt, + end_dt, + calendar.full_time_required_hours, + "for 7d starting on saturday: you get a full week duration", + ) + + def test_flexible_calendar_without_weekend_starting_mon(self): + calendar = self.calendar_flex_without_weekend + # start on saturday + start_dt = datetime(2025, 11, 3, 0, 0, 0).astimezone(self.UTC) + # end on friday midnight + end_dt = datetime(2025, 11, 10, 0, 0, 0).astimezone(self.UTC) + self._check( + calendar, + start_dt, + end_dt, + calendar.full_time_required_hours, + "for 7d starting on monday: you get a full week duration", + ) + + def test_flexible_calendar_with_weekend_interval_duration(self): + calendar = self.calendar_flex_with_weekend + start_dt = datetime(2025, 11, 1, 0, 0, 0).astimezone(self.UTC) + end_dt = datetime(2025, 11, 8, 0, 0, 0).astimezone(self.UTC) + self._check( + calendar, + start_dt, + end_dt, + 40, + "std behavior: for 7 days, you get full week duration", + ) + + def test_flexible_calendar_with_weekdend_friday_to_friday(self): + calendar = self.calendar_flex_with_weekend + # Friday (inc.) to Friday (excl.) + start_dt = datetime(2025, 11, 7, 0, 0, 0).astimezone(self.UTC) + end_dt = datetime(2025, 11, 14, 0, 0, 0).astimezone(self.UTC) + self._check( + calendar, + start_dt, + end_dt, + 40, + "std behavior: for 7 days, you get full week duration", + ) + + def test_flexible_calendar_end_before_start_returns_empty(self): + calendar = self.calendar_flex_without_weekend + start_dt = datetime(2025, 11, 10, 0, 0, 0, tzinfo=self.UTC) + end_dt = datetime(2025, 11, 9, 0, 0, 0, tzinfo=self.UTC) + + result_per_resource_id = calendar._attendance_intervals_batch(start_dt, end_dt) + + self.assertEqual(len(result_per_resource_id), 1) + self.assertEqual(list(result_per_resource_id[False]), []) + + def test_flexible_calendar_without_weekend_friday_sunday(self): + calendar = self.calendar_flex_without_weekend + start_dt = datetime(2025, 10, 31, 0, 0, 0).astimezone(self.UTC) + end_dt = datetime(2025, 11, 3, 0, 0, 0).astimezone(self.UTC) + self._check( + calendar, + start_dt, + end_dt, + 8, + "For a full day, the interval must match full time required hours", + ) + + def test_flexible_calendar_without_weekend_2w_starting_wed(self): + calendar = self.calendar_flex_without_weekend + # start on saturday + start_dt = datetime(2025, 11, 5, 0, 0, 0).astimezone(self.UTC) + # end on friday midnight + end_dt = datetime(2025, 11, 19, 0, 0, 0).astimezone(self.UTC) + self._check( + calendar, + start_dt, + end_dt, + calendar.full_time_required_hours * 2, + "for 2w starting on wed: you get 2 full weeks duration", + ) + + def test_flexible_calendar_without_weekend_10d_starting_mon(self): + calendar = self.calendar_flex_without_weekend + # start on saturday + start_dt = datetime(2025, 11, 3, 0, 0, 0).astimezone(self.UTC) + # end on friday midnight + end_dt = datetime(2025, 11, 13, 0, 0, 0).astimezone(self.UTC) + self._check( + calendar, + start_dt, + end_dt, + 8 * 8, + "for 10d starting on Mon: you get 8d", + ) + + def test_flexible_calendar_without_weekend_10d_starting_fri(self): + calendar = self.calendar_flex_without_weekend + # start on saturday + start_dt = datetime(2025, 11, 7, 0, 0, 0).astimezone(self.UTC) + # end on friday midnight + end_dt = datetime(2025, 11, 17, 0, 0, 0).astimezone(self.UTC) + self._check( + calendar, + start_dt, + end_dt, + 6 * 8, + "for 10d starting on Fri: you get 6d", + ) + + def test_flexible_calendar_non_utc_1d(self): + calendar = self.calendar_flex_without_weekend + calendar.tz = self.tz_FR.zone + # start on Mon 00:00 + start_dt = datetime(2025, 11, 2, 23, 0, 0, tzinfo=self.UTC).astimezone( + self.tz_FR + ) + # end on Tue 00:00 + end_dt = datetime(2025, 11, 3, 23, 0, 0, tzinfo=self.UTC).astimezone(self.tz_FR) + self._check( + calendar, + start_dt, + end_dt, + 1 * 8, + "for 1d starting on Mon: you get 1d", + ) + + def test_flexible_calendar_non_utc_10d_starting_fri(self): + calendar = self.calendar_flex_without_weekend + calendar.tz = self.tz_FR.zone + # start on Fri 00:00 + start_dt = datetime(2025, 11, 6, 23, 0, 0, tzinfo=self.UTC).astimezone( + self.tz_FR + ) + # end on Mon 00:00 + end_dt = datetime(2025, 11, 16, 23, 0, 0, tzinfo=self.UTC).astimezone( + self.tz_FR + ) + self._check( + calendar, + start_dt, + end_dt, + 6 * 8, + "for 10d starting on Fri: you get 6d", + ) + + def test_hr_holidays_use(self): + calendar = self.calendar_flex_without_weekend + calendar.tz = self.tz_FR.zone + start_dt = datetime(2026, 3, 19, 0, 0, 0, tzinfo=self.UTC) + # end on Mon 00:00 + end_dt = datetime(2026, 4, 30, 23, 59, 59, tzinfo=self.UTC) + result_per_resource_id = calendar._attendance_intervals_batch(start_dt, end_dt) + for _resource, intervals in result_per_resource_id.items(): + for start, end, meta in intervals: + self.assertEqual( + len(meta), 1, f"more than one attendance for {start}->{end}: {meta}" + ) + + def test_get_unusual_days_excluding_weekends(self): + calendar = self.calendar_flex_without_weekend + start_dt = datetime(2025, 11, 3, 0, 0, 0, tzinfo=self.UTC) + end_dt = datetime(2025, 11, 9, 0, 0, 0, tzinfo=self.UTC) + + unusual_days = calendar._get_unusual_days(start_dt, end_dt) + + self.assertFalse(unusual_days["2025-11-03"]) + self.assertFalse(unusual_days["2025-11-04"]) + self.assertFalse(unusual_days["2025-11-05"]) + self.assertFalse(unusual_days["2025-11-06"]) + self.assertFalse(unusual_days["2025-11-07"]) + self.assertTrue(unusual_days["2025-11-08"]) + self.assertTrue(unusual_days["2025-11-09"]) + + def test_daylight_saving_time_1(self): + calendar = self.calendar_flex_without_weekend + calendar.tz = self.tz_FR.zone + # week end of winter -> summer time + start_dt = datetime( + 2026, + 3, + 27, + 23, + 0, + 0, + ).astimezone(self.tz_FR) + end_dt = datetime( + 2026, + 3, + 29, + 22, + 0, + 0, + ).astimezone(self.tz_FR) + self._check( + calendar, + start_dt, + end_dt, + 0 * 8, + "for 2d during the CET -> CEST week end, you get 0d", + ) + + def test_daylight_saving_time_2(self): + calendar = self.calendar_flex_without_weekend + calendar.tz = self.tz_FR.zone + # week end of winter -> summer time + start_dt = datetime(2026, 3, 26, 23, 0, 0, tzinfo=self.UTC).astimezone( + self.tz_FR + ) + end_dt = datetime(2026, 3, 29, 22, 0, 0, tzinfo=self.UTC).astimezone(self.tz_FR) + self._check( + calendar, + start_dt, + end_dt, + 1 * 8, + "for 3d during the CET -> CEST week end, starting on Fri, you get 1d", + ) + + def test_daylight_saving_time_3(self): + calendar = self.calendar_flex_without_weekend + calendar.tz = self.tz_FR.zone + # week end of winter -> summer time + start_dt = datetime(2026, 3, 22, 23, 0, 0, tzinfo=self.UTC).astimezone( + self.tz_FR + ) + end_dt = datetime(2026, 3, 29, 22, 0, 0, tzinfo=self.UTC).astimezone(self.tz_FR) + self._check( + calendar, + start_dt, + end_dt, + 5 * 8, + "for 1w including the CET -> CEST week end, starting on Mon, you get 5d", + ) + + def test_daylight_saving_time_4(self): + calendar = self.calendar_flex_without_weekend + calendar.tz = self.tz_FR.zone + # week end of winter -> summer time, Fri -> Mon + start_dt = datetime(2026, 3, 26, 23, 0, 0, tzinfo=self.UTC).astimezone( + self.tz_FR + ) + end_dt = datetime(2026, 3, 30, 22, 0, 0, tzinfo=self.UTC).astimezone(self.tz_FR) + self._check( + calendar, + start_dt, + end_dt, + 2 * 8, + "for 4d including the CET -> CEST week end, Fri to Mon, you get 2d", + ) diff --git a/resource_calendar_flexible_exclude_weekend/views/resource_calendar_view.xml b/resource_calendar_flexible_exclude_weekend/views/resource_calendar_view.xml new file mode 100644 index 000000000..97640837e --- /dev/null +++ b/resource_calendar_flexible_exclude_weekend/views/resource_calendar_view.xml @@ -0,0 +1,13 @@ + + + + resource.calendar.form.inherit + resource.calendar + + + + + + + +