diff --git a/addons/hr_timesheet/models/project.py b/addons/hr_timesheet/models/project.py index c48360942a6..2a4dbca2fae 100644 --- a/addons/hr_timesheet/models/project.py +++ b/addons/hr_timesheet/models/project.py @@ -154,10 +154,10 @@ class Project(models.Model): # Timesheets may be stored in a different unit of measure, so first # we convert all of them to the reference unit # if the timesheet has no product_uom_id then we take the one of the project - total_time = sum([ - unit_amount * uoms_dict.get(product_uom_id, project.timesheet_encode_uom_id).factor_inv - for product_uom_id, unit_amount in timesheet_time_dict[project.id] - ], 0.0) + total_time = 0.0 + for product_uom_id, unit_amount in timesheet_time_dict[project.id]: + factor = uoms_dict.get(product_uom_id, project.timesheet_encode_uom_id).factor_inv + total_time += unit_amount * (1.0 if project.encode_uom_in_days else factor) # Now convert to the proper unit of measure set in the settings total_time *= project.timesheet_encode_uom_id.factor project.total_timesheet_time = int(round(total_time)) diff --git a/addons/hr_timesheet/tests/test_timesheet.py b/addons/hr_timesheet/tests/test_timesheet.py index c4347daace0..575fbee3c1b 100644 --- a/addons/hr_timesheet/tests/test_timesheet.py +++ b/addons/hr_timesheet/tests/test_timesheet.py @@ -564,3 +564,27 @@ class TestTimesheet(TestCommonTimesheet): 'unit_amount': 3, }) self.assertEqual(timesheet.employee_id, self.empl_employee) + + def test_uom_change_timesheet(self): + """ + We check that we don't over transform the timesheet unit amount when changing + the company encoding timesheet uom, we keep it in the project as hours. + So it will be transformed only once when encoding the timesheet. + """ + Timesheet = self.env['account.analytic.line'] + project = self.env['project.project'].create({ + 'name': 'Project', + 'allow_timesheets': True, + 'partner_id': self.partner.id, + }) + project.allocated_hours = 40.0 + + Timesheet.create({ + 'name': 'FirstTimeSheet', + 'project_id': project.id, + 'unit_amount': 8, + 'employee_id': self.empl_employee2.id + }) + self.env.company.timesheet_encode_uom_id = self.env.ref('uom.product_uom_day') + self.assertEqual(project.total_timesheet_time, 8, "Total timesheet time should be 8 hours") + self.assertEqual(project.timesheet_encode_uom_id.name, 'Days', "Timesheet encode uom should be 'Days'")