From 4f35751bb0afa3b88435d26fc7b24f62fa3d3eae Mon Sep 17 00:00:00 2001 From: William Braeckman Date: Thu, 4 Aug 2022 09:56:58 +0000 Subject: [PATCH] [FIX] hr_holidays: fix accrual level maximum leaves A test introduced with https://github.com/odoo/odoo/pull/96432 revealed an issue with the level limits for the accrual plans. The only applied limit was the one of the current level at the end of the run, however if for whatever reason multiple levels had to be processed at once (if the database was shut down for a long period of time for example), the individual levels would not apply any limit logic to their behaviour which meant that you could have a different behaviour between running the cron each day and running it after a long period. This commit aims to fix that issue by applying the said limit. OPW-2868297 closes odoo/odoo#97540 X-original-commit: 09fa1020f42d641fc0011d482cdd08aea4f4085a Signed-off-by: William Braeckman (wbr) Signed-off-by: Kevin Baptiste --- addons/hr_holidays/models/hr_leave_allocation.py | 2 ++ addons/hr_holidays/tests/test_accrual_allocations.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/addons/hr_holidays/models/hr_leave_allocation.py b/addons/hr_holidays/models/hr_leave_allocation.py index 9467d68584f..623e9e18915 100644 --- a/addons/hr_holidays/models/hr_leave_allocation.py +++ b/addons/hr_holidays/models/hr_leave_allocation.py @@ -442,6 +442,8 @@ class HolidaysAllocation(models.Model): gained_days = allocation._process_accrual_plan_level( current_level, period_start, allocation.lastcall, period_end, allocation.nextcall) days_added_per_level[current_level] += gained_days + if current_level.maximum_leave > 0 and sum(days_added_per_level.values()) > current_level.maximum_leave: + days_added_per_level[current_level] -= sum(days_added_per_level.values()) - current_level.maximum_leave # We have to check for end of year actions if it is within our period # since we can create retroactive allocations. if allocation.lastcall.year < allocation.nextcall.year and\ diff --git a/addons/hr_holidays/tests/test_accrual_allocations.py b/addons/hr_holidays/tests/test_accrual_allocations.py index 799270bf5ba..03caaf855c2 100644 --- a/addons/hr_holidays/tests/test_accrual_allocations.py +++ b/addons/hr_holidays/tests/test_accrual_allocations.py @@ -686,7 +686,11 @@ class TestAccrualAllocations(TestHrHolidaysCommon): allocation.action_validate() with freeze_time('2022-7-20'): allocation._update_accrual() - self.assertEqual(allocation.number_of_days, 10) + # The first level gives 3 days + # The second level could give 6 days but since the first level was already giving + # 3 days, the second level gives 3 days to reach the second level's limit. + # The third level gives 1 day since it only counts for one iteration. + self.assertEqual(allocation.number_of_days, 7) def test_accrual_lost_previous_days(self): # Test that when an allocation with two levels is made and that the first level has it's action