Multi-company Guidelines in Odoo 15
A main feature of Odoo is its ability to maintain multiple companies, which allows users to easily and smoothly maintain information from multiple companies. Also, it allows us to manage operations
from each company independently and share some data such as products, customers, etc.
Although, many multi-company-related issues will arise if we don’t manage them properly. Therefore, Odoo provides many features which help us to manage them correctly.
In this blog, we are going through each of these features.
Bellow listed are the features provided by Odoo to manage the multi-companies.
1. Company-dependent fields.
2. with_company.
3. check_company.
4. Default Company.
5. Views.
6. Security Rules.
Let’s have a detailed look at each of these features
1. Company-dependent fields.
There will be situations where we may access the same record from multiple companies. In such cases, we may need to store different values for a field when depending on each company. For example, we
may set different prices for a product for different companies.
For this, we must define our field with the attribute company_dependent, which makes the field of a record store different values.
company_details = fields.Text(string="Company Details", company_dependent=True)
The company_dependent attribute takes two values; True or False. If it is True, then the corresponding field will have different values depending on the company.
2)with_company
If we are reading the values of a company_dependent field, which is set from another company instead of the currently active company, we should make sure that we are retrieving the value for the
field from the correct company. For this purpose, Odoo ORM provides the with_company attribute.
@api.onchange('company_details')
def _onchange_company_details(self):
self = self.with_company(self.company_id)
3)check_company
If we are using records in multi-company, there will be chances to connect one company record to another. For example, if we have a company_id field in our model, then the relational field defined in that model should take values from the same company. Otherwise, it will throw multi-company-related errors.
To ensure this, Odoo provides two attributes called, _check_company_auto and check_company
_check_company_auto is a class attribute, which needs to define in a class definition below,
from odoo import fields, models
class CompanyDetail(models.Model):
_name = 'company.detail'
_check_company_auto = True
_check_company_auto attribute should be set to True. Thus, whenever creating or editing the records from this model, an automatic function will be triggered to check if the correct value is given or not.
check_company is a field attribute that can be given in a field definition like below,
user_id = fields.Many2one('res.users', string='Technician', check_company=True)
After adding check_company to a field a default domain [‘|’, ‘(‘company_id’, ‘=’, False), (‘company_id’, ‘=’, company_id)] will be added to the field. So there is no need for us to add this to such fields
4) Default Company
Odoo ORM also provides another attribute to set the default company in the company field of records. This will be more useful if the company_id field is a required field or if it is invisible to
users with no access to the group base.group_multi_company.
company_id = fields.Many2one('res.company', required=True, default=lambda self: self.env.company)
5) Views
There will be cases where the users have no access to multi companies. Such users should not have to view the company_id field in records. To hide the company field from such users, we can add a user
a group called base.group_multi_company in view like below.
<record model="ir.ui.view" id="company_detail_form_view">
<field name="name">company.detail.form</field>
<field name="model">company.detail</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<group>
<field name="company_id" groups="base.group_multi_company"/>
<field name="user_id"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
6) Security Rules
When working with multi-companies, it is important to ensure that users from other companies do not have access to records other than their companies.
Here comes the use of security rules based on company_ids which will make sure the accessibility of records of each company is secured.
If the record is shareable by all companies, the rule can be defined like this;
<record id="product_category_comp_rule" model="ir.rule">
<field name="name">Product Category multi-company</field>
<field name="model_id" ref="product.model_product_category"/>
<field name="domain_force">['|', ('company_id', 'in', company_ids), ('company_id', '=', False)]
</field>
</record>
If the record is restricted to only one company, the rule can be defined as below;
<record id="product_category_comp_rule" model="ir.rule">
<field name="name">Product Category multi-company</field>
<field name="model_id" ref="product.model_product_category"/>
<field name="domain_force">[('company_id', 'in', company_ids)]</field>
</record>
These are the features provided by Odoo to manage the multi-companies.