Sunday, 22 February 2015

Create Employee in Ax

private void createEmployee(System.Data.SqlClient.SqlDataReader   dr1)
{
    CompanyInfo                             companyInfo;
    HcmEmploymentRecId                      newEmploymentRecId;
    ValidFromDateTime                       employmentStartDateTime;
    ValidToDateTime                         employmentEndDateTime;
    HcmWorker                               newHcmWorker;
    DirPerson                               dirPerson;
    DirPersonName                           dirPersonName;
    HcmEmploymentType                       hcmEmploymentType = HcmEmploymentType::Employee;
    NumberSeq                               numberSeqPersonnelNum;
    HcmPersonPrivateDetails                 HcmPersonPrivateDetails;
    HcmEmployment                           hcmEmployment;

    HcmWorkerTitle  hcmWorkerTitle;

    DirParty                        dirparty;
    DirPartyContactInfoView         contactView;
    HcmPersonDetails                persondetails;
    HcmPersonIdentificationNumber   hcmPersonIdentificationNumber;

    utcdatetime                     _validFrom = DateTimeUtil::utcNow();

    Struct              struct;
    container           ledgerDimension;
    DimensionDefault    DimensionDefault;
    int                 k;


    TransDate       HireDate,DOB;
    HcmPersonID     Employeecode;
    str             FirstName,Title,MobilePhone,email,MaritalStatus,NationalityCode,NationalityName,PaymentMode,MiddleName,LastName;

    str             DepartmentCode,DepartmentName,SubDivisionCode,SubDivisionName,DivisionCode,DivisionName ,SectorCode, SectorName;
    str             DesignationCode,DesignationName,SponsorCode,SponsorDesc,MOLNo,PASSPORTNUMBER,Gender,BloodGroup,ReligionCategory,EmployeeCategory,Emiratization;


    ;

    Employeecode    = dr1.get_Item('Employeecode');     FirstName       = dr1.get_Item('FirstName'); MiddleName = dr1.get_Item('Middlename'); LastName = dr1.get_Item('Lastname');
    Title           = dr1.get_Item('Title');            MobilePhone     = dr1.get_Item('MobilePhone');
    email          = dr1.get_Item('email');            DOB             = dr1.get_Item('DOB');
    MaritalStatus   = dr1.get_Item('MaritalStatus');    NationalityCode = dr1.get_Item('NationalityCode');
    NationalityName = dr1.get_Item('NationalityName');  HireDate        = dr1.get_Item('HireDate');
    PaymentMode     = dr1.get_Item('PaymentMode');      DepartmentCode  = dr1.get_Item('DepartmentCode');
    DepartmentName  = dr1.get_Item('DepartmentName');   //SubDivisionCode = dr1.get_Item('SubDivisionCode');
  //  SubDivisionName = dr1.get_Item('SubDivisionName'); // DivisionCode    = dr1.get_Item('DivisionCode');
  //  DivisionName    = dr1.get_Item('DivisionName');    // SectorCode      = dr1.get_Item('SectorCode');
   // SectorName      = dr1.get_Item('SectorName');       DesignationCode = dr1.get_Item('DesignationCode');
    DesignationName  = dr1.get_Item('DesignationName'); //SponsorCode  = dr1.get_Item('SponsorCode');
    SponsorDesc     = dr1.get_Item('SponsorName');
    MOLNo           = dr1.get_Item('MOLNo');
    PASSPORTNUMBER  = dr1.get_Item('PASSPORTNUMBER');
    Gender          = dr1.get_Item('Gender');
    BloodGroup        = dr1.get_Item('BloodGroup');
    ReligionCategory  = dr1.get_Item('ReligionCategory');
    EmployeeCategory  = dr1.get_Item('EmployeeCategory');
    Emiratization   = dr1.get_Item('Emiratization');





    companyInfo = companyInfo::find();

    newHcmWorker = HcmWorker::findByPersonnelNumber(Employeecode);

    if(! newHcmWorker)
    {
        employmentStartDateTime = datetobeginUtcDateTime(HireDate, DateTimeUtil::getUserPreferredTimeZone());
        employmentEndDateTime   = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());

        dirPersonName.FirstName     = FirstName;
        dirPersonName.MiddleName    = Middlename;
        dirPersonName.LastName      = LastName;
        newHcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorker(dirPersonName
                                                                               , Employeecode
                                                                               , companyInfo.RecId
                                                                               , hcmEmploymentType
                                                                               , employmentStartDateTime
                                                                               , employmentEndDateTime));
    }
    else
    {

        dirPersonName               = DirPersonName::find(newHcmWorker.Person,true);
        dirPersonName.FirstName     = FirstName;
        dirPersonName.MiddleName    = Middlename;
        dirPersonName.LastName      = LastName;
        dirPersonName.update();
    }

    hcmPersonPrivateDetails                     = HcmPersonPrivateDetails::findByPerson(newHcmWorker.Person,true);
    hcmPersonPrivateDetails.initValue();
    hcmPersonPrivateDetails.Person              = dirPersonName.Person;
    hcmPersonPrivateDetails.BirthDate           = DOB;
    hcmPersonPrivateDetails.Gender              = SQLIntegration::convGender(Gender);
    hcmPersonPrivateDetails.CitizenshipCountryRegion = NationalityCode;
    hcmPersonPrivateDetails.BloodGroup          = SQLIntegration::convBlood(BloodGroup);
    hcmPersonPrivateDetails.ReligionCategory    = SQLIntegration::convReligion(ReligionCategory);
    hcmPersonPrivateDetails.EmployeeCategory    = SQLIntegration::convEmployeeCategory(EmployeeCategory);
    hcmPersonPrivateDetails.Emiratization       = SQLIntegration::convEmiratization(Emiratization);
    if(hcmPersonPrivateDetails)
        hcmPersonPrivateDetails.update();
    else
        hcmPersonPrivateDetails.insert();

    persondetails               = HcmPersonDetails::findByPerson(newHcmWorker.Person);
    persondetails.initValue();
    persondetails.Person        = dirPersonName.Person;
    persondetails.MaritalStatus = SQLIntegration::convMarital(MaritalStatus);
    persondetails.ValidFrom     = DateTimeUtil::utcNow();
    persondetails.ValidTo       = DateTimeUtil::maxValue();
    //if (persondetails)
    //{
        //persondetails.selectForUpdate(true);
        //persondetails.update();
    //}
    //else
    //{
       persondetails.insert();
    //}


    hcmWorkerTitle          = HcmWorkerTitle::findByWorker(newHcmWorker.RecId);
    hcmWorkerTitle.initValue();
    hcmWorkerTitle.Worker   = newHcmWorker.RecId;
    hcmWorkerTitle.Title    = HcmTitle::findByTitle(DesignationCode).RecId;
    hcmWorkerTitle.ValidFrom = DateTimeUtil::utcNow();
    hcmWorkerTitle.ValidTo  = DateTimeUtil::maxValue();
    //if(hcmWorkerTitle)
    //{
        //hcmWorkerTitle.selectForUpdate(true);
        //hcmWorkerTitle.update();
    //}
    //else
    //{
        hcmWorkerTitle.insert();
    //}

        hcmPersonIdentificationNumber = HcmPersonIdentificationNumber::findByPersonAndType(newHcmWorker.RecId,HcmIdentificationType::findByIdentificationType("SponsorCode").RecId);
        hcmPersonIdentificationNumber.Person = dirPersonName.Person;
        hcmPersonIdentificationNumber.IdentificationType =  HcmIdentificationType::findByIdentificationType("SponsorCode").RecId;
        hcmPersonIdentificationNumber.IdentificationNumber = SponsorCode;
        hcmPersonIdentificationNumber.Description = SponsorDesc;
    if(hcmPersonIdentificationNumber)
    {
        hcmPersonIdentificationNumber.selectForUpdate(true);
        hcmPersonIdentificationNumber.update();
    }
    else
    {
        hcmPersonIdentificationNumber.insert();
    }

          hcmPersonIdentificationNumber.clear();
        hcmPersonIdentificationNumber = HcmPersonIdentificationNumber::findByPersonAndType(newHcmWorker.RecId,HcmIdentificationType::findByIdentificationType("Passport").RecId);


        hcmPersonIdentificationNumber.Person = dirPersonName.Person;
        hcmPersonIdentificationNumber.IdentificationType =  HcmIdentificationType::findByIdentificationType("Passport").RecId;
        hcmPersonIdentificationNumber.IdentificationNumber = PASSPORTNUMBER;

     if(hcmPersonIdentificationNumber)
    {
        hcmPersonIdentificationNumber.selectForUpdate(true);
        hcmPersonIdentificationNumber.update();
    }
    else
    {
        hcmPersonIdentificationNumber.insert();
    }

        hcmPersonIdentificationNumber.clear();
         hcmPersonIdentificationNumber = HcmPersonIdentificationNumber::findByPersonAndType(newHcmWorker.RecId,HcmIdentificationType::findByIdentificationType("EmpPersonNo").RecId);


        hcmPersonIdentificationNumber.Person = dirPersonName.Person;
        hcmPersonIdentificationNumber.IdentificationType =  HcmIdentificationType::findByIdentificationType("EmpPersonNo").RecId;
        hcmPersonIdentificationNumber.IdentificationNumber = MOLNo;

    if(hcmPersonIdentificationNumber)
    {
        hcmPersonIdentificationNumber.selectForUpdate(true);
        hcmPersonIdentificationNumber.update();
    }
    else
    {
        hcmPersonIdentificationNumber.insert();
    }



    dirPerson                   = dirPerson::find(dirPersonName.Person, true);
    dirPerson.PersonalTitle     = DirNameAffix::find(DirNameAffixType::PersonalPrefix ,Title).RecId;
    dirPerson.update();

    //select firstonly ValidTimeState(_validFrom) hcmEmployment
                //where hcmEmployment.Worker == newHcmWorker.RecId
                //&&    hcmEmployment.LegalEntity == companyInfo.RecId;
//
    ////Department Code    Department Name    Sub Division Code    Sub Division Name    Division Code    Division Name    Sector Code
//
    //this.findorCreateDimension('Department',DepartmentCode,DepartmentName);
    //this.findorCreateDimension('SubDivision',SubDivisionCode,SubDivisionName);
    //this.findorCreateDimension('Division',DivisionCode,DivisionName);
    //this.findorCreateDimension('Sector',SectorCode,SectorName);
//
//
    //struct = new Struct();
    //struct.add('Department',DepartmentCode);
    //struct.add('Division', DivisionCode);
    //struct.add('SubDivision', SubDivisionCode);
    //struct.add('Sector', SectorCode);
//
    //ledgerDimension = conNull();
    //ledgerDimension += struct.fields();
//
    //for (k = 1; k <= struct.fields(); k++)
    //{
        //ledgerDimension += struct.fieldName(k);
        //ledgerDimension += struct.valueIndex(k);
    //}
//
    //DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimension);
    //hcmEmployment.DefaultDimension = DimensionDefault;
    //hcmEmployment.update();



    DirParty = DirParty::constructFromPartyRecId(dirPerson.recid);

    if(email && (email != "NA"))
    {
        contactView.LocationName = "Email";
        contactView.Locator      = strLRTrim(email);
        contactView.Type         = LogisticsElectronicAddressMethodType::Email;
        contactView.Party        = dirPerson.recid;
        contactView.IsPrimary    = NoYes::Yes;
        dirParty.createOrUpdateContactInfo(contactView);
    }
    if(MobilePhone && MobilePhone != "NA")
    {
        contactView.LocationName = "Phone";
        contactView.Locator      = strLRTrim(MobilePhone);
        contactView.Type         = LogisticsElectronicAddressMethodType::Phone;
        contactView.Party        = dirPerson.recid;
        contactView.IsPrimary    = NoYes::Yes;
        dirParty.createOrUpdateContactInfo(contactView);

    }
}