From 949ea5ff7b4002b06cb6b564599f80d9859300e1 Mon Sep 17 00:00:00 2001 From: "Xavier BOL (xbo)" Date: Fri, 12 Mar 2021 09:38:00 +0000 Subject: [PATCH] [FIX] sale_timesheet: show view timesheets button in SO portal Before this commit, when the user creates a SO with a service product which does not generate a task or a project, the analytic account is not set on the new SO. Then even if the user creates a task and add timesheets for this product in this SO, the compute_timesheet_ids gives 0 timesheets in the sale.order model because the SO is not linked to an analytic account. This commit removes this condition and directly searches the timesheets linked to the SO and thus provides the correct number of timesheets for it. This bug is appeared from this commit: dc9ef81ab21 Steps to reproduce: 1. Create a SO and add a SOL with 'Service on Timesheet' 2. Confirm the SO 3. Create a task in project for the same customer than the SO or a project with no customer set. 4. Add the customer of the SO in the task if it is not already the case. 5. Add the SOL in the task and add a timesheet. 6. Go to /my/orders 7. Select the SO that you have created and in this view, normally, you should have the View Timesheets buttons on the left side. Current behaviour: The 'View Timesheets' button does not appear in the view because the compute_timesheet_ids from the SO return no timesheets for it. Expected behaviour: The button should be visible in the view because we have a timesheet for this SO. closes odoo/odoo#67770 Signed-off-by: Yannick Tivisse (yti) --- addons/sale_timesheet/models/sale_order.py | 26 ++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/addons/sale_timesheet/models/sale_order.py b/addons/sale_timesheet/models/sale_order.py index 8bb46eaa28e..5d16f85201a 100644 --- a/addons/sale_timesheet/models/sale_order.py +++ b/addons/sale_timesheet/models/sale_order.py @@ -19,17 +19,25 @@ class SaleOrder(models.Model): timesheet_encode_uom_id = fields.Many2one('uom.uom', related='company_id.timesheet_encode_uom_id') timesheet_total_duration = fields.Integer("Timesheet Total Duration", compute='_compute_timesheet_total_duration', help="Total recorded duration, expressed in the encoding UoM, and rounded to the unit") - @api.depends('analytic_account_id.line_ids') def _compute_timesheet_ids(self): + timesheet_groups = self.env['account.analytic.line'].sudo().read_group( + [('so_line', 'in', self.mapped('order_line').ids), ('project_id', '!=', False)], + ['so_line', 'ids:array_agg(id)'], + ['so_line']) + timesheets_per_sol = {group['so_line'][0]: (group['ids'], group['so_line_count']) for group in timesheet_groups} + for order in self: - if order.analytic_account_id: - order.timesheet_ids = self.env['account.analytic.line'].search( - [('so_line', 'in', order.order_line.ids), - ('amount', '<=', 0.0), - ('project_id', '!=', False)]) - else: - order.timesheet_ids = [] - order.timesheet_count = len(order.timesheet_ids) + timesheet_ids = [] + timesheet_count = 0 + for sale_line_id in order.order_line.filtered('is_service').ids: + list_timesheet_ids, count = timesheets_per_sol.get(sale_line_id, ([], 0)) + timesheet_ids.extend(list_timesheet_ids) + timesheet_count += count + + order.update({ + 'timesheet_ids': self.env['account.analytic.line'].browse(timesheet_ids), + 'timesheet_count': timesheet_count, + }) @api.depends('timesheet_ids', 'company_id.timesheet_encode_uom_id') def _compute_timesheet_total_duration(self):