From f53800a17dd1caf9197d6b30f40c8ef5598ddca8 Mon Sep 17 00:00:00 2001 From: dasz Date: Fri, 3 Mar 2023 11:00:43 +0000 Subject: [PATCH] [IMP] hr_holidays: ignore future leaves in accrual computation Current behavior: An accrual uses all planned leaves to calculate how many days it can give, even if the leaves are planned in the future and could be cancelled. Desired behavior: An accrual should only take into account leaves that have already been taken or already started, and ignore future leaves. After this commit: New context key 'ignore_future' for method _get_employees_days_per_allocation decides if future leaves should be taken into account or not. Some use cases need to take into account future leaves, others don't. task 3207843 closes odoo/odoo#120456 X-original-commit: d917a406fc7933af852ad5338533b93fc3fd2218 Related: odoo/enterprise#40678 Signed-off-by: Kevin Baptiste --- addons/hr_holidays/models/hr_leave_allocation.py | 2 +- addons/hr_holidays/models/hr_leave_type.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/addons/hr_holidays/models/hr_leave_allocation.py b/addons/hr_holidays/models/hr_leave_allocation.py index 262b6f746e9..39ffced65b9 100644 --- a/addons/hr_holidays/models/hr_leave_allocation.py +++ b/addons/hr_holidays/models/hr_leave_allocation.py @@ -199,7 +199,7 @@ class HolidaysAllocation(models.Model): @api.depends('employee_id', 'holiday_status_id', 'taken_leave_ids.number_of_days', 'taken_leave_ids.state') def _compute_leaves(self): - employee_days_per_allocation = self.holiday_status_id._get_employees_days_per_allocation(self.employee_id.ids) + employee_days_per_allocation = self.holiday_status_id.with_context(ignore_future=True)._get_employees_days_per_allocation(self.employee_id.ids) for allocation in self: allocation.max_leaves = allocation.number_of_hours_display if allocation.type_request_unit == 'hour' else allocation.number_of_days allocation.leaves_taken = employee_days_per_allocation[allocation.employee_id.id][allocation.holiday_status_id][allocation]['leaves_taken'] diff --git a/addons/hr_holidays/models/hr_leave_type.py b/addons/hr_holidays/models/hr_leave_type.py index 77461e1bbba..846cd0f6589 100644 --- a/addons/hr_holidays/models/hr_leave_type.py +++ b/addons/hr_holidays/models/hr_leave_type.py @@ -208,11 +208,17 @@ class HolidaysType(models.Model): return [('id', 'in', valid_leave_types.ids)] def _get_employees_days_per_allocation(self, employee_ids, date=None): - leaves = self.env['hr.leave'].search([ + if not date: + date = fields.Date.to_date(self.env.context.get('default_date_from')) or fields.Date.context_today(self) + + leaves_domain = [ ('employee_id', 'in', employee_ids), ('state', 'in', ['confirm', 'validate1', 'validate']), ('holiday_status_id', 'in', self.ids) - ]) + ] + if self.env.context.get("ignore_future"): + leaves_domain.append(('date_from', '<=', date)) + leaves = self.env['hr.leave'].search(leaves_domain) allocations = self.env['hr.leave.allocation'].with_context(active_test=False).search([ ('employee_id', 'in', employee_ids), @@ -220,9 +226,6 @@ class HolidaysType(models.Model): ('holiday_status_id', 'in', self.ids), ]) - if not date: - date = fields.Date.to_date(self.env.context.get('default_date_from')) or fields.Date.context_today(self) - # The allocation_employees dictionary groups the allocations based on the employee and the holiday type # The structure is the following: # - KEYS: