ManishaS
ManishaS

Reputation: 45

Openerp related fields in HR module hr_employee with Department, Division...etc and Location

I have these four classes in my OpenERP hr module as below

res_company --->hr_department-->hr_division-->hr_section

    class company_new_registration(osv.osv):
        _name = "hr.company.n.registration"
        _description = "Company"
        _columns = {
            'name': fields.char('Company Name', size=128, required=True),
            'depts': fields.one2many('hr.dept.n.registration', 'company_id', 'Dept'),

        }
    company_new_registration()

    class dept_new_registration(osv.osv):
        _name = "hr.dept.n.registration"
        _description = "depts"
        _columns = {
            'name': fields.char('Dept Name', size=128, required=True),
            'company_id': fields.many2one('res.company', 'Company Name', required=True),
            'divisions': fields.one2many('hr.division.n.registration', 'dept_id', 'Division'),
            'resres':fields.one2many('res.users','alias_id')

        }

    dept_new_registration()

    class division_new_registration(osv.osv):
        _name = "hr.division.n.registration"
        _description = "Divisions"
        _columns = {
            'name': fields.char('Division Name', size=128, ),
            'dept_id': fields.many2one('hr.dept.n.registration', 'Dept. Name', select=True, required=True),
            'sections': fields.one2many('hr.section.n.registration', 'division_id', 'Section'),

        }

    division_new_registration()

    class section_new_registration(osv.osv):
        _name = "hr.section.n.registration"
        _description = "sections"
        _columns = {
            'name': fields.char('Name', size=128), 
            'section_name': fields.char('Section Name', size=128, required=True),
            'division_id': fields.many2one('hr.division.n.registration', 'Division Name', select=True, required=True),
        }

    section_new_registration()

all four classes in one2many relationship and all the above classes are have name field and one another field to have relationship respectively.

Also there is another class in my module which named hr_location which need to keep all above four classes references in order to create Organization structure.

my questions are:

  1. I need to create a field which displays "Company name/ Department name/Division name/ Section name" in hr_location table when ever I create a new Section or Division or etc

  2. I need to create four related field which stored company_id, department_id, division_id and section_id in hr_employee table when ever I select the location from above (in previous question) field.

Upvotes: 0

Views: 323

Answers (1)

Alessandro Ruffolo
Alessandro Ruffolo

Reputation: 1575

First, you need to create into section_new_registration a computed field which concatenate the names of the whole hierarchy in the "Company name/ Department name/Division name/ Section name" you need.

Or you can override the create instead of an computed field:

def create(self, cr, uid, values, context=None):
     company = self.pool.get('res.company').browse(cr, uid, values['company_id'], context=context)
     dept = self.pool.get('hr.dept.n.registration').browse(cr, uid, values['department_id'], context=context)
     division = self.pool.get('hr.division.n.registration').browse(cr, uid, values['division_id'], context=context)

     values['name'] = company.name + '/' + dept.name + '/' + division.name + '/'+ values['section_name']
     return super(hr_location, self).create(cr, uid, values, context=context)

Second, you need to create into hr_location a field such

'section_id': fields.many2one('hr.section.n.registration')

to accomplish your point (1)

Third, for your point (2) you need to create five fields as follows:

'location_id': fields.many2one('hr.location')
'section_id': fields.related('location_id', 'section_id', type='many2one', relation='hr.section.n.registration', store=True)
'division_id': fields.related('location_id', 'section_id', 'division_id', type='many2one', relation='hr.division.n.registration', store=True)
'department_id': fields.related('location_id', 'section_id', 'division_id', 'dept_id', type='many2one', relation='hr.dept.n.registration', store=True)
'company_id': fields.related('location_id', 'section_id', 'division_id', 'dept_id', 'company_id', type='many2one', relation='res.company', store=True)

Upvotes: 1

Related Questions