odoo/odoo#211533

Created by fw-bot
Merged at 632e665fbe4bc14ac3073d6e7f8939f57ff2aa27

Statuses:

label
odoo-dev:saas-18.2-16.0-stock_account-loop_var-438573-fw
head
4bb7741e8f41d9e5ad4bc5f49ea7f4b4451b77e3
merged
2 weeks ago by Logistics, Tiffany Chang (tic)
odoo/odoo
16.0 #183402
17.0 #211485
18.0 #211504
saas-18.1 #211516
saas-18.2 #211533
saas-18.3 #211545
saas-18.4 #215653
master #212086

[FW][FIX] stock_account: get singletone value within loop

Description of the issue/feature this PR addresses:

I'm not sure how to reproduce right now. Clue: these modules are installed:
- sale_stock_margin
- stock_account

Other clue: user is going to an mrp.production order and clicking on "Set as done".

Some unknown combination of facts under this situation make multiple sale.order.line have to recompute their purchase_price. It happens that some product involved is measured in Units, while some other is measured in Kg.

At the end of the rabbit hole, the code ends up here, where there's a pretty obvious bug: a singleton value being extracted from a multi-record variable.

So, yes, this is probably the worse fix description ever. 😅 However, the bug is so obvious that I'm not sure I really need something more elaborate in this case. So, here's the fix! 🎁

@moduon MT-7556

Current behavior before PR:

<details>

Traceback (most recent call last):
  File &#34;/opt/odoo/auto/addons/etl/controllers/[dataset.py](https://dataset.py/)&#34;, line 15, in _call_kw

    raise ValueError

ValueError



During handling of the above exception, another exception occurred:



Traceback (most recent call last):

  File &#34;/opt/odoo/custom/src/odoo/odoo/[models.py](https://models.py/)&#34;, line 5182, in ensure_one

    _id, = self._ids

ValueError: too many values to unpack (expected 1)



During handling of the above exception, another exception occurred:



Traceback (most recent call last):

  File &#34;/opt/odoo/custom/src/odoo/odoo/[http.py](https://http.py/)&#34;, line 1651, in _serve_db

    return service_model.retrying(self._serve_ir_http, self.env)

  File &#34;/opt/odoo/custom/src/odoo/odoo/service/[model.py](https://model.py/)&#34;, line 133, in retrying

    result = func()

  File &#34;/opt/odoo/custom/src/odoo/odoo/[http.py](https://http.py/)&#34;, line 1678, in _serve_ir_http

    response = self.dispatcher.dispatch(rule.endpoint, args)

  File &#34;/opt/odoo/custom/src/odoo/odoo/[http.py](https://http.py/)&#34;, line 1882, in dispatch

    result = self.request.registry[&#39;ir.http&#39;]._dispatch(endpoint)

  File &#34;/opt/odoo/custom/src/odoo/odoo/addons/base/models/[ir_http.py](https://ir_http.py/)&#34;, line 154, in _dispatch

    result = endpoint(**request.params)

  File &#34;/opt/odoo/custom/src/odoo/odoo/[http.py](https://http.py/)&#34;, line 734, in route_wrapper

    result = endpoint(self, *args, **params_ok)

  File &#34;/opt/odoo/auto/addons/web/controllers/[dataset.py](https://dataset.py/)&#34;, line 46, in call_button

    action = self._call_kw(model, method, args, kwargs)

  File &#34;/opt/odoo/auto/addons/etl/controllers/[dataset.py](https://dataset.py/)&#34;, line 19, in _call_kw

    return super()._call_kw(model, method, args, kwargs)

  File &#34;/opt/odoo/auto/addons/web/controllers/[dataset.py](https://dataset.py/)&#34;, line 33, in _call_kw

    return call_kw(request.env[model], method, args, kwargs)

  File &#34;/opt/odoo/custom/src/odoo/odoo/[api.py](https://api.py/)&#34;, line 469, in call_kw

    model.env.flush_all()

  File &#34;/opt/odoo/custom/src/odoo/odoo/[api.py](https://api.py/)&#34;, line 745, in flush_all

    self._recompute_all()

  File &#34;/opt/odoo/custom/src/odoo/odoo/[api.py](https://api.py/)&#34;, line 741, in _recompute_all

    self[field.model_name]._recompute_field(field)

  File &#34;/opt/odoo/custom/src/odoo/odoo/[models.py](https://models.py/)&#34;, line 6282, in _recompute_field

    field.recompute(records)

  File &#34;/opt/odoo/custom/src/odoo/odoo/[fields.py](https://fields.py/)&#34;, line 1370, in recompute

    apply_except_missing(self.compute_value, recs)

  File &#34;/opt/odoo/custom/src/odoo/odoo/[fields.py](https://fields.py/)&#34;, line 1343, in apply_except_missing

    func(records)

  File &#34;/opt/odoo/custom/src/odoo/odoo/[fields.py](https://fields.py/)&#34;, line 1392, in compute_value

    records._compute_field_value(self)

  File &#34;/opt/odoo/custom/src/odoo/odoo/[models.py](https://models.py/)&#34;, line 4240, in _compute_field_value

    fields.determine(field.compute, self)

  File &#34;/opt/odoo/custom/src/odoo/odoo/[fields.py](https://fields.py/)&#34;, line 98, in determine

    return needle(*args)

  File &#34;/opt/odoo/auto/addons/sale_stock_margin/models/[sale_order_line.py](https://sale_order_line.py/)&#34;, line 18, in _compute_purchase_price

    purch_price = product._compute_average_price(0, line.product_uom_qty, line.move_ids)

  File &#34;/opt/odoo/auto/addons/mrp_account/models/[product.py](https://product.py/)&#34;, line 54, in _compute_average_price

    return super()._compute_average_price(qty_invoiced, qty_to_invoice, stock_moves, is_returned=is_returned)

  File &#34;/opt/odoo/auto/addons/stock_account/models/[product.py](https://product.py/)&#34;, line 788, in _compute_average_price

    qty_valued, valuation = candidates._consume_specific_qty(qty_invoiced, qty_to_invoice)

  File &#34;/opt/odoo/auto/addons/stock_account/models/[stock_valuation_layer.py](https://stock_valuation_layer.py/)&#34;, line 113, in _consume_specific_qty

    rounding = self.product_id.uom_id.rounding

  File &#34;/opt/odoo/custom/src/odoo/odoo/[fields.py](https://fields.py/)&#34;, line 1154, in __get__

    record.ensure_one()

  File &#34;/opt/odoo/custom/src/odoo/odoo/[models.py](https://models.py/)&#34;, line 5185, in ensure_one

    raise ValueError(&#34;Expected singleton: %s&#34; % self)

ValueError: Expected singleton: uom.uom(1, 12)

</details>

Desired behavior after PR is merged: Fixed!


I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr

Forward-Port-Of: #183402