In Odoo, the multi-company feature allows you to manage multiple companies within a single Odoo database instance. Each company can have its own configurations while still sharing common resources such as products, customers, and suppliers.
Size: 631.28 KB
Language: en
Added: May 31, 2024
Slides: 13 pages
Slide Content
Model Attribute _check_company_auto Property Enterprise
Introduction Enterpr ise In Odoo, the multi-company feature allows you to manage multiple companies within a single Odoo database instance. Each company can have its own configurations while still sharing common resources such as products, customers, and suppliers. In some cases, 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. That is, for example if we want to restrict a particular product’s access in only one branch of the company, it can be done with this _check_company_auto attribute.
Enterprise When we are using the company_id field in our model, then creating a relational field like a many2one, many2many one2many etc needs to be from the same company. The _check_company_auto and check_company are set to True to and each create and write on this model will check that the company given is correct. 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.
Enterprise First, lets create a setup Odoo with multiple companies as here we can see Head Office, Branch Company 1 and Branch Company 2.
Enterprise Then, lets create a module, here for example travel_management. Lets create a simple model which stores data of the travel booking. And just define some basic fields, menus, tree view, form view for the model.
Enterprise Create a product, lets say ‘Booking’, which can be selected from our travel management booking system through a Many2one field later. Set the Company of the product as Head Office.
Enterprise Define some fields to manage Travel booking. Here, I’ve created the class and fields as class TravelBooking(models.Model): _name = 'travel.booking' _description = 'Travel Card' _rec_name = 'booking_id' booking_id = fields.Char(string="Booking ID", required=True, readonly=True, default='New') partner_id = fields.Many2one('res.partner', string="Partner Name", required=True) company_id = fields.Many2one('res.company',string="Company Name") product_id = fields.Many2one('product.product',string="Product") partner_mobile = fields.Char(string="Mobile") passenger_count = fields.Integer(string="No of Passengers")
Enterprise And while creating travel booking records, we can see the product Booking will be accessible for all the companies when we switch
Enterprise Here comes the need for the access control for any such resources like products here. A person can log in to one company and create records for another company which may not be accurate in all cases. So, we use the class attribute _check_company_auto and set it to true. This will make sure that each create and write on this model will check that the company given is correct. ie, class TravelBooking(models.Model): _name = 'travel.booking' _description = 'Travel Card' _rec_name = 'booking_id' _check_company_auto = True Also, set the attribute check_company=True for the relational field which needs to be available only in the created company. product_id = fields.Many2one('product.product',string="Product", check_company=True)
Enterprise The product ‘booking’ can be selected and the record gets saved without as before itself, without any issue Now, let's check the Booking creation from the current company first.
Enterprise The product ‘Booking’ can’t be seen. Then , let's check the Booking creation from the other company.
Enterprise Here comes the error which says that the product doesn’t belong to the selected company. Now, just try to edit any previous record by changing the company name, keeping the product as same. Here, even the change in company must manage to check whether the already saved product’s access is correctly working or not. The check_company and _check_company_auto will work here.
For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com