From 9a2b2f43bbdfbfffd359271c9dad7e53594ee164 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Tue, 16 Jan 2018 21:41:50 +0100 Subject: [PATCH] [IMP] stock_account: product_id in standard price update's accounting moves When updating the cost on products with realtime valuation, the journal entries do not have their product_id set, whcih make it difficult to track/filter on them. Closes #18492 Closes #22281 Task 60955 --- addons/stock_account/models/product.py | 3 ++ .../tests/test_stockvaluation.py | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/addons/stock_account/models/product.py b/addons/stock_account/models/product.py index 098e976fc7e..24c9ad93422 100644 --- a/addons/stock_account/models/product.py +++ b/addons/stock_account/models/product.py @@ -143,16 +143,19 @@ class ProductProduct(models.Model): move_vals = { 'journal_id': product_accounts[product.id]['stock_journal'].id, 'company_id': location.company_id.id, + 'ref': product.default_code, 'line_ids': [(0, 0, { 'name': _('Standard Price changed - %s') % (product.display_name), 'account_id': debit_account_id, 'debit': abs(diff * qty_available), 'credit': 0, + 'product_id': product.id, }), (0, 0, { 'name': _('Standard Price changed - %s') % (product.display_name), 'account_id': credit_account_id, 'debit': 0, 'credit': abs(diff * qty_available), + 'product_id': product.id, })], } move = AccountMove.create(move_vals) diff --git a/addons/stock_account/tests/test_stockvaluation.py b/addons/stock_account/tests/test_stockvaluation.py index ccc4a6fe76f..fa0c8e394af 100644 --- a/addons/stock_account/tests/test_stockvaluation.py +++ b/addons/stock_account/tests/test_stockvaluation.py @@ -17,6 +17,7 @@ class TestStockValuation(TransactionCase): self.product1 = self.env['product.product'].create({ 'name': 'Product A', 'type': 'product', + 'default_code': 'prda', 'categ_id': self.env.ref('product.product_category_all').id, }) self.product2 = self.env['product.product'].create({ @@ -43,6 +44,11 @@ class TestStockValuation(TransactionCase): 'code': 'Stock Valuation', 'user_type_id': self.env.ref('account.data_account_type_current_assets').id, }) + self.expense_account = Account.create({ + 'name': 'Expense Account', + 'code': 'Expense Account', + 'user_type_id': self.env.ref('account.data_account_type_expenses').id, + }) self.stock_journal = self.env['account.journal'].create({ 'name': 'Stock Journal', 'code': 'STJTEST', @@ -76,6 +82,33 @@ class TestStockValuation(TransactionCase): ('account_id', '=', self.stock_valuation_account.id), ], order='date, id') + def test_realtime(self): + """ Stock moves update stock value with product x cost price, + price change updates the stock value based on current stock level. + """ + # Enter 10 products while price is 5.0 + self.product1.standard_price = 5.0 + move1 = self.env['stock.move'].create({ + 'name': 'IN 10 units @ 10.00 per unit', + 'location_id': self.supplier_location.id, + 'location_dest_id': self.stock_location.id, + 'product_id': self.product1.id, + 'product_uom': self.uom_unit.id, + 'product_uom_qty': 10.0, + }) + move1._action_confirm() + move1._action_assign() + move1.move_line_ids.qty_done = 10.0 + move1._action_done() + + # Set price to 6.0 + self.product1.do_change_standard_price(6.0, self.expense_account.id) + stock_aml, price_change_aml = self._get_stock_valuation_move_lines() + self.assertEqual(stock_aml.debit, 50) + self.assertEqual(price_change_aml.debit, 10) + self.assertEqual(price_change_aml.ref, 'prda') + self.assertEqual(price_change_aml.product_id, self.product1) + def test_fifo_perpetual_1(self): self.product1.product_tmpl_id.cost_method = 'fifo'