diff --git a/addons/hr_holidays/models/hr_leave_allocation.py b/addons/hr_holidays/models/hr_leave_allocation.py index c84bea79989..b5171fe2d8f 100644 --- a/addons/hr_holidays/models/hr_leave_allocation.py +++ b/addons/hr_holidays/models/hr_leave_allocation.py @@ -806,16 +806,16 @@ class HolidaysAllocation(models.Model): # before every run, as if it was run from date_from, after an optional change in the allocation value # the user can simply confirm and validate the allocation. The record is in correct state for the next # call of the cron job. - @api.onchange('date_from', 'accrual_plan_id') + @api.onchange('date_from', 'accrual_plan_id', 'date_to') def _onchange_date_from(self): - now = date.today() if self.allocation_type != 'accrual' or self.state == 'validate' or not self.accrual_plan_id\ - or not self.employee_id or not (not self.date_to or self.date_to > now): + or not self.employee_id: return self.lastcall = self.date_from self.nextcall = False self.number_of_days_display = 0.0 - self._process_accrual_plans() + date_to = min(self.date_to, date.today()) if self.date_to else False + self._process_accrual_plans(date_to) # ------------------------------------------------------------ # Activity methods diff --git a/addons/hr_holidays/tests/test_accrual_allocations.py b/addons/hr_holidays/tests/test_accrual_allocations.py index 15c4bb0225c..4affc862dac 100644 --- a/addons/hr_holidays/tests/test_accrual_allocations.py +++ b/addons/hr_holidays/tests/test_accrual_allocations.py @@ -1542,3 +1542,82 @@ class TestAccrualAllocations(TestHrHolidaysCommon): accrual_allocation._update_accrual() self.assertEqual(accrual_allocation.number_of_days, 31.0, "the amount shouldn't have changed after running the cron") + + def test_accrual_creation_for_history(self): + accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).create({ + 'name': 'Monthly accrual', + 'carryover_date': 'year_start', + 'accrued_gain_time': 'end', + 'level_ids': [(0, 0, { + 'added_value_type': 'day', + 'start_count': 0, + 'start_type': 'day', + 'added_value': 1, + 'frequency': 'monthly', + 'first_day_display': 'last', + 'cap_accrued_time': False, + 'action_with_unused_accruals': 'lost', + })], + }) + with freeze_time('2024-03-02'): + accrual_allocation = self.env['hr.leave.allocation'].new({ + 'name': 'History allocation', + 'holiday_status_id': self.leave_type.id, + 'date_from': '2024-03-01', + 'employee_id': self.employee_emp.id, + 'allocation_type': 'accrual', + 'accrual_plan_id': accrual_plan.id, + }) + # As the duration is set to an onchange, we need to force that onchange to run + accrual_allocation._onchange_date_from() + self.assertAlmostEqual(accrual_allocation.number_of_days, 0, places=0) + + # Yearly Report lost + accrual_allocation.write({'date_from': '2022-01-01'}) + accrual_allocation._onchange_date_from() + self.assertAlmostEqual(accrual_allocation.number_of_days, 2, places=0) + + # Update date_to + accrual_allocation.write({'date_to': '2022-12-31'}) + accrual_allocation._onchange_date_from() + self.assertAlmostEqual(accrual_allocation.number_of_days, 12, places=0) + + def test_accrual_with_report_creation_for_history(self): + accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).create({ + 'name': 'Monthly accrual', + 'carryover_date': 'year_start', + 'accrued_gain_time': 'end', + 'level_ids': [(0, 0, { + 'added_value_type': 'day', + 'start_count': 0, + 'start_type': 'day', + 'added_value': 1, + 'frequency': 'monthly', + 'first_day_display': 'last', + 'cap_accrued_time': False, + 'action_with_unused_accruals': 'maximum', + 'postpone_max_days': 5 + })], + }) + with freeze_time('2024-03-02'): + accrual_allocation = self.env['hr.leave.allocation'].new({ + 'name': 'History allocation', + 'holiday_status_id': self.leave_type.id, + 'date_from': '2024-03-01', + 'employee_id': self.employee_emp.id, + 'allocation_type': 'accrual', + 'accrual_plan_id': accrual_plan.id, + }) + # As the duration is set to an onchange, we need to force that onchange to run + accrual_allocation._onchange_date_from() + self.assertAlmostEqual(accrual_allocation.number_of_days, 0, places=0) + + # Yearly Report capped to 5 after 2022 and after 2023 + accrual_allocation.write({'date_from': '2022-01-01'}) + accrual_allocation._onchange_date_from() + self.assertAlmostEqual(accrual_allocation.number_of_days, 7, places=0) + + # Update date_to + accrual_allocation.write({'date_to': '2022-12-31'}) + accrual_allocation._onchange_date_from() + self.assertAlmostEqual(accrual_allocation.number_of_days, 12, places=0)