[REF] stock_account: multi company

Ensure proper domains are applied and enforced on relation fields thanks
to the `check_company` attributes.

Adapt `_compute_value_svl` to return the valuation for a specific
company.

Every time a property is read, make sure `force_company` is properly in
the context.

Fixed the ir.rule.

task-1985992
This commit is contained in:
Simon Lejeune
2019-09-03 15:05:03 +00:00
parent 27a4c07187
commit d43d25020e
5 changed files with 26 additions and 25 deletions
+8 -6
View File
@@ -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')
+10 -3
View File
@@ -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:
@@ -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)
@@ -4,20 +4,12 @@
<record id="stock.group_stock_manager" model="res.groups">
<field name="implied_ids" eval="[(4, ref('account.group_account_invoice'))]"/>
</record>
<!-- <record model="ir.rule" id="stock_history_rule">
<field name="name">Stock History multi-company</field>
<field name="model_id" search="[('model','=','stock.history')]" model="ir.model"/>
<field name="global" eval="True"/>
<field name="domain_force">['|',('company_id','=',False),('company_id', 'in', company_ids)]</field>
</record> -->
<record model="ir.rule" id="stock_valuation_layer_company_rule">
<field name="name">Stock Valuation Layer Multicompany</field>
<field name="model_id" search="[('model','=','stock.valuation.layer')]" model="ir.model"/>
<field name="global" eval="True"/>
<field name="domain_force">[('company_id','=',user.company_id.id)]</field>
<field name="domain_force">[('company_id', 'in', company_ids)]</field>
</record>
</data>
</odoo>
+3 -3
View File
@@ -9,9 +9,9 @@
<field name="arch" type="xml">
<group name="account_property" position="inside">
<group name="account_stock_property" string="Account Stock Properties" groups="account.group_account_user">
<field name="property_stock_account_input_categ_id" domain="[('deprecated', '=', False)]" attrs="{'required':[ ('property_valuation', '=', 'real_time')]}" />
<field name="property_stock_account_output_categ_id" domain="[('deprecated', '=', False)]" attrs="{'required':[ ('property_valuation', '=', 'real_time')]}" />
<field name="property_stock_valuation_account_id" options="{'no_create': True}" domain="[('deprecated', '=', False)]" attrs="{'required':[ ('property_valuation', '=', 'real_time')]}"/>
<field name="property_stock_account_input_categ_id" attrs="{'required':[ ('property_valuation', '=', 'real_time')]}" />
<field name="property_stock_account_output_categ_id" attrs="{'required':[ ('property_valuation', '=', 'real_time')]}" />
<field name="property_stock_valuation_account_id" options="{'no_create': True}" attrs="{'required':[ ('property_valuation', '=', 'real_time')]}"/>
<field name="property_stock_journal" attrs="{'required':[ ('property_valuation', '=', 'real_time')]}" />
</group>
</group>