Odoo-india > News  > Multi-Company in Odoo Custom Module

Multi-Company in Odoo Custom Module

One of the important thing features in Odoo is multi-company flexibility. When we customise modules we must always guarantee that it would match the multi-company setup. In order to deal with odoo multi-company features, Odoo ORM supplies totally different options.let’s take a look.
First, we’re going to focus on among the subject attributes, class attributes, safety guidelines that we used to configure a number of corporations.
class BlogDemo(models.Model):
    _name = ‘blog.demo 
    company_id = fields.Many2one('res.company', string='Company',  default=lambda self: self.env.company)
We ought to outline an organization subject which is the essential factor that we must always add whereas defining a mannequin. We could make our document made shareable between a number of corporations by defining an organization subject.establishing a default worth for the corporate is an effective apply.for that we are able to use totally different strategies.
company_dependent
In many instances, there will probably be some fields whose worth will probably be totally different for various corporations so in order to deal with that now we have a particular attribute referred to as company_dependent.it may have two values True and False. whether it is set to True if the actual subject will probably be having totally different values primarily based on the corporate.
company_dependent_value_ex = fields.Char(company_dependent=True)
The firm dependent fields should not saved in the mannequin‘s desk. it’s saved beneath `ir.property`. when the need to fetch the worth of firm dependent subject an ‘ir.property’ is searched return the worth linked to the present company.
If we modified the worth of a subject it modifies both present property for the present document if it would not exist it would create a brand new one for the present firm.
force_company
In order to learn firm dependent subject values, we use this attribute. In this, we try to learn the worth of the sphere as the corporate Company Test.
record.with_context(force_company=Company Test.id).company_dependent_field
Check_company
When we make our data shareable between corporations there could also be an opportunity to link one firm document to one other. In order to deal with that odoo provides two attributes.
BlogDemo(models.Model):
    _name = ‘blog.demo’
   _check_company_auto = True
Relational_field1 =  fields.Many2one('other.relation model’, check_company=True)
We have to set the category attribute _check_company_auto to True on a mannequin it would ensure ‘_check_company’ known as on creating and write strategies enforcing the multi-company domain.
The field attribute ‘check_company=True’  will set a default area [‘|’, ‘(‘company_id’, ‘=’, False), (‘company_id’, ‘=’, company_id)] if no area is outlined on the relational subject and likewise permits you to name _check_company on the data to make sure the area is revered.
Views
If the consumer doesn’t have entry to a number of corporations often the corporate subject is hidden from view. This is achieved by including teams=”base.group_multi_company” in our view
<field name="company_id" groups="base.group_multi_company"/>
Security rules
When we use multi-company arrange we must always guarantee that the consumer in one firm didn’t have entry to the data in one other firm. For that, we must always write safety guidelines. We are including the area to the company_id field.
If the record is shareable by all companies we are able to have a rule like this
<record model="ir.rule" id="record_shared_multi_company_rule">
    <field name="name">Shared Records:model</field>
    <field name="model_id" ref="module.model_model"/>
    <field name="global" eval="True"/>
    <subject title="domain_force">
        ['|', ('company_id', '=', False), ('company_id', 'in', company_ids)]
    </field>
</record>
company_ids incorporates present corporations of the consumer
If the document is restricted to one firm
<record model="ir.rule" id="record_not_shared_multi_company_rule">
    <field name="name">Non Shared Records:model</field>
    <field name="model_id" ref="module.model_model"/>
    <field name="global" eval="True"/>
    <field name="domain_force">
       [('company_id', 'in', company_ids)]
    </field>
</record>
No Comments

Leave a reply