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

Auto-incremented field

By
Jihen BEN ALI
on 7/25/15, 5:58 AM 502 views

Hi,

I had added a auto-incremented field in the customer form and the supplier. My problem is when I add a new customer automatically the supplier_code is incremented: in the res_partner table I had 2 fields(row) code_client and code_supplier when I add a new customer or a new supplier the code_cleint or the code_supplier is incremented even that I had not add a supplier.

My code is as follow:

python file:

class snc_specify(osv.osv):

_name='res.partner'

_inherit='res.partner'

_columns={

# 'max-mont':fields.float('Credit Maximum',required=True,digits_compute=dp.get_precision('deci3')),

'code_tva' : fields.char('Code TVA',translate=True,size=50),

'code_client' : fields.char('Code Client', size=64),

'code_supplier' : fields.char('Code Fournisseur', size=64),

}

_sql_constraints = [

('name_uniq', 'unique(code_client, company_id)', 'Number of client must be unique!'),

('name_uniq', 'unique(code_supplier, company_id)', 'Number of supplier must be unique!'),

]

def create_cl(self, cr, uid, vals, context=None):
        context = context or {}
        if not vals.get('code_client') and self._needscode(cr, uid, vals=vals,
                                                  context=context):
            vals['code_client'] = self.pool.get('ir.sequence')\
                                   .next_by_code(cr, uid, 'res.partner.customer')
        return super(snc_specify, self).create_cl(cr, uid, vals, context)
   
   
    def create_fr(self, cr, uid, vals, context=None):
        context = context or {}
        if not vals.get('code_supplier') and self._needscode(cr, uid, vals=vals,
                                                  context=context):
            vals['code_supplier'] = self.pool.get('ir.sequence')\
                                   .next_by_code(cr, uid, 'res.partner.supplier')
        return super(snc_specify, self).create_fr(cr, uid, vals, context)
   
   
    def copy_cl(self, cr, uid, id, default=None, context=None):
        default = default or {}
        if self._needscode(cr, uid, id=id, context=context):
            default['code_client'] = self.pool.get('ir.sequence')\
                                      .next_by_code(cr, uid, 'res.partner.customer',
                                                    context=context)
        return super(snc_specify, self).copy_cl(cr, uid, id, default,
                                            context=context)
    def copy_fr(self, cr, uid, id, default=None, context=None):
        default = default or {}
        if self._needscode(cr, uid, id=id, context=context):
            default['code_supplier'] = self.pool.get('ir.sequence')\
                                      .next_by_code(cr, uid, 'res.partner.supplier',
                                                    context=context)
        return super(snc_specify, self).copy_fr(cr, uid, id, default,
                                            context=context) 

_defaults = {

'code_client': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'res.partner.customer'),

'code_supplier': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'res.partner.supplier'),

}

sequence file:

<openerp>
<data noupdate="1">
 <!-- Incrementation code -->
        <record model="ir.sequence.type" id="seq_type_res_partner">
            <field name="name">number_client_sequence</field>
            <field name="code">res.partner.customer</field>
        </record>
        <record model="ir.sequence" id="seq_res_partner">
            <field name="name">number_client_sequence</field>
            <field name="code">res.partner.customer</field>
            <field name="prefix">CL</field>
            <field name="padding">5</field>
            <field name="company_id" eval="False"/>
        </record>
        <record model="ir.sequence.type" id="seq_type_res_supplier">
            <field name="name">number_supplier_sequence</field>
            <field name="code">res.partner.supplier</field>
        </record>
        <record model="ir.sequence" id="seq_res_supplier">
            <field name="name">number_supplier_sequence</field>
            <field name="code">res.partner.supplier</field>
            <field name="prefix">FR</field>
            <field name="padding">5</field>
            <field name="company_id" eval="False"/>
        </record>
 
</data>
</openerp>

Help me please.

0

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


Ahmed M.Elmubarak
On 7/26/15, 3:14 AM

Hello, 
The customer and supplier are the same 'res.partner' they just different in type,

So I suggest to remove calling the sequence from the default, and put it in the create method, just like the sale.py  , so by checking the vals you can know which sequence you'll use, 

Regards ...

0

Drees Far

--Drees Far--
1130
| 5 2 6
Tunis, Tunisia
--Drees Far--
Drees Far
On 7/25/15, 9:41 AM

I used to use the id attribute in any table. As you know odoo create for each table defaults attributes such as id, uid, write...

So call the id of the table and it will give you an auto incremented field.

I used to used it in every time that i need an auto incremented field.

Good Luck & Regards.

0

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
896
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

Tarek Mohamed Ibrahim
On 7/25/15, 6:52 AM


The cause of your problem is the defaults lines:

_defaults = { 
        'code_client': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'res.partner.customer'),         'code_supplier': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'res.partner.supplier'), }

as this code segment is called at the creation of any new record in the res.partner table, that contains the customers and suppliers differentiated by type field.

My suggestion is to remove this piece of code and the sequence will be created automatically by your other methods create_cl and create_fr where either of them calls the appropriate sequence type.

If I remove the defaults I don't get the sequence it is not created automatically

Jihen BEN ALI
on 7/25/15, 7:02 AM

Did you save the record and check the saved data, the create_cl or create_fr is supposed to be called from the create method?

Tarek Mohamed Ibrahim
on 7/25/15, 7:47 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

1 follower(s)

Stats

Asked: 7/25/15, 5:58 AM
Seen: 502 times
Last updated: 7/26/15, 3:14 AM