Import des employés via un fichier CSV dans OpenERP 6.1

Import des employés via un fichier CSV dans OpenERP 6.1

Quentin C. 10 April 2012

Fichier “import_employee.py”:

from osv import osv
from osv import fields
import netsvc
import csv
import logging
import time

class import_employees(osv.osv):

    _name = "hr.employee"
    _inherit = "hr.employee"

    def logit(self, txt):
        netsvc.Logger().notifyChannel('hr.employee', netsvc.LOG_INFO, txt)

        return True

    def transform_date(self, date):
        # DATE (12/02/1972)
        parts = [int(x) for x in date.split('/')]
        # RETURN (1972-12-30)
        return '%4d-%02d-%02d' % (parts[2],parts[0],parts[1])

        return True

    def parser_csv(self, cr, uid, ids,context = None):
        self.logit('Parser start 1')
        logger = logging.getLogger('orm.import_employees')
        logger.warning("1.Try Open")
        # Structure CSV : 
        #name,login,password
        #Quentin,QCL,azerty
        spamReader = csv.reader(open('C:\\mon-dossier\\file_user.csv', 'r'))
        self.logit('File Open')
        compteur = 0
        for row in spamReader:
            if compteur == 0 :
                logger.warning('First line')
                logger.warning(row)
                compteur = compteur + 1
            else:
                user_id = self.pool.get('hr.employee').search(cr, uid, [('name', '=', str(row[1]))])
                if user_id:
                        logger.warning('User already present in DB')
                else:

                    logger.warning('TEST')
                    #Linkage du pays
                    if row[7]=='BEL':
                        country_code='BE'
                    else:
                        if row[7]=='FRA':
                            country_code='FR'
                        else:
                            country_code=row[7]

                    logger.warning(country_code)

                    country_id = self.pool.get('res.country').search(cr, uid, [('code', '=', country_code)])
                    logger.warning(country_id[0])   

                    #Creation de l'address
                    self.pool.get('res.partner.address').create(cr, uid,  {
                                                            'name': 'Home Address of '+row[1] +" "+ row[2],
                                                            'phone':  row[9],
                                                            'zip': row[5],
                                                            'street':  row[3] +", "+ row[4],
                                                            'street2':  row[9],
                                                            'city':  row[6],
                                                            'country_id': country_id[0]
                                                            })

                    #Linkage de l'addresse
                    address_id = self.pool.get('res.partner.address').search(cr, uid, [('name', '=', 'Home Address of '+row[1] +" "+ row[2])])
                    logger.warning(address_id[0])                  

                    #Linkage du departement
                    department = str(row[14])
                    department_id = self.pool.get('hr.department').search(cr, uid, [('name', '=', department)])
                    logger.warning(department)
                    logger.warning(department_id[0])

                    #Creation de l'employee
                    self.pool.get('hr.employee').create(cr, uid,  {
                                                            'identification_id': row[0], #ID-NO
                                                            'name': row[1] +" "+ row[2],
                                                            'company_id': 1,
                                                            'ssnid':  row[8], #GOVT-REG-NO
                                                            'work_phone':  row[9],
                                                            'mobile_phone' :  row[10],
                                                            'birthday' :  row[11],
                                                            'sinid':  row[12], #PAYR-REF
                                                            'marital': '',
                                                            'country_id': 21,

                                                            #Linkage avec d'autre model
                                                            'department_id': department_id[0],
                                                            'address_home_id': address_id[0],

                                                            #'user_id': 1,
                                                            #'work_location': 1,
                                                            #'parent_id': 1,
                                                            #'gender': 1,
                                                            #'marital': 1     DATE =    2012-12-30
                                                            #'work_email': 'toto@gmail.com',
                                                            #'notes': 1,
                                                            # 'write_uid': 1,
                                                            #'password': '',
                                                            })

                logger.warning(row)
        logger.warning('Parser end')

        return True

import_employees()

2 thoughts on “Import des employés via un fichier CSV dans OpenERP 6.1

Leave a Reply

Your email address will not be published.