From 62a7c0cee1e2497ce28404a48fd64bd2f6867ae6 Mon Sep 17 00:00:00 2001 From: "Audric Onockx (auon)" Date: Wed, 7 Sep 2022 15:18:32 +0000 Subject: [PATCH] [FIX] resource: copy default company and calendar Prior to this fix, when a ResourceMixin was copied and a `company_id` set in its default values, it was not taken into account, because ResourceMixin.copy_data first copied self.resource_id without default values, then set the company of it into the default values for its own copy. Same went with `resource_calendar_id`, just copying the calendar of its resource. With this fix, if present, we first set these two values in the Resource default values (`company_id` and `calendar_id`) and only then copy them in the ResourceMixin default values (as `company_id` and `resource_calendar_id`). closes odoo/odoo#93823 Related: odoo/enterprise#28490 Signed-off-by: Laurent Stukkens (ltu) --- addons/hr_timesheet/models/hr_timesheet.py | 13 ++++++------- addons/resource/models/resource_mixin.py | 9 ++++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index 27c79bf7713..f174af5a5ea 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -5,7 +5,7 @@ from collections import defaultdict from lxml import etree import re -from odoo import api, Command, fields, models, _ +from odoo import api, Command, fields, models, _, _lt from odoo.exceptions import UserError, AccessError, ValidationError from odoo.osv import expression @@ -143,10 +143,9 @@ class AccountAnalyticLine(models.Model): user_ids.append(user_id) # 2/ Search all employees related to user_ids and employee_ids, in the selected companies - Employee = self.env['hr.employee'] - employees = Employee.sudo().search([ + employees = self.env['hr.employee'].sudo().search([ '&', '|', ('user_id', 'in', user_ids), ('id', 'in', employee_ids), ('company_id', 'in', self.env.companies.ids) - ]) or Employee + ]) # ┌───── in search results = active/in companies ────────> was found with... ─── employee_id ───> (A) There is nothing to do, we will use this employee_id # 3/ Each employee └──── user_id ──────> (B)** We'll need to select the right employee for this user @@ -163,7 +162,7 @@ class AccountAnalyticLine(models.Model): employee_id_per_company_per_user[employee.user_id.id][employee.company_id.id] = employee.id # 4/ Put valid employee_id in each vals - get_error_msg = lambda: _('Timesheets must be created with an active employee in the selected companies.') + error_msg = _lt('Timesheets must be created with an active employee in the selected companies.') for vals in vals_list: if not vals.get('project_id'): continue @@ -173,7 +172,7 @@ class AccountAnalyticLine(models.Model): vals['user_id'] = valid_employee_per_id[employee_in_id].sudo().user_id.id # (A) OK continue else: - raise ValidationError(get_error_msg()) # (C) KO + raise ValidationError(error_msg) # (C) KO else: user_id = vals.get('user_id', default_user_id) # (B)... @@ -189,7 +188,7 @@ class AccountAnalyticLine(models.Model): vals['employee_id'] = employee_out_id vals['user_id'] = user_id else: # ...and raise an error if they fail - raise ValidationError(get_error_msg()) + raise ValidationError(error_msg) # 5/ Finally, create the timesheets lines = super(AccountAnalyticLine, self).create(vals_list) diff --git a/addons/resource/models/resource_mixin.py b/addons/resource/models/resource_mixin.py index e71a690b154..934ce18260a 100644 --- a/addons/resource/models/resource_mixin.py +++ b/addons/resource/models/resource_mixin.py @@ -69,7 +69,14 @@ class ResourceMixin(models.AbstractModel): def copy_data(self, default=None): if default is None: default = {} - resource = self.resource_id.copy() + + resource_default = {} + if 'company_id' in default: + resource_default['company_id'] = default['company_id'] + if 'resource_calendar_id' in default: + resource_default['calendar_id'] = default['resource_calendar_id'] + resource = self.resource_id.copy(resource_default) + default['resource_id'] = resource.id default['company_id'] = resource.company_id.id default['resource_calendar_id'] = resource.calendar_id.id