Odoo Help

Welcome!

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.

0

Same field Branch sequence different how to do?

By
sridhar
on 3/19/14, 6:01 AM 2,468 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.

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

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

sridhar
on 4/2/14, 8:25 AM
2

Prakash

--Prakash--
3173
| 5 4 7
Bikaner, India
--Prakash--

OpenERP Devloper at SunARC Technology

Prakash
On 4/3/14, 12:52 PM

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

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.

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

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

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

Prakash
on 4/5/14, 4:12 AM

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

sridhar
on 4/5/14, 4:20 AM

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

Prakash
on 4/5/14, 4:23 AM

but it is now updating in sequence why?

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

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

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

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

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

Remya
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

Prakash
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

Prakash
on 4/5/14, 5:57 AM
1

Nehal

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

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.

Thanks Nehal how to do this.

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

Stats

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