Wednesday, 3 February 2016

HR Data Import

public class ImportHRData extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    DialogField                 dialogFilename;
    boolean                     ret;
    FirstName                   firstname,lastname;
    DirPersonName               dirPersonNameLoc;
    HcmWorker                   hcmWorker;

    #define.CurrentVersion(2)
    #localmacro.CurrentList
    filename,
    insertIncorrectRecords,
    #endmacro
    #localmacro.ListVersion1
    filename,
    insertIncorrectRecords
    #endmacro
    #File
}
private void hrCreate(container _createcon)
{
    CompanyInfo                         companyInfo;
    HcmWorker                           newHcmWorker;
    DirPersonName                       dirPersonName;
    HcmEmploymentRecId                  newEmploymentRecId;
    utcDateTime                         hiredate,termination,lastdateworked,employmentEndDate;
    str                                 job;
    OMOperatingUnitNumber               businessunit,department;
    HcmEmploymentType                   hcmEmploymentType;
    HcmPosition                         hcmPosition;
    HcmPositionDetail                   hcmPositionDetail;
    HcmPositionWorkerAssignment         hcmPositionWorkerAssignment;
    HcmPositionDuration                 hcmPositionDuration;
    RetailStaffTable                    retailStaffTable;
    NoYes                               blockPosLogin;
    DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();
    DimensionDefault                    result;
    DimensionAttribute                  dimensionAttribute;
    DimensionAttributeValue             dimensionAttributeValue;
    HcmJob                              hcmjob;
    HcmEmployment                       hcmEmployment;
    HcmEmploymentDetail                 hcmEmploymentDetail,nwhcmEmploymentDetail;
    NumberSeq                           numberSeq;

    companyInfo = companyInfo::find();
    hiredate                    =   DateTimeUtil::newDateTime(str2date(conpeek(_createcon, 1),213),0);
    firstname                   =   conpeek(_createcon, 2);
    lastname                    =   conpeek(_createcon, 3);
    hcmEmploymentType           =   str2enum(hcmEmploymentType,conpeek(_createcon, 4));
    termination                 =   DateTimeUtil::newDateTime(str2date(conpeek(_createcon, 5),213),0);
    lastdateworked              =   DateTimeUtil::newDateTime(str2date(conpeek(_createcon, 6),213),0);
    employmentEndDate           =   DateTimeUtil::newDateTime(str2date(conpeek(_createcon, 7),213),0);
    blockPosLogin               =   str2enum(blockPosLogin,conPeek(_createcon,8));
    job                         =   conPeek(_createcon,9);
    businessunit                =   conPeek(_createcon,10);
    department                  =   conPeek(_createcon,11);
    if(!employmentEndDate)
        employmentEndDate       = DateTimeUtil::maxValue();
    dirPersonName.FirstName     = firstname;
    dirPersonName.LastName      = lastname;

    //Insert New Woker
    newHcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorker(dirPersonName
                                                                        , firstname
                                                                        , companyInfo.RecId
                                                                        , hcmEmploymentType
                                                                        , hiredate
                                                                        , employmentEndDate));
        if (newHcmWorker.RecId == 0)
        {
            ret = false;
        }
        if(newHcmWorker.RecId == 0)
        {

            // Insert Active Employment for the worker is no active employment for the worker
            newEmploymentRecId = HcmWorkerTransition::newCreateHcmEmployment(newHcmWorker.RecId, companyInfo.RecId,
                    hcmEmploymentType, hiredate, employmentEndDate);
            if (newEmploymentRecId == 0)
            {
                ret = false;
            }
        }

     //Set Dimension value
    dimensionAttribute = dimensionAttribute::findByName("@SYS343395");
    dimensionAttributeValue =  dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,businessunit,false,true);
    valueSetStorage.addItem(dimensionAttributeValue);
    dimensionAttribute = dimensionAttribute::findByName("@SYS181623");
    dimensionAttributeValue =  dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,department,false,true);
    valueSetStorage.addItem(dimensionAttributeValue);
    result = valueSetStorage.save();
    hcmEmployment   =   hcmEmployment::getActiveEmploymentsByWorker(newHcmWorker.RecId, hiredate, employmentEndDate, true);
    if(hcmEmployment)
    {
        hcmEmployment.DefaultDimension  =   result;
        hcmEmployment.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
        hcmEmployment.update();
         //Insert HcmEmploymentDetail
        select * from hcmEmploymentDetail order by RecId desc
            where hcmEmploymentDetail.Employment    ==  hcmEmployment.RecId;
        if(hcmEmploymentDetail)
        {
            hcmEmploymentDetail.selectForUpdate(true);
            ttsBegin;
            hcmEmploymentDetail.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
            hcmEmploymentDetail.ValidTo    = DateTimeUtil::utcNow();
            hcmEmploymentDetail.update();
            ttsCommit;
        }
        nwhcmEmploymentDetail.Employment        =   hcmEmployment.RecId;
        nwhcmEmploymentDetail.LastDateWorked    =   lastdateworked;
        nwhcmEmploymentDetail.TransitionDate    =   termination;
        nwhcmEmploymentDetail.ValidFrom         =   DateTimeUtil::utcNow();
        nwhcmEmploymentDetail.ValidTo           =   DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());
        nwhcmEmploymentDetail.insert();
    }

    //Block POS Login Update
    retailStaffTable            =   RetailStaffTable::find(newHcmWorker.PersonnelNumber,true);
    //Check for Retails Staff Table
    if(retailStaffTable)
    {
        ttsBegin;
        retailStaffTable.blocked    =   blockPosLogin;
        retailStaffTable.doUpdate();
        ttsCommit;
    }
    //Retails Staff Table do not exist for the worker
    else
    {
        retailStaffTable.initValue();
        retailStaffTable.staffId    =   newHcmWorker.PersonnelNumber;
        retailStaffTable.blocked    =   blockPosLogin;
        retailStaffTable.insert();
    }


    if(!HcmJob::findByJob(job).RecId)
    {
        hcmJob.JobId  =   job;
        hcmjob.insert();
    }
    numberSeq                       =   numberSeq::newGetNum(NumberSeqReference::findReference (extendedTypeNum(HcmPositionId)));
    hcmPosition.PositionId          =  numberSeq.num();
    hcmPosition.insert();

    hcmPositionDetail.Job           = HcmJob::findByJob(job).RecId;
    hcmPositionDetail.Position      = hcmPosition.RecId;

    hcmPositionDetail.Title         = HcmTitle::findByTitle(HcmJobDetail::findByJob(HcmPositionDetail.Job).Description).RecId;
    hcmPositionDetail.ValidFrom     = hiredate;
    if(employmentEndDate)
        hcmPositionDetail.ValidTo       = employmentEndDate;
    else
        hcmPositionDetail.ValidTo       = DateTimeUtil::maxValue();

    hcmPositionDetail.insert();

    hcmPositionWorkerAssignment.ValidFrom   = hiredate;
    if(employmentEndDate)
        hcmPositionWorkerAssignment.ValidTo       = employmentEndDate;
    else
        hcmPositionWorkerAssignment.ValidTo       = DateTimeUtil::maxValue();

    hcmPositionWorkerAssignment.Position    = HcmPosition.RecId;
    hcmPositionWorkerAssignment.Worker      = newHcmWorker.RecId;
    hcmPositionWorkerAssignment.insert();

    hcmPositionDuration.Position    = HcmPosition.RecId;
    hcmPositionDuration.ValidFrom   = HcmPositionWorkerAssignment.ValidFrom;
    if(employmentEndDate)
        hcmPositionDuration.ValidTo       = employmentEndDate;
    else
        hcmPositionDuration.ValidTo       = DateTimeUtil::maxValue();
    hcmPositionDuration.insert();
}
public Object dialog()
{
    DialogRunbase       dialog = super();
    ;
    dialogFilename  = dialog.addField(extendedTypeStr(FilenameOpen));
    dialogFilename.value(filename);
    dialog.filenameLookupFilter(["@SYS54806", #AllFiles]);
    return dialog;
}
public boolean getFromDialog()
{
    filename                = dialogFilename.value();
    return true;
}
private void hrUpdate(container _createcon)
{
    HcmWorker                           newHcmWorker;
    utcDateTime                         hiredate,termination,lastdateworked,employmentEndDate;
    str                                 job;
    OMOperatingUnitNumber               businessunit,department;
    HcmEmploymentType                   hcmEmploymentType;
    RetailStaffTable                    retailStaffTable;
    NoYes                               blockPosLogin;
    DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();
    DimensionDefault                    result;
    DimensionAttribute                  dimensionAttribute;
    DimensionAttributeValue             dimensionAttributeValue;
    HcmEmployment                       hcmEmployment;
    HcmEmploymentDetail                 hcmEmploymentDetail,nwhcmEmploymentDetail;
    HcmJob                              hcmjob;
    HcmPosition                         hcmPosition;
    HcmPositionDetail                   hcmPositionDetail;
    HcmPositionWorkerAssignment         hcmPositionWorkerAssignment;
    HcmPositionDuration                 hcmPositionDuration;
    NumberSeq                           numberSeq;

    hiredate                    =   DateTimeUtil::newDateTime(str2date(conpeek(_createcon, 1),213),0);
    firstname                   =   conpeek(_createcon, 2);
    lastname                    =   conpeek(_createcon, 3);
    hcmEmploymentType           =   str2enum(hcmEmploymentType,conpeek(_createcon, 4));
    termination                 =   DateTimeUtil::newDateTime(str2date(conpeek(_createcon, 5),213),0);
    lastdateworked              =   DateTimeUtil::newDateTime(str2date(conpeek(_createcon, 6),213),0);
    employmentEndDate           =   DateTimeUtil::newDateTime(str2date(conpeek(_createcon, 7),213),0);
    if(!employmentEndDate)
        employmentEndDate       = DateTimeUtil::maxValue();
    blockPosLogin               =   str2enum(blockPosLogin,conPeek(_createcon,8));
    job                         =   conPeek(_createcon,9);
    businessunit                =   conPeek(_createcon,10);
    department                  =   conPeek(_createcon,11);

    if(hcmWorker.RecId)
    {
         //Block POS Login Update
        retailStaffTable            =   RetailStaffTable::find(hcmWorker.PersonnelNumber,true);
        //Check for Retails Staff Table
        if(retailStaffTable)
        {
            if(retailStaffTable.blocked != blockPosLogin)
            {
                ttsBegin;
                retailStaffTable.blocked    =   blockPosLogin;
                retailStaffTable.doUpdate();
                ttsCommit;
            }
        }
        //Retails Staff Table do not exist for the worker
        else
        {
            retailStaffTable.initValue();
            retailStaffTable.staffId    =   newHcmWorker.PersonnelNumber;
            retailStaffTable.blocked    =   blockPosLogin;
            retailStaffTable.insert();
        }
    //Set Dimension value
        dimensionAttribute = dimensionAttribute::findByName("@SYS343395");
        dimensionAttributeValue =  dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,businessunit,false,true);
        valueSetStorage.addItem(dimensionAttributeValue);
        dimensionAttribute = dimensionAttribute::findByName("@SYS181623");
        dimensionAttributeValue =  dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,department,false,true);
        valueSetStorage.addItem(dimensionAttributeValue);
        result = valueSetStorage.save();
        hcmEmployment   =   hcmEmployment::getActiveEmploymentsByWorker(hcmWorker.RecId, hiredate, employmentEndDate, true);
        if(hcmEmployment)
        {
            ttsBegin;
            if(hcmEmployment.ValidFrom    !=  hiredate || hcmEmployment.ValidTo !=  employmentEndDate)
            {
                HcmWorkerTransition::newUpdateHcmEmployment(hcmEmployment, hiredate, employmentEndDate);
            }
            hcmEmployment.ValidFrom         =   hiredate;           
            hcmEmployment.ValidTo           =   employmentEndDate;
            if(!employmentEndDate)
                hcmEmployment.ValidTo   =   DateTimeUtil::maxValue();
            hcmEmployment.DefaultDimension  =   result;
            hcmEmployment.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
            hcmEmployment.update();
            ttsCommit;
             //Update HcmEmploymentDetail
            select * from hcmEmploymentDetail order by RecId desc
                where hcmEmploymentDetail.Employment    ==  hcmEmployment.RecId;
            if((hcmEmploymentDetail.TransitionDate   != termination)||(hcmEmploymentDetail.LastDateWorked !=  lastdateworked))
            {
                if(hcmEmploymentDetail)
                {
                    hcmEmploymentDetail.selectForUpdate(true);
                    ttsBegin;
                    hcmEmploymentDetail.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
                    hcmEmploymentDetail.ValidTo    = DateTimeUtil::utcNow();
                    hcmEmploymentDetail.update();
                    ttsCommit;
                }
                nwhcmEmploymentDetail.Employment        =   hcmEmployment.RecId;
                nwhcmEmploymentDetail.LastDateWorked    =   lastdateworked;
                nwhcmEmploymentDetail.TransitionDate    =   termination;
                nwhcmEmploymentDetail.ValidFrom         =   DateTimeUtil::utcNow();
                nwhcmEmploymentDetail.ValidTo           =   DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());
                nwhcmEmploymentDetail.insert();
            }
        }
        if(!HcmJob::findByJob(job).RecId)
        {
            hcmjob.JobId  =   job;
            hcmjob.insert();
        }
        else
        {
            hcmjob = HcmJob::findByJob(job);
        }
        select hcmPositionWorkerAssignment
            where hcmPositionWorkerAssignment.Worker==hcmWorker.RecId
                join hcmPositionDetail where hcmPositionDetail.Job == hcmjob.recId
                   && hcmPositionDetail.Position == hcmPositionWorkerAssignment.Position;
        if (!hcmPositionDetail)
        {
            numberSeq                       =   numberSeq::newGetNum(NumberSeqReference::findReference (extendedTypeNum(HcmPositionId)));
            hcmPosition.PositionId          =  numberSeq.num();
            hcmPosition.insert();

            hcmPositionDetail.Job           = HcmJob::findByJob(job).RecId;
            hcmPositionDetail.Position      = hcmPosition.RecId;

            hcmPositionDetail.Title         = HcmTitle::findByTitle(HcmJobDetail::findByJob(HcmPositionDetail.Job).Description).RecId;
            hcmPositionDetail.ValidFrom     = hiredate;
            if(employmentEndDate)
                hcmPositionDetail.ValidTo       = employmentEndDate;
            else
                hcmPositionDetail.ValidTo       = DateTimeUtil::maxValue();

            hcmPositionDetail.insert();

            hcmPositionWorkerAssignment.ValidFrom   = hiredate;
            if(employmentEndDate)
                hcmPositionWorkerAssignment.ValidTo       = employmentEndDate;
            else
                hcmPositionWorkerAssignment.ValidTo       = DateTimeUtil::maxValue();

            hcmPositionWorkerAssignment.Position    = HcmPosition.RecId;
            hcmPositionWorkerAssignment.Worker      = hcmWorker.RecId;
            hcmPositionWorkerAssignment.insert();

            hcmPositionDuration.Position    = HcmPosition.RecId;
            hcmPositionDuration.ValidFrom   = HcmPositionWorkerAssignment.ValidFrom;
            if(employmentEndDate)
                hcmPositionDuration.ValidTo       = employmentEndDate;
            else
                hcmPositionDuration.ValidTo       = DateTimeUtil::maxValue();
            hcmPositionDuration.insert();
        }

    }

}
void run()
{
    container                               readCon;
    counter                                 icount,inserted,updated;
    ;
    ret =true;
    csvFile = new CommaIO(filename, 'r');
    try
    {
        if (csvFile)
        {
             readCon = csvFile.read();
           // ttsbegin;
            while (csvFile.status() == IO_Status::OK)
            {
                readCon = csvFile.read();
                if(readCon)
                {
                    firstname                   =   conpeek(readCon, 2);
                    lastname                    =   conpeek(readCon, 3);
                    select FirstName,LastName,person from dirPersonNameLoc
                        where dirPersonNameLoc.FirstName     == firstname
                        && dirPersonNameLoc.LastName      == lastname;
                    hcmWorker = HcmWorker::findByPerson(dirPersonNameLoc.person);
                    if (hcmWorker.RecId == 0)
                        {
                            //Insert employee
                            this.hrCreate(readCon);
                            inserted++;
                        }
                        else
                        {
                            //Update employee
                            this.hrUpdate(readCon);
                            updated++;
                        }
                icount++;
                }
            }
        }
        //icount--;//Remove header recount from total record count
        info(strFmt("@CIT231",icount,inserted,updated));
    }
    catch(Exception::Error)
    {
        info(strFmt("%1 %2",Exception::Error,icount));
    }
}
static void main(Args  args)
{
    ImportHRData        importHRData;
    ;
    importHRData =  new ImportHRData();
    if(importHRData.prompt())
    {
        importHRData.run();
    }
}

No comments:

Post a Comment