diff options
| author | Rémy Voet (ryv) <ryv@odoo.com> | 2021-01-04 15:04:51 +0000 |
|---|---|---|
| committer | Rémy Voet (ryv) <ryv@odoo.com> | 2021-01-04 15:07:48 +0000 |
| commit | b8d7e48f27fc4f0868c538977be8276058944e57 (patch) | |
| tree | 13bb520f1299b1552dfeb6b5b865c9bbadd247ea /addons | |
| parent | 209f421b7360a42ab43c09d9aacec6b53ad24917 (diff) | |
[FIX] stock_landed_cost: efficient validate landed cost
Update the standard_price outside the loop to avoid recompute
quantity_svl at each iteration. quantity_svl is not modify in the
process and the standard_price is not read in process.
NewPrice = OldPrice + (cost_to_add_x1) / quantity_svl + (cost_to_add_x2) / quantity_svl
Becomes
NewPrice = OldPrice + (cost_to_add_x1 + cost_to_add_x2) / quantity_svl
which is equals.
opw-2374405
odoo/odoo#64011
Forward-port-of: odoo/odoo#63742
Signed-off-by: Arnold Moyaux <amoyaux@users.noreply.github.com>
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/stock_landed_costs/models/stock_landed_cost.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/addons/stock_landed_costs/models/stock_landed_cost.py b/addons/stock_landed_costs/models/stock_landed_cost.py index 984662d42c3..abd79f66618 100644 --- a/addons/stock_landed_costs/models/stock_landed_cost.py +++ b/addons/stock_landed_costs/models/stock_landed_cost.py @@ -137,6 +137,7 @@ class StockLandedCost(models.Model): 'move_type': 'entry', } valuation_layer_ids = [] + cost_to_add_byproduct = defaultdict(lambda: 0.0) for line in cost.valuation_adjustment_lines.filtered(lambda line: line.move_id): remaining_qty = sum(line.move_id.stock_valuation_layer_ids.mapped('remaining_qty')) linked_layer = line.move_id.stock_valuation_layer_ids[:1] @@ -160,8 +161,8 @@ class StockLandedCost(models.Model): valuation_layer_ids.append(valuation_layer.id) # Update the AVCO product = line.move_id.product_id - if product.cost_method == 'average' and not float_is_zero(product.quantity_svl, precision_rounding=product.uom_id.rounding): - product.with_company(self.company_id).sudo().with_context(disable_auto_svl=True).standard_price += cost_to_add / product.quantity_svl + if product.cost_method == 'average': + cost_to_add_byproduct[product] += cost_to_add # `remaining_qty` is negative if the move is out and delivered proudcts that were not # in stock. qty_out = 0 @@ -171,6 +172,12 @@ class StockLandedCost(models.Model): qty_out = line.move_id.product_qty move_vals['line_ids'] += line._create_accounting_entries(move, qty_out) + # batch standard price computation avoid recompute quantity_svl at each iteration + products = self.env['product.product'].browse(p.id for p in cost_to_add_byproduct.keys()) + for product in products: # iterate on recordset to prefetch efficiently quantity_svl + if not float_is_zero(product.quantity_svl, precision_rounding=product.uom_id.rounding): + product.with_company(cost.company_id).sudo().with_context(disable_auto_svl=True).standard_price += cost_to_add_byproduct[product] / product.quantity_svl + move_vals['stock_valuation_layer_ids'] = [(6, None, valuation_layer_ids)] move = move.create(move_vals) cost.write({'state': 'done', 'account_move_id': move.id}) |
