From 9f519c44b33ef80d8c4a9a968fda9eaa76dccc8c Mon Sep 17 00:00:00 2001 From: Kartik Chavda Date: Wed, 28 Jul 2021 10:13:15 +0000 Subject: [PATCH] [FIX] sale_timesheet: fix access right error while opening sale order Purpose of this commit to fix access right error while user try to open sale order when he has no acces right of timesheet. So, In this commit improve code for timesheet_total_duration compute method to directly compute timesheet_total_duration from unit_amount of timesheets without any access right. closes odoo/odoo#75272 Taskid: 2514517 X-original-commit: 2f7c5020cdd2073dc55ecda40d45f30c42e508e8 Signed-off-by: Yannick Tivisse (yti) --- addons/sale_timesheet/models/sale_order.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/addons/sale_timesheet/models/sale_order.py b/addons/sale_timesheet/models/sale_order.py index afc20297b7e..f2b625054dc 100644 --- a/addons/sale_timesheet/models/sale_order.py +++ b/addons/sale_timesheet/models/sale_order.py @@ -40,16 +40,18 @@ class SaleOrder(models.Model): 'timesheet_count': timesheet_count, }) - @api.depends('timesheet_ids', 'company_id.timesheet_encode_uom_id') + @api.depends('company_id.project_time_mode_id', 'timesheet_ids', 'company_id.timesheet_encode_uom_id') def _compute_timesheet_total_duration(self): + if not self.user_has_groups('hr_timesheet.group_hr_timesheet_user'): + self.update({'timesheet_total_duration': 0}) + return + group_data = self.env['account.analytic.line'].sudo().read_group([ + ('order_id', 'in', self.ids) + ], ['order_id', 'unit_amount'], ['order_id']) + timesheet_unit_amount_dict = defaultdict(float) + timesheet_unit_amount_dict.update({data['order_id'][0]: data['unit_amount'] for data in group_data}) for sale_order in self: - timesheets = sale_order.timesheet_ids if self.user_has_groups('hr_timesheet.group_hr_timesheet_approver') else sale_order.timesheet_ids.filtered(lambda t: t.user_id.id == self.env.uid) - total_time = 0.0 - for timesheet in timesheets: - # Timesheets may be stored in a different unit of measure, so first we convert all of them to the reference unit - total_time += timesheet.unit_amount * timesheet.product_uom_id.factor_inv - # Now convert to the proper unit of measure - total_time *= sale_order.timesheet_encode_uom_id.factor + total_time = sale_order.company_id.project_time_mode_id._compute_quantity(timesheet_unit_amount_dict[sale_order.id], sale_order.timesheet_encode_uom_id) sale_order.timesheet_total_duration = round(total_time) def _compute_field_value(self, field):