Skip to Content
Menu
This question has been flagged
5 Replies
2938 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.

Avatar
Discard
Best Answer

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

Avatar
Discard
Best Answer

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.

Avatar
Discard
Best Answer


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.

Avatar
Discard
Author

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

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?