project QadFinancials > class BMfgCostCenter > method ApiReplicateFromFinancials

Description

Replicate Cost Centre to MFG Cost Center


Parameters


tCopyCostCentreinputtemp-table
icDomainCodeinputcharacterWhen the domaincode is filled --> Replicate only to this domain.
If the domaincode is empty --> Replicate to all domains in this shared set.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCostCentre.ApiReplicateFromDaemon


program code (program9/bmfgcostcenter.p)

/* ====================== */
/* Check input parameters */
/* ====================== */
if not can-find(first tCopyCostCentre)
then return.

/* ================== */
/* Exception Handling */
/* ================== */
assign oiReturnStatus                  = -98
       viLocalReturnStatus             = 0
       vlQueryStartedDomainBySharedSet = false
       vlQueryStartedTranslation       = false
       vcListCcMstr                    = '':U.

CONVERTBLOCK:
DO:
    /* ===================================== */
    /* Check which records need to be loaded */
    /* ===================================== */
    for each tCopyCostCentre
        break by tCopyCostCentre.SharedSet_ID:
    
        /* =============== */
        /* Start the query */
        /* =============== */
        if vlQueryStartedDomainBySharedSet = false
        then do:
            <Q-1 run DomainBySharedSetDomain  (Start) in BDomain >
            assign vlQueryStartedDomainBySharedSet = true.
        end. /* if vlQueryStartedDomainBySharedSet = false */
    
        /* ================================================================================================== */
        /* Check if the Cost Centre 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(tCopyCostCentre.SharedSet_ID)
        then do:
            <Q-2 run DomainBySharedSetDomain (all) (Read) (Cache)
               (input tCopyCostCentre.SharedSet_ID, (SharedSetId)
                input 0, (DomainID)
                input icDomainCode, (DomainCode)
                output dataset tqDomainBySharedSetDomain) in BDomain >
        end. /* if first-of(tCopyCostCentre.SharedSet_ID) */
            
        for each tqDomainBySharedSetDomain where
                 tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:
            /* ============================================================ */
            /* Create a list of all tcc_mstr records that need to be loaded */
            /* ============================================================ */
            <Q-19 assign vlFcQueryRecordsAvailable = CcMstrByCtrDomain (NoCache)
               (input tCopyCostCentre.CostCentreCode, (Ctr)
                input tqDomainBySharedSetDomain.tcDomainCode, (Domain)) in BMfgCostCenter >
            if vlFcQueryRecordsAvailable = true
            then assign vcListCcMstr = vcListCcMstr                           + 
                                       (if vcListCcMstr = '':U
                                        then '':U
                                        else chr(4))                          +
                                       tqDomainBySharedSetDomain.tcDomainCode +
                                       chr(2)                                 +
                                       tCopyCostCentre.CostCentreCode.
        end. /* for each tqDomainBySharedSetDomain: */
    end. /* for each tCopyCostCentre */
    
    /* ========================= */
    /* Load the tcc_mstr records */
    /* ========================= */
    if vcListCcMstr <> "":U and 
       vcListCcMstr <> ?
    then do :
        <M-4 run DataLoad
           (input  '':U (icRowids), 
            input  vcListCcMstr (icPkeys), 
            input  '':U (icObjectIds), 
            input  '':U (icFreeform), 
            input  false (ilKeepPrevious), 
            output viFcReturnSuper (oiReturnStatus)) in BMfgCostCenter>
        if viFcReturnSuper <> 0
        then do:
            assign viLocalReturnStatus = viFcReturnSuper.
            leave CONVERTBLOCK.
        end. /* if viFcReturnSuper <> 0 */
    end. /* if vcListCcMstr <> "":U and */
    /* =============================================== */
    /* Convert table tCopyCostCentre to table tcc_mstr */
    /* =============================================== */
    for each tCopyCostCentre
        break by tCopyCostCentre.SharedSet_ID:
    
        /* =============== */
        /* Start the query */
        /* =============== */
        if vlQueryStartedDomainBySharedSet = false
        then do:
            <Q-5 run DomainBySharedSetDomain  (Start) in BDomain >
            assign vlQueryStartedDomainBySharedSet = true.
        end. /* if vlQueryStartedDomainBySharedSet = false */
    
        /* ================================================================================================== */
        /* Check if the Cost Centre 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(tCopyCostCentre.SharedSet_ID)
        then do:
            if tCopyCostCentre.SharedSet_ID = 0 or
               tCopyCostCentre.SharedSet_ID = ?
            then do:
                assign vcMessage           = trim(substitute(#T-15'The shared set ID for cost center (&1) was not defined.':255(69591)t-15#, trim(tCopyCostCentre.CostCentreCode) ))
                       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-8178':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BMfgCostCenter>
                leave CONVERTBLOCK.
            end. /* if tCopyCostCentre.SharedSet_ID = 0 or */

            <Q-7 run DomainBySharedSetDomain (all) (Read) (Cache)
               (input tCopyCostCentre.SharedSet_ID, (SharedSetId)
                input 0, (DomainID)
                input icDomainCode, (DomainCode)
                output dataset tqDomainBySharedSetDomain) in BDomain >            
        end. /* if first-of(tCopyCostCentre.SharedSet_ID) */
            
        for each tqDomainBySharedSetDomain  where
                 tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:
            /* ========================================================== */
            /* Create or update tcc_mstr records based on tCopyCostCentre */
            /* ========================================================== */
            find tcc_mstr where
                 tcc_mstr.cc_ctr    = tCopyCostCentre.CostCentreCode and
                 tcc_mstr.cc_domain = tqDomainBySharedSetDomain.tcDomainCode
                 no-error.
            if not available tcc_mstr
            then do:
                if tCopyCostCentre.tc_Status = "D"
                then next.

                <M-8 run AddDetailLine
                   (input  'cc_mstr':U (icTable), 
                    input  '':U (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BMfgCostCenter>
                if viFcReturnSuper <> 0
                then do:
                    assign viLocalReturnStatus = viFcReturnSuper.
                    leave CONVERTBLOCK.
                end. /* if viFcReturnSuper <> 0 */                

                assign tcc_mstr.oid_cc_mstr = <M-9 GetNextOidValue  () in BMfgCostCenter> no-error.
                if error-status:error
                then do:
                    assign vcMessage           = trim(#T-16'Cannot retrieve an oid-value while replicating cost centers.':255(69592)t-16#) + chr(10) +
                                                 trim(substitute(#T-17'Cost center: &1.':255(710)T-17#, trim(tCopyCostCentre.CostCentreCode)))
                           viLocalReturnStatus = -1.
                    if ERROR-STATUS:NUM-MESSAGES > 0
                    then assign vcMessage = vcMessage + chr(10) + 
                                            trim(substitute(#T-18'Detailed info: &1 (&2).':255(466)T-18#,ERROR-STATUS:GET-MESSAGE(1),string(ERROR-STATUS:GET-NUMBER(1)))).
                    <M-10 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-8179':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgCostCenter>
                    leave CONVERTBLOCK.
                end.
            end. /* if not available tcc_mstr */  
            else assign tcc_mstr.tc_Status = "C":U.
           
            assign tcc_mstr.cc_ctr           = tCopyCostCentre.CostCentreCode 
                   tcc_mstr.cc_domain        = tqDomainBySharedSetDomain.tcDomainCode
                   tcc_mstr.cc_desc          = tCopyCostCentre.CostCentreDescription
                   tcc_mstr.cc_active        = tCopyCostCentre.CostCentreIsActive
                   tcc_mstr.tc_Status         = (if tCopyCostCentre.tc_Status = "D":U
                                                 then tCopyCostCentre.tc_Status
                                                 else tcc_mstr.tc_Status).

            if vlQueryStartedTranslation = false
            then do:
                <Q-20 run TranslationByIDLngParent  (Start) in BTranslation >                  
                assign vlQueryStartedTranslation = true.
            end.

            <Q-21 run TranslationByIDLngParent (all) (Read) (NoCache)
               (input tqDomainBySharedSetDomain.tiLng_ID, (LngID)
                input tCopyCostCentre.CostCentre_ID, (ParentID)
                input 0, (TranslationID)
                output dataset tqTranslationByIDLngParent) in BTranslation >            
            find first tqTranslationByIDLngParent no-error.
            if available tqTranslationByIDLngParent 
            then assign tcc_mstr.cc_desc = tqTranslationByIDLngParent.tcTranslationStringText.
        end. /* for each tqDomainBySharedSetDomain: */
    end. /* for each tCopyCostCentre */
end. /* CONVERTBLOCK */

/* ============== */
/* Stop the query */
/* ============== */
if vlQueryStartedDomainBySharedSet = true
then do:
    <Q-11 run DomainBySharedSetDomain  (Stop) in BDomain >
    assign vlQueryStartedDomainBySharedSet = false.
end. /* if vlQueryStartedDomainBySharedSet = true */

if vlQueryStartedTranslation = true
then do:
    <Q-22 run TranslationByIDLngParent  (Stop) in BTranslation >                  
    assign vlQueryStartedTranslation = false.
end.

VALIDATEBLOCK:
DO:
    if viLocalReturnStatus < 0
    then leave VALIDATEBLOCK.

    /* ========== */
    /* ValidateBC */
    /* ========== */
    <M-12 run ValidateBC  (output viFcReturnSuper (oiReturnStatus)) in BMfgCostCenter>
    if viFcReturnSuper <> 0
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then leave VALIDATEBLOCK.

    /* ================== */
    /* Additional Updates */
    /* ================== */
    <M-13 run AdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BMfgCostCenter>
    if viFcReturnSuper <> 0
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then leave VALIDATEBLOCK.
    
    /* ======== */
    /* DataSave */
    /* ======== */
    <M-14 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BMfgCostCenter>
    if viFcReturnSuper <> 0
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then leave VALIDATEBLOCK.
end. /* VALIDATEBLOCK */

/* ================== */                        
/* 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 = "BMfgCostCenter".
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/bmfgcostcenter.apireplicatefromfinancials.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icDomainCode = <parameter value>.

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