From 4dc9e42a644d905e94cb41f68cb545e5d12ac63c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Wed, 9 Aug 2023 14:53:34 +0000 Subject: [PATCH] [FIX] *: fix usages of `_read_group` with NewId In https://github.com/odoo/odoo/pull/110737, I didn't consider that compute could be called on NewId record with `_origin`. Some compute methods are badly refactored with the new signature of `_read_group`. We use recordsets returning from `_read_group` to assign field value to `self`. But if `self` contains `NewId` with `origin`, these records don't represent `self`, it contains real record instead of the one with NewId + origin. Then the assignations are done on records not in `self` which may lead to generate traceback or write to other records during an onchange. Fix multiple compute to work correctly with NewId (origin set) record. X-original-commit: bd22d0a5c479a72cdaf799309387e41ce692bb29 Part-of: odoo/odoo#132261 --- addons/event_sale/models/event_event.py | 11 ++++++----- addons/stock/models/product.py | 5 +++-- addons/stock_account/models/product.py | 11 +++-------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/addons/event_sale/models/event_event.py b/addons/event_sale/models/event_event.py index cdb2cc3558f..4ab99bbe0ab 100644 --- a/addons/event_sale/models/event_event.py +++ b/addons/event_sale/models/event_event.py @@ -32,21 +32,22 @@ class Event(models.Model): we sell a single event ticket). """ date_now = fields.Datetime.now() event_subtotals = self.env['sale.order.line']._read_group( - [('event_id', 'in', self.ids), - ('price_subtotal', '!=', 0)], + [('event_id', 'in', self.ids), ('price_subtotal', '!=', 0)], ['event_id', 'currency_id'], ['price_subtotal:sum'], ) - - self.sale_price_subtotal = 0 + event_subtotals_mapping = dict.fromkeys(self._origin, 0) for event, currency, sum_price_subtotal in event_subtotals: - event.sale_price_subtotal += event.currency_id._convert( + event_subtotals_mapping[event] += event.currency_id._convert( sum_price_subtotal, currency, event.company_id or self.env.company, date_now, ) + for event in self: + event.sale_price_subtotal = event_subtotals_mapping.get(event._origin, 0) + def action_view_linked_orders(self): """ Redirects to the orders linked to the current events """ sale_order_action = self.env["ir.actions.actions"]._for_xml_id("sale.action_orders") diff --git a/addons/stock/models/product.py b/addons/stock/models/product.py index 3e816836b5f..acc3d8e6523 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -393,12 +393,13 @@ class Product(models.Model): return list(product_ids) def _compute_nbr_reordering_rules(self): - self.nbr_reordering_rules = self.reordering_min_qty = self.reordering_max_qty = 0 read_group_res = self.env['stock.warehouse.orderpoint']._read_group( [('product_id', 'in', self.ids)], ['product_id'], ['__count', 'product_min_qty:sum', 'product_max_qty:sum']) - for product, count, product_min_qty_sum, product_max_qty_sum in read_group_res: + mapped_res = {product: aggregates for product, *aggregates in read_group_res} + for product in self: + count, product_min_qty_sum, product_max_qty_sum = mapped_res.get(product._origin, (0, 0, 0)) product.nbr_reordering_rules = count product.reordering_min_qty = product_min_qty_sum product.reordering_max_qty = product_max_qty_sum diff --git a/addons/stock_account/models/product.py b/addons/stock_account/models/product.py index 2a0ae743616..43dd2a4556d 100644 --- a/addons/stock_account/models/product.py +++ b/addons/stock_account/models/product.py @@ -129,21 +129,16 @@ class ProductProduct(models.Model): groupby=['product_id'], aggregates=['value:sum', 'quantity:sum'], ) - remaining = self # Browse all products and compute products' quantities_dict in batch. - for product, value_sum, quantity_sum in groups: + group_mapping = {product: aggregates for product, *aggregates in groups} + for product in self: + value_sum, quantity_sum = group_mapping.get(product._origin, (0, 0)) value_svl = company_id.currency_id.round(value_sum) avg_cost = value_svl / quantity_sum if quantity_sum else 0 product.value_svl = value_svl product.quantity_svl = quantity_sum product.avg_cost = avg_cost product.total_value = avg_cost * product.sudo(False).qty_available - remaining -= product - - remaining.value_svl = 0 - remaining.quantity_svl = 0 - remaining.avg_cost = 0 - remaining.total_value = 0 # ------------------------------------------------------------------------- # Actions