From 4e20c2a49e6e5e6d1fb70de8c2473b1ada53e304 Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Mon, 18 Jun 2018 14:28:26 +0200 Subject: [PATCH] [FIX] stock_account: inventory valuation When the initial inventory adjustement is made after the accounting openeing entry, no AML is created with the initial quantities. The valuation report is therefore wrong. Partial revert of 21099f3eaf7b01a4f605c. Although the existing solution has also flaws, the fix highlights them even more. opw-1859204 --- addons/stock_account/models/product.py | 60 ++++++++++++-------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/addons/stock_account/models/product.py b/addons/stock_account/models/product.py index 2c028bd4fc2..27630eb1011 100644 --- a/addons/stock_account/models/product.py +++ b/addons/stock_account/models/product.py @@ -198,45 +198,39 @@ class ProductProduct(models.Model): fifo_automated_values[(row[0], row[1])] = (row[2], row[3], list(row[4])) for product in self: - if to_date: - price_used = product.get_history_price( - self.env.user.company_id.id, - date=to_date, - ) if product.cost_method in ['standard', 'average'] else 0.0 - 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.qty_at_date = product.with_context(company_owned=True, owner_id=False).qty_available - if product.cost_method == 'fifo': + if product.cost_method in ['standard', 'average']: + qty_available = product.with_context(company_owned=True, owner_id=False).qty_available + price_used = product.standard_price + if to_date: + price_used = product.get_history_price( + self.env.user.company_id.id, + date=to_date, + ) + product.stock_value = price_used * qty_available + product.qty_at_date = qty_available + 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.qty_at_date = product.with_context(company_owned=True, owner_id=False).qty_available product.stock_fifo_manual_move_ids = StockMove.browse(moves.ids) - elif product.cost_method in ['standard', 'average']: - product.stock_value = product.qty_at_date * price_used - 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, []) - product.qty_at_date = quantity - if product.cost_method == 'fifo': + 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, []) product.stock_value = value + product.qty_at_date = quantity product.stock_fifo_real_time_aml_ids = self.env['account.move.line'].browse(aml_ids) - elif product.cost_method in ['standard', 'average']: - product.stock_value = quantity * price_used - else: - if product.product_tmpl_id.valuation == 'manual_periodic': + else: + product.stock_value, moves = product._sum_remaining_values() product.qty_at_date = product.with_context(company_owned=True, owner_id=False).qty_available - if product.cost_method == 'fifo': - product.stock_value, product.stock_fifo_manual_move_ids = product._sum_remaining_values() - elif product.cost_method in ['standard', 'average']: - product.stock_value = product.qty_at_date * product.standard_price - 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, []) - product.qty_at_date = quantity - if product.cost_method == 'fifo': - product.stock_value = value + if product.product_tmpl_id.valuation == 'manual_periodic': + product.stock_fifo_manual_move_ids = moves + 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, []) product.stock_fifo_real_time_aml_ids = self.env['account.move.line'].browse(aml_ids) - elif product.cost_method in ['standard', 'average']: - product.stock_value = quantity * product.standard_price def action_valuation_at_date_details(self): """ Returns an action with either a list view of all the valued stock moves of `self` if the