[FIX] hr_holidays: correct name search in leave type

Currently, when requesting time off for multiple employees, the search for the leave type correspond to the search of the current user.

Steps to reproduce:
-------------------
* Go to the **Time Off** app
* Select **Configuration** > **Time Off Type**
* Create a new time off type
  * Approval: By Employee's Approver and Time Off Oficcer
  * Requires allocation: Yes
  * Employee Requests: Extra Days Requests Allowed
  * Approval: Approved by Time Off Officer
  * Notified Time off officer: Mitchell Admin
* Select **Management** > **Allocations**
* Create a new allocation
  * Employees: Mitchell Admin
  * Time off time: The one created previously
* Validate the allocation
* Select **Management** > **Time Off**
* create a new time off
  * Employees: Any Employee A & Employee B
  * Time off type:
  > Observation: The new time off time is present in the name search while both employees don't have any allocation for it.

Why the fix:
------------
The name search searches for time off type with
```
['|', ['requires_allocation', '=', 'no'], '&', ['has_valid_allocation', '=', True], '&', ['max_leaves', '>', '0'], '|', ['allows_negative', '=', True], '&', ['virtual_remaining_leaves', '>', 0], ['allows_negative', '=', False]]
```
By configuration, the time off has `requires_allocation = yes` therefore it shouldn't appear here and it does not -> ok

`has_valid_allocation` has a search method `_search_valid`
https://github.com/odoo/odoo/blob/bb0cb2896236ead6b474cd1b3a685ff447716b95/addons/hr_holidays/models/hr_leave_type.py#L109-L138

`max_leaves` has a search method `_search_max_leaves`
https://github.com/odoo/odoo/blob/bb0cb2896236ead6b474cd1b3a685ff447716b95/addons/hr_holidays/models/hr_leave_type.py#L165-L192

Both use the function `_get_contextual_employee` to make their search.
https://github.com/odoo/odoo/blob/bb0cb2896236ead6b474cd1b3a685ff447716b95/addons/hr_holidays/models/hr_employee.py#L386-L388

When there are more than one employee selected on the hr leave form, the context contains `employee_id: False`. Thus here we are making the search using the current user, which is Mitchell Admin.

The search shouldn't be made using the current user in this case since he doesn't correspond to any of the employees we added of the form.

opw-3816442

closes odoo/odoo#161713

Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
This commit is contained in:
Sarah Bellefroid
2024-04-26 17:17:53 +00:00
parent cd9c69bd7d
commit e15c7e5442
2 changed files with 39 additions and 1 deletions
+5 -1
View File
@@ -385,7 +385,11 @@ class HrEmployee(models.Model):
@api.model
def _get_contextual_employee(self):
ctx = self.env.context
return self.browse(ctx.get('employee_id') or ctx.get('default_employee_id')) or self.env.user.employee_id
if 'employee_id' in ctx:
return self.browse(ctx.get('employee_id'))
if 'default_employee_id' in ctx:
return self.browse(ctx.get('default_employee_id'))
return self.env.user.employee_id
def _get_consumed_leaves(self, leave_types, target_date=False, ignore_future=False):
employees = self or self._get_contextual_employee()
@@ -1168,3 +1168,37 @@ class TestLeaveRequests(TestHrHolidaysCommon):
leave_form.holiday_status_id = self.holidays_type_2
leave = leave_form.save()
self.assertEqual(leave.number_of_days, 1.0)
def test_filter_time_off_type_multiple_employees(self):
""" This test mimics the behavior of creating time off for multiple employees.
We check that the time off types that the user can select are correct.
In this example, we use a time off type that requires allocations.
Only the current user has an allocation for the time off type.
This time off type should not appear when multiple employees are select (user included or not).
"""
self.assertFalse(self.env['hr.leave.allocation'].search([['holiday_status_id', '=', self.holidays_type_2.id]]))
self.env.user.employee_id = self.employee_hruser_id
allocation = self.env['hr.leave.allocation'].create({
'holiday_type': 'employee',
'employee_id': self.employee_hruser_id,
'holiday_status_id': self.holidays_type_2.id,
'allocation_type': 'regular'
})
self.assertEqual(allocation.state, 'validate')
search_domain = ['|',
['requires_allocation', '=', 'no'],
'&',
['has_valid_allocation', '=', True],
'&',
['max_leaves', '>', '0'],
'|',
['allows_negative', '=', True],
'&',
['virtual_remaining_leaves', '>', 0],
['allows_negative', '=', False]]
search_result = self.env['hr.leave.type'].with_context(employee_id=False).name_search(args=search_domain)
self.assertFalse(self.holidays_type_2.id in [alloc_id for (alloc_id, _) in search_result])