From 21a51d04c3f6bf09faf9d3e528d825eee37ac34b Mon Sep 17 00:00:00 2001 From: MaxLarue Date: Fri, 6 Sep 2019 12:08:02 +0000 Subject: [PATCH] [FIX] sale_timesheet: display timesheeted quantity correctly on sale order There was a stat-button on sale orders coming from a task with timesheet that displayed the quantity of timesheets linked to the origin task. This stat-button was re labeled with the rimesheet uom (hours/days/...) but the field still displayed the number of timesheets instead of the total timesheeted amount in the correct uom. This commit introduces a new computed field on the sale order, which computes the amount of time on timesheets depending on the used uom. The compute method uses the product_uom_hour record as it is the uom in which the amount is stored on the model, that record is guaranteed to exist as it's model unlink method was overridden to prevent removing working-time uom. Task-2065079 --- addons/sale_timesheet/models/sale_order.py | 13 +++++++++++++ addons/sale_timesheet/views/sale_order_views.xml | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/addons/sale_timesheet/models/sale_order.py b/addons/sale_timesheet/models/sale_order.py index 4f06e298a00..81fba1c3da1 100644 --- a/addons/sale_timesheet/models/sale_order.py +++ b/addons/sale_timesheet/models/sale_order.py @@ -24,6 +24,7 @@ class SaleOrder(models.Model): help='Select a non billable project on which tasks can be created.') project_ids = fields.Many2many('project.project', compute="_compute_project_ids", string='Projects', copy=False, groups="project.group_project_user", help="Projects used in this sales order.") timesheet_encode_uom_id = fields.Many2one('uom.uom', related='company_id.timesheet_encode_uom_id') + timesheet_total_duration = fields.Float("Timesheet Total Duration", compute='_compute_timesheet_total_duration', help="Total recorded duration, expressed in the encoding UoM") @api.depends('analytic_account_id.line_ids') def _compute_timesheet_ids(self): @@ -60,6 +61,18 @@ class SaleOrder(models.Model): projects |= order.project_id order.project_ids = projects + @api.depends('timesheet_ids', 'company_id.timesheet_encode_uom_id') + def _compute_timesheet_total_duration(self): + for sale_order in self: + duration_list = [] + for timesheet in sale_order.timesheet_ids: + timesheet_uom = timesheet.product_uom_id or timesheet.company_id.project_time_mode_id + if timesheet_uom != sale_order.timesheet_encode_uom_id and timesheet_uom.category_id == sale_order.timesheet_encode_uom_id.category_id: + duration_list.append(timesheet_uom._compute_quantity(timesheet.unit_amount, sale_order.timesheet_encode_uom_id)) + else: + duration_list.append(timesheet.unit_amount) + sale_order.timesheet_total_duration = sum(duration_list) + @api.onchange('project_id') def _onchange_project_id(self): """ Set the SO analytic account to the selected project's analytic account """ diff --git a/addons/sale_timesheet/views/sale_order_views.xml b/addons/sale_timesheet/views/sale_order_views.xml index 016e0ce66de..0b4a502cd8e 100644 --- a/addons/sale_timesheet/views/sale_order_views.xml +++ b/addons/sale_timesheet/views/sale_order_views.xml @@ -26,6 +26,7 @@ groups="project.group_project_user"> +