[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
This commit is contained in:
Rémy Voet (ryv)
2023-08-18 12:17:56 +02:00
parent 0d99f4b9d9
commit 4dc9e42a64
3 changed files with 12 additions and 15 deletions
+6 -5
View File
@@ -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")
+3 -2
View File
@@ -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
+3 -8
View File
@@ -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