czwartek, 31 maja 2012

Tworzenie pracownika z kodu

void createEmplTable(TmpEmplTable _tmp)
{
    EmplTable                           emplTable;
    DirPartyTable                       dirPartyTable, dirPartyTableUpd;
    DirPartyType                        dirPartyType = DirPartyType::Person;
    HRMPartyEmployeeRelationship        hRMPartyEmployeeRelationship;
    Address                             address;
    DirECommunicationAddress            dirECommunicationAddress;
    DirPartyECommunicationRelationship  dirPartyECommunicationRelationship;


#localMacro.addComunicationAddress
        dirECommunicationAddress.clear();
        dirECommunicationAddress.initValue();
        dirECommunicationAddress.ECommunicationTypeId = %2;
        dirECommunicationAddress.Phone = %1;
        dirECommunicationAddress = dirECommunicationAddress::findOrCreate(dirECommunicationAddress);

        select forupdate dirPartyECommunicationRelationship
            where dirPartyECommunicationRelationship.PartyId        == dirPartyTableUpd.PartyId
               && dirPartyECommunicationRelationship.ValuesRecId    == dirECommunicationAddress.RecId;
        if (!dirPartyECommunicationRelationship)
        {
            dirPartyECommunicationRelationship.clear();
            dirPartyECommunicationRelationship.initValue();
            dirPartyECommunicationRelationship.PartyId          = dirPartyTableUpd.PartyId;
            dirPartyECommunicationRelationship.PrivacyGroupId   = "Employee";
            dirPartyECommunicationRelationship.ValuesRecId = dirECommunicationAddress.RecId;
            dirPartyECommunicationRelationship.insert();
        }
        
#endMacro
    ;
    ttsBegin;
    dirPartyTable   = DirPartyTable::createNew(dirPartyType, _tmp.Name);

    emplTable.clear();
    emplTable.initValue();
    emplTable.PartyId                               =   dirPartyTable.PartyId;
    emplTable.InventSiteId                          =   _tmp.InventSiteId;
    emplTable.EmplId                                =   _tmp.EmplId;
    emplTable.Name                                  =   _tmp.Name;
    emplTable.DEL_Name                              =   _tmp.Name;
    emplTable.Dimension                             =   _tmp.Dimension;
    emplTable.Training                              =   _tmp.Training;
    emplTable.AltNum                                =   _tmp.AltNum;
    emplTable.CalendarId                            =   _tmp.CalendarId;
    emplTable.BirthDate                             =   _tmp.BirthDate;
    emplTable.ProjPeriodId                          =   _tmp.ProjPeriodId;
    emplTable.ItemBuyerGroupId                      =   _tmp.ItemBuyerGroupId;
    emplTable.Title                                 =   _tmp.Title;
    emplTable.DEL_Address                           =   _tmp.Address;
    emplTable.DEL_Phone                             =   _tmp.Phone;
    emplTable.DEL_PhoneLocal                        =   _tmp.PhoneLocal;
    emplTable.DEL_UserId                            =   _tmp.UserId;
    emplTable.DEL_ZipCodeId                         =   _tmp.ZipCodeId;
    emplTable.DEL_CountryRegionId                   =   _tmp.CountryId;
    emplTable.DEL_StateId                           =   _tmp.StateId;
    emplTable.DEL_CountyId                          =   _tmp.CountyId;
    emplTable.DEL_URL                               =   _tmp.URL;
    emplTable.DEL_Email                             =   _tmp.Email;
    emplTable.DEL_Telex                             =   _tmp.Telex;
    emplTable.DEL_CellularPhone                     =   _tmp.CellularPhone;
    emplTable.DEL_TeleFax                           =   _tmp.TeleFax;
    emplTable.DEL_Alias                             =   _tmp.Alias;
    emplTable.DEL_City                              =   _tmp.City;
    emplTable.DEL_Street                            =   _tmp.Street;
    emplTable.DEL_Pager                             =   _tmp.Pager;
    emplTable.DEL_SMS                               =   _tmp.SMS;
    emplTable.DEL_RefZipCode                        =   _tmp.RefZipCode;
    emplTable.DEL_currency                          =   _tmp.currency;
    emplTable.DEL_firstName                         =   _tmp.firstName;
    emplTable.DEL_middleName                        =   _tmp.middleName;
    emplTable.DEL_lastName                          =   _tmp.lastName;
    emplTable.insert();

    hRMPartyEmployeeRelationship = HRMPartyEmployeeRelationship::find(emplTable.EmplId);
    if (!hRMPartyEmployeeRelationship)
    {
        hRMPartyEmployeeRelationship.clear();
        hRMPartyEmployeeRelationship.initValue();
        hRMPartyEmployeeRelationship.EmplId = emplTable.EmplID;
        hRMPartyEmployeeRelationship.insert();
    }

    dirPartyTableUpd = DirPartyTable::find(empltable.PartyId, true);
    dirPartyTableUpd.modifiedField( fieldNum(dirPartyTable, name));

    if ( emplTable.DEL_firstName)
    {
        dirPartyTableUpd.FirstName     = emplTable.DEL_firstName;
        dirPartyTableUpd.modifiedField( fieldNum(dirPartyTable, firstName));
    }
    if ( emplTable.DEL_middleName)
    {
        dirPartyTableUpd.MiddleName    = emplTable.DEL_middleName;
        dirPartyTableUpd.modifiedField( fieldNum(dirPartyTable, MiddleName));
    }
    if ( emplTable.DEL_lastName)
    {
        dirPartyTableUpd.LastName      = emplTable.DEL_lastName;
        dirPartyTableUpd.modifiedField( fieldNum(dirPartyTable, LastName));
    }
    dirPartyTableUpd.update();

    address = Address::find(dirPartyTableUpd.TableId, dirPartyTableUpd.RecId, AddressType::Home, true);
    if (!address)
    {
        address.clear();
        address.initValue();
        address.AddrRecId   = dirPartyTableUpd.RecId;
        address.AddrTableId = dirPartyTableUpd.TableId;
    }

    address.type            = addressType::Home;
    address.IsPrimary       = NoYes::Yes;
    address.Name            = emplTable.Name;
    address.Address         = emplTable.DEL_Address;
    address.CountryRegionId = emplTable.DEL_CountryRegionId;
    address.ZipCode         = emplTable.DEL_ZipCodeId;
    address.City            = emplTable.DEL_City;
    address.Street          = emplTable.DEL_Street;
    address.write();

    if (emplTable.DEL_Phone)         { #addComunicationAddress(emplTable.DEL_Phone,     "HomePhone")}
    if (emplTable.DEL_Telex)         { #addComunicationAddress(emplTable.DEL_Telex,     "BusinessFax")}
    if (emplTable.DEL_Email)         { #addComunicationAddress(emplTable.DEL_Email,     "HomeEmail")}
    if (emplTable.DEL_URL)           { #addComunicationAddress(emplTable.DEL_URL,       "PersonalHomePage")}
    if (emplTable.DEL_CellularPhone) { #addComunicationAddress(emplTable.DEL_CellularPhone, "CellularPhone")}

    ttsCommit;
}