How to Handle Multi-Company Data in Odoo 13

How to Handle Multi-Company Data in Odoo 13

2 minutes, 51 seconds Read

While in a multi-company environment, we need to consider some important factors in order to make sure that the data in each company don’t get mixed up and lead to access errors. For example, a person can log in to one company and create records for another company which may not be accurate in all cases. Odoo’s ORM provides some features to make sure that the company behaviors are correctly managed. This includes:
    1. Company dependent fields
    2. Multi-company consistency
    3. Default company
    4. Views
    5. Security rules 

Company dependant fields:
While creating a new model, we should consider the fields that will have different values based on the company. While creating such fields, we should set the attribute company_dependant as True.

company_data = fields.Text(string='Company Data”', company_dependent=True)

The value read from the company dependent field will be based on the current company.

If you want to read data of a particular field from another company, with the id of the required company you can use the context key force_company.

move = self.env[required.model].with_context(force_company=company_id.id).company_data

Multi-Company consistency:
When we are using the company_id field in our model, then creating a relational field like a many-to-one, many2many one2many etc needs to be from the same company. So we should set the class attribute _check_company_auto as true and make sure that the relational field in the model is given the attribute check_company as true. This will make sure that each create and write on this model will check that the company given is correct.

If the check company auto is set to true, it will call the _check_company function and check if the value given is correct or not. When a field is defined without any domain and check_company is set to true, then a domain of the form: [‘|’, ‘(‘company_id’, ‘=’, False), (‘company_id’, ‘=’, company_id)] gets added to the field by default. The company_id field need not be given the check company attribute.

class CrmTeam(models.Model):
   _name = "crm.team"
_check_company_auto = True
user_id = fields.Many2one('res.users', string='Team Leader', check_company=True)

Default company:
The records created from the current company will most probably use the current company in the company_id field. Also if the company_id is a required field, then it will be better to set a default value for it.

company_id = fields.Many2one('res.company', string='Company', required=True, readonly=True, default=lambda self: self.env.user.company_id)

Views:
We can set the visibility of the company_id field based on whether we are in a multi-company environment or not. The group base.group_multi_company will determine this case. So we can set the visibility based on this group.

<field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>

Security rules:
Security rules or record rules can be set to make sure that one company data is not accessible to another company. 

<record model="ir.rule" id="sale_order_comp_rule">
   <field name="name">Sales Order multi-company</field>
   <field name="model_id" ref="model_sale_order"/>
   <field name="global" eval="True"/>
   <field name="domain_force">[('company_id', 'in', company_ids)]</field>
</record>

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *