Skip to content

Commit 6731163

Browse files
committed
[FIX] stock_account: added company context to products in fifo svls
**Steps to reproduce:** - Install Accounting + Inventory + Sales apps - Enable Automatic Accounting in Settings - Ensure two companies are available - Set one product category (same for both companies) - For company 1 (current) -> FIFO + automatic valuation (`'real_time'`) - For company 2 -> FIFO + manual valuation (`'manual_periodic'`) - Create a storable product for this category - From company 1: - Buy 10 products at 10$ each and receive them (Purchase + Delivery) - Sell 15 products at 10$ each and deliver them (Sale + Delivery) - Buy 10 products at 10$ (Purchase) - Switch to company 2 (keep company 1 checked) - From company 2: - Validate the third delivery order (WH/IN) - Go to Inventory/Reporting/Valuation - One journal entry is missing for the last move **Issue:** During the processing of the SVL in `_run_fifo_vacuum`, the product used its current env context company instead of the one provided explicitly in the parameters. This caused inconsistencies in valuation logic when the method used differed between the given companies, which results in a missing `account_move_id`. **Fix:** Ensure that the product is evaluated in the correct company context by using `self.with_company(company.id)` when looping on the products before the valuation logic. opw-4732067 closes odoo#214966 X-original-commit: fccb574 Signed-off-by: Steve Van Essche <[email protected]> Signed-off-by: Florentin Delcourt (defl) <[email protected]>
1 parent 49d0d87 commit 6731163

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

addons/stock_account/models/product.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def _run_fifo_vacuum(self, company=None):
446446
new_svl_vals_manual = []
447447
real_time_svls_to_vacuum = ValuationLayer
448448

449-
for product in self:
449+
for product in self.with_company(company.id):
450450
all_candidates = all_candidates_by_product[product.id]
451451
current_real_time_svls = ValuationLayer
452452
for svl_to_vacuum in svls_to_vacuum_by_product[product.id]:

addons/stock_account/tests/test_stock_valuation_layer_revaluation.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
# Part of Odoo. See LICENSE file for full copyright and licensing details.
33

4+
from odoo import Command
45
from odoo.exceptions import UserError
56
from odoo.tests import Form
67
from odoo.addons.stock_account.tests.test_stockvaluation import _create_accounting_data
@@ -321,3 +322,47 @@ def test_stock_valuation_layer_revaluation_partial(self):
321322
'active_ids': [old_layers[2].id],
322323
'active_model': 'stock.valuation.layer'
323324
})).save()
325+
326+
def test_multi_company_fifo_svl_negative_revaluation(self):
327+
"""
328+
Check that the journal entries and stock valuation layers are created for the company related
329+
to the stock move even if the picking is validated using a different one.
330+
"""
331+
company1 = self.env.company
332+
company2 = self.env['res.company'].create({
333+
'name': 'Lovely Company',
334+
})
335+
self.env.companies = company1 | company2
336+
337+
product = self.product1
338+
product.categ_id.write({
339+
'property_cost_method': 'fifo',
340+
'property_valuation': 'real_time',
341+
})
342+
# Modify valuation to manual_periodic for company2
343+
product.categ_id.with_company(company2).property_valuation = 'manual_periodic'
344+
345+
# Create moves to revaluate for company1
346+
self._make_in_move(product, 10, unit_cost=10, create_picking=True)
347+
self._make_out_move(product, 15, create_picking=True)
348+
349+
receipt = self.env['stock.picking'].create({
350+
'picking_type_id': self.picking_type_in.id,
351+
'location_id': self.supplier_location.id,
352+
'location_dest_id': self.stock_location.id,
353+
'move_ids': [Command.create({
354+
'name': 'test fifo',
355+
'product_id': product.id,
356+
'location_id': self.supplier_location.id,
357+
'location_dest_id': self.stock_location.id,
358+
'product_uom': self.uom_unit.id,
359+
'product_uom_qty': 10,
360+
'price_unit': 7,
361+
})]
362+
}).with_company(company2)
363+
receipt.action_confirm()
364+
receipt.button_validate()
365+
366+
svls = self.env['stock.valuation.layer'].search([('product_id', '=', product.id)])
367+
self.assertEqual(len(svls), 4, "Expected 4 valuation layers")
368+
self.assertTrue(all(svl.account_move_id for svl in svls), "All SVLs should be linked to a journal entry")

0 commit comments

Comments
 (0)