From dd9ee7e49344c3dd251a4f54b42ded9be7756d0a Mon Sep 17 00:00:00 2001 From: "Xavier BOL (xbo)" Date: Mon, 30 Jan 2023 10:23:17 +0000 Subject: [PATCH] [FIX] hr_timesheet: create timesheet with default employee in context Before this commit, the default employee set in the context to create a timesheet is unused in the create method. This commit uses the context to set the employee when no user and no employee are given in the vals parameter. task-2702636 Part-of: odoo/odoo#108605 --- addons/hr_timesheet/models/hr_timesheet.py | 11 ++++++----- addons/hr_timesheet/tests/test_timesheet.py | 9 +++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index c1dbb52cdd9..021c53d13de 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -145,12 +145,13 @@ class AccountAnalyticLine(models.Model): continue if not vals.get('name'): vals['name'] = '/' - employee_id = vals.get('employee_id') - user_id = vals.get('user_id', default_user_id) + employee_id = vals.get('employee_id', self._context.get('default_employee_id', False)) if employee_id and employee_id not in employee_ids: employee_ids.append(employee_id) - elif user_id not in user_ids: - user_ids.append(user_id) + else: + user_id = vals.get('user_id', default_user_id) + if user_id not in user_ids: + user_ids.append(user_id) # 2/ Search all employees related to user_ids and employee_ids, in the selected companies employees = self.env['hr.employee'].sudo().search([ @@ -176,7 +177,7 @@ class AccountAnalyticLine(models.Model): for vals in vals_list: if not vals.get('project_id'): continue - employee_in_id = vals.get('employee_id') + employee_in_id = vals.get('employee_id', self._context.get('default_employee_id', False)) if employee_in_id: if employee_in_id in valid_employee_per_id: vals['user_id'] = valid_employee_per_id[employee_in_id].sudo().user_id.id # (A) OK diff --git a/addons/hr_timesheet/tests/test_timesheet.py b/addons/hr_timesheet/tests/test_timesheet.py index 2c069e974ec..c4347daace0 100644 --- a/addons/hr_timesheet/tests/test_timesheet.py +++ b/addons/hr_timesheet/tests/test_timesheet.py @@ -555,3 +555,12 @@ class TestTimesheet(TestCommonTimesheet): self.project_customer.analytic_account_id.company_id = False timesheet = self.env['account.analytic.line'].with_user(self.user_employee).create({'unit_amount': 1.0, 'project_id': self.project_customer.id}) self.assertFalse(timesheet.product_uom_id, "The product_uom_id of the timesheet should be set to False for its analytic account has no company_id") + + def test_create_timesheet_with_default_employee_in_context(self): + timesheet = self.env['account.analytic.line'].with_context(default_employee_id=self.empl_employee.id).create({ + 'project_id': self.project_customer.id, + 'task_id': self.task1.id, + 'name': 'Timesheet with default employee in context', + 'unit_amount': 3, + }) + self.assertEqual(timesheet.employee_id, self.empl_employee)