[FIX] stock_account: optimize computation of stock value on product

This commit is contained in:
Raphael Collet
2019-01-25 15:14:48 +00:00
committed by Nicolas Martinelli
parent 5f77d0e77d
commit 1e6b35ad96
+16 -5
View File
@@ -199,6 +199,19 @@ class ProductProduct(models.Model):
for row in res:
fifo_automated_values[(row[0], row[1])] = (row[2], row[3], list(row[4]))
product_values = {product: 0 for product in self}
product_move_ids = {product: [] for product in self}
if to_date:
domain = [('product_id', 'in', self.ids), ('date', '<=', to_date)] + StockMove._get_all_base_domain()
for move in StockMove.search(domain).with_context(prefetch_fields=False):
product_values[move.product_id] += move.value
product_move_ids[move.product_id].append(move.id)
else:
domain = [('product_id', 'in', self.ids)] + StockMove._get_all_base_domain()
for move in StockMove.search(domain).with_context(prefetch_fields=False):
product_values[move.product_id] += move.remaining_value
product_move_ids[move.product_id].append(move.id)
for product in self:
if product.cost_method in ['standard', 'average']:
qty_available = product.with_context(company_owned=True, owner_id=False).qty_available
@@ -213,11 +226,9 @@ class ProductProduct(models.Model):
elif product.cost_method == 'fifo':
if to_date:
if product.product_tmpl_id.valuation == 'manual_periodic':
domain = [('product_id', '=', product.id), ('date', '<=', to_date)] + StockMove._get_all_base_domain()
moves = StockMove.search(domain)
product.stock_value = sum(moves.mapped('value'))
product.stock_value = product_values[product]
product.qty_at_date = product.with_context(company_owned=True, owner_id=False).qty_available
product.stock_fifo_manual_move_ids = StockMove.browse(moves.ids)
product.stock_fifo_manual_move_ids = StockMove.browse(product_move_ids[product])
elif product.product_tmpl_id.valuation == 'real_time':
valuation_account_id = product.categ_id.property_stock_valuation_account_id.id
value, quantity, aml_ids = fifo_automated_values.get((product.id, valuation_account_id)) or (0, 0, [])
@@ -225,7 +236,7 @@ class ProductProduct(models.Model):
product.qty_at_date = quantity
product.stock_fifo_real_time_aml_ids = self.env['account.move.line'].browse(aml_ids)
else:
product.stock_value, moves = product._sum_remaining_values()
product.stock_value, moves = product_values[product], StockMove.browse(product_move_ids[product])
product.qty_at_date = product.with_context(company_owned=True, owner_id=False).qty_available
if product.product_tmpl_id.valuation == 'manual_periodic':
product.stock_fifo_manual_move_ids = moves