project QadFinancials > class BMfgSupplier > method ApiReplicateFromFinancials


Parameters


tCreditorinputtemp-table
icDomainCodeinputcharacter
ilIsKeepInstancesOpeninputlogical
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCreditor.AdditionalUpdates
method BCreditor.ApiReplicateFromDaemon


program code (program9/bmfgsupplier.p)

MAIN_BLOCK:
do on error undo, leave:
    /* ====================== */
    /* Check input parameters */
    /* ====================== */
    if not can-find(first tCreditor)
    then return.
    
    assign vlIsKeepInstancesOpenBMfgSu = ilIsKeepInstancesOpen.
    
    /* ================== */
    /* Exception Handling */
    /* ================== */
    assign oiReturnStatus                  = -98
           viLocalReturnStatus             = 0
           vcListVdMstr                    = '':U
           vcDomainCodeForReplication      = icDomainCode.
    
    /* ============================================================ *
     * Get Head Office Address Type Id                              *
     * ============================================================ */
    <M-91 run GetAddressTypeId
       (output viHeadOfficeAddressTypeId (oiHeadOfficeAddressTypeId), 
        output viFcReturnSuper (oiReturnStatus)) in BMfgSupplier>
    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 tCreditor
            break by tCreditor.SharedSet_ID:
        
            /* ============================================================================================== */
            /* Check if the Account needs to be replicated to one domain or to all domains of this shared set */
            /* If icDomainCode is left blank, all domains for this shared set will be replicated.             */
            /* ============================================================================================== */
            if first-of(tCreditor.SharedSet_ID)
            then do:
                <Q-2 run DomainBySharedSetDomain (all) (Read) (Cache)
                   (input tCreditor.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain >
            end. /* if first-of(tCreditor.SharedSet_ID) */
            
            
            /* =============================================================================================== */
            /* Get all Mfg Customers which are already defined in the database                                 */
            /* =============================================================================================== */
            <Q-86 run DomainSharedSetForVdMstr (all) (Read) (NoCache)
               (input tCreditor.SharedSet_ID, (SharedSetId)
                input icDomainCode, (DomainCode)
                input tCreditor.CreditorCode, (Address)
                output dataset tqDomainSharedSetForVdMstr) in BDomain >
                
            for each tqDomainBySharedSetDomain where
                     tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:
                /* ============================================================ */
                /* Create a list of all tvd_mstr records that need to be loaded */
                /* ============================================================ */
                if can-find(first tqDomainSharedSetForVdMstr where
                                  tqDomainSharedSetForVdMstr.tcDomainCode = tqDomainBySharedSetDomain.tcDomainCode and
                                  tqDomainSharedSetForVdMstr.tcvd_addr    = tCreditor.CreditorCode)
                then assign vcListVdMstr = vcListVdMstr                           + 
                                           (if vcListVdMstr = '':U
                                            then '':U
                                            else chr(4))                          +
                                           tqDomainBySharedSetDomain.tcDomainCode +
                                           chr(2)                                 +
                                           tCreditor.CreditorCode.
                                           
                if length(vcListVdMstr, "CHARACTER") > 10000
                then do:
                    /* ========================= */
                    /* Load the tad_mstr records */
                    /* ========================= */
                    <M-29 run DataLoad
                       (input  '':U (icRowids), 
                        input  vcListVdMstr (icPkeys), 
                        input  '':U (icObjectIds), 
                        input  '':U (icFreeform), 
                        input  true (ilKeepPrevious), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgSupplier>
                            
                     if viFcReturnSuper <> 0
                     then assign viLocalReturnStatus = viFcReturnSuper.
    
                     assign vcListVdMstr = '':U.   
    
                end. /* length */              
            end. /* for each tqDomainBySharedSetDomain: */
        end. /* for each tCreditor */
        
        /* ========================= */
        /* Load the tvd_mstr records */
        /* ========================= */
        if vcListVdMstr <> "":U and 
           vcListVdMstr <> ?
        then do :
            <M-4 run DataLoad
               (input  '':U (icRowids), 
                input  vcListVdMstr (icPkeys), 
                input  '':U (icObjectIds), 
                input  '':U (icFreeform), 
                input  true (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BMfgSupplier>
            if viFcReturnSuper <> 0
            then do:
                assign viLocalReturnStatus = viFcReturnSuper.
                leave CONVERTBLOCK.
            end. /* if viFcReturnSuper <> 0 */
        end. /* if vcListVdMstr <> "":U and */
    
        /* ========================================= */
        /* Convert table tCreditor to table tvd_mstr */
        /* ========================================= */
        for each tCreditor
            break by tCreditor.SharedSet_ID:
        
            /* =============================================================================================== */
            /* Check if the Supplier needs to be replicated to one domain or to all domains of this shared set */
            /* If icDomainCode is left blank, all domains for this shared set will be replicated.              */
            /* =============================================================================================== */
            if first-of(tCreditor.SharedSet_ID)
            then do:
                if tCreditor.SharedSet_ID = 0 or
                   tCreditor.SharedSet_ID = ?
                then do:
                    assign vcMessage           = trim(substitute(#T-15'The shared set ID for this supplier (&1) was not defined.':255(69187)T-15#, trim(tCreditor.CreditorCode) ))
                           viLocalReturnStatus = -3.
                    <M-6 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'E':U (icType), 
                        input  1 (iiSeverity), 
                        input  '':U (icRowid), 
                        input  'QadFin-7948':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgSupplier>
                    leave CONVERTBLOCK.
                end. /* if tCreditor.SharedSet_ID = 0 or */
    
                <Q-7 run DomainBySharedSetDomain (all) (Read) (Cache)
                   (input tCreditor.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain >            
            end. /* if first-of(tCreditor.SharedSet_ID) */
            
            /* ======================================================================= *
             * Get additional information about the Supplier's business relation       *
             * ======================================================================= */
            <Q-57 run AddressForReplication (all) (Read) (NoCache)
               (input tCreditor.BusinessRelation_ID, (BusinessRelationId)
                input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
                input viHeadOfficeAddressTypeId, (AddressTypeId)
                output dataset tqAddressForReplication) in BBusinessRelation >
            
            find first tqAddressForReplication where
                       tqAddressForReplication.tiBusinessRelation_ID = tCreditor.BusinessRelation_ID
                       no-error.
            if not available tqAddressForReplication
            then do:
                assign vcMessage = #T-78'Cannot read Supplier head office address definition (Supplier = &1).':255(650071665)T-78#
                       vcMessage = substitute(vcMessage, tCreditor.CreditorCode).
                <M-44 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  tCreditor.tc_Rowid (icRowid), 
                    input  'qadfin-204518':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BMfgSupplier>
                if viLocalReturnStatus >= 0 then assign viLocalReturnStatus = -1.
                next.
            end.                
    
            for each tqDomainBySharedSetDomain where
                     tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:
                /* ==================================================== */
                /* Create or update tvd_mstr records based on tCreditor */
                /* ==================================================== */
                find tvd_mstr where
                     tvd_mstr.vd_addr   = tCreditor.CreditorCode and
                     tvd_mstr.vd_domain = tqDomainBySharedSetDomain.tcDomainCode
                     no-error.
                if not available tvd_mstr
                then do:
                    if tCreditor.tc_Status = "D":U
                    then next.
    
                    <M-8 run AddDetailLine
                       (input  'vd_mstr':U (icTable), 
                        input  '':U (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgSupplier>
                    if viFcReturnSuper <> 0
                    then do:
                        assign viLocalReturnStatus = viFcReturnSuper.
                        leave CONVERTBLOCK.
                    end. /* if viFcReturnSuper <> 0 */                
                end. /* if not available tvd_mstr */  
                else assign tvd_mstr.tc_Status = "C":U.
    
                assign tvd_mstr.vd_addr         = tCreditor.CreditorCode 
                       tvd_mstr.vd_domain       = tqDomainBySharedSetDomain.tcDomainCode
                       tvd_mstr.vd_active       = tCreditor.CreditorIsActive
                       tvd_mstr.vd_type         = tCreditor.tcCreditorTypeCode
                       tvd_mstr.vd_db           = tCreditor.CreditorDBNumber
                       tvd_mstr.vd_debtor       = tCreditor.CreditorDebtorNumber
                       tvd_mstr.vd_1099         = tCreditor.CreditorIsTaxReport
                       tvd_mstr.vd_tid_notice   = tCreditor.CreditorTIDNotice
                       tvd_mstr.vd_curr         = tCreditor.tcCurrencyCode
                       tvd_mstr.vd_mod_date     = today
                       tvd_mstr.vd_userid       = vcUserLogin
                       tvd_mstr.vd_cr_terms     = tCreditor.tcNormalPaymentConditionCode
                       tvd_mstr.vd_tax_zone     = tCreditor.TxzTaxZone
                       tvd_mstr.vd_taxc         = tCreditor.TxclTaxCls
                       tvd_mstr.vd_tax_usage    = tCreditor.TxuTaxUsage
                       tvd_mstr.vd_gst_id       = tCreditor.CreditorTaxIDFederal
                       tvd_mstr.vd_pst          = tCreditor.CreditorTaxIDState
                       tvd_mstr.vd_misc1_id     = tCreditor.CreditorTaxIDMisc1
                       tvd_mstr.vd_misc2_id     = tCreditor.CreditorTaxIDMisc2
                       tvd_mstr.vd_misc3_id     = tCreditor.CreditorTaxIDMisc3
                       tvd_mstr.vd_taxable      = tCreditor.CreditorIsTaxable
                       tvd_mstr.vd_tax_in       = tCreditor.CreditorIsTaxIncluded
                       tvd_mstr.vd_tax_in_city  = tCreditor.CreditorIsTaxInCity
                       tvd_mstr.vd_lang         = tqAddressForReplication.tcAddressLngCode
                       tvd_mstr.vd_sort         = tqAddressForReplication.tcAddressSearchName
                       tvd_mstr.tc_Status       = (if tCreditor.tc_Status = "D":U
                                                   then tCreditor.tc_Status
                                                   else tvd_mstr.tc_Status).
            end. /* for each tqDomainBySharedSetDomain: */
        end. /* for each tCreditor */
    end. /* CONVERTBLOCK */
    
    VALIDATEBLOCK:
    DO:
        if viLocalReturnStatus < 0
        then leave VALIDATEBLOCK.
    
        /* ========== */
        /* ValidateBC */
        /* ========== */
        <M-12 run ValidateBC  (output viFcReturnSuper (oiReturnStatus)) in BMfgSupplier>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
    
        /* ================== */
        /* Additional Updates */
        /* ================== */
        <M-13 run AdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BMfgSupplier>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
        
        /* ======== */
        /* DataSave */
        /* ======== */
        <M-14 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BMfgSupplier>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
    end. /* VALIDATEBLOCK */
end. /* 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 = "BMfgSupplier".
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/bmfgsupplier.apireplicatefromfinancials.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icDomainCode = <parameter value>
       vhParameter::ilIsKeepInstancesOpen = <parameter value>.

vhParameter = vhInputDS:get-buffer-handle("tCreditor").
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.