I want to create a custom sequence. I've tried it but it doesn't work in the 'company_register' section.
This is "company.xml":
BODEBEK 1
Bogor 1
Depok 1
Bekasi 1
Company 2 2 Company 3 3 Company 4 4 Company 5 5 Company 6 6 Company 7 7 Company 8 8 Company 9 9 Company 10 10 Company 11 11 Company 12 12 Company 13 13 Company 14 14 Company 15 15 Company 16 16 Company 17 17
This is the Company model, so there are 17 companies:
class Company(models.Model)
_inherit = 'res.company'
parent_id = fields.Many2one('res.company', string="Parent Company")
child_ids = fields.One2many('res.company', 'parent_id', string="Branch Companies")
company_registry = fields.Char(string="Company Registry", required=True)
@api.model
def create_branch_companies(self):
parent_company = self.env['res.company'].search([('name', '=', 'BODEBEK')], limit=1)
if not parent_company:
raise ValueError("Parent company 'BODEBEK' not found")
branch_names = ['Bogor', 'Depok', 'Bekasi']
for branch_name in branch_names:
self.env['res.company'].create({
'name': branch_name,
'parent_id': parent_company.id,
'company_registry': '1',
})
return True
and this is the Sale Order model where 'reference_code' field resides:
class SaleOrder(models.Model):
_inherit = 'sale.order'
reference_code = fields.Char(string="Order Reference", readonly=True, copy=False)
company_id = fields.Many2one('res.company', string="Company", required=True)
@api.model
def _get_sequence(self, company_id):
company = self.env['res.company'].browse(company_id)
sequence_code = 'sale.order'
sequence = self.env['ir.sequence'].search([
('code', '=', sequence_code),
('company_id', '=', company_id)
])
if not sequence:
sequence = self.env['ir.sequence'].create({
'name': f'Sale Order Reference {company.company_registry}',
'code': sequence_code,
'company_id': company_id,
'prefix': f'{company.company_registry}-',
'padding': 5,
'number_next': 1,
'number_increment': 1,
})
return sequence
@api.model
def create(self, vals):
if vals.get('company_id'):
sequence = self._get_sequence(vals['company_id'])
vals['reference_code'] = f"{sequence.prefix{sequence.next_by_id(sequence.id)}"
return super(SaleOrder, self).create(vals)
can you explain better? especially with the 'company.xml' file