diff --git a/hr_holidays_cumulative_timeoff/README.rst b/hr_holidays_cumulative_timeoff/README.rst new file mode 100644 index 00000000..12cd8b9d --- /dev/null +++ b/hr_holidays_cumulative_timeoff/README.rst @@ -0,0 +1,102 @@ +============================== +HR Holidays Cumulative Timeoff +============================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:c1d9511043671d5f3b7785bdbed135aef231f952c6962f77a2118c24052dbecd + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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--holidays-lightgray.png?logo=github + :target: https://github.com/OCA/hr-holidays/tree/14.0/hr_holidays_cumulative_timeoff + :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-14-0/hr-holidays-14-0-hr_holidays_cumulative_timeoff + :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=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds a couple of fields showing how many time-off units (days/hours) have +been used at the time of each time-off, separated by time-off type. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Go to Time Off > Reporting > by Employee > View pivot + +In the pivot view you shall see the fields "Cumulative Remaining Timeoff" and +"Cumulative Used Timeoff". These fields show the total amount of used and remaining +days for each period, separated by employee and leave category + +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 SRL `_ + + * Sebastiano Picchi + +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-aleuffre| image:: https://github.com/aleuffre.png?size=40px + :target: https://github.com/aleuffre + :alt: aleuffre +.. |maintainer-renda-dev| image:: https://github.com/renda-dev.png?size=40px + :target: https://github.com/renda-dev + :alt: renda-dev +.. |maintainer-PicchiSeba| image:: https://github.com/PicchiSeba.png?size=40px + :target: https://github.com/PicchiSeba + :alt: PicchiSeba + +Current `maintainers `__: + +|maintainer-aleuffre| |maintainer-renda-dev| |maintainer-PicchiSeba| + +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_cumulative_timeoff/__init__.py b/hr_holidays_cumulative_timeoff/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/hr_holidays_cumulative_timeoff/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/hr_holidays_cumulative_timeoff/__manifest__.py b/hr_holidays_cumulative_timeoff/__manifest__.py new file mode 100644 index 00000000..326f1ed5 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/__manifest__.py @@ -0,0 +1,18 @@ +{ + "name": "HR Holidays Cumulative Timeoff", + "version": "14.0.1.0.0", + "category": "Human Resources", + "website": "https://github.com/OCA/hr-holidays", + "author": "PyTech SRL, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["hr_holidays"], + "data": [ + "views/hr_leave_view.xml", + ], + "installable": True, + "maintainers": [ + "aleuffre", + "renda-dev", + "PicchiSeba", + ], +} diff --git a/hr_holidays_cumulative_timeoff/models/__init__.py b/hr_holidays_cumulative_timeoff/models/__init__.py new file mode 100644 index 00000000..458baf20 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/models/__init__.py @@ -0,0 +1,2 @@ +from . import hr_leave +from . import hr_leave_allocation diff --git a/hr_holidays_cumulative_timeoff/models/hr_leave.py b/hr_holidays_cumulative_timeoff/models/hr_leave.py new file mode 100644 index 00000000..2d929719 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/models/hr_leave.py @@ -0,0 +1,46 @@ +from odoo import api, fields, models + + +class HrLeave(models.Model): + _inherit = "hr.leave" + + cumulative_remaining_timeoff = fields.Float( + compute="_compute_timeoffs", + store=True, + ) + cumulative_used_timeoff = fields.Float( + compute="_compute_timeoffs", + store=True, + ) + + def _get_plain_unit_value(self): + self.ensure_one() + if self.holiday_status_id.request_unit == "day": + return self.number_of_days_display + else: + return self.number_of_hours_display + + @api.depends("state", "date_from", "date_to", "holiday_status_id.request_unit") + def _compute_timeoffs(self): + employees = self.mapped("employee_id") + for employee in employees: + leaves = self.filtered(lambda lv, emp=employee: lv.employee_id == emp) + leave_types = leaves.mapped("holiday_status_id") + for leave_type in leave_types: + if leave_type.allocation_type == "no": + continue + max_leave_time = leave_type.with_context( + employee_id=employee.id + ).max_leaves + relevant_leaves = self.search( + [ + ("employee_id", "=", employee.id), + ("holiday_status_id", "=", leave_type.id), + ], + order="date_from asc", + ) + time_used = 0 + for leave in relevant_leaves: + time_used += leave._get_plain_unit_value() + leave.cumulative_used_timeoff = time_used + leave.cumulative_remaining_timeoff = max_leave_time - time_used diff --git a/hr_holidays_cumulative_timeoff/models/hr_leave_allocation.py b/hr_holidays_cumulative_timeoff/models/hr_leave_allocation.py new file mode 100644 index 00000000..a3618ee3 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/models/hr_leave_allocation.py @@ -0,0 +1,40 @@ +from odoo import models + + +class HrLeaveAllocation(models.Model): + _inherit = "hr.leave.allocation" + + def _propagate_cumulative_timeoff_recompute(self): + HrLeave = self.env["hr.leave"] + to_propagate = HrLeave + for hla in self: + to_propagate += HrLeave.search( + [ + ("employee_id", "=", hla.employee_id.id), + ("holiday_status_id", "=", hla.holiday_status_id.id), + ] + ) + if to_propagate: + to_propagate._compute_timeoffs() + + def action_approve(self): + res = super().action_approve() + + valid_allocations = self.filtered( + lambda hla: hla.holiday_status_id.leave_validation_type in ("hr", "manager") + ) + if valid_allocations: + valid_allocations._propagate_cumulative_timeoff_recompute() + + return res + + def action_validate(self): + res = super().action_validate() + + valid_allocations = self.filtered( + lambda hla: hla.holiday_status_id.leave_validation_type == "both" + ) + if valid_allocations: + valid_allocations._propagate_cumulative_timeoff_recompute() + + return res diff --git a/hr_holidays_cumulative_timeoff/readme/CONTRIBUTORS.rst b/hr_holidays_cumulative_timeoff/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..f510d7b7 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `PyTech SRL `_ + + * Sebastiano Picchi diff --git a/hr_holidays_cumulative_timeoff/readme/DESCRIPTION.rst b/hr_holidays_cumulative_timeoff/readme/DESCRIPTION.rst new file mode 100644 index 00000000..a50c2320 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module adds a couple of fields showing how many time-off units (days/hours) have +been used at the time of each time-off, separated by time-off type. diff --git a/hr_holidays_cumulative_timeoff/readme/USAGE.rst b/hr_holidays_cumulative_timeoff/readme/USAGE.rst new file mode 100644 index 00000000..2d1c4510 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/readme/USAGE.rst @@ -0,0 +1,5 @@ +Go to Time Off > Reporting > by Employee > View pivot + +In the pivot view you shall see the fields "Cumulative Remaining Timeoff" and +"Cumulative Used Timeoff". These fields show the total amount of used and remaining +days for each period, separated by employee and leave category diff --git a/hr_holidays_cumulative_timeoff/static/description/index.html b/hr_holidays_cumulative_timeoff/static/description/index.html new file mode 100644 index 00000000..a9888c66 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/static/description/index.html @@ -0,0 +1,437 @@ + + + + + +HR Holidays Cumulative Timeoff + + + +
+

HR Holidays Cumulative Timeoff

+ + +

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

+

This module adds a couple of fields showing how many time-off units (days/hours) have +been used at the time of each time-off, separated by time-off type.

+

Table of contents

+ +
+

Usage

+

Go to Time Off > Reporting > by Employee > View pivot

+

In the pivot view you shall see the fields “Cumulative Remaining Timeoff” and +“Cumulative Used Timeoff”. These fields show the total amount of used and remaining +days for each period, separated by employee and leave category

+
+
+

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.

+

Current maintainers:

+

aleuffre renda-dev PicchiSeba

+

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_cumulative_timeoff/tests/__init__.py b/hr_holidays_cumulative_timeoff/tests/__init__.py new file mode 100644 index 00000000..842e15c7 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/tests/__init__.py @@ -0,0 +1 @@ +from . import test_hr_leave_cumulative_fields diff --git a/hr_holidays_cumulative_timeoff/tests/test_hr_leave_cumulative_fields.py b/hr_holidays_cumulative_timeoff/tests/test_hr_leave_cumulative_fields.py new file mode 100644 index 00000000..71fd22ef --- /dev/null +++ b/hr_holidays_cumulative_timeoff/tests/test_hr_leave_cumulative_fields.py @@ -0,0 +1,88 @@ +from odoo.tests.common import SavepointCase + + +class TestHrLeaveComulativeFields(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.HrLeave = cls.env["hr.leave"] + cls.HrLeaveAllocation = cls.env["hr.leave.allocation"] + cls.HrLeaveType = cls.env["hr.leave.type"] + + cls.employee = cls.env.ref("hr.employee_qdp") + + cls.custom_hr_leave_type = cls.HrLeaveType.create( + [ + { + "name": "Custom Leave Type", + "request_unit": "day", + "allocation_type": "fixed", + "leave_validation_type": "hr", + } + ] + ) + + def test_cumulative_time_off_values(self): + allocation_1 = self.HrLeaveAllocation.create( + [ + { + "name": "New Allocation for Marc Demo", + "holiday_type": "employee", + "employee_id": self.employee.id, + "holiday_status_id": self.custom_hr_leave_type.id, + "allocation_type": "regular", + "number_of_days": 7, + } + ] + ) + allocation_1.action_approve() + starting_leave = self.HrLeave.create( + [ + { + "employee_id": self.employee.id, + "holiday_status_id": self.custom_hr_leave_type.id, + "holiday_type": "employee", + "date_from": "2024-06-03", + "date_to": "2024-06-08", + }, + ] + ) + + self.assertEqual(starting_leave.cumulative_used_timeoff, 5) + self.assertEqual(starting_leave.cumulative_remaining_timeoff, 2) + + allocation_2 = self.HrLeaveAllocation.create( + [ + { + "name": "New Allocation for Marc Demo", + "holiday_type": "employee", + "employee_id": self.employee.id, + "holiday_status_id": self.custom_hr_leave_type.id, + "allocation_type": "regular", + "number_of_days": 20, + } + ] + ) + allocation_2.action_approve() + + self.assertEqual(starting_leave.cumulative_used_timeoff, 5) + self.assertEqual(starting_leave.cumulative_remaining_timeoff, 22) + + latest_leave = self.HrLeave.create( + [ + { + "employee_id": self.employee.id, + "holiday_status_id": self.custom_hr_leave_type.id, + "holiday_type": "employee", + "date_from": "2024-06-17", + "date_to": "2024-06-22", + }, + ] + ) + + self.assertEqual(starting_leave.cumulative_used_timeoff, 5) + self.assertEqual(starting_leave.cumulative_remaining_timeoff, 22) + + self.assertEqual(latest_leave.cumulative_used_timeoff, 10) + self.assertEqual(latest_leave.cumulative_remaining_timeoff, 17) diff --git a/hr_holidays_cumulative_timeoff/views/hr_leave_view.xml b/hr_holidays_cumulative_timeoff/views/hr_leave_view.xml new file mode 100644 index 00000000..6ecee679 --- /dev/null +++ b/hr_holidays_cumulative_timeoff/views/hr_leave_view.xml @@ -0,0 +1,14 @@ + + + + hr.holidays.report_pivot + hr.leave + + + + + + + + + diff --git a/setup/hr_holidays_cumulative_timeoff/odoo/addons/hr_holidays_cumulative_timeoff b/setup/hr_holidays_cumulative_timeoff/odoo/addons/hr_holidays_cumulative_timeoff new file mode 120000 index 00000000..d29d5091 --- /dev/null +++ b/setup/hr_holidays_cumulative_timeoff/odoo/addons/hr_holidays_cumulative_timeoff @@ -0,0 +1 @@ +../../../../hr_holidays_cumulative_timeoff \ No newline at end of file diff --git a/setup/hr_holidays_cumulative_timeoff/setup.py b/setup/hr_holidays_cumulative_timeoff/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/hr_holidays_cumulative_timeoff/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)