Implementing Company-Specific Sequences in Odoo 19: A Complete Developer Guide
When managing multiple companies in Odoo 19, maintaining separate and organized numbering systems for documents like invoices, sales orders, and purchase orders is crucial. This tutorial provides a comprehensive guide to implementing company-specific sequences that ensure each business entity has its own independent numbering scheme.
Why Company-Wise Sequences Matter
In a multi-company Odoo environment, having distinct sequences for each company helps with:
- Clear identification - Instantly know which company a document belongs to
- Regulatory compliance - Meet legal requirements for separate numbering
- Audit trails - Maintain clean, company-specific document histories
- Avoiding conflicts - Prevent duplicate numbers across companies
Step 1: Define the Model
First, create your model with a field that will store the sequence number and link it to a company:
from odoo import models, fields, api
class CompanyDocument(models.Model):
_name = 'company.document'
_description = 'Company Document with Sequence'
name = fields.Char(
string='Reference',
required=True,
copy=False,
readonly=True,
default='New'
)
company_id = fields.Many2one(
'res.company',
string='Company',
required=True,
default=lambda self: self.env.company
)
description = fields.Text('Description')
date = fields.Date('Date', default=fields.Date.today)
@api.model
def create(self, vals):
if vals.get('name', 'New') == 'New':
company_id = vals.get('company_id') or self.env.company.id
vals['name'] = self.env['ir.sequence'].with_company(
company_id
).next_by_code('company.document') or 'New'
return super().create(vals)
Step 2: Define the Sequence in XML
Create the sequence definition in your module's data file. The key is setting company_id to False initially, then creating company-specific sequences:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<!-- Base sequence (template) -->
<record id="seq_company_document" model="ir.sequence">
<field name="name">Company Document Sequence</field>
<field name="code">company.document</field>
<field name="prefix">DOC/%(year)s/</field>
<field name="padding">5</field>
<field name="company_id" eval="False"/>
</record>
<!-- Company A specific sequence -->
<record id="seq_company_document_company_a" model="ir.sequence">
<field name="name">Company A Document Sequence</field>
<field name="code">company.document</field>
<field name="prefix">COMP-A/%(year)s/</field>
<field name="padding">5</field>
<field name="company_id" ref="base.main_company"/>
</record>
</data>
</odoo>
Step 3: Using with_company() Method
The with_company() method is crucial for fetching the correct sequence:
# Get sequence for specific company
sequence = self.env['ir.sequence'].with_company(company_id).next_by_code('company.document')
# Or use the current user's company
sequence = self.env['ir.sequence'].next_by_code('company.document')
Best Practices
- Use meaningful prefixes - Include company codes or abbreviations in prefixes
- Include year/month in sequences - Helps with document organization
- Set appropriate padding - Ensure enough digits for your volume
- Test with multiple companies - Verify sequences work correctly across all companies
- Handle edge cases - What happens when company_id is not set?
Conclusion
Implementing company-wise sequences in Odoo 19 is straightforward once you understand the pattern. By using the with_company() method and properly configuring your sequences, you can maintain clean, organized numbering across all your business entities.
Need Help with Multi-Company Odoo Setup?
Our experts can help you configure company-wise sequences, multi-company accounting, and more.
About the author
Founder & CEO, Braincuber Technologies
Founder and CEO of Braincuber. Has scoped and shipped 500+ Odoo, AI, and cloud projects for US mid-market and global brands. Takes every founder call personally — no SDR layer between buyers and the people building the system.
