Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][MIG] resource_multi_week_calendar #1389

Open
wants to merge 15 commits into
base: 12.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions resource_multi_week_calendar/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
====================
Multi-week calendars
====================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:71adeb4c733912c857b2051610ef2056cebe834c7ff4857c7f861e8ecd5940ed
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |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/licence-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-lightgray.png?logo=github
:target: https://github.com/OCA/hr/tree/16.0/resource_multi_week_calendar
:alt: OCA/hr
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/hr-16-0/hr-16-0-resource_multi_week_calendar
: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&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

Allow a calendar to alternate between multiple weeks.

An implementation of this functionality exists in Odoo's ``resource`` module
since version 13. In Odoo's implementation, you can only alternate between two
weeks. Furthermore, the implementation is more than a little wonky.

The advantage of this module over the implementation in ``resource`` is that you
can alternate between more than two weeks. The implementation is (hopefully)
better.

The downside of adopting this module is that all modules which interact with the
week-alternating functionality of ``resource`` must be adapted to be compatible
with this module. At the time of writing (2024-07-29), the only Odoo module
which does this is ``hr_holidays``.

**Table of contents**

.. contents::
:local:

Known issues / Roadmap
======================

This module is a template for building on top of. It _will_ need glue modules to
work with various other modules. Most notably, ``hr_holidays`` will not work
without modification.

The existing base Odoo two-week calendar functionality is hidden rather than
disabled. This may or may not be desirable.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/hr/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 <https://github.com/OCA/hr/issues/new?body=module:%20resource_multi_week_calendar%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* Coop IT Easy SC

Contributors
~~~~~~~~~~~~

* `Coop IT Easy SC <https://coopiteasy.be>`_:

* Carmen Bianca BAKKER

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.

.. |maintainer-carmenbianca| image:: https://github.com/carmenbianca.png?size=40px
:target: https://github.com/carmenbianca
:alt: carmenbianca

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-carmenbianca|

This module is part of the `OCA/hr <https://github.com/OCA/hr/tree/16.0/resource_multi_week_calendar>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
5 changes: 5 additions & 0 deletions resource_multi_week_calendar/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from . import models
22 changes: 22 additions & 0 deletions resource_multi_week_calendar/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

{
"name": "Multi-week calendars",
"summary": """
Allow a calendar to alternate between multiple weeks.""",
"version": "12.0.1.0.0",
"category": "Hidden",
"website": "https://github.com/OCA/hr",
"author": "Coop IT Easy SC, Odoo Community Association (OCA)",
"maintainers": ["carmenbianca"],
"license": "AGPL-3",
"application": False,
"depends": [
"resource",
],
"data": [
"views/resource_calendar_views.xml",
],
}
5 changes: 5 additions & 0 deletions resource_multi_week_calendar/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from . import resource_calendar
248 changes: 248 additions & 0 deletions resource_multi_week_calendar/models/resource_calendar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

import math
from datetime import datetime, timedelta

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.addons.resource.models.resource import Intervals


class ResourceCalendar(models.Model):
_inherit = "resource.calendar"

parent_calendar_id = fields.Many2one(
comodel_name="resource.calendar",
domain=[("parent_calendar_id", "=", False)],
ondelete="cascade",
string="Main Working Time",
)
child_calendar_ids = fields.One2many(
comodel_name="resource.calendar",
inverse_name="parent_calendar_id",
string="Alternating Working Times",
copy=True,
)
# These are all your siblings (including yourself) if you are a child, or
# all your children if you are a parent. This is not a sorted set.
multi_week_calendar_ids = fields.One2many(
comodel_name="resource.calendar",
compute="_compute_multi_week_calendar_ids",
recursive=True,
)
is_multi_week = fields.Boolean(compute="_compute_is_multi_week", store=True)

# Making week_number a computed derivative of week_sequence has the
# advantage of being able to drag calendars around in a table, and not
# having to manually fiddle with every week number (nor make sure that no
# weeks are skipped).
#
# However, week sequences MUST be unique. Unfortunately, creating a
# constraint on (parent_calendar_id, week_sequence) does not work. The
# constraint method is called before all children/siblings are saved,
# meaning that they can conflict with each other in this interim stage.
#
# If this value is not unique, the order is preserved between the identical
# elements. The elements of child_calendar_ids are always sorted by _order,
# which is id by default. The value may not be unique when new calendars are
# added.
week_sequence = fields.Integer(default=0)
week_number = fields.Integer(
compute="_compute_week_number",
store=True,
recursive=True,
)
current_week_number = fields.Integer(
compute="_compute_current_week",
recursive=True,
)
current_multi_week_calendar_id = fields.Many2one(
comodel_name="resource.calendar",
compute="_compute_current_week",
recursive=True,
)

multi_week_epoch_date = fields.Date(
string="Date of First Week",
help="""When using alternating weeks, the week which contains the
specified date becomes the first week, and all subsequent weeks
alternate in order.""",
required=True,
default="1970-01-01",
)

def copy(self, default=None):
self.ensure_one()
if default is None:
default = {}
sequences = sorted(self.multi_week_calendar_ids.mapped("week_sequence"))
if sequences:
# Assign highest value sequence.
default["week_sequence"] = sequences[-1] + 1
return super().copy(default=default)

@api.depends(
"child_calendar_ids",
"parent_calendar_id",
"parent_calendar_id.child_calendar_ids",
)
def _compute_multi_week_calendar_ids(self):
for calendar in self:
parent = calendar.parent_calendar_id or calendar
calendar.multi_week_calendar_ids = parent.child_calendar_ids

@api.depends(
"child_calendar_ids",
"parent_calendar_id",
)
def _compute_is_multi_week(self):
for calendar in self:
calendar.is_multi_week = bool(
calendar.child_calendar_ids or calendar.parent_calendar_id
)

@api.depends(
"week_sequence",
"parent_calendar_id",
"parent_calendar_id.child_calendar_ids",
"parent_calendar_id.child_calendar_ids.week_sequence",
)
def _compute_week_number(self):
for calendar in self:
parent = calendar.parent_calendar_id
if parent:
for week_number, sibling in enumerate(
parent.child_calendar_ids.sorted(lambda item: item.week_sequence),
start=1,
):
if calendar == sibling:
calendar.week_number = week_number
break
else:
# Parent calendars have no week number.
calendar.week_number = 0

def _get_first_day_of_epoch_week(self):
self.ensure_one()
epoch_date = self.get_multi_week_epoch_date()
return epoch_date - timedelta(days=epoch_date.weekday())

def _get_week_number(self, day=None):
self.ensure_one()
if not self.is_multi_week:
return 0
if day is None:
day = fields.Date.today()
carmenbianca marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(day, datetime):
day = day.date()
calendar_count = len(self.multi_week_calendar_ids)
weeks_since_epoch = math.floor(
(day - self._get_first_day_of_epoch_week()).days / 7
)
return (weeks_since_epoch % calendar_count) + 1

def _get_multi_week_calendar(self, day=None):
self.ensure_one()
if not self.is_multi_week:
return self
week_number = self._get_week_number(day=day)
# Should return a 1-item recordset. If it does not, we've hit a bug.
return self.multi_week_calendar_ids.filtered(
lambda item: item.week_number == week_number
)
carmenbianca marked this conversation as resolved.
Show resolved Hide resolved

@api.depends(
"multi_week_epoch_date",
"week_number",
"multi_week_calendar_ids",
)
def _compute_current_week(self):
for calendar in self:
current_calendar = calendar._get_multi_week_calendar()
calendar.current_multi_week_calendar_id = current_calendar
calendar.current_week_number = current_calendar.week_number

@api.constrains("parent_calendar_id", "child_calendar_ids")
def _check_child_is_not_parent(self):
err_str = _(
"Working Time '%(name)s' may not be the Main Working Time of"
" another Working Time ('%(child)s') while it has a Main Working"
" Time itself ('%(parent)s')"
)
for calendar in self:
if calendar.parent_calendar_id and calendar.child_calendar_ids:
raise ValidationError(
err_str
% {
"name": calendar.name,
"child": calendar.child_calendar_ids[0].name,
"parent": calendar.parent_calendar_id.name,
}
)
# This constraint isn't triggered on calendars which have children
# added to them. Therefore, we also check whether our parent already
# has a parent.
if (
calendar.parent_calendar_id
and calendar.parent_calendar_id.parent_calendar_id
):
raise ValidationError(
err_str
% {
"name": calendar.parent_calendar_id.name,
"child": calendar.name,
"parent": calendar.parent_calendar_id.parent_calendar_id.name,
}
)

def get_multi_week_epoch_date(self):
self.ensure_one()
if self.parent_calendar_id:
return self.parent_calendar_id.multi_week_epoch_date
return self.multi_week_epoch_date

@api.model
def _split_into_weeks(self, start_dt, end_dt):
# TODO: This method splits weeks on the timezone of start_dt. Maybe it
# should split weeks on the timezone of the calendar. It is not
# immediately clear to me how to implement that.
carmenbianca marked this conversation as resolved.
Show resolved Hide resolved
current_start = start_dt
while current_start < end_dt:
# Calculate the end of the week (Monday 00:00:00, the threshold
# of Sunday-to-Monday.)
days_until_monday = 7 - current_start.weekday()
week_end = current_start + timedelta(days=days_until_monday)
carmenbianca marked this conversation as resolved.
Show resolved Hide resolved
week_end = week_end.replace(hour=0, minute=0, second=0, microsecond=0)

current_end = min(week_end, end_dt)
yield (current_start, current_end)

# Move to the next week (start of next Monday)
current_start = current_end

def _attendance_intervals(self, start_dt, end_dt, resource=None):
self.ensure_one()
if not self.is_multi_week:
return super()._attendance_intervals(start_dt, end_dt, resource=resource)

calendars_by_week = {
calendar.week_number: calendar for calendar in self.multi_week_calendar_ids
}
result = Intervals()

# Calculate each week separately, choosing the correct calendar for each
# week.
for week_start, week_end in self._split_into_weeks(start_dt, end_dt):
result |= super(
ResourceCalendar,
calendars_by_week[self._get_week_number(week_start)].with_context(
# This context is not used here, but could possibly be
# used by other modules that use this module. I am not
# sure how useful it is.
recursive_multi_week=True
),
)._attendance_intervals(week_start, week_end, resource=resource)

return result
3 changes: 3 additions & 0 deletions resource_multi_week_calendar/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Coop IT Easy SC <https://coopiteasy.be>`_:

* Carmen Bianca BAKKER
Loading
Loading