From 3f5e3b0513f6a7e4b7d87dd685c3b60e3acf97f3 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Wed, 8 Jun 2022 07:34:47 +0000 Subject: [PATCH] [FIX] hr_work_entry_holidays: Allow overlapping leaves if same schedule Purpose ======= We currently prevent leaves overlapping several contracts to avoid messing with the generated resource.calendar.leave. In case the contracts are on the same resource calendar, this shouldn't be an issue actually. Taskid: 2836901 X-original-commit: 4e87c30bb136083153091e67e6dd9d9f64fa94c2 Part-of: odoo/odoo#94885 --- .../i18n/hr_work_entry_holidays.pot | 2 +- .../hr_work_entry_holidays/models/hr_leave.py | 6 ++--- .../tests/test_multi_contract.py | 22 ++++++++++++++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/addons/hr_work_entry_holidays/i18n/hr_work_entry_holidays.pot b/addons/hr_work_entry_holidays/i18n/hr_work_entry_holidays.pot index 295680027e0..f93a2313598 100644 --- a/addons/hr_work_entry_holidays/i18n/hr_work_entry_holidays.pot +++ b/addons/hr_work_entry_holidays/i18n/hr_work_entry_holidays.pot @@ -24,7 +24,7 @@ msgstr "" #. module: hr_work_entry_holidays #: code:addons/hr_work_entry_holidays/models/hr_leave.py:0 #, python-format -msgid "A leave cannot be set across multiple contracts." +msgid "A leave cannot be set across multiple contracts with different working schedules." msgstr "" #. module: hr_work_entry_holidays diff --git a/addons/hr_work_entry_holidays/models/hr_leave.py b/addons/hr_work_entry_holidays/models/hr_leave.py index 94c9cd77f4d..8614901de4a 100644 --- a/addons/hr_work_entry_holidays/models/hr_leave.py +++ b/addons/hr_work_entry_holidays/models/hr_leave.py @@ -69,11 +69,11 @@ class HrLeave(models.Model): ('date_end', '=', False), ('state', '!=', 'close') ] - nbr_contracts = self.env['hr.contract'].sudo().search_count(domain) - if nbr_contracts > 1: + contracts = self.env['hr.contract'].sudo().search(domain) + if len(contracts.resource_calendar_id) > 1: contracts = self.env['hr.contract'].sudo().search(domain) raise ValidationError( - _('A leave cannot be set across multiple contracts.') + '\n%s\n%s' % ( + _('A leave cannot be set across multiple contracts with different working schedules.') + '\n%s\n%s' % ( ', '.join(contracts.mapped('name')), holiday.display_name)) diff --git a/addons/hr_work_entry_holidays/tests/test_multi_contract.py b/addons/hr_work_entry_holidays/tests/test_multi_contract.py index aa9cffb285b..8ffd8caa0d2 100644 --- a/addons/hr_work_entry_holidays/tests/test_multi_contract.py +++ b/addons/hr_work_entry_holidays/tests/test_multi_contract.py @@ -1,11 +1,13 @@ # # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from datetime import datetime +from datetime import datetime, date from odoo.exceptions import ValidationError +from odoo.tests import tagged from odoo.addons.hr_work_entry_holidays.tests.common import TestWorkEntryHolidaysBase +@tagged('work_entry_multi_contract') class TestWorkEntryHolidaysMultiContract(TestWorkEntryHolidaysBase): @classmethod @@ -118,3 +120,21 @@ class TestWorkEntryHolidaysMultiContract(TestWorkEntryHolidaysBase): leave._compute_number_of_hours_display() self.assertEqual(leave.number_of_hours_display, 14, "It should count hours according to the future contract.") + + def test_leave_multi_contracts_same_schedule(self): + # Allow leaves overlapping multiple contracts if same + # resource calendar + leave = self.create_leave(datetime(2022, 6, 1, 7, 0, 0), datetime(2022, 6, 30, 18, 0, 0)) + leave.action_approve() + self.contract_cdi.date_end = date(2022, 6, 15) + + new_contract_cdi = self.env['hr.contract'].create({ + 'date_start': date(2022, 6, 16), + 'name': 'New Contract for Jules', + 'resource_calendar_id': self.calendar_35h.id, + 'wage': 5000.0, + 'employee_id': self.jules_emp.id, + 'state': 'draft', + 'kanban_state': 'normal', + }) + new_contract_cdi.state = 'open'