[FIX] hr_holidays: ensure consistency with cap accrued time

Steps to reproduce:
-------------------
- create a time off accrual plan with:
    - cap accrued time equals to `True`
    - maximum leave equals to `0 days`
- create an allocation with this accrual plan
- run the scheduled action "Accrual Time Off: Updates the number of time off"
    (make sure you have the right "lastcall" and "nextcall")

Issue:
------
The number of days of the allocation is set to zero.
This scenario makes no sense from a business point of view.
In effect, we are blocking the accumulation of allocations at zero.
The result is that there will never be any days allocated to these allocations.

Solution:
---------
Add a constraint to force a maximum leave number
when cap accrued time is set.

opw-3847408

closes odoo/odoo#160626

Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
This commit is contained in:
Thomas Lefebvre (thle)
2024-04-09 15:06:48 +00:00
parent bc0e9b3804
commit 47030fd605
3 changed files with 39 additions and 0 deletions
+8
View File
@@ -4441,6 +4441,14 @@ msgid ""
" manager"
msgstr ""
#. module: hr_holidays
#. odoo-python
#: code:addons/hr_holidays/models/hr_leave_accrual_plan_level.py:0
#, python-format
msgid ""
"You cannot have a cap on accrued time without setting a maximum amount."
msgstr ""
#. module: hr_holidays
#. odoo-python
#: code:addons/hr_holidays/models/hr_leave_allocation.py:0
@@ -4,6 +4,7 @@
from dateutil.relativedelta import relativedelta
from odoo import _, api, fields, models
from odoo.exceptions import UserError
DAYS = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
@@ -134,6 +135,12 @@ class AccrualPlanLevel(models.Model):
('added_value_greater_than_zero', 'CHECK(added_value > 0)', 'You must give a rate greater than 0 in accrual plan levels.')
]
@api.constrains('maximum_leave')
def _check_maximum_leave(self):
for level in self:
if level.cap_accrued_time and level.maximum_leave < 1:
raise UserError(_("You cannot have a cap on accrued time without setting a maximum amount."))
@api.depends('start_count', 'start_type')
def _compute_sequence(self):
# Not 100% accurate because of odd months/years, but good enough
@@ -5,6 +5,7 @@ from freezegun import freeze_time
from dateutil.relativedelta import relativedelta
from odoo import Command
from odoo.exceptions import UserError
from odoo.tests import tagged
from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon
@@ -41,6 +42,29 @@ class TestAccrualAllocations(TestHrHolidaysCommon):
WHERE id = %s
""" % (date, allocation_id))
def test_consistency_between_cap_accrued_time_and_maximum_leave(self):
accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).create({
'name': 'Accrual Plan For Test',
'level_ids': [(0, 0, {
'start_count': 1,
'start_type': 'day',
'added_value': 1,
'added_value_type': 'day',
'frequency': 'hourly',
'cap_accrued_time': True,
'maximum_leave': 10000
})],
})
level = accrual_plan.level_ids
level.maximum_leave = 10
self.assertEqual(accrual_plan.level_ids.maximum_leave, 10)
with self.assertRaises(UserError):
level.maximum_leave = 0
level.cap_accrued_time = False
self.assertEqual(accrual_plan.level_ids.maximum_leave, 0)
def test_frequency_hourly_calendar(self):
with freeze_time("2017-12-5"):
accrual_plan = self.env['hr.leave.accrual.plan'].with_context(tracking_disable=True).create({