diff --git a/addons/stock_account/models/product.py b/addons/stock_account/models/product.py index 03f9625f466..2576343ea9c 100644 --- a/addons/stock_account/models/product.py +++ b/addons/stock_account/models/product.py @@ -97,12 +97,13 @@ class ProductProduct(models.Model): stock_valuation_layer_ids = fields.One2many('stock.valuation.layer', 'product_id') @api.depends('stock_valuation_layer_ids') - @api.depends_context('to_date') + @api.depends_context('to_date', 'force_company') def _compute_value_svl(self): """Compute `value_svl` and `quantity_svl`.""" + company_id = self.env.context.get('force_company', self.env.company.id) domain = [ ('product_id', 'in', self.ids), - ('company_id', '=', self.env.company.id) + ('company_id', '=', company_id), ] if self.env.context.get('to_date'): to_date = fields.Datetime.to_datetime(self.env.context['to_date']) @@ -172,7 +173,7 @@ class ProductProduct(models.Model): """ # Handle stock valuation layers. svl_vals_list = [] - company_id = self.env.user.company_id + company_id = self.env.company for product in self: if product.cost_method not in ('standard', 'average'): continue @@ -634,22 +635,23 @@ class ProductCategory(models.Model): """) property_stock_journal = fields.Many2one( 'account.journal', 'Stock Journal', company_dependent=True, + domain="[('company_id', '=', allowed_company_ids[0])]", check_company=True, help="When doing automated inventory valuation, this is the Accounting Journal in which entries will be automatically posted when stock moves are processed.") property_stock_account_input_categ_id = fields.Many2one( 'account.account', 'Stock Input Account', company_dependent=True, - domain=[('deprecated', '=', False)], + domain="[('company_id', '=', allowed_company_ids[0]), ('deprecated', '=', False)]", check_company=True, help="""When doing automated inventory valuation, counterpart journal items for all incoming stock moves will be posted in this account, unless there is a specific valuation account set on the source location. This is the default value for all products in this category. It can also directly be set on each product.""") property_stock_account_output_categ_id = fields.Many2one( 'account.account', 'Stock Output Account', company_dependent=True, - domain=[('deprecated', '=', False)], + domain="[('company_id', '=', allowed_company_ids[0]), ('deprecated', '=', False)]", check_company=True, help="""When doing automated inventory valuation, counterpart journal items for all outgoing stock moves will be posted in this account, unless there is a specific valuation account set on the destination location. This is the default value for all products in this category. It can also directly be set on each product.""") property_stock_valuation_account_id = fields.Many2one( 'account.account', 'Stock Valuation Account', company_dependent=True, - domain=[('deprecated', '=', False)], + domain="[('company_id', '=', allowed_company_ids[0]), ('deprecated', '=', False)]", check_company=True, help="""When automated inventory valuation is enabled on a product, this account will hold the current value of the products.""",) @api.constrains('property_stock_valuation_account_id', 'property_stock_account_output_categ_id', 'property_stock_account_input_categ_id') diff --git a/addons/stock_account/models/stock_move.py b/addons/stock_account/models/stock_move.py index d41ffa3fd68..d5eb694f6b7 100644 --- a/addons/stock_account/models/stock_move.py +++ b/addons/stock_account/models/stock_move.py @@ -148,6 +148,7 @@ class StockMove(models.Model): """ svl_vals_list = [] for move in self: + move = move.with_context(force_company=move.company_id.id) valued_move_lines = move._get_in_move_lines() valued_quantity = 0 for valued_move_line in valued_move_lines: @@ -170,6 +171,7 @@ class StockMove(models.Model): """ svl_vals_list = [] for move in self: + move = move.with_context(force_company=move.company_id.id) valued_move_lines = move._get_out_move_lines() valued_quantity = 0 for valued_move_line in valued_move_lines: @@ -189,6 +191,7 @@ class StockMove(models.Model): """ svl_vals_list = [] for move in self: + move = move.with_context(force_company=move.company_id.id) valued_move_lines = move.move_line_ids valued_quantity = 0 for valued_move_line in valued_move_lines: @@ -251,6 +254,7 @@ class StockMove(models.Model): stock_valuation_layers |= getattr(todo_valued_moves, '_create_%s_svl' % valued_type)() continue + for svl in stock_valuation_layers: if not svl.product_id.valuation == 'real_time': continue @@ -258,6 +262,8 @@ class StockMove(models.Model): continue svl.stock_move_id._account_entry_move(svl.quantity, svl.description, svl.id, svl.value) + stock_valuation_layers._check_company() + # For every in move, run the vacuum for the linked product. products_to_vacuum = valued_moves['in'].mapped('product_id') company = valued_moves['in'].mapped('company_id') and valued_moves['in'].mapped('company_id')[0] or self.env.company @@ -288,8 +294,8 @@ class StockMove(models.Model): tmpl_dict = defaultdict(lambda: 0.0) # adapt standard price on incomming moves if the product cost_method is 'average' std_price_update = {} - for move in self.filtered(lambda move: move._is_in() and move.product_id.cost_method == 'average'): - product_tot_qty_available = move.product_id.quantity_svl + tmpl_dict[move.product_id.id] + for move in self.filtered(lambda move: move._is_in() and move.with_context(force_company=move.company_id.id).product_id.cost_method == 'average'): + product_tot_qty_available = move.product_id.with_context(force_company=move.company_id.id).quantity_svl + tmpl_dict[move.product_id.id] rounding = move.product_id.uom_id.rounding valued_move_lines = move._get_in_move_lines() @@ -304,7 +310,7 @@ class StockMove(models.Model): new_std_price = move._get_price_unit() else: # Get the standard price - amount_unit = std_price_update.get((move.company_id.id, move.product_id.id)) or move.product_id.standard_price + amount_unit = std_price_update.get((move.company_id.id, move.product_id.id)) or move.product_id.with_context(force_company=move.company_id.id).standard_price qty = forced_qty or qty_done new_std_price = ((amount_unit * product_tot_qty_available) + (move._get_price_unit() * qty)) / (product_tot_qty_available + qty) @@ -317,6 +323,7 @@ class StockMove(models.Model): """ Return the accounts and journal to use to post Journal Entries for the real-time valuation of the quant. """ self.ensure_one() + self = self.with_context(force_company=self.company_id.id) accounts_data = self.product_id.product_tmpl_id.get_product_accounts() if self.location_id.valuation_out_account_id: diff --git a/addons/stock_account/models/stock_valuation_layer.py b/addons/stock_account/models/stock_valuation_layer.py index 2e17ae0a247..3f20ff617af 100644 --- a/addons/stock_account/models/stock_valuation_layer.py +++ b/addons/stock_account/models/stock_valuation_layer.py @@ -14,7 +14,7 @@ class StockValuationLayer(models.Model): _rec_name = 'product_id' company_id = fields.Many2one('res.company', 'Company', readonly=True, required=True) - product_id = fields.Many2one('product.product', 'Product', readonly=True, required=True) + product_id = fields.Many2one('product.product', 'Product', readonly=True, required=True, check_company=True) quantity = fields.Float('Quantity', digits=0, help='Quantity', readonly=True) uom_id = fields.Many2one(related='product_id.uom_id', readonly=True, required=True) currency_id = fields.Many2one('res.currency', 'Currency', related='company_id.currency_id', readonly=True, required=True) @@ -23,8 +23,8 @@ class StockValuationLayer(models.Model): remaining_qty = fields.Float(digits=0, readonly=True) remaining_value = fields.Monetary('remaining_value Value', readonly=True) description = fields.Char('Description', readonly=True) - stock_valuation_layer_id = fields.Many2one('stock.valuation.layer', 'Linked To', readonly=True) + stock_valuation_layer_id = fields.Many2one('stock.valuation.layer', 'Linked To', readonly=True, check_company=True) stock_valuation_layer_ids = fields.One2many('stock.valuation.layer', 'stock_valuation_layer_id') - stock_move_id = fields.Many2one('stock.move', 'Stock Move', readonly=True) - account_move_id = fields.Many2one('account.move', 'Journal Entry', readonly=True) + stock_move_id = fields.Many2one('stock.move', 'Stock Move', readonly=True, check_company=True) + account_move_id = fields.Many2one('account.move', 'Journal Entry', readonly=True, check_company=True) diff --git a/addons/stock_account/security/stock_account_security.xml b/addons/stock_account/security/stock_account_security.xml index 4a6f38ee61a..b3bc52742e3 100644 --- a/addons/stock_account/security/stock_account_security.xml +++ b/addons/stock_account/security/stock_account_security.xml @@ -4,20 +4,12 @@ - - - Stock Valuation Layer Multicompany - [('company_id','=',user.company_id.id)] + [('company_id', 'in', company_ids)] diff --git a/addons/stock_account/views/product_views.xml b/addons/stock_account/views/product_views.xml index 737efedb15a..950005a750d 100644 --- a/addons/stock_account/views/product_views.xml +++ b/addons/stock_account/views/product_views.xml @@ -9,9 +9,9 @@ - - - + + +