From e48e517072df2b9162cdc050d9cbe3b1e7e77ccf Mon Sep 17 00:00:00 2001 From: "Victor Piryns (pivi)" Date: Wed, 29 Mar 2023 12:02:44 +0000 Subject: [PATCH] [FIX] hr_timesheet: timesheet uom fallback when AAL's company is False Current behaviour: When creating a timesheet, if in the vals passed the AAL doesn't have a company (possible since it's not required), then the `uom` of the timesheet is False, which doesn't make sense. Expected behaviour: The timesheet `uom` should fallback on the uom of the company of the project. Steps to reproduce: - Install hr_timesheet, Accounting - Activate the AAL in the settings - Create a project with an AAL - Remove the company on the AAL of the project - In that project create a task and log some timesheet. - `uom` field should be empty Reason for the problem: When setting the `product_uom_id` in the vals when creating/writing a timesheet, we just take the `project_time_mode_id` on the company linked to the AAL. The problem is that the company on an AAL is not a required field, so it can be `False`, leading the setting the val for `product_uom_id` to `False`. Fix: In case the AAL has no company, we get the `uom` from the company linked to the project. Affected versions: - 14.0 - 15.0 - saas-15.2 - 16.0 - saas-16.1 - saas-16.2 - master opw-3245671 closes odoo/odoo#117834 X-original-commit: 4cce77bd0288eeb3326ba57e91ef11d436f8a0ce Signed-off-by: Xavier Bol (xbo) Signed-off-by: Piryns Victor (pivi) --- addons/hr_timesheet/models/hr_timesheet.py | 3 ++- addons/hr_timesheet/tests/test_timesheet.py | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index 96ba98a2431..1b669377921 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -338,7 +338,8 @@ class AccountAnalyticLine(models.Model): if not vals.get('partner_id'): vals['partner_id'] = data.partner_id.id if not vals.get('product_uom_id'): - vals['product_uom_id'] = uom_id_per_company.get(account_per_id[vals['account_id']].company_id, False) + company = account_per_id[vals['account_id']].company_id or data.company_id + vals['product_uom_id'] = uom_id_per_company.get(company.id, company.project_time_mode_id.id) return vals_list def _timesheet_postprocess(self, values): diff --git a/addons/hr_timesheet/tests/test_timesheet.py b/addons/hr_timesheet/tests/test_timesheet.py index b5b81687f2f..920abffdb0c 100644 --- a/addons/hr_timesheet/tests/test_timesheet.py +++ b/addons/hr_timesheet/tests/test_timesheet.py @@ -552,8 +552,17 @@ class TestTimesheet(TestCommonTimesheet): def test_create_timesheet_with_companyless_analytic_account(self): """ This test ensures that a timesheet can be created on an analytic account whose company_id is set to False""" 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") + timesheet_with_project = self.env['account.analytic.line'].with_user(self.user_employee).create( + {'unit_amount': 1.0, 'project_id': self.project_customer.id}) + self.assertEqual(timesheet_with_project.product_uom_id, self.project_customer.company_id.project_time_mode_id, + "The product_uom_id of the timesheet should be equal to the project's company uom " + "if the project's analytic account has no company_id and no task_id is defined in the vals") + timesheet_with_task = self.env['account.analytic.line'].with_user(self.user_employee).create({ + 'unit_amount': 1.0, 'task_id': self.task1.id + }) + self.assertEqual(timesheet_with_task.product_uom_id, self.task1.company_id.project_time_mode_id, + "The product_uom_id of the timesheet should be equal to the task's company uom " + "if the project's 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({