Employee code can be generated in following three simple steps:
1. Create a sequence file
emp_code_sequence_view.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Sequences for employee code -->
<record id="emp_code_sequence" model="ir.sequence.type">
<field name="name">Employee Code</field>
<field name="code">hr.employee</field>
</record>
<record id="seq_hr_employee" model="ir.sequence">
<field name="name">Employee Code</field>
<field name="code">hr.employee</field>
<field name="prefix">EMP</field>
<field name="padding">5</field>
<field name="company_id" eval="False"/>
</record>
<!-- End of Sequence code for employee code -->
</data>
</openerp>
2. Add new field to class
class hr_employee(osv.osv):
_name = 'hr.employee'
_inherit = 'hr.employee'
_columns = {
'emp_code':fields.char('Employee Code', size=8, readonly=True)
}
3. Update database
def create(self, cr, uid, vals, context=None):
vals['emp_code']=self.pool.get('ir.sequence').get(cr, uid,'hr.employee')
res=super(hr_employee, self).create(cr, uid, vals)
return res
create function will be called only when new employee is added.
and you are done.
Employee code will start from EMP00001
You can generate emp_code without sequence file as well and write code directly in create function as well. But standard way is using sequence file only.
Note: Don't forget to include 'emp_code_sequence_view.xml' file in data section of '__openerp__.py' file.
Thanks!!!!!!!!! Enjoy Programming :)
1. Create a sequence file
emp_code_sequence_view.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Sequences for employee code -->
<record id="emp_code_sequence" model="ir.sequence.type">
<field name="name">Employee Code</field>
<field name="code">hr.employee</field>
</record>
<record id="seq_hr_employee" model="ir.sequence">
<field name="name">Employee Code</field>
<field name="code">hr.employee</field>
<field name="prefix">EMP</field>
<field name="padding">5</field>
<field name="company_id" eval="False"/>
</record>
<!-- End of Sequence code for employee code -->
</data>
</openerp>
2. Add new field to class
class hr_employee(osv.osv):
_name = 'hr.employee'
_inherit = 'hr.employee'
_columns = {
'emp_code':fields.char('Employee Code', size=8, readonly=True)
}
3. Update database
def create(self, cr, uid, vals, context=None):
vals['emp_code']=self.pool.get('ir.sequence').get(cr, uid,'hr.employee')
res=super(hr_employee, self).create(cr, uid, vals)
return res
create function will be called only when new employee is added.
and you are done.
Employee code will start from EMP00001
You can generate emp_code without sequence file as well and write code directly in create function as well. But standard way is using sequence file only.
Note: Don't forget to include 'emp_code_sequence_view.xml' file in data section of '__openerp__.py' file.
Thanks!!!!!!!!! Enjoy Programming :)
Nice Article...really helped me get started with sequences
ReplyDeleteThanks
Deletevery nice...
ReplyDeletei wanna work in interface how can i call a sequence in new field
ReplyDeletejust change the field name
DeleteI want to generate the same but as a product_id in the product.template model. How can I achieve that?
ReplyDelete