Skip to Content
Menu
This question has been flagged
2 Replies
8918 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>
    <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"/>
    </record>

How to solve this.

Avatar
Discard

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

Author

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.

Best Answer

ir_sequence_branch.py

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()
        else:
            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()
        try:
            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

branch_sequence.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <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>

        <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>

        <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>

       <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 -->
        </record>
    </data>
</openerp>

brachseq.py

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

Avatar
Discard
Author

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.

Author

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

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

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

Author

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

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

Author

but it is now updating in sequence why?

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

Author

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.

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

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

<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>

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

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

Best Answer

Hi,

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.

Avatar
Discard
Author

Thanks Nehal how to do this.