From d0363dd475b7a359b2eb91c45b6d7b7f939d8dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9lanie?= Date: Thu, 30 Nov 2023 14:52:59 +0100 Subject: [PATCH] [FIX] hr_holidays : fix duplicate allocation for batch allocation STEP TO REPRODUCE : 1. create a time off type with alloation_validation_type == 'no' 2. create a batch allocation (with company, department, employee tag or employee mode but with some employees) 3. records will be duplicated Expected behaviour : no duplication task : 3615657 Part-of: odoo/odoo#144394 --- .../hr_holidays/models/hr_leave_allocation.py | 14 +-- addons/hr_holidays/tests/__init__.py | 1 + addons/hr_holidays/tests/test_allocations.py | 91 +++++++++++++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 addons/hr_holidays/tests/test_allocations.py diff --git a/addons/hr_holidays/models/hr_leave_allocation.py b/addons/hr_holidays/models/hr_leave_allocation.py index 991ae1a6efe..3507d91c733 100644 --- a/addons/hr_holidays/models/hr_leave_allocation.py +++ b/addons/hr_holidays/models/hr_leave_allocation.py @@ -708,12 +708,14 @@ class HolidaysAllocation(models.Model): } for employee in employees] def action_validate(self): - self.write({ - 'state': 'validate', - 'approver_id': self.env.user.employee_id.id - }) - self._action_validate_create_childs() - self.activity_update() + to_validate = self.filtered(lambda alloc: alloc.state != 'validate') + if to_validate: + to_validate.write({ + 'state': 'validate', + 'approver_id': self.env.user.employee_id.id + }) + to_validate._action_validate_create_childs() + to_validate.activity_update() return True def _action_validate_create_childs(self): diff --git a/addons/hr_holidays/tests/__init__.py b/addons/hr_holidays/tests/__init__.py index 8b1189393be..8a8765d5b0d 100644 --- a/addons/hr_holidays/tests/__init__.py +++ b/addons/hr_holidays/tests/__init__.py @@ -20,3 +20,4 @@ from . import test_uninstall from . import test_holidays_calendar from . import test_negative from . import test_past_accruals +from . import test_allocations diff --git a/addons/hr_holidays/tests/test_allocations.py b/addons/hr_holidays/tests/test_allocations.py new file mode 100644 index 00000000000..1ffec87901f --- /dev/null +++ b/addons/hr_holidays/tests/test_allocations.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo.tests import tagged + +from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon + + +@tagged('allocation') +class TestAllocations(TestHrHolidaysCommon): + @classmethod + def setUpClass(cls): + super(TestAllocations, cls).setUpClass() + cls.leave_type = cls.env['hr.leave.type'].create({ + 'name': 'Time Off with no validation for approval', + 'time_type': 'leave', + 'requires_allocation': 'yes', + 'allocation_validation_type': 'no', + }) + cls.department = cls.env['hr.department'].create({ + 'name': 'Test Department', + }) + cls.category_tag = cls.env['hr.employee.category'].create({ + 'name': 'Test category' + }) + cls.employee = cls.env['hr.employee'].create({ + 'name': 'My Employee', + 'company_id': cls.company.id, + 'department_id': cls.department.id, + 'category_ids': [(4, cls.category_tag.id)], + }) + + def test_allocation_whole_company(self): + company_allocation = self.env['hr.leave.allocation'].create({ + 'name': 'Bank Holiday', + 'holiday_type': 'company', + 'mode_company_id': self.company.id, + 'holiday_status_id': self.leave_type.id, + 'number_of_days': 2, + 'allocation_type': 'regular', + }) + + company_allocation.action_validate() + + num_of_allocations = self.env['hr.leave.allocation'].search_count([('employee_id', '=', self.employee.id), ('multi_employee', '=', False)]) + self.assertEqual(num_of_allocations, 1) + + def test_allocation_multi_employee(self): + employee_allocation = self.env['hr.leave.allocation'].create({ + 'name': 'Bank Holiday', + 'holiday_type': 'employee', + 'employee_ids': [(4, self.employee.id), (4, self.employee_emp.id)], + 'employee_id': self.employee.id, + 'holiday_status_id': self.leave_type.id, + 'number_of_days': 2, + 'allocation_type': 'regular', + }) + + employee_allocation.action_validate() + + num_of_allocations = self.env['hr.leave.allocation'].search_count([('employee_id', '=', self.employee.id), ('multi_employee', '=', False), ('parent_id', '!=', False)]) + self.assertEqual(num_of_allocations, 1) + + def test_allocation_department(self): + department_allocation = self.env['hr.leave.allocation'].create({ + 'name': 'Bank Holiday', + 'holiday_type': 'department', + 'department_id': self.department.id, + 'holiday_status_id': self.leave_type.id, + 'number_of_days': 2, + 'allocation_type': 'regular', + }) + + department_allocation.action_validate() + + num_of_allocations = self.env['hr.leave.allocation'].search_count([('employee_id', '=', self.employee.id), ('multi_employee', '=', False)]) + self.assertEqual(num_of_allocations, 1) + + def test_allocation_category(self): + category_allocation = self.env['hr.leave.allocation'].create({ + 'name': 'Bank Holiday', + 'holiday_type': 'category', + 'category_id': self.category_tag.id, + 'holiday_status_id': self.leave_type.id, + 'number_of_days': 2, + 'allocation_type': 'regular', + }) + + category_allocation.action_validate() + + num_of_allocations = self.env['hr.leave.allocation'].search_count([('employee_id', '=', self.employee.id), ('multi_employee', '=', False)]) + self.assertEqual(num_of_allocations, 1)