project QadFinancials > class BMfgEndUser > method ApiReplicateFromFinancials

Description

Replicate Debtor End User to MFG End User


Parameters


tDebtorEndUserinputtemp-table
icDomainCodeinputcharacterWhen the domaincode is filled --> Replicate only to this domain.
If the domaincode is empty --> Replicate to all domains in this shared set.
iiBBusinessRelationIDinputintegerInstance ID of BBusinessRelation when this is already started before.
ihBBusinessRelationInstinputhandle
ilIsKeepInstancesOpeninputlogical
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDebtorEndUser.AdditionalUpdates
method BDebtorEndUser.ApiReplicateFromDaemon


program code (program9/bmfgenduser.p)

MAIN_BLOCK:
do on error undo, leave:

    /* ====================== */
    /* Check input parameters */
    /* ====================== */
    if not can-find(first tDebtorEndUser)
    then return.
    
    /* ===================================================================================== */
    /* Overtake started instances                                                            */
    /* ===================================================================================== */
    assign viBBusinessRelationMfgEndUserID   = iiBBusinessRelationID
           vhBBusinessRelationMfgEndUserInst = ihBBusinessRelationInst
           vlIsKeepInstancesOpenBMfgEndUs    = ilIsKeepInstancesOpen.

    /* ================== */
    /* Exception Handling */
    /* ================== */
    assign oiReturnStatus                  = -98
           viLocalReturnStatus             = 0
           vcListEuMstr                    = '':U
           vcEndUserDomainCode             = icDomainCode.
    
    CONVERTBLOCK:
    DO:
        
        /* ===================================== */
        /* Check which records need to be loaded */
        /* ===================================== */
        for each tDebtorEndUser
            break by tDebtorEndUser.SharedSet_ID:
        
            /* ======================================================================================================== */
            /* Check if the Customer End User 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(tDebtorEndUser.SharedSet_ID)
            then do:
                <Q-2 run DomainBySharedSetDomain (all) (Read) (Cache)
                   (input tDebtorEndUser.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain >
            end. /* if first-of(tDebtorEndUser.SharedSet_ID) */

            /* =============================================================================================== */
            /* Get all Mfg Addresses which are already defined in the database                                 */
            /* =============================================================================================== */
            <Q-48 run DomainSharedSetForEuMstr (all) (Read) (NoCache)
               (input tDebtorEndUser.SharedSet_ID, (SharedSetId)
                input ?, (DomainCode)
                input tDebtorEndUser.DebtorEndUserCode, (Address)
                output dataset tqDomainSharedSetForEuMstr) in BDomain >
                
            for each tqDomainBySharedSetDomain where
                     tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:
                /* ============================================================ */
                /* Create a list of all teu_mstr records that need to be loaded */
                /* ============================================================ */
                if can-find(first tqDomainSharedSetForEuMstr where
                                  tqDomainSharedSetForEuMstr.tcDomainCode = tqDomainBySharedSetDomain.tcDomainCode and
                                  tqDomainSharedSetForEuMstr.tceu_addr    = tDebtorEndUser.DebtorEndUserCode)
                then assign vcListEuMstr = vcListEuMstr                           + 
                                           (if vcListEuMstr = '':U
                                            then '':U
                                            else chr(4))                          +
                                           tqDomainBySharedSetDomain.tcDomainCode +
                                           chr(2)                                 +
                                           tDebtorEndUser.DebtorEndUserCode.

                if length(vcListEuMstr, "CHARACTER") > 30000
                then do:
                    /* ========================= */
                    /* Load the tad_mstr records */
                    /* ========================= */
                      <M-86 run DataLoad
                         (input  '':U (icRowids), 
                          input  vcListEuMstr (icPkeys), 
                          input  '':U (icObjectIds), 
                          input  '':U (icFreeform), 
                          input  true (ilKeepPrevious), 
                          output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
                                            
                     if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                     if viFcReturnSuper < 0 then leave MAIN_BLOCK.
    
                     assign vcListEuMstr = '':U.   
                end. /* length */    
            end. /* for each tqDomainBySharedSetDomain: */
        end. /* for each tDebtorEndUser */
        
        /* ========================= */
        /* Load the teu_mstr records */
        /* ========================= */
        if vcListEuMstr <> "":U and 
           vcListEuMstr <> ?
        then do :
            <M-4 run DataLoad
               (input  '':U (icRowids), 
                input  vcListEuMstr (icPkeys), 
                input  '':U (icObjectIds), 
                input  '':U (icFreeform), 
                input  false (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave MAIN_BLOCK.
        end. /* if vcListEuMstr <> "":U and */

        /* ============================================== */
        /* Convert table tDebtorEndUser to table teu_mstr */
        /* ============================================== */
        for each tDebtorEndUser
            break by tDebtorEndUser.SharedSet_ID:
        
            /* ======================================================================================================== */
            /* Check if the Customer End User 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(tDebtorEndUser.SharedSet_ID)
            then do:
                if tDebtorEndUser.SharedSet_ID = 0 or
                   tDebtorEndUser.SharedSet_ID = ?
                then do:
                    assign vcMessage           = trim(substitute(#T-15'The Shared Set ID for this customer end user (&1) was not filled.':255(69601)T-15#, trim(tDebtorEndUser.DebtorEndUserCode) ))
                           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-8187':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
                    leave CONVERTBLOCK.
                end. /* if tDebtorEndUser.SharedSet_ID = 0 or */
    
                <Q-7 run DomainBySharedSetDomain (all) (Read) (Cache)
                   (input tDebtorEndUser.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain >            
            end. /* if first-of(tDebtorEndUser.SharedSet_ID) */
                
            /* ========================================================================= *
             *              Get Address and Contact data of the Mfg Address              *
             * ========================================================================= *
             * Address can be created newly, or existing one can be reused               *
             * ========================================================================= */
            /* First check, if the business relation component contains the address data */
            empty temp-table tEndUserAddress.
            empty temp-table tContact.
    
            if viBBusinessRelationMfgEndUserID <> 0 and
               viBBusinessRelationMfgEndUserID <> ?
            then do:
                if not valid-handle(vhBBusinessRelationMfgEndUserInst)
                then do:
                    <I-72 {bFcOpenInstance
                         &CLASS           = "BBusinessRelation"}>
                    assign vlIsBBusRelOpenedHrBMfgEndUsr = true.
                end.
                
                <M-77 run GetAddressWithID
                   (input  tDebtorEndUser.Address_ID (iiAddressID), 
                    input-output tEndUserAddress (tApiAddress), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0 then leave CONVERTBLOCK.
    
                <M-39 run GetContactByAddress
                   (input  tDebtorEndUser.Address_ID (iiAddressID), 
                    input-output tContact (tApiContact), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>

                if tDebtorEndUser.tc_Status = "C":U 
                then do :
                     <Q-62 run ContactByAddress (all) (Read) (NoCache)
                        (input tDebtorEndUser.Address_ID, (AddressId)
                         output dataset tqContactByAddress) in BBusinessRelation>
                end.        
                     
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0 then leave CONVERTBLOCK.
            end.
             
            if not can-find(first tEndUserAddress where
                                  tEndUserAddress.Address_ID =  tDebtorEndUser.Address_ID)
            then do:
                /* Get address details */
                <Q-3 run AddressById (all) (Read) (NoCache)
                   (input tDebtorEndUser.Address_ID, (AddressID)
                    output dataset tqAddressById) in BBusinessRelation >
                for each tqAddressById where
                         tqAddressById.tiAddress_ID = tDebtorEndUser.Address_ID:
                    
                    create tEndUserAddress.
                    <M-34 run BufferCopy
                       (input  buffer tqAddressById:handle (ihFrom), 
                        input  buffer tEndUserAddress:handle (ihTo), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
                    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                    if viFcReturnSuper <  0 then leave CONVERTBLOCK.
    
                end.
    
                /* Get contact details */
                <Q-36 run ContactByAddress (all) (Read) (NoCache)
                   (input tDebtorEndUser.Address_ID, (AddressId)
                    output dataset tqContactByAddress) in BBusinessRelation >
                for each tqContactByAddress where
                         tqContactByAddress.tiAddress_ID = tDebtorEndUser.Address_ID:
                    create tContact.
                    <M-71 run BufferCopy
                       (input  buffer tqContactByAddress:handle (ihFrom), 
                        input  buffer tContact:handle (ihTo), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
                end.
            end.
    
            find first tEndUserAddress where
                       tEndUserAddress.Address_ID = tDebtorEndUser.Address_ID
                 no-error.
            if not available tEndUserAddress
            then do:
                assign vcMessage           = trim(#T-57'Address record not available for this customer Ship-to.':255(69641)T-57#)
                       viLocalReturnStatus = -1.
                <M-64 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-686829':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
                leave CONVERTBLOCK.
            end.

            for each tqDomainBySharedSetDomain where
                     tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:
                /* ================================================== */
                /* Create or update teu_mstr records based on tDebtorEndUser */
                /* ================================================== */
                find teu_mstr where
                     teu_mstr.eu_addr   = tDebtorEndUser.DebtorEndUserCode and
                     teu_mstr.eu_domain = tqDomainBySharedSetDomain.tcDomainCode
                     no-error.
                if not available teu_mstr
                then do:
                    if tDebtorEndUser.tc_Status = "D"
                    then next.
    
                    <M-8 run AddDetailLine
                       (input  'eu_mstr':U (icTable), 
                        input  '':U (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
                    if viFcReturnSuper <> 0
                    then do:
                        assign viLocalReturnStatus = viFcReturnSuper.
                        leave CONVERTBLOCK.
                    end. /* if viFcReturnSuper <> 0 */                
                end. /* if not available teu_mstr */  
                else assign teu_mstr.tc_Status = "C":U.
    
                assign teu_mstr.eu_addr        = tDebtorEndUser.DebtorEndUserCode
                       teu_mstr.eu_domain      = tqDomainBySharedSetDomain.tcDomainCode
                       teu_mstr.eu_cm_nbr      = tDebtorEndUser.tcDebtorCode
                       teu_mstr.eu_lang        = tEndUserAddress.tcLngCode
                       teu_mstr.eu_sort        = tEndUserAddress.AddressSearchName
                       teu_mstr.tc_Status      = (if tDebtorEndUser.tc_Status = "D":U
                                                  then tDebtorEndUser.tc_Status
                                                  else teu_mstr.tc_Status).
                                                  
                                                  
                 <Q-67 run ServiceControlByDomain (all) (Read) (NoCache)
                    (input teu_mstr.eu_domain, (DomainCode)
                     output dataset tqServiceControlByDomain) in BMfgServiceControl>
                  find first tqServiceControlByDomain no-lock no-error. 
                     
                  <Q-79 run CustomerByDomainAddress (all) (Read) (NoCache)
                     (input teu_mstr.eu_domain, (DomainCode)
                      input tDebtorEndUser.tcDebtorCode, (Address)
                      output dataset tqCustomerByDomainAddress) in BMfgCustomer>
                   find first tqCustomerByDomainAddress no-lock no-error.
                      
                  <Q-68 run GetEndUserControlByDomain (all) (Read) (NoCache)
                     (input teu_mstr.eu_domain, (DomainCode)
                      output dataset tqGetEndUserControlByDomain) in BMfgEndUserControl>
                   find first tqGetEndUserControlByDomain no-lock no-error.
                      
                   assign teu_mstr.eu_trv_um        =    if available tqServiceControlByDomain
                                                                           then tqServiceControlByDomain.tcsvc_trvl_um
                                                                           else ''                                                  
                          teu_mstr.eu_po_reqd       =  if available tqCustomerByDomainAddress
                                                                           then tqCustomerByDomainAddress.tlcm_po_reqd
                                                                           else no
                          teu_mstr.eu_create_isb    =  if available tqGetEndUserControlByDomain
                                                                           then tqGetEndUserControlByDomain.tleuc_create_isb
                                                                           else no.    
    
                for each tContact break by tContact.tc_Status :
                    /* Create an eud_det record for each contact assoicated with the address */           
                    
                   if tContact.tc_Status = "C"
                    then do:
                         /* Get the old contact name (ie one currently stored in db) this allows us to indentify the mfg/pro record
                         correctly to update the name */         
                         /* We need to delete this record as its key value is changing */
                         /* zjc eB3RegTst1-2636 Begin */
                        for first tqContactByAddress 
                            where tqContactByAddress.tiContact_id = tContact.contact_id:
                            for first teud_det 
                                where teud_det.eud_domain = teu_mstr.eu_domain
                                  and teud_det.eud_addr = tDebtorEndUser.DebtorEndUserCode
                                  and teud_det.eud_sort = tqContactByAddress.tcContactName:
                                if tqContactByAddress.tcContactName <> tContact.ContactName  
                                then assign teud_det.tc_status = "D".
                            end.                                
                        end.
                        /* zjc eB3RegTst1-2636 end */
                    end.

                    /* Check to see if we have an existing teud_det record for the contact*/
                    for first teud_det 
                         where teud_det.eud_domain = teu_mstr.eu_domain
                           and teud_det.eud_addr = tDebtorEndUser.DebtorEndUserCode
                           and teud_det.eud_sort = tContact.ContactName:
                        if  teud_det.tc_status <> "D":U   
                        then assign teud_det.tc_status = tContact.tc_status.
                    end.


                    /* Check to see if we have an existing teud_det record for the contact*/
                    find first teud_det where
                               teud_det.eud_domain = teu_mstr.eu_domain               and
                               teud_det.eud_addr   = tDebtorEndUser.DebtorEndUserCode and
                               teud_det.eud_sort   = tContact.ContactName
                               no-error.
                                    
                    /* if we don't have eud_det record for this contact then create one */  
                    /* or if the contact name is changed then create a new eud_det as the preivous one is marked as "D" */  
                    if not available(teud_det) or (available(teud_det) and teud_det.tc_status = "D" and tContact.tc_Status = "C" )
                    then do:
                
                        <M-78 run AddDetailLine
                           (input  'eud_det':u (icTable), 
                            input  teu_mstr.tc_Rowid (icParentRowid), 
                            output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser> 
                        assign  
                            teud_det.oid_eud_det = <M-65 GetNextOidValue  () in BMfgEndUser>
                            teud_det.eud_ls_type = {&ADDRESSTYPECODESYSTEM-ENDUSER}                          
                            teud_det.eud_domain = teu_mstr.eu_domain.                
                    end. 

                    /* assign complex mapping fields */                                            
                    assign teud_det.eud_domain   = teu_mstr.eu_domain
                           teud_det.eud_sort     = tContact.ContactName
                           teud_det.eud_title    = tContact.ContactTitle
                           teud_det.eud_type     = tContact.ContactFunction
                           teud_det.eud_phone    = tContact.ContactTelephone
                           teud_det.eud_fax      = tContact.ContactFax                       
                           teud_det.eud_updt     = TODAY
                           teud_det.eud_nametype = if tContact.ContactisPrimary then "P" else ""            
                           teud_det.eud_addr     = tDebtorEndUser.DebtorEndUserCode.                                              
                end. /* for each tContact */                                          
            end. /* for each tqDomainBySharedSetDomain: */
        end. /* for each tDebtorEndUser */    
    end. /* CONVERTBLOCK */
    
    VALIDATEBLOCK:
    DO:
        if viLocalReturnStatus < 0
        then leave VALIDATEBLOCK.
    
        /* ========== */
        /* ValidateBC */
        /* ========== */
        <M-12 run ValidateBC  (output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
    
        /* ================== */
        /* Additional Updates */
        /* ================== */
        <M-13 run AdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
        
        /* ======== */
        /* DataSave */
        /* ======== */
        <M-14 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BMfgEndUser>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
    end. /* VALIDATEBLOCK */
end. /* of MAIN_BLOCK */

/* Close components */
if vlIsBBusRelOpenedHrBMfgEndUsr and
   valid-handle(vhBBusinessRelationMfgEndUserInst)
then do:
    <I-91 {bFcCloseInstance
         &CLASS           = "BBusinessRelation"}>
    assign vlIsBBusRelOpenedHrBMfgEndUsr = false.
end.
    
/* ================== */                        
/* 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 = "BMfgEndUser".
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/bmfgenduser.apireplicatefromfinancials.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icDomainCode = <parameter value>
       vhParameter::iiBBusinessRelationID = <parameter value>
       vhParameter::ihBBusinessRelationInst = <parameter value>
       vhParameter::ilIsKeepInstancesOpen = <parameter value>.

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