From 86ec3e7f43fbaf81213b4240c1e9865132fe3666 Mon Sep 17 00:00:00 2001 From: Quirino Leone Date: Fri, 27 Jun 2025 17:29:38 +0200 Subject: [PATCH] [ADD] hr_holidays_accrual_by_tag: new module Co-authored-by: Simone Rubino --- hr_holidays_accrual_by_tag/README.rst | 85 ++++ hr_holidays_accrual_by_tag/__init__.py | 1 + hr_holidays_accrual_by_tag/__manifest__.py | 13 + hr_holidays_accrual_by_tag/models/__init__.py | 1 + .../models/hr_employee.py | 79 ++++ .../readme/CONTRIBUTORS.rst | 4 + .../readme/DESCRIPTION.rst | 3 + .../static/description/index.html | 435 ++++++++++++++++++ hr_holidays_accrual_by_tag/tests/__init__.py | 1 + .../tests/test_hr_holidays_accrual_by_tag.py | 112 +++++ .../odoo/addons/hr_holidays_accrual_by_tag | 1 + setup/hr_holidays_accrual_by_tag/setup.py | 6 + 12 files changed, 741 insertions(+) create mode 100644 hr_holidays_accrual_by_tag/README.rst create mode 100644 hr_holidays_accrual_by_tag/__init__.py create mode 100644 hr_holidays_accrual_by_tag/__manifest__.py create mode 100644 hr_holidays_accrual_by_tag/models/__init__.py create mode 100644 hr_holidays_accrual_by_tag/models/hr_employee.py create mode 100644 hr_holidays_accrual_by_tag/readme/CONTRIBUTORS.rst create mode 100644 hr_holidays_accrual_by_tag/readme/DESCRIPTION.rst create mode 100644 hr_holidays_accrual_by_tag/static/description/index.html create mode 100644 hr_holidays_accrual_by_tag/tests/__init__.py create mode 100644 hr_holidays_accrual_by_tag/tests/test_hr_holidays_accrual_by_tag.py create mode 120000 setup/hr_holidays_accrual_by_tag/odoo/addons/hr_holidays_accrual_by_tag create mode 100644 setup/hr_holidays_accrual_by_tag/setup.py diff --git a/hr_holidays_accrual_by_tag/README.rst b/hr_holidays_accrual_by_tag/README.rst new file mode 100644 index 000000000..5003741e4 --- /dev/null +++ b/hr_holidays_accrual_by_tag/README.rst @@ -0,0 +1,85 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +========================== +HR Holidays Accrual By Tag +========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:6a371341422010f0c2c1103e2663fc7bdd9fdd42bb6af0640786c2805713fc1c + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/16.0/hr_holidays_accrual_by_tag + :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-16-0/hr-holidays-16-0-hr_holidays_accrual_by_tag + :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=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module manages automatically accrual allocations by employee tags. +When a new employee is created, accrual allocations are created starting from active accrual plans, based on its tags. +Adding tags later and removing them is also managed. + +**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 +~~~~~~~ + +* PyTech SRL + +Contributors +~~~~~~~~~~~~ + +* `PyTech `_: + + * Quirino Leone + * Simone Rubino + +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/hr_holidays_accrual_by_tag/__init__.py b/hr_holidays_accrual_by_tag/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/hr_holidays_accrual_by_tag/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/hr_holidays_accrual_by_tag/__manifest__.py b/hr_holidays_accrual_by_tag/__manifest__.py new file mode 100644 index 000000000..df20d90a1 --- /dev/null +++ b/hr_holidays_accrual_by_tag/__manifest__.py @@ -0,0 +1,13 @@ +{ + "name": "HR Holidays Accrual By Tag", + "version": "16.0.1.0.0", + "summary": "Automates accrual plan assignment based on employee tags.", + "category": "Human Resources", + "author": "PyTech SRL, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/hr-holidays", + "depends": ["hr", "hr_holidays"], + "data": [], + "installable": True, + "application": False, + "license": "AGPL-3", +} diff --git a/hr_holidays_accrual_by_tag/models/__init__.py b/hr_holidays_accrual_by_tag/models/__init__.py new file mode 100644 index 000000000..e11a62f98 --- /dev/null +++ b/hr_holidays_accrual_by_tag/models/__init__.py @@ -0,0 +1 @@ +from . import hr_employee diff --git a/hr_holidays_accrual_by_tag/models/hr_employee.py b/hr_holidays_accrual_by_tag/models/hr_employee.py new file mode 100644 index 000000000..5bf398954 --- /dev/null +++ b/hr_holidays_accrual_by_tag/models/hr_employee.py @@ -0,0 +1,79 @@ +from odoo import api, fields, models + + +class HrEmployee(models.Model): + _inherit = "hr.employee" + + @api.model_create_multi + def create(self, vals_list): + employees = super().create(vals_list) + employees._assign_accrual_plans_by_tags() + return employees + + def write(self, vals): + res = super().write(vals) + if "category_ids" in vals: + self._assign_accrual_plans_by_tags() + return res + + def _assign_accrual_plans_by_tags(self): + accrual_allocations = self.env["hr.leave.allocation"].search( + [ + ("employee_id", "=", False), + ("holiday_type", "=", "category"), + ("accrual_plan_id", "!=", False), + ] + ) + existing_assignments = self.env["hr.leave.allocation"].search( + [ + ("employee_id", "in", self.ids), + ("accrual_plan_id", "in", accrual_allocations.accrual_plan_id.ids), + ("date_to", "=", False), + ], + ) + + new_allocations_values = [] + for employee in self: + employee_tags = employee.category_ids + + for allocation in accrual_allocations: + employee_assignment = fields.first( + existing_assignments.filtered( + lambda assignment, allocation=allocation, employee=employee: ( + assignment.accrual_plan_id == allocation.accrual_plan_id + and assignment.employee_id == employee + ) + ) + ) + if allocation.category_id in employee_tags and not employee_assignment: + new_allocations_values.append( + { + "name": allocation.name, + "holiday_type": "employee", + "holiday_status_id": allocation.holiday_status_id.id, + "notes": allocation.notes, + "number_of_days": allocation.number_of_days, + "parent_id": allocation.id, + "employee_id": self.id, + "employee_ids": [(6, 0, [self.id])], + "state": "confirm", + "allocation_type": allocation.allocation_type, + "date_from": fields.Date.context_today(self), + "accrual_plan_id": allocation.accrual_plan_id.id, + } + ) + elif ( + allocation.category_id not in employee_tags and employee_assignment + ): + employee_assignment.date_to = fields.Date.context_today(self) + + if new_allocations_values: + new_allocations = ( + self.env["hr.leave.allocation"] + .with_context( + mail_notify_force_send=False, + mail_activity_automation_skip=True, + ) + .create(new_allocations_values) + ) + new_allocations.action_validate() diff --git a/hr_holidays_accrual_by_tag/readme/CONTRIBUTORS.rst b/hr_holidays_accrual_by_tag/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..d3ca83baf --- /dev/null +++ b/hr_holidays_accrual_by_tag/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* `PyTech `_: + + * Quirino Leone + * Simone Rubino diff --git a/hr_holidays_accrual_by_tag/readme/DESCRIPTION.rst b/hr_holidays_accrual_by_tag/readme/DESCRIPTION.rst new file mode 100644 index 000000000..70c5af8d4 --- /dev/null +++ b/hr_holidays_accrual_by_tag/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This module manages automatically accrual allocations by employee tags. +When a new employee is created, accrual allocations are created starting from active accrual plans, based on its tags. +Adding tags later and removing them is also managed. diff --git a/hr_holidays_accrual_by_tag/static/description/index.html b/hr_holidays_accrual_by_tag/static/description/index.html new file mode 100644 index 000000000..3bb0c7e58 --- /dev/null +++ b/hr_holidays_accrual_by_tag/static/description/index.html @@ -0,0 +1,435 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

HR Holidays Accrual By Tag

+ +

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

+

This module manages automatically accrual allocations by employee tags. +When a new employee is created, accrual allocations are created starting from active accrual plans, based on its tags. +Adding tags later and removing them is also managed.

+

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

+
    +
  • PyTech SRL
  • +
+
+
+

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/hr-holidays project on GitHub.

+

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

+
+
+
+
+ + diff --git a/hr_holidays_accrual_by_tag/tests/__init__.py b/hr_holidays_accrual_by_tag/tests/__init__.py new file mode 100644 index 000000000..8cefb67a9 --- /dev/null +++ b/hr_holidays_accrual_by_tag/tests/__init__.py @@ -0,0 +1 @@ +from . import test_hr_holidays_accrual_by_tag diff --git a/hr_holidays_accrual_by_tag/tests/test_hr_holidays_accrual_by_tag.py b/hr_holidays_accrual_by_tag/tests/test_hr_holidays_accrual_by_tag.py new file mode 100644 index 000000000..e041e8eec --- /dev/null +++ b/hr_holidays_accrual_by_tag/tests/test_hr_holidays_accrual_by_tag.py @@ -0,0 +1,112 @@ +from datetime import date + +from odoo import Command +from odoo.tests.common import TransactionCase + + +class TestHrHolidaysAccrualsByTag(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.company = cls.env.ref("base.main_company") + cls.accrual_plan_model = cls.env["hr.leave.accrual.plan"] + cls.leave_type_model = cls.env["hr.leave.type"] + cls.accrual_allocation_model = cls.env["hr.leave.allocation"] + cls.employee_categ_model = cls.env["hr.employee.category"] + + cls.accrual_plan1 = cls.accrual_plan_model.create( + {"name": "Test Accrual Plan 1"} + ) + cls.accrual_plan2 = cls.accrual_plan_model.create( + {"name": "Test Accrual Plan 2"} + ) + + cls.leave_type = cls.leave_type_model.create( + {"name": "Test Leave", "time_type": "leave"} + ) + + cls.tag1 = cls.employee_categ_model.create({"name": "Category 1"}) + cls.tag2 = cls.employee_categ_model.create({"name": "Category 2"}) + cls.tag3 = cls.employee_categ_model.create({"name": "Category 3"}) + + cls.accrual_allocation_model.create( + { + "date_from": date.today(), + "holiday_status_id": cls.leave_type.id, + "holiday_type": "category", + "accrual_plan_id": cls.accrual_plan1.id, + "category_id": cls.tag1.id, + } + ) + cls.accrual_allocation_model.create( + { + "date_from": date.today(), + "holiday_status_id": cls.leave_type.id, + "holiday_type": "category", + "accrual_plan_id": cls.accrual_plan2.id, + "category_id": cls.tag2.id, + } + ) + + def test_create_new_employee(self): + # Employee without tags won't have any allocation + employee1 = self.env["hr.employee"].create( + { + "name": "Test Employee 1", + "company_id": self.company.id, + } + ) + allocations = self.accrual_allocation_model.search( + [("employee_id", "=", employee1.id)] + ) + self.assertFalse(allocations) + + # Creation of accrual allocation only with tags that have a related accrual plan + employee2 = self.env["hr.employee"].create( + { + "name": "Test Employee 3", + "company_id": self.company.id, + "category_ids": [Command.set([self.tag1.id, self.tag3.id])], + } + ) + allocations = self.accrual_allocation_model.search( + [("employee_id", "=", employee2.id)] + ) + self.assertEqual(len(allocations), 1) + self.assertEqual(allocations.accrual_plan_id, self.accrual_plan1) + + # Adding a new tag with a related accrual plan + employee2.category_ids = self.tag1 + self.tag2 + allocations = self.accrual_allocation_model.search( + [("employee_id", "=", employee2.id)] + ) + self.assertEqual(len(allocations), 2) + self.assertFalse(any(allocation.date_to for allocation in allocations)) + + # Updating tag list will update accrual allocations + employee2.category_ids = self.tag2 + allocation1 = self.accrual_allocation_model.search( + [ + ("employee_id", "=", employee2.id), + ("accrual_plan_id", "=", self.accrual_plan1.id), + ] + ) + self.assertTrue(allocation1.date_to) + allocation2 = self.accrual_allocation_model.search( + [ + ("employee_id", "=", employee2.id), + ("accrual_plan_id", "=", self.accrual_plan2.id), + ] + ) + self.assertFalse(allocation2.date_to) + + # Adding same tag again will create new accrual allocation + employee2.category_ids = self.tag1 + self.tag2 + allocations = self.accrual_allocation_model.search( + [ + ("employee_id", "=", employee2.id), + ("accrual_plan_id", "=", self.accrual_plan1.id), + ] + ) + self.assertEqual(len(allocations), 2) + self.assertFalse(all(allocation.date_to for allocation in allocations)) diff --git a/setup/hr_holidays_accrual_by_tag/odoo/addons/hr_holidays_accrual_by_tag b/setup/hr_holidays_accrual_by_tag/odoo/addons/hr_holidays_accrual_by_tag new file mode 120000 index 000000000..4184b3a21 --- /dev/null +++ b/setup/hr_holidays_accrual_by_tag/odoo/addons/hr_holidays_accrual_by_tag @@ -0,0 +1 @@ +../../../../hr_holidays_accrual_by_tag \ No newline at end of file diff --git a/setup/hr_holidays_accrual_by_tag/setup.py b/setup/hr_holidays_accrual_by_tag/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/hr_holidays_accrual_by_tag/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)