From 0f08587e6c003d3863cb27ecda8e600a1f70703c Mon Sep 17 00:00:00 2001 From: "Thomas Lefebvre (thle)" Date: Thu, 3 Aug 2023 09:00:23 +0000 Subject: [PATCH] [FIX] resource: manage two weeks calendar and company creation Steps to reproduce: ------------------- - Go to Working Time and modify a calendar using "Switch to 2 weeks calendar" (not the default company calendar); - In the Employees App settings, change the "Company Working Hours" to the edited calendar; - Create a new company. Issue: ------ We have the error: "Attendances can't overlap.". Cause: ------ When we create a company, we will use the `_default_get` method to get the default values for the `attendance_ids`. So we will copy the attendances from the current company, but we won't copy the `week_type` value. In addition, the `two_weeks_calendar` value will be `False`. As a result, overlaps will be checked as for a one-week calendar. This will trigger the error. Solution: --------- If you want to get the current company's attendances by default, make sure it uses a one-week calendar. If this is not the case, we take the default (hardcoded) attendances. Note: ----- Since the commit 292508e8a749bf32e40995454da6122cd3c1df77, it was no longer possible to modify a company's default calendar. It is reverted. With this FIX, this is now possible. opw-3446789 closes odoo/odoo#131872 X-original-commit: 22c12db1f009686fa4c3861c398ba178b2785ead Signed-off-by: Sofie Gvaladze (sgv) Signed-off-by: Thomas Lefebvre (thle) --- addons/resource/i18n/resource.pot | 7 ------- addons/resource/models/resource_calendar.py | 7 ++----- addons/test_resource/tests/common.py | 19 +++++++++++++++++++ addons/test_resource/tests/test_resource.py | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/addons/resource/i18n/resource.pot b/addons/resource/i18n/resource.pot index 87fafcd0229..8f17492dbc3 100644 --- a/addons/resource/i18n/resource.pot +++ b/addons/resource/i18n/resource.pot @@ -272,13 +272,6 @@ msgid "" "Time without removing it." msgstr "" -#. module: resource -#. odoo-python -#: code:addons/resource/models/resource_calendar.py:0 -#, python-format -msgid "Impossible to switch calendar type for the default company schedule." -msgstr "" - #. module: resource #. odoo-python #: code:addons/resource/models/resource_calendar.py:0 diff --git a/addons/resource/models/resource_calendar.py b/addons/resource/models/resource_calendar.py index 0e31849f657..8e4f1a20e0a 100644 --- a/addons/resource/models/resource_calendar.py +++ b/addons/resource/models/resource_calendar.py @@ -14,7 +14,7 @@ from pytz import timezone, utc from odoo import api, fields, models, _ from odoo.addons.base.models.res_partner import _tz_get -from odoo.exceptions import ValidationError, UserError +from odoo.exceptions import ValidationError from odoo.osv import expression from odoo.tools.float_utils import float_round @@ -45,7 +45,7 @@ class ResourceCalendar(models.Model): company_id = res.get('company_id', self.env.company.id) company = self.env['res.company'].browse(company_id) company_attendance_ids = company.resource_calendar_id.attendance_ids - if company_attendance_ids: + if not company.resource_calendar_id.two_weeks_calendar and company_attendance_ids: res['attendance_ids'] = [ (0, 0, { 'name': attendance.name, @@ -184,9 +184,6 @@ class ResourceCalendar(models.Model): return float_round(hour_count / float(number_of_days), precision_digits=2) def switch_calendar_type(self): - if self == self.env.company.resource_calendar_id: - raise UserError(_('Impossible to switch calendar type for the default company schedule.')) - if not self.two_weeks_calendar: self.attendance_ids.unlink() self.attendance_ids = [ diff --git a/addons/test_resource/tests/common.py b/addons/test_resource/tests/common.py index ee2dee6dff7..94fd46fa9e6 100644 --- a/addons/test_resource/tests/common.py +++ b/addons/test_resource/tests/common.py @@ -82,3 +82,22 @@ class TestResourceCommon(TransactionCase): 'name': 'Paul', 'resource_calendar_id': cls.calendar_paul.id, }) + + cls.two_weeks_resource = cls._define_calendar_2_weeks( + 'Two weeks resource', + [ + (0, 0, 0, '0', 'line_section', 0), + (8, 16, 0, '0', False, 1), + (8, 16, 1, '0', False, 2), + (8, 16, 2, '0', False, 3), + (8, 16, 3, '0', False, 4), + (8, 16, 4, '0', False, 5), + (0, 0, 0, '1', 'line_section', 10), + (8, 16, 0, '1', False, 11), + (8, 16, 1, '1', False, 12), + (8, 16, 2, '1', False, 13), + (8, 16, 3, '1', False, 14), + (8, 16, 4, '1', False, 15) + ], + 'Europe/Brussels' + ) diff --git a/addons/test_resource/tests/test_resource.py b/addons/test_resource/tests/test_resource.py index 03cb1e1f311..ad33358d5a6 100644 --- a/addons/test_resource/tests/test_resource.py +++ b/addons/test_resource/tests/test_resource.py @@ -1320,3 +1320,21 @@ class TestResource(TestResourceCommon): self.assertEqual(31, sum_work_intervals_jules, "Sum of the work intervals for the calendar of jules should be Wodd:15h+Wpair:16h = 31h") sum_work_intervals_patel = sum_intervals(calendars_intervals[self.calendar_patel.id]) self.assertEqual(49, sum_work_intervals_patel, "Sum of the work intervals for the calendar of patel should be 14+35h = 49h") + + def test_switch_two_weeks_resource(self): + """ + Check that it is possible to switch the company's default calendar + """ + self.env.company.resource_calendar_id = self.two_weeks_resource + company_resource = self.env.company.resource_calendar_id + # Switch two times to be sure to test both cases + company_resource.switch_calendar_type() + company_resource.switch_calendar_type() + + def test_create_company_using_two_weeks_resource(self): + """ + Check that we can create a new company + if the default company calendar is two weeks + """ + self.env.company.resource_calendar_id = self.two_weeks_resource + self.env['res.company'].create({'name': 'New Company'})