project QadFinancials > class BMfgEmployee > method ApiReplicateFromFinancials

Description

Replicate Employee to MFG Employee


Parameters


tMfgEmployeeinputtemp-table
ilIsKeepInstancesOpeninputlogical
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBusinessRelation.PostSave
method BEmployee.AdditionalUpdates
method BEmployee.ApiReplicateFromDaemon


program code (program9/bmfgemployee.p)

MAIN_BLOCK:
do on error undo, leave:
    /* ====================== */
    /* Check input parameters */
    /* ====================== */
    if not can-find(first tMfgEmployee)
    then return.
    
    assign vlIsKeepInstOpenBMfgEmp = ilIsKeepInstancesOpen.
    
    /* ================== */
    /* Exception Handling */
    /* ================== */
    assign oiReturnStatus                 = -98
           viLocalReturnStatus            = 0
           vcListEmpMstr                  = '':U.
    
    <M-91 run GetAddressTypeId
       (output viHeadOfficeAddressTypeId (oiHeadOfficeAddressTypeId), 
        output viFcReturnSuper (oiReturnStatus)) in BMfgEmployee>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
        
    CONVERTBLOCK:
    DO:
        
        /* ===================================== */
        /* Check which records need to be loaded */
        /* ===================================== */
        for each tMfgEmployee
            break by tMfgEmployee.Company_ID:
        
            /* ============================================================= */
            /* Check if to which domain the Employee needs to be replicated  */        
            /* ============================================================= */
            if first-of(tMfgEmployee.Company_ID)
            then do:
                <Q-19 run CompanyByCompanyCodeDomainCode (all) (Read) (Cache)
                   (input tMfgEmployee.Company_ID, (CompanyId)
                    input '':U, (CompanyCode)
                    input '':U, (DomainCode)
                    input 0, (DomainId)
                    input ?, (CompanyIsActive)
                    output dataset tqCompanyByCompanyCodeDomainCode) in BCompany>
                find tqCompanyByCompanyCodeDomainCode where
                     tqCompanyByCompanyCodeDomainCode.tiCompany_ID = tMfgEmployee.Company_ID
                     no-lock no-error.
                if not available tqCompanyByCompanyCodeDomainCode
                then do:
                    assign vcMessage      = trim(#T-21'The system cannot find the entity linked to the employee.':255(69605)t-21#)       + chr(10) +
                                            trim(substitute(#T-22'Employee: &1':255(69606)T-22#, trim(tMfgEmployee.EmployeeCode))) + chr(10) +
                                            trim(substitute(#T-23'Company ID: &1.':255(63317)t-23#, trim(string(tMfgEmployee.Company_ID))))
                           viLocalReturnStatus = -1.
                    <M-24 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  'tMfgEmployee.Company_ID':U (icFieldName), 
                        input  string(tMfgEmployee.Company_ID) (icFieldValue), 
                        input  'E':U (icType), 
                        input  3 (iiSeverity), 
                        input  tMfgEmployee.tc_Rowid (icRowid), 
                        input  'QadFin-8193':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgEmployee>
                end. /* if not available tqCompanyByCompanyCodeDomainCode */
            end. /* if first-of(tMfgEmployee.Company_ID) */
                        
            /* ============================================================= */
            /* Create a list of all temp_mstr records that need to be loaded */
            /* ============================================================= */
            <Q-25 assign vlFcQueryRecordsAvailable = GetEmployeeForCode (NoCache)
               (input tqCompanyByCompanyCodeDomainCode.tcDomainCode, (Domain)
                input tMfgEmployee.EmployeeCode, (Employee)) in BMfgEmployee >    
            if vlFcQueryRecordsAvailable = true
            then assign vcListEmpMstr = vcListEmpMstr                           + 
                                       (if vcListEmpMstr = '':U
                                        then '':U
                                        else chr(4))                                       +
                                             tqCompanyByCompanyCodeDomainCode.tcDomainCode +
                                             chr(2)                                        +
                                             tMfgEmployee.EmployeeCode.
        end. /* for each tMfgEmployee */
        
        /* ========================== */
        /* Load the temp_mstr records */
        /* ========================== */
        if vcListEmpMstr <> "":U and 
           vcListEmpMstr <> ?
        then do :
            <M-4 run DataLoad
               (input  '':U (icRowids), 
                input  vcListEmpMstr (icPkeys), 
                input  '':U (icObjectIds), 
                input  '':U (icFreeform), 
                input  false (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BMfgEmployee>
            if viFcReturnSuper <> 0
            then do:
                assign viLocalReturnStatus = viFcReturnSuper.
                leave CONVERTBLOCK.
            end. /* if viFcReturnSuper <> 0 */
        end. /* if vcListEmpMstr <> "":U and */
        
        /* ============================================= */
        /* Convert table tMfgEmployee to table temp_mstr */
        /* ============================================= */
        for each tMfgEmployee
            break by tMfgEmployee.Company_ID:
            
            /* ============================================================= */
            /* Check if to which domain the Employee needs to be replicated  */        
            /* ============================================================= */
            if first-of(tMfgEmployee.Company_ID)
            then do:
                <Q-27 run CompanyByCompanyCodeDomainCode (all) (Read) (Cache)
                   (input tMfgEmployee.Company_ID, (CompanyId)
                    input '':U, (CompanyCode)
                    input '':U, (DomainCode)
                    input 0, (DomainId)
                    input ?, (CompanyIsActive)
                    output dataset tqCompanyByCompanyCodeDomainCode) in BCompany >
                find tqCompanyByCompanyCodeDomainCode where
                     tqCompanyByCompanyCodeDomainCode.tiCompany_ID = tMfgEmployee.Company_ID
                     no-lock no-error.
                if not available tqCompanyByCompanyCodeDomainCode
                then do:
                    assign vcMessage      = trim(#T-29'The system cannot find the entity linked to the employee.':255(69605)t-29#)       + chr(10) +
                                            trim(substitute(#T-30'Employee: &1':255(69606)T-30#, trim(tMfgEmployee.EmployeeCode) )) + chr(10) +
                                            trim(substitute(#T-31'Company ID: &1.':255(63317)t-31#, trim(string(tMfgEmployee.Company_ID)) ))
                           viLocalReturnStatus = -1.
                    <M-28 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  'tMfgEmployee.Company_ID':U (icFieldName), 
                        input  string(tMfgEmployee.Company_ID) (icFieldValue), 
                        input  'E':U (icType), 
                        input  3 (iiSeverity), 
                        input  tMfgEmployee.tc_Rowid (icRowid), 
                        input  'QadFin-8194':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgEmployee>
                end. /* if not available tqCompanyByCompanyCodeDomainCode */
            end. /* if first-of(tMfgEmployee.Company_ID) */
            
            /* ================================================== */
            /* Create or update temp_mstr records based on tMfgEmployee */
            /* ================================================== */
            find temp_mstr where
                 temp_mstr.emp_addr   = tMfgEmployee.EmployeeCode and
                 temp_mstr.emp_domain = tqCompanyByCompanyCodeDomainCode.tcDomainCode
                 no-error.
            if not available temp_mstr
            then do:
                if tMfgEmployee.tc_Status = "D"
                then next.
    
                <M-8 run AddDetailLine
                   (input  'emp_mstr':U (icTable), 
                    input  '':U (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BMfgEmployee>
                if viFcReturnSuper <> 0
                then do:
                    assign viLocalReturnStatus = viFcReturnSuper.
                    leave CONVERTBLOCK.
                end. /* if viFcReturnSuper <> 0 */                
            end. /* if not available temp_mstr */  
            else assign temp_mstr.tc_Status = "C":U.
            
            assign temp_mstr.emp_addr        = tMfgEmployee.EmployeeCode
                   temp_mstr.emp_domain      = tqCompanyByCompanyCodeDomainCode.tcDomainCode
                   temp_mstr.emp_title       = tMfgEmployee.EmployeeJobTitle
                   temp_mstr.emp_emp_date    = tMfgEmployee.EmployeeStartDate
                   temp_mstr.emp_trm_date    = tMfgEmployee.EmployeeEndDate
                   temp_mstr.emp_dept        = tMfgEmployee.EmployeeDepartmentCode
                   temp_mstr.emp_active      = tMfgEmployee.EmployeeIsActive
                   temp_mstr.emp_status      = (if tMfgEmployee.EmployeeIsActive = true
                                                then "AC":U 
                                                else "TR":U)
                   temp_mstr.tc_Status      = (if tMfgEmployee.tc_Status = "D":U
                                               then tMfgEmployee.tc_Status
                                               else temp_mstr.tc_Status).
    
            if temp_mstr.tc_Status = 'C':U or 
               temp_mstr.tc_Status = 'N':U
            then do:
                find tMfgAddress where 
                     tMfgAddress.BusinessRelation_ID = tMfgEmployee.BusinessRelation_ID and
                     tMfgAddress.tcAddressTypeCode   = {&ADDRESSTYPECODESYSTEM-HEADOFFICE} 
                     no-error.
                /* Assign address & contact information from passed in temp-table values */
                if available tMfgAddress
                then do:
                    <Q-32 run StateByIDAllInfo (all) (Read) (Cache)
                       (input tMfgAddress.State_ID, (State_ID)
                        output dataset tqStateByIDAllInfo) in BState >                
                    find first tqStateByIDAllInfo where 
                               tqStateByIDAllInfo.tiState_ID = tMfgAddress.State_ID
                               no-error.
                    if available tqStateByIDAllInfo
                    then assign temp_mstr.emp_state = tqStateByIDAllInfo.tcStateCode.
                                    
                    assign temp_mstr.emp_name   = tMfgAddress.AddressName
                           temp_mstr.emp_sort   = tMfgAddress.AddressSearchName
                           temp_mstr.emp_line1  = tMfgAddress.AddressStreet1
                           temp_mstr.emp_line2  = tMfgAddress.AddressStreet2
                           temp_mstr.emp_line3  = tMfgAddress.AddressStreet3
                           temp_mstr.emp_city   = tMfgAddress.AddressCity
                           temp_mstr.emp_zip    = tMfgAddress.AddressZip.
                    
                    /* Resolve company (entity) name */
                    <Q-33 run CompanyPrim (all) (Read) (Cache)
                       (input tMfgEmployee.Company_ID, (LookupCompanyId)
                        input ?, (CompanyCode)
                        output dataset tqCompanyPrim) in BCompany >
                    find first tqCompanyPrim where
                               tqCompanyPrim.tiCompany_ID = tMfgEmployee.Company_ID
                               no-lock no-error.
                    if available tqCompanyPrim
                    then assign temp_mstr.emp_entity = tqCompanyPrim.tcCompanyCode.
    
                    /* Retrieve country code & description */
                     <Q-34 run CountryByCountry (all) (Read) (Cache)
                        (input tMfgAddress.Country_ID, (CountryId)
                         input ?, (CountryCode)
                         output dataset tqCountryByCountry) in BCountry >
                    find first tqCountryByCountry where 
                               tqCountryByCountry.tiCountry_ID = tMfgAddress.Country_ID
                               no-error.
                    if available tqCountryByCountry
                    then assign temp_mstr.emp_country = tqCountryByCountry.tcCountryDescription
                                temp_mstr.emp_ctry    = tqCountryByCountry.tcCountryCode.
            
                    /* Assign primary and secondary contact phone numbers */
                    find first tMfgContact where 
                               tMfgContact.Address_ID = tMfgAddress.Address_ID
                               no-error.
                    if available tMfgContact
                    then do:
                        if tMfgContact.ContactIsPrimary = true
                        then assign temp_mstr.emp_bs_phone = tMfgContact.ContactTelephone.                    
                        if tMfgContact.ContactIsSecondary = true
                        then assign temp_mstr.emp_phone = tMfgContact.ContactTelephone.
                    end. /* if available tMfgContact */                
                end. /* if available tMfgAddress */
                else do:
                    /* Assign address information from "HEADOFFICE" address */
                    <Q-56 run AddressForReplication (all) (Read) (NoCache)
                       (input tMfgEmployee.BusinessRelation_ID, (BusinessRelationId)
                        input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
                        input viHeadOfficeAddressTypeId, (AddressTypeId)
                        output dataset tqAddressForReplication) in BBusinessRelation >
                    find first tqAddressForReplication where
                               tqAddressForReplication.tiBusinessRelation_ID = tMfgEmployee.BusinessRelation_ID and
                               tqAddressForReplication.tcAddressTypeCode     = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}
                               no-error.
                    if not available tqAddressForReplication
                    then do:
                        assign vcMessage = #T-64'Cannot read Employes &1 Head office address.':255(347879993)T-64#
                               vcMessage = substitute(vcMessage, tMfgEmployee.EmployeeCode).
                        <M-11 run SetMessage
                           (input  vcMessage (icMessage), 
                            input  '':U (icArguments), 
                            input  '':U (icFieldName), 
                            input  '':U (icFieldValue), 
                            input  'S':U (icType), 
                            input  3 (iiSeverity), 
                            input  tMfgEmployee.tc_Rowid (icRowid), 
                            input  'qadfin-146585':U (icFcMsgNumber), 
                            input  '':U (icFcExplanation), 
                            input  '':U (icFcIdentification), 
                            input  '':U (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BMfgEmployee>
                        assign viLocalReturnStatus = -1.
                        next.
                    end.

                    assign temp_mstr.emp_state   = tqAddressForReplication.tcStateCode
                           temp_mstr.emp_name    = tqAddressForReplication.tcAddressName
                           temp_mstr.emp_sort    = tqAddressForReplication.tcAddressSearchName
                           temp_mstr.emp_line1   = tqAddressForReplication.tcAddressStreet1
                           temp_mstr.emp_line2   = tqAddressForReplication.tcAddressStreet2
                           temp_mstr.emp_line3   = tqAddressForReplication.tcAddressStreet3
                           temp_mstr.emp_city    = tqAddressForReplication.tcAddressCity                           
                           temp_mstr.emp_zip     = tqAddressForReplication.tcAddressZip
                           temp_mstr.emp_country = tqAddressForReplication.tcCountryDescription
                           temp_mstr.emp_ctry    = tqAddressForReplication.tcCountryCode.
                    
                    /* Resolve company (entity) name */
                    <Q-37 run CompanyPrim (all) (Read) (Cache)
                       (input tMfgEmployee.Company_ID, (LookupCompanyId)
                        input ?, (CompanyCode)
                        output dataset tqCompanyPrim) in BCompany >
                    find first tqCompanyPrim where
                               tqCompanyPrim.tiCompany_ID = tMfgEmployee.Company_ID
                               no-lock no-error.
                    if available tqCompanyPrim
                    then assign temp_mstr.emp_entity = tqCompanyPrim.tcCompanyCode.
       
                    /* Assign primary and secondary contact phone numbers */
                    <Q-39 run ContactByAddress (all) (Read) (Cache)
                       (input tqAddressForReplication.tiAddress_ID, (AddressId)
                        output dataset tqContactByAddress) in BBusinessRelation >
                    for each tqContactByAddress:
                        if tqContactByAddress.tlContactIsPrimary = true
                        then assign temp_mstr.emp_bs_phone = tqContactByAddress.tcContactTelephone.
                        if tqContactByAddress.tlContactIsSecondary = true
                        then assign temp_mstr.emp_phone = tqContactByAddress.tcContactTelephone.
                    end. /* for each tqContactByAddress */
                end. /* else do */                
            end. /* if temp_mstr */
        end. /* for each tMfgEmployee */    
    end. /* CONVERTBLOCK */
    
    VALIDATEBLOCK:
    DO:
        if viLocalReturnStatus < 0
        then leave VALIDATEBLOCK.
    
        /* ========== */
        /* ValidateBC */
        /* ========== */
        <M-12 run ValidateBC  (output viFcReturnSuper (oiReturnStatus)) in BMfgEmployee>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
    
        /* ================== */
        /* Additional Updates */
        /* ================== */
        <M-13 run AdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BMfgEmployee>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
        
        /* ======== */
        /* DataSave */
        /* ======== */
        <M-14 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BMfgEmployee>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
    end. /* VALIDATEBLOCK */

end. /* of MAIN_BLOCK */
    
/* ================== */                        
/* Exception Handling */
/* ================== */
assign oiReturnStatus = viLocalReturnStatus.


Sample code: how to call this method through RPCRequestService (QXtend Inbound)

define temp-table ttContext no-undo
    field propertyQualifier as character
    field propertyName as character
    field propertyValue as character
    index entityContext is primary unique
        propertyQualifier
        propertyName
    index propertyQualifier
        propertyQualifier.

define dataset dsContext for ttContext.

define variable vhContextDS as handle no-undo.
define variable vhExceptionDS as handle no-undo.
define variable vhServer as handle no-undo.
define variable vhInputDS as handle no-undo.
define variable vhInputOutputDS as handle no-undo.
define variable vhOutputDS as handle no-undo.
define variable vhParameter as handle no-undo.

/* Create context */
create ttContext.
assign ttContext.propertyName = "programName"
       ttContext.propertyValue = "BMfgEmployee".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "ApiReplicateFromFinancials".
create ttContext.
assign ttContext.propertyName = "applicationId"
       ttContext.propertyValue = "fin".
create ttContext.
assign ttContext.propertyName = "entity"
       ttContext.propertyValue = "1000".
create ttContext.
assign ttContext.propertyName = "userName"
       ttContext.propertyValue = "mfg".
create ttContext.
assign ttContext.propertyName = "password"
       ttContext.propertyValue = "".

/* Create input dataset */
create dataset vhInputDS.
vhInputDS:read-xmlschema("file", "xml/bmfgemployee.apireplicatefromfinancials.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::ilIsKeepInstancesOpen = <parameter value>.

vhParameter = vhInputDS:get-buffer-handle("tMfgEmployee").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

/* Connect the AppServer */
create server vhServer.
vhServer:connect("-URL <appserver-url>").

if not vhServer:connected()
then do:
    message "Could not connect AppServer" view-as alert-box error title "Error".
    return.
end.

/* Run */
assign vhContextDS = dataset dsContext:handle.

run program/rpcrequestservice.p on vhServer
    (input-output dataset-handle vhContextDS by-reference,
           output dataset-handle vhExceptionDS,
     input        dataset-handle vhInputDS by-reference,
     input-output dataset-handle vhInputOutputDS by-reference,
           output dataset-handle vhOutputDS).

/* Handle output however you want, in this example, we dump it to xml */
if valid-handle(vhExceptionDS)
then vhExceptionDS:write-xml("file", "Exceptions.xml", true).

if valid-handle(vhOutputDS)
then vhOutputDS:write-xml("file", "Output.xml", true).

/* Cleanup */
vhServer:disconnect().
assign vhServer = ?.

if valid-handle(vhInputDS)
then delete object vhInputDS.

if valid-handle(vhOutputDS)
then delete object vhOutputDS.

if valid-handle(vhExceptionDS)
then delete object vhExceptionDS.