From 5ce17f327af4915f8ff3903e3c95cc9fe70dbae9 Mon Sep 17 00:00:00 2001 From: "Hamza (hisl)" Date: Thu, 11 May 2023 13:01:08 +0000 Subject: [PATCH] [FIX] hr_timesheet : update effective_hours for progress_hours computation Steps to reproduce the issue: Add a project and create a task with a timesheet in it set the allocated hours to 00:03 add a line in the timesheet with hours spent 00:03 Current Behaviour: The percentage calculated would be 96%, even though the time allocated and time spent are equal. Desired Behaviour: The percentage should be 100 as both values i.e. time spent and time allocated are equal. This is happening because effective_hours value is being rounded off to 2 decimal places, and is not accurate enough to compute the progress_hours. Here, I have used the same line, that is used to compute effective_hours, to compute the task_total_hours but without rounding off. This will make the calculate more precise and accurate. OPW-3270858 closes odoo/odoo#125619 X-original-commit: 94dd1d570410c9a994027b7030ee9204ca1ec3e9 Signed-off-by: Hamza Islam (hisl) Signed-off-by: Xavier Bol (xbo) --- addons/hr_timesheet/models/project_task.py | 4 +-- addons/hr_timesheet/tests/test_timesheet.py | 28 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/addons/hr_timesheet/models/project_task.py b/addons/hr_timesheet/models/project_task.py index 3ee502ad559..8dea201957d 100644 --- a/addons/hr_timesheet/models/project_task.py +++ b/addons/hr_timesheet/models/project_task.py @@ -65,12 +65,12 @@ class Task(models.Model): def _compute_effective_hours(self): if not any(self._ids): for task in self: - task.effective_hours = round(sum(task.timesheet_ids.mapped('unit_amount')), 2) + task.effective_hours = sum(task.timesheet_ids.mapped('unit_amount')) return timesheet_read_group = self.env['account.analytic.line']._read_group([('task_id', 'in', self.ids)], ['task_id'], ['unit_amount:sum']) timesheets_per_task = {task.id: amount for task, amount in timesheet_read_group} for task in self: - task.effective_hours = round(timesheets_per_task.get(task.id, 0.0), 2) + task.effective_hours = timesheets_per_task.get(task.id, 0.0) @api.depends('effective_hours', 'subtask_effective_hours', 'planned_hours') def _compute_progress_hours(self): diff --git a/addons/hr_timesheet/tests/test_timesheet.py b/addons/hr_timesheet/tests/test_timesheet.py index 197086c5348..eb414391ba7 100644 --- a/addons/hr_timesheet/tests/test_timesheet.py +++ b/addons/hr_timesheet/tests/test_timesheet.py @@ -665,3 +665,31 @@ class TestTimesheet(TestCommonTimesheet): with self.assertRaises(UserError): self.task1.write({'project_id': False, 'parent_id': False}) + + def test_percentage_of_planned_hours(self): + """ Test the percentage of planned hours on a task. """ + self.task1.planned_hours = 11/60 + self.assertEqual(self.task1.effective_hours, 0, 'No timesheet should be created yet.') + self.assertEqual(self.task1.progress, 0, 'No timesheet should be created yet.') + self.env['account.analytic.line'].create([ + { + 'name': 'Timesheet', + 'project_id': self.project_customer.id, + 'task_id': self.task1.id, + 'unit_amount': 3/60, + 'employee_id': self.empl_employee.id, + }, { + 'name': 'Timesheet', + 'project_id': self.project_customer.id, + 'task_id': self.task1.id, + 'unit_amount': 4/60, + 'employee_id': self.empl_employee.id, + }, { + 'name': 'Timesheet', + 'project_id': self.project_customer.id, + 'task_id': self.task1.id, + 'unit_amount': 4/60, + 'employee_id': self.empl_employee.id, + }, + ]) + self.assertEqual(self.task1.progress, 100, 'The percentage of planned hours should be 100%.')