From 264fd61db8251b77fc8eb728ec02437d75ca8bcf Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Tue, 11 Apr 2023 13:48:57 +0200 Subject: [PATCH] [ADD] hr_holidays_leave_self_approve --- hr_holidays_leave_self_approve/README.rst | 1 + hr_holidays_leave_self_approve/__init__.py | 3 + .../__manifest__.py | 15 ++++ .../models/__init__.py | 3 + .../models/hr_leave.py | 18 +++++ .../readme/CONFIGURE.rst | 1 + .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 1 + .../tests/__init__.py | 3 + .../test_hr_holidays_leave_self_approve.py | 77 +++++++++++++++++++ 10 files changed, 123 insertions(+) create mode 100644 hr_holidays_leave_self_approve/README.rst create mode 100644 hr_holidays_leave_self_approve/__init__.py create mode 100644 hr_holidays_leave_self_approve/__manifest__.py create mode 100644 hr_holidays_leave_self_approve/models/__init__.py create mode 100644 hr_holidays_leave_self_approve/models/hr_leave.py create mode 100644 hr_holidays_leave_self_approve/readme/CONFIGURE.rst create mode 100644 hr_holidays_leave_self_approve/readme/CONTRIBUTORS.rst create mode 100644 hr_holidays_leave_self_approve/readme/DESCRIPTION.rst create mode 100644 hr_holidays_leave_self_approve/tests/__init__.py create mode 100644 hr_holidays_leave_self_approve/tests/test_hr_holidays_leave_self_approve.py diff --git a/hr_holidays_leave_self_approve/README.rst b/hr_holidays_leave_self_approve/README.rst new file mode 100644 index 00000000..f24a174a --- /dev/null +++ b/hr_holidays_leave_self_approve/README.rst @@ -0,0 +1 @@ +autogenerated diff --git a/hr_holidays_leave_self_approve/__init__.py b/hr_holidays_leave_self_approve/__init__.py new file mode 100644 index 00000000..83e553ac --- /dev/null +++ b/hr_holidays_leave_self_approve/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import models diff --git a/hr_holidays_leave_self_approve/__manifest__.py b/hr_holidays_leave_self_approve/__manifest__.py new file mode 100644 index 00000000..02d2ff00 --- /dev/null +++ b/hr_holidays_leave_self_approve/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2023 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Self Approve Leaves", + "version": "15.0.1.0.0", + "license": "AGPL-3", + "summary": "Allow employee to approve his own leaves", + "author": "Camptocamp SA, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/hr-holidays", + "category": "Human Resources", + "depends": ["hr_holidays"], + "data": [], + "installable": True, +} diff --git a/hr_holidays_leave_self_approve/models/__init__.py b/hr_holidays_leave_self_approve/models/__init__.py new file mode 100644 index 00000000..b1c9b691 --- /dev/null +++ b/hr_holidays_leave_self_approve/models/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import hr_leave diff --git a/hr_holidays_leave_self_approve/models/hr_leave.py b/hr_holidays_leave_self_approve/models/hr_leave.py new file mode 100644 index 00000000..6438bfeb --- /dev/null +++ b/hr_holidays_leave_self_approve/models/hr_leave.py @@ -0,0 +1,18 @@ +# Copyright 2023 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class HrLeave(models.Model): + _inherit = "hr.leave" + + def _check_approval_update(self, state): + if self.env.is_admin(): + return + if state == "validate": + current_employee = self.env.user.employee_id + if current_employee.user_id == current_employee.leave_manager_id: + # Filter out self-approved leaves + self = self.filtered_domain([("employee_id", "!=", current_employee)]) + super()._check_approval_update(state) diff --git a/hr_holidays_leave_self_approve/readme/CONFIGURE.rst b/hr_holidays_leave_self_approve/readme/CONFIGURE.rst new file mode 100644 index 00000000..886a7ed1 --- /dev/null +++ b/hr_holidays_leave_self_approve/readme/CONFIGURE.rst @@ -0,0 +1 @@ +Nothing to configure. When employee "Time Off" approver is set to himself, it will work as expected. diff --git a/hr_holidays_leave_self_approve/readme/CONTRIBUTORS.rst b/hr_holidays_leave_self_approve/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..bf65fe58 --- /dev/null +++ b/hr_holidays_leave_self_approve/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Florent Xiclnua diff --git a/hr_holidays_leave_self_approve/readme/DESCRIPTION.rst b/hr_holidays_leave_self_approve/readme/DESCRIPTION.rst new file mode 100644 index 00000000..d5e221d7 --- /dev/null +++ b/hr_holidays_leave_self_approve/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module allows employee to approve their own leave requests, when their "Time Off" approver is themself. diff --git a/hr_holidays_leave_self_approve/tests/__init__.py b/hr_holidays_leave_self_approve/tests/__init__.py new file mode 100644 index 00000000..10ab23c9 --- /dev/null +++ b/hr_holidays_leave_self_approve/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import test_hr_holidays_leave_self_approve diff --git a/hr_holidays_leave_self_approve/tests/test_hr_holidays_leave_self_approve.py b/hr_holidays_leave_self_approve/tests/test_hr_holidays_leave_self_approve.py new file mode 100644 index 00000000..f44262fd --- /dev/null +++ b/hr_holidays_leave_self_approve/tests/test_hr_holidays_leave_self_approve.py @@ -0,0 +1,77 @@ +# Copyright 2023 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from datetime import datetime, timedelta + +from odoo.tests.common import TransactionCase + + +class TestHolidaysAutoValidate(TransactionCase): + def setUp(self): + super().setUp() + self.employee_model = self.env["hr.employee"] + self.user_model = self.env["res.users"] + self.leave_request_model = self.env["hr.leave"] + self.leave_allocation_model = self.env["hr.leave.allocation"] + + # Create an employee user to make leave requests + self.test_user_id = self.user_model.create( + {"name": "Test User", "login": "test_user", "email": "mymail@test.com"} + ) + + # Create an employee related to the user to make leave requests + # Assign user as self-approver + self.test_employee_id = self.employee_model.create( + { + "name": "Test Employee", + "user_id": self.test_user_id.id, + "leave_manager_id": self.test_user_id.id, + ) + + # "Paid Leave" type + leave_type = self.env.ref("hr_holidays.holiday_status_cl") + + # Create leave allocation request + self.leave_allocation = self.leave_allocation_model.create( + { + "name": "Test Allocation Request", + "holiday_status_id": leave_type.id, + "holiday_type": "employee", + "employee_id": self.test_employee_id.id, + "number_of_days": 10, + } + ) + + def test_allocation_request_state(self): + # Check for leave_allocation state + self.assertEqual(self.leave_allocation.state, "confirm") + + # Validate the leave_allocation + self.leave_allocation.action_approve() + + # Check for leave_allocation state + self.assertEqual(self.leave_allocation.state, "validate") + + def test_leave_request_state(self): + today = datetime.today() + + # Create leave request + leave = self.leave_request_model.create( + { + "name": "Test Leave Request", + "holiday_status_id": self.leave_allocation.holiday_status_id.id, + "date_from": today, + "date_to": today + timedelta(days=2), + "holiday_type": "employee", + "employee_id": self.test_employee_id.id, + } + ) + + # Check for leave state + self.assertEqual(leave.state, "confirm") + + # Self approve + leave.with_user(self.test_user_id).action_approve() + + # Check for leave state + self.assertEqual(leave.state, "validate")