From 8c59f5d345d763fed2f8616e63488db271dcb50c Mon Sep 17 00:00:00 2001 From: Damien Bouvy Date: Mon, 4 Nov 2019 18:22:50 +0000 Subject: [PATCH] [FIX] sale(_timesheet): faulty computation The 'delivered quantity' field sometime needs to trigger a search on related objects (e.g. analytic lines) to know how much has been delivered. For performance reasons, this search is done using a read_group where the results are grouped by SO line. Unfortunately, this computation is sometimes launched during recomputes/onchanges where the line in question has a NewId instead of a regular id. This means that the mapping will return something like {240: 50.0} indicating that (e.g.) there are 50 timesheet hours for the SO line with id 240; unfortunately by doing mapping.get(line.id, 0.0) the NewId (240) is used - therefore returning a delivered quantity of 0.0 for a line which has actual timesheets. This commit ensures that if the line has a NewId and an original underlying record, the id of the latter is used as the key to fetch the value from the dict that contains the read_group result. Note that the problem was quite tricky to reproduce as it depended on the order of operations launched by triggers - the order of operations was important since triggering the compute with the 'real' record put things in order before any other piece of code could have spotted the issue. closes odoo/odoo#39781 X-original-commit: cf54606edb9130f2c5b351150f0e37b75f10ae66 Signed-off-by: Damien Bouvy (dbo) --- addons/sale/models/sale.py | 2 +- addons/sale_timesheet/models/sale_order.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index dcda7270d15..a5b2988202c 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -1271,7 +1271,7 @@ class SaleOrderLine(models.Model): lines_by_analytic = self.filtered(lambda sol: sol.qty_delivered_method == 'analytic') mapping = lines_by_analytic._get_delivered_quantity_by_analytic([('amount', '<=', 0.0)]) for so_line in lines_by_analytic: - so_line.qty_delivered = mapping.get(so_line.id, 0.0) + so_line.qty_delivered = mapping.get(so_line.id or so_line._origin.id, 0.0) # compute for manual lines for line in self: if line.qty_delivered_method == 'manual': diff --git a/addons/sale_timesheet/models/sale_order.py b/addons/sale_timesheet/models/sale_order.py index 5efe1029f68..15317fc4ddd 100644 --- a/addons/sale_timesheet/models/sale_order.py +++ b/addons/sale_timesheet/models/sale_order.py @@ -176,7 +176,7 @@ class SaleOrderLine(models.Model): domain = lines_by_timesheet._timesheet_compute_delivered_quantity_domain() mapping = lines_by_timesheet.sudo()._get_delivered_quantity_by_analytic(domain) for line in lines_by_timesheet: - line.qty_delivered = mapping.get(line.id, 0.0) + line.qty_delivered = mapping.get(line.id or line._origin.id, 0.0) def _timesheet_compute_delivered_quantity_domain(self): """ Hook for validated timesheet in addionnal module """