diff --git a/addons/hr_holidays/models/hr_leave_type.py b/addons/hr_holidays/models/hr_leave_type.py index 9a1ef169a07b958337d051bb97fbb7376a89dd45..7c53682c646e03fea2846694fb0e50be8a014608 100644 --- a/addons/hr_holidays/models/hr_leave_type.py +++ b/addons/hr_holidays/models/hr_leave_type.py @@ -338,40 +338,42 @@ class HolidaysType(models.Model): days_consumed[False]['leaves_taken'] += leave_duration # Future available leaves + future_allocations_date_from = fields.datetime.combine(date, time.min) + future_allocations_date_to = fields.datetime.combine(date, time.max) + timedelta(days=5*365) for employee_id, allocation_intervals_by_status in allocation_employees.items(): + employee = self.env['hr.employee'].browse(employee_id) for holiday_status_id, intervals in allocation_intervals_by_status.items(): if not intervals: continue future_allocation_intervals = intervals & Intervals([( - fields.datetime.combine(date, time.min), - fields.datetime.combine(date, time.max) + timedelta(days=5*365), + future_allocations_date_from, + future_allocations_date_to, self.env['hr.leave'])]) search_date = date - for future_allocation_interval in future_allocation_intervals._items: - if future_allocation_interval[0].date() > search_date: + for interval_from, interval_to, interval_allocations in future_allocation_intervals._items: + if interval_from.date() > search_date: continue - employee_quantity_available = future_allocation_interval[2].employee_id._get_work_days_data_batch( - future_allocation_interval[0], - future_allocation_interval[1], - compute_leaves=False, - domain=company_domain)[employee_id] - for allocation in future_allocation_interval[2]: - if not allocation.active or allocation.date_from > search_date: + interval_allocations = interval_allocations.filtered('active') + if not interval_allocations: + continue + # If no end date to the allocation, consider the number of days remaining as infinite + employee_quantity_available = ( + employee._get_work_days_data_batch(interval_from, interval_to, compute_leaves=False, domain=company_domain)[employee_id] + if interval_to != future_allocations_date_to + else {'days': float('inf'), 'hours': float('inf')} + ) + for allocation in interval_allocations: + if allocation.date_from > search_date: continue days_consumed = allocations_days_consumed[employee_id][holiday_status_id][allocation] - if future_allocation_interval[1] != fields.datetime.combine(date, time.max) + timedelta(days=5*365): - quantity_available = employee_quantity_available - else: - # If no end date to the allocation, consider the number of days remaining as infinite - quantity_available = {'days': float('inf'), 'hours': float('inf')} if allocation.type_request_unit in ['day', 'half_day']: - quantity_available = quantity_available['days'] + quantity_available = employee_quantity_available['days'] remaining_days_allocation = (allocation.number_of_days - days_consumed['virtual_leaves_taken']) else: - quantity_available = quantity_available['hours'] + quantity_available = employee_quantity_available['hours'] remaining_days_allocation = (allocation.number_of_hours_display - days_consumed['virtual_leaves_taken']) if quantity_available <= remaining_days_allocation: - search_date = future_allocation_interval[1].date() + timedelta(days=1) + search_date = interval_to.date() + timedelta(days=1) days_consumed['virtual_remaining_leaves'] += min(quantity_available, remaining_days_allocation) days_consumed['max_leaves'] = allocation.number_of_days if allocation.type_request_unit in ['day', 'half_day'] else allocation.number_of_hours_display days_consumed['remaining_leaves'] = days_consumed['max_leaves'] - days_consumed['leaves_taken']