From e448962785ecb4354ea034b1e86b4e9df673ca00 Mon Sep 17 00:00:00 2001 From: "Thomas Lefebvre (thle)" Date: Wed, 1 Feb 2023 08:37:57 +0000 Subject: [PATCH] [FIX] resource: prevent week switch for default schedule Steps to reproduce: In Working Times, click on "SWITCH TO 2 WEEKS CALENDAR" for the default calendar used by the company. Issue: A ValidationError appears: 'Attendances can't overlap.' Cause: To create a two-week schedule, by default, we will use attendances provided for the company's default schedule. When we want to switch from a one-week schedule to a two-week schedule, we first delete the attendances from the schedule to be modified. However, if this schedule is the company's default schedule, it will no longer have the default attendances that we must use to build the two-week schedule. So we end up with the two "fictitious" attendances that are used to delimit the two weeks. With only these two attendances, the constraint of not having two overlapping attendances is not respected (because the two attendances created will be modified to belong to the same week). Solution: Check that the calendar to be modified is not the default calendar used by the company. opw-3127337 closes odoo/odoo#112912 X-original-commit: 2f56ffbc2f1d3a2afbf51a859a5a5730af06fd6f Signed-off-by: Kevin Baptiste --- addons/resource/i18n/resource.pot | 6 ++++++ addons/resource/models/resource_calendar.py | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/addons/resource/i18n/resource.pot b/addons/resource/i18n/resource.pot index 7d120650736..c7646ec2873 100644 --- a/addons/resource/i18n/resource.pot +++ b/addons/resource/i18n/resource.pot @@ -258,6 +258,12 @@ msgstr "" #. odoo-python #: code:addons/resource/models/resource.py:0 #, python-format +msgid "Impossible to switch calendar type for the default company schedule." +msgstr "" + +#. module: resource +#: code:addons/resource/models/resource.py:0 +#, python-format msgid "" "In a calendar with 2 weeks mode, all periods need to be in the sections." msgstr "" diff --git a/addons/resource/models/resource_calendar.py b/addons/resource/models/resource_calendar.py index 16fcfee5068..7627d9fc0b5 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 +from odoo.exceptions import ValidationError, UserError from odoo.osv import expression from odoo.tools.float_utils import float_round @@ -184,6 +184,9 @@ 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 = [