From 9d0c0a290b2dcfe29b19deb9e5cb2c000d128d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Fri, 5 Apr 2024 13:15:18 +0200 Subject: [PATCH] [FIX] hr_holidays_natural_period: Recalculate the duration value on creation It is necessary to recalculate the duration value in the create() method when it is done by UX. Fixes https://github.com/OCA/hr-holidays/issues/105 --- hr_holidays_natural_period/models/hr_leave.py | 14 ++++++++++++-- hr_holidays_natural_period/models/hr_leave_type.py | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/hr_holidays_natural_period/models/hr_leave.py b/hr_holidays_natural_period/models/hr_leave.py index 726f9a26..2d7fe792 100644 --- a/hr_holidays_natural_period/models/hr_leave.py +++ b/hr_holidays_natural_period/models/hr_leave.py @@ -1,7 +1,7 @@ -# Copyright 2020-2022 Tecnativa - Víctor Martínez +# Copyright 2020-2024 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import models +from odoo import api, models class HrLeave(models.Model): @@ -14,3 +14,13 @@ def _get_number_of_days(self, date_from, date_to, employee_id): return super(HrLeave, instance)._get_number_of_days( date_from, date_to, employee_id ) + + @api.model_create_multi + def create(self, vals_list): + """Only in UX an incorrect value is set, recalculate. + https://github.com/OCA/hr-holidays/issues/105.""" + res = super().create(vals_list) + res.filtered( + lambda x: x.holiday_status_id.request_unit == "natural_day" + )._compute_number_of_days() + return res diff --git a/hr_holidays_natural_period/models/hr_leave_type.py b/hr_holidays_natural_period/models/hr_leave_type.py index 269146dc..7c8178b0 100644 --- a/hr_holidays_natural_period/models/hr_leave_type.py +++ b/hr_holidays_natural_period/models/hr_leave_type.py @@ -16,8 +16,8 @@ def _get_employees_days_per_allocation(self, employee_ids, date=None): """We need to set request_unit as 'day' to avoid the calculations being done as hours. Related code: - hr_holidays/models/hr_leave_type.py#L313 - hr_holidays/models/hr_leave_type.py#L367 + hr_holidays/models/hr_leave_type.py#L326 + hr_holidays/models/hr_leave_type.py#L389 """ old_request_unit_data = {} for item in self.filtered(lambda x: x.request_unit == "natural_day"):