project QadFinancials > class BMfgCustomer > method ApiReplicateFromFinancials

Description

Replicate Debtors to MFG Customers


Parameters


tDebtorinputtemp-table
icDomainCodeinputcharacterWhen the domaincode is filled --> Replicate only to this domain.
If the domaincode is empty --> Replicate to all domains in this shared set.
ilIsKeepInstancesOpeninputlogicalThis parameter defines, whether system can keep instances opened after methods are executed (eg. for API integration) or whether system has to close the instances after they are not needed in the component itself (eg. for UI processing).
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDebtor.AdditionalUpdates
method BDebtor.ApiReplicateFromDaemon


program code (program9/bmfgcustomer.p)

MAIN_BLOCK:
do on error undo, leave:

    /* ====================== */
    /* Check input parameters */
    /* ====================== */
    if not can-find(first tDebtor)
    then return.
    
    assign vlIsKeepInstancesOpenBMfgCu = ilIsKeepInstancesOpen.
    
    /* ================== */
    /* Exception Handling */
    /* ================== */
    assign oiReturnStatus                  = -98
           viLocalReturnStatus             = 0
           vcListCmMstr                    = '':U
           vcReplicateDomainCode           = icDomainCode.
           
    /* ============================================================ *
     * Get Head Office Address Type Id                              *
     * ============================================================ */
    <M-96 run GetAddressTypeId
       (output viHeadOfficeAddressTypeId (oiHeadOfficeAddressTypeId), 
        output viFcReturnSuper (oiReturnStatus)) in BMfgCustomer>

    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 tDebtor where tDebtor.tc_Status <> 'N'
            break by tDebtor.SharedSet_ID:
        
            /* =============================================================================================== */
            /* Check if the Customer 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(tDebtor.SharedSet_ID)
            then do:
                <Q-2 run DomainBySharedSetDomain (all) (Read) (NoCache)
                   (input tDebtor.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain>
            end. /* if first-of(tDebtor.SharedSet_ID) */
            
            /* =============================================================================================== */
            /* Get all Mfg Customers which are already defined in the database                                 */
            /* =============================================================================================== */
            <Q-84 run DomainSharedSetForCmMstr (all) (Read) (NoCache)
               (input tDebtor.SharedSet_ID, (SharedSetId)
                input icDomainCode, (DomainCode)
                input tDebtor.DebtorCode, (Address)
                output dataset tqDomainSharedSetForCmMstr) in BDomain>            
                
            /* ============================================================ */
            /* If the cm_mstr exists, we load it                            */
            /* ============================================================ */
            if can-find(first tqDomainSharedSetForCmMstr)
            then do:
                for each tqDomainBySharedSetDomain where
                         tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:
    
                    /* ============================================================ */
                    /* Create a list of all tcm_mstr records that need to be loaded */
                    /* ============================================================ */
                    if can-find(first tqDomainSharedSetForCmMstr where
                                      tqDomainSharedSetForCmMstr.tcDomainCode = tqDomainBySharedSetDomain.tcDomainCode and
                                      tqDomainSharedSetForCmMstr.tccm_addr    = tDebtor.DebtorCode)
                    then assign vcListCmMstr = vcListCmMstr                           + 
                                               (if vcListCmMstr = '':U
                                                then '':U
                                                else chr(4))                          +
                                               tqDomainBySharedSetDomain.tcDomainCode +
                                               chr(2)                                 +
                                               tDebtor.DebtorCode.
               
                    if length(vcListCmMstr, "CHARACTER") > 10000
                    then do:
                        /* ========================= */
                        /* Load the tcm_mstr records */
                        /* ========================= */
                        <M-28 run DataLoad
                           (input  '':U (icRowids), 
                            input  vcListCmMstr (icPkeys), 
                            input  '':U (icObjectIds), 
                            input  '':U (icFreeform), 
                            input  true (ilKeepPrevious), 
                            output viFcReturnSuper (oiReturnStatus)) in BMfgCustomer>                         
                                
                        if viFcReturnSuper <> 0
                        then assign viLocalReturnStatus = viFcReturnSuper.
        
                        assign vcListCmMstr = '':U.   
        
                    end. /* length */    

                end. /* for each tqDomainBySharedSetDomain: */

            end. /* if can-find(first tqDomainSharedSetForCmMstr) */

        end. /* for each tDebtor */
            
        /* ========================= */
        /* Load the tcm_mstr records */
        /* ========================= */
        if vcListCmMstr <> "":U and vcListCmMstr <> ?
        then do :
            <M-4 run DataLoad
               (input  '':U (icRowids), 
                input  vcListCmMstr (icPkeys), 
                input  '':U (icObjectIds), 
                input  '':U (icFreeform), 
                input  true (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BMfgCustomer>
            if viFcReturnSuper <> 0
            then do:
                assign viLocalReturnStatus = viFcReturnSuper.
                leave CONVERTBLOCK.
            end. /* if viFcReturnSuper <> 0 */
        end. /* if vcListCmMstr <> "":U and */

        /* ======================================= */
        /* Convert table tDebtor to table tcm_mstr */
        /* ======================================= */
        for each tDebtor
            break by tDebtor.SharedSet_ID:
        
            /* =============================================================================================== */
            /* Check if the Customer 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(tDebtor.SharedSet_ID)
            then do:
                if tDebtor.SharedSet_ID = 0 or
                   tDebtor.SharedSet_ID = ?
                then do:
                    assign vcMessage           = trim(substitute(#T-15'The shared set ID for this customer (&1) was not defined.':255(69584)t-15#, trim(tDebtor.DebtorCode) ))
                           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-8168':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgCustomer>
                    leave CONVERTBLOCK.
                end. /* if tDebtor.SharedSet_ID = 0 or */
    
                <Q-7 run DomainBySharedSetDomain (all) (Read) (Cache)
                   (input tDebtor.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain >   

            end. /* if first-of(tDebtor.SharedSet_ID) */
            
            /* ========================================================================= *
             *              Get Address and Contact data of the Customer                 *
             * ========================================================================= */
            <Q-69 run AddressForReplication (all) (Read) (NoCache)
               (input tDebtor.BusinessRelation_ID, (BusinessRelationId)
                input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
                input viHeadOfficeAddressTypeId, (AddressTypeId)
                output dataset tqAddressForReplication) in BBusinessRelation>

            find first tqAddressForReplication where
                       tqAddressForReplication.tiBusinessRelation_ID = tDebtor.BusinessRelation_ID and
                       tqAddressForReplication.tcAddressTypeCode     = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}
                       no-error.               
            if not available tqAddressForReplication 
            then do:
                assign vcMessage = #T-55'Cannot read Customer head office address definition (Customer = &1).':255(473136780)T-55#
                       vcMessage = substitute(vcMessage, tDebtor.DebtorCode).
                <M-83 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  tDebtor.tc_Rowid (icRowid), 
                    input  'qadfin-594456':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BMfgCustomer>
                if viLocalReturnStatus >= 0 then assign viLocalReturnStatus = -1.
                next.
            end.
                
            for each tqDomainBySharedSetDomain where
                     tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:

                /* ================================================== */
                /* Create or update tcm_mstr records based on tDebtor */
                /* ================================================== */
                if tDebtor.tc_Status <> 'N'
                then do:
                    find tcm_mstr where
                         tcm_mstr.cm_addr   = tDebtor.DebtorCode and
                         tcm_mstr.cm_domain = tqDomainBySharedSetDomain.tcDomainCode
                         no-error.
                end.

                if not available tcm_mstr or tDebtor.tc_Status = 'N'
                then do:
                    if tDebtor.tc_Status = "D"
                    then next.
    
                    <M-8 run AddDetailLine
                       (input  'cm_mstr':U (icTable), 
                        input  '':U (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgCustomer>

                    if viFcReturnSuper <> 0
                    then do:
                        assign viLocalReturnStatus = viFcReturnSuper.
                        leave CONVERTBLOCK.
                    end. /* if viFcReturnSuper <> 0 */                
    
                    
                end. /* if not available tcm_mstr */  
                else assign tcm_mstr.tc_Status = "C":U.
                
                if vlAutoGenACEdit = ? then 
                do:
                    <Q-49 run PricingControlByDomain (all) (Read) (NoCache)
                       (input vcDomainCode, (Domain)
                        output dataset tqPricingControlByDomain) in BMfgPricingControl>
                    find first tqPricingControlByDomain where tqPricingControlByDomain.tcpic_domain = Vcdomaincode no-error.
                    if available tqPricingControlByDomain and
                        substring(tqPricingControlByDomain.tcpic__qadc01,1,1,"CHARACTER":U) = "Y" 
                    then vlAutoGenACEdit = true.
                    else vlAutoGenACEdit = false.
                end.

                if vlAutoGenACEdit and tcm_mstr.tc_Status = "C":U then 
                do:
                    if tcm_mstr.cm_type <> tDebtor.tcDebtorTypeCode or
                       tcm_mstr.cm_bill <> tDebtor.tcBillToDebtorCode
                    then do:
                         find first tAutoGenCustList where tAutoGenCustList.tcanx_domain = tcm_mstr.cm_domain and 
                             tAutoGenCustList.tcanx_node = tcm_mstr.cm_addr 
                         no-error.
                         if not available tAutoGenCustList then
                         do:
                             create tAutoGenCustList.
                             assign tAutoGenCustList.tcanx_domain = tcm_mstr.cm_domain
                                    tAutoGenCustList.tcanx_node   = tcm_mstr.cm_addr
                                    tAutoGenCustList.tcanx_type   = "9".
                         end.
                    end.
                end.

                assign tcm_mstr.cm_addr        = tDebtor.DebtorCode
                       tcm_mstr.cm_domain      = tqDomainBySharedSetDomain.tcDomainCode
                       tcm_mstr.cm_curr        = tDebtor.tcCurrencyCode
                       tcm_mstr.cm_type        = tDebtor.tcDebtorTypeCode
                       tcm_mstr.cm__qadl01     = tDebtor.DebtorIsInvoiceByAuth
                       tcm_mstr.cm_bill        = tDebtor.tcBillToDebtorCode
                       tcm_mstr.cm_active      = tDebtor.DebtorIsActive
                       tcm_mstr.cm_cr_terms    = tDebtor.tcNormalPaymentConditionCode
                       tcm_mstr.cm_tax_zone    = tDebtor.TxzTaxZone
                       tcm_mstr.cm_taxc        = tDebtor.TxclTaxCls
                       tcm_mstr.cm_tax_usage   = tDebtor.TxuTaxUsage
                       tcm_mstr.cm_gst_id      = tDebtor.DebtorTaxIDFederal
                       tcm_mstr.cm_pst_id      = tDebtor.DebtorTaxIDState
                       tcm_mstr.cm_misc1_id    = tDebtor.DebtorTaxIDMisc1
                       tcm_mstr.cm_misc2_id    = tDebtor.DebtorTaxIDMisc2
                       tcm_mstr.cm_misc3_id    = tDebtor.DebtorTaxIDMisc3
                       tcm_mstr.cm_taxable     = tDebtor.DebtorIsTaxable
                       tcm_mstr.cm_tax_in      = tDebtor.DebtorIsTaxIncluded
                       tcm_mstr.cm_tax_in_city = tDebtor.DebtorIsTaxInCity
                       tcm_mstr.cm_sort        = tqAddressForReplication.tcAddressSearchName
                       tcm_mstr.cm_lang        = tqAddressForReplication.tcAddressLngCode
                       tcm_mstr.cm_mod_date    = today
                       tcm_mstr.cm_userid      = vcUserLogin
                       tcm_mstr.tc_Status      = (if tDebtor.tc_Status = "D":U
                                                  then tDebtor.tc_Status
                                                  else tcm_mstr.tc_Status).

            end. /* for each tqDomainBySharedSetDomain: */

        end. /* for each tDebtor */

    end. /* CONVERTBLOCK */
    
    VALIDATEBLOCK:
    DO:
        if viLocalReturnStatus < 0
        then leave VALIDATEBLOCK.
    
        /* ========== */
        /* ValidateBC */
        /* ========== */
        <M-12 run ValidateBC  (output viFcReturnSuper (oiReturnStatus)) in BMfgCustomer>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
    
        /* ================== */
        /* Additional Updates */
        /* ================== */
        <M-13 run AdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BMfgCustomer>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
        
        /* ======== */
        /* DataSave */
        /* ======== */
        <M-14 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BMfgCustomer>
        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 = "BMfgCustomer".
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/bmfgcustomer.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("tDebtor").
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.