In Odoo, sequences are used to generate unique identifiers for various documents such as orders, invoices, and quotations. While Odoo allows you to manually configure sequences, it’s often necessary to create default sequences dynamically, especially when dealing with multiple companies within the same Odoo instance. This is crucial in a multi-company setup, as each company should have its own set of document sequences to maintain proper isolation between their records.
In this article, we will walk through the steps of creating sequences dynamically for each company when the company is created in Odoo.
Step-by-Step Implementation
The implementation involves creating an XML file to trigger the sequence creation process, as well as extending the ir.sequence model in Python to define the logic for sequence creation.
1. Creating the XML File to Trigger Sequence Creation
First, we need to create an XML file that will trigger the creation of the sequences when a new company is created. This file contains the call to the Python method responsible for generating the sequences.
File Path: data/ir_sequence_data.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data noupdate="0">
<function model="ir.sequence" name="_create_sequences_for_companies"/>
</data>
</odoo>
Here, the function tag is used to call the _create_sequences_for_companies method in the ir.sequence model. The noupdate=”0″ ensures that the data is updated even when the module is upgraded.
2. Creating the Python Logic for Sequence Generation
Next, we need to define the method that will create the sequences for each company. This is done by extending the ir.sequence model and writing a custom method to generate sequences dynamically based on predefined settings.
File Path: models/ir_sequence.py
Code:
# -*- coding: utf-8 -*-
from odoo import models, api
class IrSequence(models.Model):
_inherit = ‘ir.sequence’
@api.model
def _create_sequences_for_companies(self):
"""
Create default sequences for each company.
This method is typically used in a multi-company setup to ensure
each company has its unique document sequences.
"""
companies = self.env[‘res.company’].search([])
models = [
{‘model’: ‘sale.order’, ‘prefix’: ‘SO’},
{‘model’: ‘purchase.order’, ‘prefix’: ‘PO’},
]
for company in companies:
for model_info in models:
model = model_info[‘model’]
prefix = model_info[‘prefix’]
sequence_code = f"{model}"
sequence_name = f"{company.name} – {model}"
# Check if sequence already exists for the company and model
existing_sequence = self.env[‘ir.sequence’].search([
(‘code’, ‘=’, sequence_code),
(‘company_id’, ‘=’, company.id)
], limit=1)
if not existing_sequence:
# Create a new sequence
self.env[‘ir.sequence’].create({
‘name’: sequence_name,
‘code’: sequence_code,
‘prefix’: prefix,
‘company_id’: company.id,
‘padding’: 4,
‘number_increment’: 1,
‘number_next’: 1,
‘implementation’: ‘no_gap’,
})
Explanation of the Code
- Model Inheritance (_inherit = ‘ir.sequence’):
- We extend the ir.sequence model to add our custom method _create_sequences_for_companies.
- _create_sequences_for_companies Method:
- This method fetches all the companies (res.company) in the system using self.env[‘res.company’].search([]).
- We define a list of models (models) for which we want to create sequences. Each model is paired with a prefix that will be used for the sequence.
- Creating Sequences:
- For each company and model, we check if a sequence already exists using self.env[‘ir.sequence’].search(…).
- If no existing sequence is found, a new sequence is created with the given prefix, company-specific settings (e.g., company_id), and sequence parameters such as padding, number_increment, and number_next.
- Avoiding Duplicates:
- The code ensures that sequences are not duplicated by checking if a sequence already exists for a specific model and company. This is done through the search query with (‘code’, ‘=’, sequence_code) and (‘company_id’, ‘=’, company.id).
Why Is This Useful?
In a multi-company setup, each company may need different sequence numbers for documents. Instead of manually creating sequences for each company, this method ensures that sequences are created automatically whenever a new company is added to the system.
By implementing this approach, It helps businesses keeps the system scalable and easier to maintain, especially when dealing with a large number of companies.
Conclusion
Automating the sequence creation process for new companies is a valuable feature in Odoo. By leveraging custom code in their.sequence model and using XML to trigger sequence creation, you can ensure each company has its own unique document sequences without having to manually configure them each time a new company is added.
This approach is ideal for businesses using Odoo in a multi-company environment, making document management more efficient and streamlined.