From c962fe4e19911cbe9557255481234ce2acc7c532 Mon Sep 17 00:00:00 2001 From: alt-odoo Date: Mon, 9 May 2022 08:47:17 +0000 Subject: [PATCH] [FIX] hr_holidays: prevent deletion of allocation with taken leaves In case we try to delete an allocation, we should add a check if there are already taken leaves, otherwise it could lead to an incoherent situation with validated leaves without allocation while their associated time off type is requiring allocation. closes odoo/odoo#90932 X-original-commit: 45567dddacae82f9b4e985703cbb9e7736e9beb0 Signed-off-by: Kevin Baptiste Signed-off-by: Alex Thuyls (alt) --- addons/hr_holidays/i18n/hr_holidays.pot | 6 ++++++ addons/hr_holidays/models/hr_leave_allocation.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/addons/hr_holidays/i18n/hr_holidays.pot b/addons/hr_holidays/i18n/hr_holidays.pot index 3c743b36c66..f11266a6272 100644 --- a/addons/hr_holidays/i18n/hr_holidays.pot +++ b/addons/hr_holidays/i18n/hr_holidays.pot @@ -4132,6 +4132,12 @@ msgstr "" msgid "You cannot delete an allocation request which is in %s state." msgstr "" +#. module: hr_holidays +#: code:addons/hr_holidays/models/hr_leave_allocation.py:0 +#, python-format +msgid "You cannot delete an allocation request which has some validated leaves." +msgstr "" + #. module: hr_holidays #: code:addons/hr_holidays/models/hr_leave.py:0 #, python-format diff --git a/addons/hr_holidays/models/hr_leave_allocation.py b/addons/hr_holidays/models/hr_leave_allocation.py index 51404fdb5d3..42c639f54b0 100644 --- a/addons/hr_holidays/models/hr_leave_allocation.py +++ b/addons/hr_holidays/models/hr_leave_allocation.py @@ -583,6 +583,11 @@ class HolidaysAllocation(models.Model): for holiday in self.filtered(lambda holiday: holiday.state not in ['draft', 'cancel', 'confirm']): raise UserError(_('You cannot delete an allocation request which is in %s state.') % (state_description_values.get(holiday.state),)) + @api.ondelete(at_uninstall=False) + def _unlink_if_no_leaves(self): + if any(allocation.holiday_status_id.requires_allocation == 'yes' and allocation.leaves_taken > 0 for allocation in self): + raise UserError(_('You cannot delete an allocation request which has some validated leaves.')) + def _get_mail_redirect_suggested_company(self): return self.holiday_status_id.company_id