Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Same field Branch sequence different how to do?

on 3/19/14, 6:01 AM 2,385 views

I having one field, I set that auto generation field. There are 5 more branches in that company if one branch user is open and create the that number is QWE001 at the same time another branch user open and create the form that number is ASD001, other branch ZXC001 like this how to do in OpenERP is it possible to do. This is my code how to do it.

class abc_product(osv.osv):
    _name = 'abc.product'
    _columns = {       
        'branch_id':fields.many2one('branch', 'Branch', required=True,),
        'sample_s_no':fields.char('Sample S.No', size=12, readonly=True, states={'draft': [('readonly', False)], 'waiting_approve': [('readonly', False)]}),

   def create(self, cr, uid, vals, context=None):        
        if context is None:
            context = {}
        context['force_branch'] = vals['branch_id']
        print context['force_branch'],"ddddddddddddddddddddd"      
        if vals.get('sample_s_no', '/') == False:
            vals['sample_s_no'] = self.pool.get('ir.sequence').next_by_code(
           cr, uid, 'abc.product', context=context) or '/'
        return super(abc_product, self).create(cr, uid, vals, context=context)

and the XML is <record id="seq_type_abc_product" model="ir.sequence.type"> <field name="name">ABC Product</field> <field name="code">abc.product</field> </record>

    <record id="seq_abc_product" model="ir.sequence">
        <field name="name">Abc Product</field>
        <field name="code">abc.product</field>
        <field name="prefix">KKR</field>
        <field name="padding">4</field>
        <field name="suffix">-%(year)s</field>
        <field name="branch_id" eval="1"/>
    <record id="seq_abc_products" model="ir.sequence">
        <field name="name">ABC Product</field>
        <field name="code">abc.product</field>
        <field name="prefix">AMN</field>
        <field name="padding">4</field>
        <field name="suffix">-%(year)s</field>
        <field name="branch_id" eval="2"/>

How to solve this.

@Sridhar. Did you solve the issue? Please help me too.

on 4/2/14, 8:21 AM

No, I dont complete this, I put Nehal's function in my code this code is calling, thats why i put pending this task, if you complete help me.

on 4/2/14, 8:25 AM


| 5 4 7
Bikaner, India

OpenERP Devloper at SunARC Technology

On 4/3/14, 12:52 PM


class ir_sequence(osv.osv):
    _inherit = "ir.sequence"

    _columns = {
        'branch_id': fields.many2one('sample.branch', 'Branch'),

    def _next(self, cr, uid, seq_ids, context=None):
        if not seq_ids:
            return False
        if context is None:
            context = {}
        force_company = context.get('force_company')
        force_branch = context.get('force_branch')
        if not force_company:
            force_company = self.pool.get('res.users').browse(cr, uid, uid).company_id.id
        sequences = self.read(cr, uid, seq_ids, ['name','branch_id','implementation','number_next','prefix','suffix','padding'])
        preferred_sequences = [s for s in sequences if s['branch_id'] and s['branch_id'][0] == force_branch ]
        seq = preferred_sequences[0] if preferred_sequences else sequences[0]
        if seq['implementation'] == 'standard':
            cr.execute("SELECT nextval('ir_sequence_%03d')" % seq['id'])
            seq['number_next'] = cr.fetchone()
            cr.execute("SELECT number_next FROM ir_sequence WHERE id=%s FOR UPDATE NOWAIT", (seq['id'],))
            cr.execute("UPDATE ir_sequence SET number_next=number_next+number_increment WHERE id=%s ", (seq['id'],))
        d = self._interpolation_dict()
            interpolated_prefix = self._interpolate(seq['prefix'], d)
            interpolated_suffix = self._interpolate(seq['suffix'], d)
        except ValueError:
            raise osv.except_osv(_('Warning'), _('Invalid prefix or suffix for sequence \'%s\'') % (seq.get('name')))
        return interpolated_prefix + '%%0%sd' % seq['padding'] % seq['number_next'] + interpolated_suffix


<?xml version="1.0" encoding="utf-8"?>
    <data noupdate="1">

        <!-- Sequences for branch.tname -->
        <record id="seq_type_sample_branch" model="ir.sequence.type">
            <field name="name">Branch</field>
            <field name="code">branch.tname</field>

        <record id="seq_branch1" model="ir.sequence">
            <field name="name">Branch 1</field>
            <field name="code">branch.tname</field>
            <field name="prefix">QWE</field>
            <field name="padding">3</field>
            <field name="branch_id" eval="1"/> <!-- set branch 1 DB ID value -->

        <record id="seq_branch2" model="ir.sequence">
            <field name="name">Branch 2</field>
            <field name="code">branch.tname</field>
            <field name="prefix">ASD</field>
            <field name="padding">3</field>
            <field name="branch_id" eval="2"/> <!-- set branch 2 DB ID value -->

       <record id="seq_branch3" model="ir.sequence">
            <field name="name">Branch 3</field>
            <field name="code">branch.tname</field>
            <field name="prefix">ZXC</field>
            <field name="padding">3</field>
            <field name="branch_id" eval="3"/> <!-- set branch 3 DB ID value -->


class branch_tname(osv.osv):
    _name ="branch.tname"

    _columns = {
        'branch_id': fields.many2one('sample.branch', 'Branch'),
        'sequence_code': fields.char('Employee ID', size = 16, translate = True),  

    def create(self, cr, uid, vals, context=None):        
        if context is None:
            context = {}
        context['force_branch'] = vals['branch_id']
        if vals.get('sequence_code', '/') == False:
            vals['sequence_code'] = self.pool.get('ir.sequence').next_by_code(
           cr, uid, 'branch.tname', context=context) or '/'        
        return super(branch_tname, self).create(cr, uid, vals, context=context)

Updated the code based on your requirement

Hi Prakash, Thanks for reply, Actually Here i create a branch basic that is i create Branch as many2one, user can select this branch, i didn't use base company table, i want everything as new. how to do that.

on 4/4/14, 2:04 AM

In the xml eval="1" means branch_id's ID is 1 means this sequence will call is it correct.

on 4/5/14, 4:02 AM

In the xml code set QWE Sequence for Branch 1 (DB id), ASD Sequence for Branch 2 (DB id) and so on..

on 4/5/14, 4:07 AM

In the create method sequence return based on context['force_branch'] branch id. so it call difference sequence based on the branch .

on 4/5/14, 4:12 AM

when i print context['force_branch'] i got value 1 or 2.

on 4/5/14, 4:20 AM

It will show selected branch DB ID then it return related Branch sequence output

on 4/5/14, 4:23 AM

but it is now updating in sequence why?

on 4/5/14, 4:25 AM

sorry, what u get output? create 3 different records and with difference branch and pls give me the result.

on 4/5/14, 4:28 AM

Now It is working Fine Thank You..., <field name="branch_id" eval="1"/> i make mistake to this part now only i identify that, Problem solved.

on 4/5/14, 4:31 AM

@Remya I checked your module in emp.py file create method is not called. pls you check the same.

on 4/5/14, 4:51 AM

check your each sequence created with different company. using select company_id, name from ir_sequence where code='hr.employee'

on 4/5/14, 5:16 AM

<record id="seq_hr_employee_com1" model="ir.sequence"> <field name="name">Employee Id(com1)</field><field name="code">hr.employee</field><field name="prefix">SOF</field> <field name="padding">3</field> <field name="company_id" eval="1"/> <!-- SET SOf Company ID!--> </record> <record id="seq_hr_employee_com2" model="ir.sequence"> <field name="name">Employee Id(com2)</field> <field name="code">hr.employee</field> <field name="prefix">FAC</field> <field name="padding">3</field> <field name="company_id" eval="3"/></record>

on 4/5/14, 5:33 AM

pls give me the output of the below query 1) select company_id, name from ir_sequence where code='hr.employee' 2) select id, name from res_company

on 4/5/14, 5:44 AM

Make sure the above output each sequence assigned related company. In the create method return the sequence value based on assigned company. you can debug the code using print statement in create method and check the result

on 4/5/14, 5:57 AM


| 5 2 8
Ahmadabad, India
On 3/19/14, 7:04 AM


You need to override the method next_by_code of ir_sequence. You can pass the branch user field in context and then by checking value of context, you can call the different sequence based on branch user.

You may try something like :

Check for the sequence of specific company of user

def next_by_code(self, cr, uid, sequence_code, context=None):
    self.check_access_rights(cr, uid, 'read')
    company_ids = self.pool.get('res.company').search(cr, uid, [], context=context) + [False]
    users_company_id = self.pool.get('res.users').browse(cr, uid, uid, context).company_id
    ids = self.search(cr, uid, ['&', ('code', '=', sequence_code), ('company_id', '=', users_company_id)])
    return self._next(cr, uid, ids, context)

and for create method:

def create(self, cr, uid, vals, context=None):        
        if vals.get('auto_seq', '/') == False:
            vals['auto_seq'] = self.pool.get('ir.sequence').next_by_code(
                cr, uid, 'crm.lead') or '/'

Hope this helps.

Thanks Nehal how to do this.

on 3/19/14, 7:21 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)


Asked: 3/19/14, 6:01 AM
Seen: 2385 times
Last updated: 10/20/15, 2:47 PM