[FIX] hr_holidays: respect unit in future accrual calculations

Issue:
- When checking future accruals in the Time Off dashboard, the Balance
 view doesn't update correctly. For example, with an accrual plan of 1h
 per month starting on 30/11/23, the balance in December should show 2h
 but incorrectly shows only 1.12h.

-This happens because the '_get_future_leaves_on' method always returns
 values in days, ignoring the 'type_request_unit' of the allocation.

Steps to Reproduce:
- In the time-off app set an accrual plan: 1h per month, accrued on the
 first day of the month.
- add a new allocation to Mitchell Admin with that plan
- Notice that In the dashboard the balance is 1h
- Change the date to next month, new amount is 1.12h, in stead of 2

Solution:
- Added a check to correctly calculate future accruals in hours when
 type_request_unit is 'hour', ensuring accurate hour-based balances.

opw-3685077

closes odoo/odoo#152870

Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
This commit is contained in:
kdeb@odoo.com
2024-02-12 20:24:05 +00:00
parent d5232aebf9
commit 6ba48be96a
2 changed files with 39 additions and 0 deletions
@@ -562,6 +562,8 @@ class HolidaysAllocation(models.Model):
fake_allocation = self.env['hr.leave.allocation'].new(origin=self)
fake_allocation.sudo()._process_accrual_plans(accrual_date, log=False)
if self.type_request_unit in ['hour']:
return float_round(fake_allocation.number_of_hours_display - self.number_of_hours_display, precision_digits=2)
return round((fake_allocation.number_of_days - self.number_of_days), 2)
####################################################
@@ -1446,3 +1446,40 @@ class TestAccrualAllocations(TestHrHolidaysCommon):
# The amount being already computed, the amount should stay the same after the cron
# running on the same day.
self.assertAlmostEqual(accrual_allocation.number_of_days, 34.0, places=0)
def test_future_accural_time(self):
leave_type = self.env['hr.leave.type'].create({
'name': 'Test Leave Type',
'time_type': 'leave',
'requires_allocation': 'yes',
'allocation_validation_type': 'no',
'request_unit': 'hour',
})
with freeze_time("2023-12-31"):
accrual_plan = self.env['hr.leave.accrual.plan'].create({
'name': 'Accrual Plan For Test',
'is_based_on_worked_time': False,
'accrued_gain_time': 'end',
'carryover_date': 'year_start',
'level_ids': [(0, 0, {
'start_count': 1,
'start_type': 'day',
'added_value': 1,
'added_value_type': 'hour',
'frequency': 'monthly',
'cap_accrued_time': True,
'maximum_leave': 100,
})],
})
allocation = self.env['hr.leave.allocation'].create({
'name': 'Accrual allocation for employee',
'accrual_plan_id': accrual_plan.id,
'employee_id': self.employee_emp.id,
'holiday_status_id': leave_type.id,
'number_of_days': 0.125,
'allocation_type': 'accrual',
'holiday_type': 'employee',
})
allocation.action_validate()
allocation_data = leave_type.get_allocation_data(self.employee_emp, datetime.date(2024, 2, 1))
self.assertEqual(allocation_data[self.employee_emp][0][1]['virtual_remaining_leaves'], 2)