This question has been flagged
2 Replies
18665 Views

I have three companies, FACTORY,SOFTWARE,HARDWARE

I have many employees in each companies.

First i edit employee nam and then select company name. when i select company name, employee id should be generated automatically according to the below steps.

If employee1 is in FACTORY then employee id FAC0001

else if employee1 is in SOFTWARE then employee id SOF0001

else employee1 is in HARDWARE then employee id HAR0001

How cani achieve this?

code

hr_sequence.py

import openerp
from openerp.osv import fields, osv

    class employee_sequence(osv.osv):
        _inherit = "hr.employee"
        _columns = {
                   'sequence_code': fields.char('Employee ID',size = 16,required = True,translate = True),                          

                   }


   def create(self, cr, uid, vals, context=None):        
        if context is None:
            context = {}
        context['force_company'] = vals['company_id']
        if vals.get('sequence_code', '/') == False:
            vals['sequence_code'] = self.pool.get('ir.sequence').next_by_code(
           cr, uid, 'hr.employee', context=context) or '/'        
        return super(employee_sequence, self).create(cr, uid, vals, context=context

)

employee_sequence()

hr_sequence.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data noupdate="1">

        <!-- Sequences for hr.employee -->
    <record id="seq_type_hr_employee" model="ir.sequence.type">
        <field name="name">Employee Id</field>
        <field name="code">hr.employee</field>
    </record>

    <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 FAC Company ID! (select id from res_company where name='FAC')-->
    </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">SOF</field>
        <field name="padding">3</field>
        <field name="company_id" eval="3"/> <!-- SET FAC Company ID! (select id from res_company where name='FAC')-->
    </record>
    </data>
</openerp>

hr_sequence_view.xml

<?xml version="1.0" encoding="utf-8"?>

<openerp>   
    <data>


    <record id="hr_sequence_form" model="ir.ui.view">
            <field name="name">hr.sequence.form.inherit</field>
            <field name="model">hr.employee</field>
            <field name="inherit_id" ref="hr.view_employee_form"/>
            <field name="arch" type="xml">
                <field name='name' position='after'>
            <field name="sequence_code"/>
               </field>
            </field>
        </record>

       <record id="hr_sequence_extra" model="ir.actions.act_window">
            <field name="name">Employee</field>
            <field name="res_model">hr.employee</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>          
        </record>
   </data>           
</openerp>

please rewrite the code.

image description

Avatar
Discard

Hi Remya, I am newbie to OpenERP. Can you please tell me at what location i should create these python and xml files?

Best Answer

The below code will work based on the your requirement

hr_sequence.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data noupdate="1">

        <!-- Sequences for hr.employee -->
        <record id="seq_type_hr_employee" model="ir.sequence.type">
            <field name="name">Employee Id</field>
            <field name="code">hr.employee</field>
        </record>

        <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">FAC</field>
            <field name="padding">3</field>
            <field name="company_id" eval="1"/> <!-- SET FAC Company ID! (select id from res_company where name='FAC')-->
        </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">SOF</field>
            <field name="padding">3</field>
            <field name="company_id" eval="2"/> <!-- SET SOF Company ID!-->
        </record>

       <record id="seq_hr_employee_com3" model="ir.sequence">
            <field name="name">Employee Id(com3)</field>
            <field name="code">hr.employee</field>
            <field name="prefix">HAR</field>
            <field name="padding">3</field>
            <field name="company_id" eval="3"/> <!-- SET HAR Company ID!-->
        </record>

    </data>
</openerp>

In Python File

class employee_sequence(osv.osv):
    _inherit = "hr.employee"
    _columns = {
               '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_company'] = vals['company_id']
        if vals.get('sequence_code', '/') == False:
            vals['sequence_code'] = self.pool.get('ir.sequence').next_by_code(
           cr, uid, 'hr.employee', context=context) or '/'        
        return super(employee_sequence, self).create(cr, uid, vals, context=context)

Let me know if any issue.

The above two line code working in the way:-

context['force_company'] = vals['company_id']
 vals['sequence_code'] = self.pool.get('ir.sequence').next_by_code( cr, uid, 'hr.employee', context=context) or '/'

In the context['force_company'] passed company ID value it return the ir_sequence value based on company.

Checking purpose context['force_company'] assign value and check the result:- Example:-

context['force_company'] = 2

And check the result

context['force_company'] = 3

And check the result

Avatar
Discard

context['force_company'] what is it meant force_company?, I put my branch table name as this, id values are get like 2, one branch sequence only coming other branch sequence is not coming.

When i calling the create method i put print vals it gets id of the branch at the same time i put on the xml <field name="name" eval="2"/> it is not get exact output. Wrong output only coming how to do this.

I have 3 sequence codes, when selecting branch 1 sequence 1 will display branch2 means seq 2 like this how to do it.

@Remya just use my code it will works. Follow the below steps in xml file change company ID based on your Database. And if you have already hr_sequence_type and hr_sequence hr.employee data please delete the entry and install the code.

can u post your error?.

Delete previously created code using the query delete from ir_sequence_type where code='hr.employee' and try or Install with New DB

If you have company_id field in hr.employee table Or while creating employee how to you select company (which field u you using). Value will appearing after the selected company and saved the record.

Then it will works after saving the record. Have you run the query select code, company_id from ir_sequence where code='hr.employee' and check each ir_sequence code have separate company_id.

Basic error check 1) Dependency Module added. 2) use same class name and create method super class name 3) check line no 22: vals['sequence_code'] = self.pool.get('ir.sequence').next_by_code( cr, uid, 'hr.employee', context=context) or '/' # if hr_employee then replace with #hr.employee

No, Prakash told, will tel the solution at evening thats why i am waiting for his response.

You try to new DB you get success it will work no issue.

check the sequence .xml file.

Best Answer

Hi,

This will help you https://accounts.openerp.com/forum/Help-1/question/46903#46908

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.

class employee_sequence(osv.osv): _inherit = "hr.employee" _columns = { 'sequence_code': fields.char('Employee ID',size = 16,required = True,translate = True),

           }

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] employee_company_id = self.pool.get('hr.employee').browse(cr, uid, uid, context).company_id

if context.has_key('sequence_code'): 
    context['sequence_code']=employee_company_id

return self._next(cr, uid, ids, context)

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, 'hr.employee') or '/'        
    return super(hr_employee, self).create(cr, uid, vals, context=context)

employee_sequence()

    <record id="seq_type_hr_employee" model="ir.sequence.type">
        <field name="name">Employee Id</field>
        <field name="code">hr.employee</field>
    </record>

    <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"/> <!-- SET FAC Company ID! -->
    </record>

    <record id="seq_hr_employee_com3" model="ir.sequence">
        <field name="name">Employee Id(com3)</field>
        <field name="code">hr.employee</field>
        <field name="prefix">HAR</field>
        <field name="padding">3</field>
        <field name="company_id" eval="3"/> <!-- SET HAR Company ID! -->
    </record>

image description

image description

Avatar
Discard

I have post code from given link pls check

create employee_id(type char) and add sequence on that field then apply this code(to apply sequence to field see sale.py module)

create employee_id(type char) and add sequence on that field then apply this code(to apply sequence to field see sale.py module)

Author

but i need different requirement as sale module. in sale module only one sequence number generated. in my requirement more than three sequence number(employee id) is generated according to different companies. is it possible with above code?

How can i help, ?

I have update answer

I have update answer.

check it now

It seems correct you just debug your code creating 3 records with different company.

Author

I tried all of it Prakash, but still the same issue. SOF sequence generating for all company.

context['force_company'] = vals['company_id']

I updated the answer checking purpose assign value in the create method context['force_company'] = 2 and check the result ( restart server and create new record)

Author

its absolutely fine working. thanks for helping Prakash. Very thanks