From c799b5c122f3dea22b143f2e996fb4b1bfb97564 Mon Sep 17 00:00:00 2001 From: Cyril VINH-TUNG Date: Thu, 4 Jun 2026 21:25:47 -1000 Subject: [PATCH] [ADD] hr_shift_pattern: allow more flexibility in planning by adding 'patterns of templates' --- hr_shift_pattern/README.rst | 102 ++++ hr_shift_pattern/__init__.py | 3 + hr_shift_pattern/__manifest__.py | 21 + hr_shift_pattern/demo/demo.xml | 79 +++ hr_shift_pattern/models/__init__.py | 7 + hr_shift_pattern/models/hr_employee.py | 70 +++ hr_shift_pattern/models/shift_pattern.py | 27 ++ hr_shift_pattern/models/shift_pattern_week.py | 39 ++ hr_shift_pattern/models/shift_planning.py | 49 ++ .../models/shift_planning_shift.py | 96 ++++ hr_shift_pattern/pyproject.toml | 3 + hr_shift_pattern/readme/CONTRIBUTORS.md | 1 + hr_shift_pattern/readme/DESCRIPTION.md | 4 + hr_shift_pattern/readme/USAGE.md | 17 + hr_shift_pattern/security/ir.model.access.csv | 9 + .../static/description/index.html | 449 ++++++++++++++++++ hr_shift_pattern/tests/__init__.py | 3 + hr_shift_pattern/tests/test_shift_pattern.py | 153 ++++++ hr_shift_pattern/views/hr_employee_views.xml | 20 + hr_shift_pattern/views/menus.xml | 50 ++ .../views/shift_pattern_views.xml | 43 ++ .../views/shift_pattern_week_views.xml | 43 ++ .../views/shift_planning_views.xml | 274 +++++++++++ 23 files changed, 1562 insertions(+) create mode 100644 hr_shift_pattern/README.rst create mode 100644 hr_shift_pattern/__init__.py create mode 100644 hr_shift_pattern/__manifest__.py create mode 100644 hr_shift_pattern/demo/demo.xml create mode 100644 hr_shift_pattern/models/__init__.py create mode 100644 hr_shift_pattern/models/hr_employee.py create mode 100644 hr_shift_pattern/models/shift_pattern.py create mode 100644 hr_shift_pattern/models/shift_pattern_week.py create mode 100644 hr_shift_pattern/models/shift_planning.py create mode 100644 hr_shift_pattern/models/shift_planning_shift.py create mode 100644 hr_shift_pattern/pyproject.toml create mode 100644 hr_shift_pattern/readme/CONTRIBUTORS.md create mode 100644 hr_shift_pattern/readme/DESCRIPTION.md create mode 100644 hr_shift_pattern/readme/USAGE.md create mode 100644 hr_shift_pattern/security/ir.model.access.csv create mode 100644 hr_shift_pattern/static/description/index.html create mode 100644 hr_shift_pattern/tests/__init__.py create mode 100644 hr_shift_pattern/tests/test_shift_pattern.py create mode 100644 hr_shift_pattern/views/hr_employee_views.xml create mode 100644 hr_shift_pattern/views/menus.xml create mode 100644 hr_shift_pattern/views/shift_pattern_views.xml create mode 100644 hr_shift_pattern/views/shift_pattern_week_views.xml create mode 100644 hr_shift_pattern/views/shift_planning_views.xml diff --git a/hr_shift_pattern/README.rst b/hr_shift_pattern/README.rst new file mode 100644 index 0000000..e1a0bd6 --- /dev/null +++ b/hr_shift_pattern/README.rst @@ -0,0 +1,102 @@ +========================= +Employees Shifts Patterns +========================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:00a5304a942cfc0b9b0d0154486df5b8f92887df53fb5d8ff43cea23d5b3efc8 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fshift--planning-lightgray.png?logo=github + :target: https://github.com/OCA/shift-planning/tree/18.0/hr_shift_pattern + :alt: OCA/shift-planning +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/shift-planning-18-0/shift-planning-18-0-hr_shift_pattern + :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/shift-planning&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds reusable week templates and multi-week rotation cycles +on top of *hr_shift*. A week template (a *semaine type*) maps each +worked day to a shift template. A cycle is an ordered list of week +templates. On a planning, each employee is placed on a week template, +and their days are filled from it. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Configuration: + +1. In *Shifts > Configuration > Week templates*, create a week template + and add one line per worked day (the day and the shift template that + applies). +2. In *Shifts > Configuration > Cycles*, create a cycle and order its + week templates. +3. On each employee, set the cycle and the week they start it on. + +On a planning: + +1. Generate the shifts: every employee is filled from the cycle step + matching that planning week. +2. The *Shifts* button shows the shifts grouped by week template; drag + an employee to another column for a one-off change of that week. + +Changing an employee's cycle or start week re-fills their future +plannings; the current and past weeks are left untouched. + +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 +------- + +* INVITU + +Contributors +------------ + +- Cyril VINH-TUNG + +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/shift-planning `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/hr_shift_pattern/__init__.py b/hr_shift_pattern/__init__.py new file mode 100644 index 0000000..1e8ff6f --- /dev/null +++ b/hr_shift_pattern/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import models diff --git a/hr_shift_pattern/__manifest__.py b/hr_shift_pattern/__manifest__.py new file mode 100644 index 0000000..d9742f9 --- /dev/null +++ b/hr_shift_pattern/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +{ + "name": "Employees Shifts Patterns", + "summary": "Build reusable week templates and multi-week rotation cycles", + "version": "18.0.1.0.0", + "author": "INVITU, Odoo Community Association (OCA)", + "license": "AGPL-3", + "website": "https://github.com/OCA/shift-planning", + "category": "Human Resources/Shifts", + "depends": ["hr_shift"], + "demo": ["demo/demo.xml"], + "data": [ + "security/ir.model.access.csv", + "views/shift_pattern_week_views.xml", + "views/shift_pattern_views.xml", + "views/hr_employee_views.xml", + "views/shift_planning_views.xml", + "views/menus.xml", + ], +} diff --git a/hr_shift_pattern/demo/demo.xml b/hr_shift_pattern/demo/demo.xml new file mode 100644 index 0000000..4c9b8b9 --- /dev/null +++ b/hr_shift_pattern/demo/demo.xml @@ -0,0 +1,79 @@ + + + + Mornings week + 10 + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + Afternoons week + 4 + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + Mornings/Afternoons rotation + 7 + + + + 10 + + + + + 20 + + + + + 2025-01-06 + + diff --git a/hr_shift_pattern/models/__init__.py b/hr_shift_pattern/models/__init__.py new file mode 100644 index 0000000..3d046f1 --- /dev/null +++ b/hr_shift_pattern/models/__init__.py @@ -0,0 +1,7 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import shift_pattern_week +from . import shift_pattern +from . import hr_employee +from . import shift_planning_shift +from . import shift_planning diff --git a/hr_shift_pattern/models/hr_employee.py b/hr_shift_pattern/models/hr_employee.py new file mode 100644 index 0000000..dd0a177 --- /dev/null +++ b/hr_shift_pattern/models/hr_employee.py @@ -0,0 +1,70 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from datetime import timedelta + +from odoo import _, api, fields, models + + +class HrEmployee(models.Model): + _inherit = "hr.employee" + + shift_pattern_id = fields.Many2one( + comodel_name="hr.shift.pattern", + string="Shift cycle", + help="Rotation cycle the employee follows.", + ) + shift_pattern_start_date = fields.Date( + string="Cycle start week", + help="Week the employee starts on the first cycle step (set to its Monday).", + ) + shift_pattern_start_week = fields.Char( + string="ISO week", compute="_compute_shift_pattern_start_week" + ) + + @api.depends("shift_pattern_start_date") + def _compute_shift_pattern_start_week(self): + for employee in self: + date = employee.shift_pattern_start_date + if date: + iso = date.isocalendar() + employee.shift_pattern_start_week = _( + "Week %(week)s / %(year)s", week=iso[1], year=iso[0] + ) + else: + employee.shift_pattern_start_week = "" + + @api.onchange("shift_pattern_start_date") + def _onchange_shift_pattern_start_date(self): + if self.shift_pattern_start_date: + date = self.shift_pattern_start_date + self.shift_pattern_start_date = date - timedelta(days=date.weekday()) + + @api.model + def _normalize_start(self, vals): + if vals.get("shift_pattern_start_date"): + date = fields.Date.to_date(vals["shift_pattern_start_date"]) + vals["shift_pattern_start_date"] = date - timedelta(days=date.weekday()) + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + self._normalize_start(vals) + return super().create(vals_list) + + def write(self, vals): + self._normalize_start(vals) + res = super().write(vals) + if {"shift_pattern_id", "shift_pattern_start_date"} & set(vals): + self._recompute_future_shifts() + return res + + def _recompute_future_shifts(self): + today = fields.Date.context_today(self) + monday = today - timedelta(days=today.weekday()) + shifts = self.env["hr.shift.planning.shift"].search( + [ + ("employee_id", "in", self.ids), + ("planning_id.start_date", ">", monday), + ] + ) + shifts._apply_cycle() diff --git a/hr_shift_pattern/models/shift_pattern.py b/hr_shift_pattern/models/shift_pattern.py new file mode 100644 index 0000000..1735916 --- /dev/null +++ b/hr_shift_pattern/models/shift_pattern.py @@ -0,0 +1,27 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from odoo import fields, models + + +class ShiftPattern(models.Model): + _name = "hr.shift.pattern" + _description = "Shift rotation cycle" + + name = fields.Char(required=True) + color = fields.Integer() + step_ids = fields.One2many( + comodel_name="hr.shift.pattern.step", inverse_name="pattern_id" + ) + active = fields.Boolean(default=True) + + +class ShiftPatternStep(models.Model): + _name = "hr.shift.pattern.step" + _description = "Shift rotation cycle step" + _order = "pattern_id, sequence, id" + + pattern_id = fields.Many2one( + comodel_name="hr.shift.pattern", required=True, ondelete="cascade" + ) + sequence = fields.Integer(default=10) + week_id = fields.Many2one(comodel_name="hr.shift.pattern.week", required=True) diff --git a/hr_shift_pattern/models/shift_pattern_week.py b/hr_shift_pattern/models/shift_pattern_week.py new file mode 100644 index 0000000..14e51f9 --- /dev/null +++ b/hr_shift_pattern/models/shift_pattern_week.py @@ -0,0 +1,39 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from odoo import fields, models + +from odoo.addons.hr_shift.models.shift_template import WEEK_DAYS_SELECTION + + +class ShiftPatternWeek(models.Model): + _name = "hr.shift.pattern.week" + _description = "Shift week template" + + name = fields.Char(required=True) + color = fields.Integer() + line_ids = fields.One2many( + comodel_name="hr.shift.pattern.week.line", inverse_name="week_id" + ) + active = fields.Boolean(default=True) + + +class ShiftPatternWeekLine(models.Model): + _name = "hr.shift.pattern.week.line" + _description = "Shift week template day" + _order = "week_id, day_number" + + week_id = fields.Many2one( + comodel_name="hr.shift.pattern.week", required=True, ondelete="cascade" + ) + day_number = fields.Selection( + selection=WEEK_DAYS_SELECTION, string="Day", required=True + ) + template_id = fields.Many2one(comodel_name="hr.shift.template", required=True) + + _sql_constraints = [ + ( + "day_unique_per_week", + "unique(week_id, day_number)", + "Each day can only appear once per week template.", + ) + ] diff --git a/hr_shift_pattern/models/shift_planning.py b/hr_shift_pattern/models/shift_planning.py new file mode 100644 index 0000000..a61b6fc --- /dev/null +++ b/hr_shift_pattern/models/shift_planning.py @@ -0,0 +1,49 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from odoo import _, api, fields, models + + +class ShiftPlanning(models.Model): + _inherit = "hr.shift.planning" + + cycle_shifts_count = fields.Integer(compute="_compute_shift_counts") + manual_shifts_count = fields.Integer(compute="_compute_shift_counts") + all_lines_count = fields.Integer(compute="_compute_all_lines_count") + + @api.depends("shift_ids.employee_id.shift_pattern_id") + def _compute_shift_counts(self): + for planning in self: + cycle = planning.shift_ids.filtered( + lambda shift: shift.employee_id.shift_pattern_id + ) + planning.cycle_shifts_count = len(cycle) + planning.manual_shifts_count = len(planning.shift_ids) - len(cycle) + + @api.depends("shift_ids.line_ids") + def _compute_all_lines_count(self): + for planning in self: + planning.all_lines_count = len(planning.shift_ids.line_ids) + + def action_view_cycle_shifts(self): + self.ensure_one() + action = self.env["ir.actions.act_window"]._for_xml_id( + "hr_shift_pattern.shift_planning_cycle_lines_action" + ) + action["display_name"] = f"{_('Cycle shifts of')} {self.display_name}" + return action + + def action_view_manual_shifts(self): + self.ensure_one() + action = self.env["ir.actions.act_window"]._for_xml_id( + "hr_shift_pattern.shift_planning_manual_action" + ) + action["display_name"] = f"{_('Manual shifts of')} {self.display_name}" + return action + + def action_view_all_lines(self): + self.ensure_one() + action = self.env["ir.actions.act_window"]._for_xml_id( + "hr_shift_pattern.shift_planning_lines_action" + ) + action["display_name"] = f"{_('Shifts of')} {self.display_name}" + return action diff --git a/hr_shift_pattern/models/shift_planning_shift.py b/hr_shift_pattern/models/shift_planning_shift.py new file mode 100644 index 0000000..2921263 --- /dev/null +++ b/hr_shift_pattern/models/shift_planning_shift.py @@ -0,0 +1,96 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from datetime import date + +from odoo import api, fields, models + + +class ShiftPlanningShift(models.Model): + _inherit = "hr.shift.planning.shift" + + pattern_week_id = fields.Many2one( + comodel_name="hr.shift.pattern.week", + string="Week template", + group_expand="_group_expand_pattern_week", + help="Week template applied to the employee this planning week.", + ) + cycle_step = fields.Char(string="Cycle step", compute="_compute_cycle_step") + + def _group_expand_pattern_week(self, values, domain): + return self.env["hr.shift.pattern.week"].search([]) + + def _cycle_index(self): + """0-based step index for this shift, or -1 if the cycle has not started. + + With a start date, the phase is counted from it. Without one, the cycle is + always active and its phase is anchored on the calendar (absolute week). + """ + self.ensure_one() + pattern = self.employee_id.shift_pattern_id + if not pattern.step_ids: + return -1 + total = len(pattern.step_ids) + monday = self.planning_id.start_date + start = self.employee_id.shift_pattern_start_date + if start: + weeks = (monday - start).days // 7 + if weeks < 0: + return -1 + return weeks % total + epoch_monday = date(1970, 1, 5) # any Monday as a fixed calendar anchor + return ((monday - epoch_monday).days // 7) % total + + @api.depends( + "employee_id.shift_pattern_id", + "employee_id.shift_pattern_start_date", + "planning_id.start_date", + ) + def _compute_cycle_step(self): + for shift in self: + index = shift._cycle_index() + total = len(shift.employee_id.shift_pattern_id.step_ids) + shift.cycle_step = f"{index + 1} / {total}" if index >= 0 else "" + + def create(self, vals_list): + res = super().create(vals_list) + res._apply_cycle() + return res + + def write(self, vals): + res = super().write(vals) + if "pattern_week_id" in vals and not self.env.context.get("skip_apply_week"): + self._apply_pattern_week() + return res + + def _apply_cycle(self): + for shift in self: + index = shift._cycle_index() + if index < 0: + continue + week = shift.employee_id.shift_pattern_id.step_ids[index].week_id + shift.with_context(skip_apply_week=True).pattern_week_id = week + shift._apply_pattern_week() + + def _apply_pattern_week(self): + for shift in self: + week = shift.pattern_week_id + if not week.line_ids: + continue + # A real template puts the lines in the "assigned" state, so the per-day + # template set right after is kept (same trick as the base write). + shift.template_id = week.line_ids[0].template_id + shift.line_ids.filtered( + lambda line: line.state not in ("holiday", "on_leave") + ).unlink() + existing_days = shift.line_ids.mapped("day_number") + for week_line in week.line_ids: + if week_line.day_number in existing_days: + continue + line = shift.line_ids.create( + { + "shift_id": shift.id, + "day_number": week_line.day_number, + "template_id": shift.template_id.id, + } + ) + line.template_id = week_line.template_id diff --git a/hr_shift_pattern/pyproject.toml b/hr_shift_pattern/pyproject.toml new file mode 100644 index 0000000..4231d0c --- /dev/null +++ b/hr_shift_pattern/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/hr_shift_pattern/readme/CONTRIBUTORS.md b/hr_shift_pattern/readme/CONTRIBUTORS.md new file mode 100644 index 0000000..e552444 --- /dev/null +++ b/hr_shift_pattern/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Cyril VINH-TUNG \<\> diff --git a/hr_shift_pattern/readme/DESCRIPTION.md b/hr_shift_pattern/readme/DESCRIPTION.md new file mode 100644 index 0000000..232da8e --- /dev/null +++ b/hr_shift_pattern/readme/DESCRIPTION.md @@ -0,0 +1,4 @@ +This module adds reusable week templates and multi-week rotation cycles on top of +*hr_shift*. A week template (a *semaine type*) maps each worked day to a shift +template. A cycle is an ordered list of week templates. On a planning, each +employee is placed on a week template, and their days are filled from it. diff --git a/hr_shift_pattern/readme/USAGE.md b/hr_shift_pattern/readme/USAGE.md new file mode 100644 index 0000000..68192d8 --- /dev/null +++ b/hr_shift_pattern/readme/USAGE.md @@ -0,0 +1,17 @@ +Configuration: + +1. In *Shifts > Configuration > Week templates*, create a week template and add + one line per worked day (the day and the shift template that applies). +2. In *Shifts > Configuration > Cycles*, create a cycle and order its week + templates. +3. On each employee, set the cycle and the week they start it on. + +On a planning: + +1. Generate the shifts: every employee is filled from the cycle step matching + that planning week. +2. The *Shifts* button shows the shifts grouped by week template; drag an + employee to another column for a one-off change of that week. + +Changing an employee's cycle or start week re-fills their future plannings; the +current and past weeks are left untouched. diff --git a/hr_shift_pattern/security/ir.model.access.csv b/hr_shift_pattern/security/ir.model.access.csv new file mode 100644 index 0000000..c3ae4ba --- /dev/null +++ b/hr_shift_pattern/security/ir.model.access.csv @@ -0,0 +1,9 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_hr_shift_pattern_week_user,hr.shift.pattern.week user,model_hr_shift_pattern_week,base.group_user,1,0,0,0 +access_hr_shift_pattern_week_manager,hr.shift.pattern.week manager,model_hr_shift_pattern_week,hr_shift.group_shift_manager,1,1,1,1 +access_hr_shift_pattern_week_line_user,hr.shift.pattern.week.line user,model_hr_shift_pattern_week_line,base.group_user,1,0,0,0 +access_hr_shift_pattern_week_line_manager,hr.shift.pattern.week.line manager,model_hr_shift_pattern_week_line,hr_shift.group_shift_manager,1,1,1,1 +access_hr_shift_pattern_user,hr.shift.pattern user,model_hr_shift_pattern,base.group_user,1,0,0,0 +access_hr_shift_pattern_manager,hr.shift.pattern manager,model_hr_shift_pattern,hr_shift.group_shift_manager,1,1,1,1 +access_hr_shift_pattern_step_user,hr.shift.pattern.step user,model_hr_shift_pattern_step,base.group_user,1,0,0,0 +access_hr_shift_pattern_step_manager,hr.shift.pattern.step manager,model_hr_shift_pattern_step,hr_shift.group_shift_manager,1,1,1,1 diff --git a/hr_shift_pattern/static/description/index.html b/hr_shift_pattern/static/description/index.html new file mode 100644 index 0000000..e1622e9 --- /dev/null +++ b/hr_shift_pattern/static/description/index.html @@ -0,0 +1,449 @@ + + + + + +Employees Shifts Patterns + + + +
+

Employees Shifts Patterns

+ + +

Beta License: AGPL-3 OCA/shift-planning Translate me on Weblate Try me on Runboat

+

This module adds reusable week templates and multi-week rotation cycles +on top of hr_shift. A week template (a semaine type) maps each +worked day to a shift template. A cycle is an ordered list of week +templates. On a planning, each employee is placed on a week template, +and their days are filled from it.

+

Table of contents

+ +
+

Usage

+

Configuration:

+
    +
  1. In Shifts > Configuration > Week templates, create a week template +and add one line per worked day (the day and the shift template that +applies).
  2. +
  3. In Shifts > Configuration > Cycles, create a cycle and order its +week templates.
  4. +
  5. On each employee, set the cycle and the week they start it on.
  6. +
+

On a planning:

+
    +
  1. Generate the shifts: every employee is filled from the cycle step +matching that planning week.
  2. +
  3. The Shifts button shows the shifts grouped by week template; drag +an employee to another column for a one-off change of that week.
  4. +
+

Changing an employee’s cycle or start week re-fills their future +plannings; the current and past weeks are left untouched.

+
+
+

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

+
    +
  • INVITU
  • +
+
+
+

Contributors

+ +
+
+

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/shift-planning project on GitHub.

+

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

+
+
+
+ + diff --git a/hr_shift_pattern/tests/__init__.py b/hr_shift_pattern/tests/__init__.py new file mode 100644 index 0000000..11022cd --- /dev/null +++ b/hr_shift_pattern/tests/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import test_shift_pattern diff --git a/hr_shift_pattern/tests/test_shift_pattern.py b/hr_shift_pattern/tests/test_shift_pattern.py new file mode 100644 index 0000000..4c4a933 --- /dev/null +++ b/hr_shift_pattern/tests/test_shift_pattern.py @@ -0,0 +1,153 @@ +# Copyright 2026 INVITU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from datetime import date + +from odoo.addons.hr_shift.tests.common import TestHrShiftBase + + +class TestShiftPattern(TestHrShiftBase): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Demo data may flag other employees with shift_planning=True; restrict + # the planning generation to the test employees only. + cls.env["hr.employee"].search( + [ + ("shift_planning", "=", True), + ("id", "not in", (cls.employee_a + cls.employee_b).ids), + ] + ).shift_planning = False + cls.week_morning = cls.env["hr.shift.pattern.week"].create( + { + "name": "Morning week", + "line_ids": [ + (0, 0, {"day_number": "0", "template_id": cls.template_morning.id}), + (0, 0, {"day_number": "4", "template_id": cls.template_morning.id}), + ], + } + ) + cls.week_afternoon = cls.env["hr.shift.pattern.week"].create( + { + "name": "Afternoon week", + "line_ids": [ + ( + 0, + 0, + {"day_number": "0", "template_id": cls.template_afternoon.id}, + ), + ], + } + ) + cls.pattern = cls.env["hr.shift.pattern"].create( + { + "name": "Two weeks", + "step_ids": [ + (0, 0, {"sequence": 10, "week_id": cls.week_morning.id}), + (0, 0, {"sequence": 20, "week_id": cls.week_afternoon.id}), + ], + } + ) + # Future weeks, so the recompute (future only) actually applies + cls.start = date.fromisocalendar(2026, 30, 1) + cls.employee_a.write( + { + "shift_pattern_id": cls.pattern.id, + "shift_pattern_start_date": cls.start, + } + ) + + def _make_planning(self, year, week): + planning = self.env["hr.shift.planning"].create( + { + "year": year, + "week_number": week, + "start_date": date.fromisocalendar(year, week, 1), + "end_date": date.fromisocalendar(year, week, 7), + } + ) + planning.generate_shifts() + return planning + + def _shift(self, planning): + return planning.shift_ids.filtered( + lambda shift: shift.employee_id == self.employee_a + ) + + def _monday(self, shift): + return shift.line_ids.filtered(lambda line: line.day_number == "0") + + def test_start_week_snapped_to_monday(self): + # A mid-week date is normalised to its Monday + self.employee_a.shift_pattern_start_date = date(2026, 7, 22) # Wednesday + self.assertEqual(self.employee_a.shift_pattern_start_date, date(2026, 7, 20)) + self.employee_a.shift_pattern_start_date = self.start + + def test_first_cycle_step(self): + shift = self._shift(self._make_planning(2026, 30)) + self.assertEqual(shift.cycle_step, "1 / 2") + self.assertEqual(self._monday(shift).template_id, self.template_morning) + + def test_second_cycle_step(self): + shift = self._shift(self._make_planning(2026, 31)) + self.assertEqual(shift.cycle_step, "2 / 2") + self.assertEqual(self._monday(shift).template_id, self.template_afternoon) + + def test_recompute_future_on_start_change(self): + planning = self._make_planning(2026, 31) + self.assertEqual( + self._monday(self._shift(planning)).template_id, self.template_afternoon + ) + # Shift the start one week later: week 31 becomes step 1 (morning) + self.employee_a.shift_pattern_start_date = date.fromisocalendar(2026, 31, 1) + self.assertEqual( + self._monday(self._shift(planning)).template_id, self.template_morning + ) + + def test_manual_override(self): + planning = self._make_planning(2026, 30) + shift = self._shift(planning) + shift.pattern_week_id = self.week_afternoon + self.assertEqual(self._monday(shift).template_id, self.template_afternoon) + + def test_no_start_date_calendar_anchor(self): + # Without a start date the cycle is always active (calendar-anchored) + self.employee_a.shift_pattern_start_date = False + shift = self._shift(self._make_planning(2026, 30)) + self.assertTrue(shift.cycle_step) + self.assertIn( + self._monday(shift).template_id, + self.template_morning + self.template_afternoon, + ) + + def test_button_routes_to_manual_when_extra(self): + # employee_b has no cycle -> it is counted as a manual shift + planning = self._make_planning(2026, 30) + self.assertEqual(planning.cycle_shifts_count, 1) + self.assertEqual(planning.manual_shifts_count, 1) + action = planning.action_view_manual_shifts() + self.assertIn("shift_pattern_id", str(action["domain"])) + self.assertIn("'=', False", str(action["domain"])) + + def test_button_routes_to_cycle_when_all_have_cycle(self): + self.employee_b.write( + { + "shift_pattern_id": self.pattern.id, + "shift_pattern_start_date": self.start, + } + ) + planning = self._make_planning(2026, 30) + self.assertEqual(planning.cycle_shifts_count, 2) + self.assertEqual(planning.manual_shifts_count, 0) + action = planning.action_view_cycle_shifts() + self.assertEqual(action["res_model"], "hr.shift.planning.line") + self.assertIn("shift_pattern_id", str(action["domain"])) + self.assertIn("'!=', False", str(action["domain"])) + + def test_all_lines_action_includes_cycle_and_manual(self): + planning = self._make_planning(2026, 30) + # both employees have shifts in the planning (one cycle, one extra) + self.assertGreater(planning.all_lines_count, 0) + action = planning.action_view_all_lines() + self.assertEqual(action["res_model"], "hr.shift.planning.line") + # no population filter in the domain: both cycle and manual visible + self.assertNotIn("shift_pattern_id", str(action["domain"])) diff --git a/hr_shift_pattern/views/hr_employee_views.xml b/hr_shift_pattern/views/hr_employee_views.xml new file mode 100644 index 0000000..8894b65 --- /dev/null +++ b/hr_shift_pattern/views/hr_employee_views.xml @@ -0,0 +1,20 @@ + + + + hr.employee + + + + + + + + + + diff --git a/hr_shift_pattern/views/menus.xml b/hr_shift_pattern/views/menus.xml new file mode 100644 index 0000000..5fe4abd --- /dev/null +++ b/hr_shift_pattern/views/menus.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + diff --git a/hr_shift_pattern/views/shift_pattern_views.xml b/hr_shift_pattern/views/shift_pattern_views.xml new file mode 100644 index 0000000..3accc27 --- /dev/null +++ b/hr_shift_pattern/views/shift_pattern_views.xml @@ -0,0 +1,43 @@ + + + + hr.shift.pattern + +
+ + + + + + + + + + + + + + +
+
+
+ + hr.shift.pattern + + + + + + + + + Cycles + hr.shift.pattern + list,form + +
diff --git a/hr_shift_pattern/views/shift_pattern_week_views.xml b/hr_shift_pattern/views/shift_pattern_week_views.xml new file mode 100644 index 0000000..06955ca --- /dev/null +++ b/hr_shift_pattern/views/shift_pattern_week_views.xml @@ -0,0 +1,43 @@ + + + + hr.shift.pattern.week + +
+ + + + + + + + + + + + + + +
+
+
+ + hr.shift.pattern.week + + + + + + + + + Week templates + hr.shift.pattern.week + list,form + +
diff --git a/hr_shift_pattern/views/shift_planning_views.xml b/hr_shift_pattern/views/shift_planning_views.xml new file mode 100644 index 0000000..8605ff9 --- /dev/null +++ b/hr_shift_pattern/views/shift_planning_views.xml @@ -0,0 +1,274 @@ + + + + hr.shift.planning.shift + + primary + + + 0 + 0 + + + + + hr.shift.planning.line + + + + + + + + + + + + + + + + + + + + + + + + hr.shift.planning.line + + + + + + + + + + + hr.shift.planning.line + + + + + + + +
+
+ + +
+
+ + + + + + +
+
+
+
+
+
+
+ + Cycle shifts + hr.shift.planning.line + kanban,list,form + + [('shift_id.planning_id', '=', active_id), ('shift_id.employee_id.shift_pattern_id', '!=', False)] + + + 1 + kanban + + + + + 2 + list + + + + + Manual shifts + hr.shift.planning.shift + kanban,list,form + + {'group_by': 'pattern_week_id'} + [('planning_id', '=', active_id), ('employee_id.shift_pattern_id', '=', False)] + + + Shifts + hr.shift.planning.line + kanban,list,form + + [('shift_id.planning_id', '=', active_id)] + + + 1 + kanban + + + + + 2 + list + + + + + hr.shift.planning.line + + + + employee_id + + + + + All Shifts + hr.shift.planning.line + calendar,list + [('state', '!=', 'unassigned')] + + + 1 + calendar + + + + + hr.shift.planning + + + + +
+ + + +
+
+
+