From 43285ebc9644fb2937cb5f5aa9d19092eb1d2e57 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Fri, 2 Feb 2024 10:01:29 +0100 Subject: [PATCH] [FIX] hr_holidays: Compute resource_calendar_id even without work entries Part-of: odoo/odoo#152311 --- addons/hr_holidays/models/hr_leave.py | 20 ++++++++++++++++ .../hr_work_entry_holidays/models/hr_leave.py | 23 ------------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/addons/hr_holidays/models/hr_leave.py b/addons/hr_holidays/models/hr_leave.py index 99b1b12d2c1..4a94cbc75fe 100644 --- a/addons/hr_holidays/models/hr_leave.py +++ b/addons/hr_holidays/models/hr_leave.py @@ -363,6 +363,26 @@ class HolidaysRequest(models.Model): calendar = False if leave.holiday_type == 'employee': calendar = leave.employee_id.resource_calendar_id + # YTI: Crappy hack: Move this to a new dedicated hr_holidays_contract module + # We use the request dates to find the contracts, because date_from + # and date_to are not set yet at this point. Since these dates are + # used to get the contracts for which these leaves apply and + # contract start- and end-dates are just dates (and not datetimes) + # these dates are comparable. + if 'hr.contract' in self.env and leave.employee_id: + contracts = self.env['hr.contract'].search([ + '|', ('state', 'in', ['open', 'close']), + '&', ('state', '=', 'draft'), + ('kanban_state', '=', 'done'), + ('employee_id', '=', leave.employee_id.id), + ('date_start', '<=', leave.request_date_to), + '|', ('date_end', '=', False), + ('date_end', '>=', leave.request_date_from), + ]) + if contracts: + # If there are more than one contract they should all have the + # same calendar, otherwise a constraint is violated. + calendar = contracts[:1].resource_calendar_id elif leave.holiday_type == 'department': calendar = leave.department_id.company_id.resource_calendar_id elif leave.holiday_type == 'company': diff --git a/addons/hr_work_entry_holidays/models/hr_leave.py b/addons/hr_work_entry_holidays/models/hr_leave.py index 741d37313c5..c0276be6285 100644 --- a/addons/hr_work_entry_holidays/models/hr_leave.py +++ b/addons/hr_work_entry_holidays/models/hr_leave.py @@ -20,29 +20,6 @@ class HrLeaveType(models.Model): class HrLeave(models.Model): _inherit = 'hr.leave' - def _compute_resource_calendar_id(self): - super()._compute_resource_calendar_id() - for leave in self.filtered(lambda l: l.employee_id): - # We use the request dates to find the contracts, because date_from - # and date_to are not set yet at this point. Since these dates are - # used to get the contracts for which these leaves apply and - # contract start- and end-dates are just dates (and not datetimes) - # these dates are comparable. - if leave.employee_id: - contracts = self.env['hr.contract'].search([ - '|', ('state', 'in', ['open', 'close']), - '&', ('state', '=', 'draft'), - ('kanban_state', '=', 'done'), - ('employee_id', '=', leave.employee_id.id), - ('date_start', '<=', leave.request_date_to), - '|', ('date_end', '=', False), - ('date_end', '>=', leave.request_date_from), - ]) - if contracts: - # If there are more than one contract they should all have the - # same calendar, otherwise a constraint is violated. - leave.resource_calendar_id = contracts[:1].resource_calendar_id - def _prepare_resource_leave_vals(self): vals = super(HrLeave, self)._prepare_resource_leave_vals() vals['work_entry_type_id'] = self.holiday_status_id.work_entry_type_id.id