project QadFinancials > class BMfgAddressListDetail > method MaintainDataDebtorEnduser


Parameters


tDebtorEndusersToUpdate2inputtemp-table
iiBusinessRelationInstanceIDinputinteger
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDebtorEndUser.ApiReplicateFromDaemon
method BMfgEndUser.AdditionalUpdates


program code (program1/bmfgaddresslistdetail.p)

assign oiReturnStatus = -98.
   
<M-17 run ClearData (output viFcReturnSuper (oiReturnStatus)) in BMfgAddressListDetail>
empty temp-table tAddress2.

if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0 then Return.

/* Store the instance id of the Business Relation */
assign viBBusinessRelation8ID = iiBusinessRelationInstanceID.

/* If an business relation doesn't exist start and open it - otherwise just open the existing isntance */
if viBBusinessRelation8ID = 0 or
   viBBusinessRelation8ID = ? 
then do:
    <I-10 {bFcStartAndOpenInstance
         &ADD-TO-TRANSACTION = "true"
         &CLASS              = "BBusinessRelation"}>
end.
else do:
    <I-11 {bFcOpenInstance
         &CLASS           = "BBusinessRelation"}>
end.   

/* Start queries */
<Q-61 run DebtorByCodeSharedSetID  (Start) in BDebtor >
<Q-7 run CompanyBySharedSet  (Start) in BCompany >

endusers:
for each tDebtorEndusersToUpdate2 where
         tDebtorEndusersToUpdate2.tc_status = "N":U or
         tDebtorEndusersToUpdate2.tc_status = "D":U:
      
    /* Clear the tAddres temp table */
    empty temp-table tAddress2.
    
    /* Get the addres details for the current address */           
    <M-14 run GetAddressWithID
       (input  tDebtorEndusersToUpdate2.address_id (iiAddressID), 
        input-output tAddress2 (tApiAddress), 
        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>     
        
    /* Find the t_saddress record we just created */
    find first tAddress2 where 
               tAddress2.address_id = tDebtorEndusersToUpdate2.address_id
               no-error.
    
    /* We should always be able to find an address but in case we don't then raise an error */
    if not available tAddress2   
    then do:
        assign 
            vcMessageText = trim(#T-16'The address details are not defined in the system.':35(31629)t-16#) + chr(10) +
                            trim(substitute(#T-24'Address ID: &1.':255(71123)T-24#, string(tDebtorEndusersToUpdate2.Address_ID)))
            oiReturnStatus = -1.
        <M-15 run SetMessage
           (input  vcMessageText (icMessage), 
            input  '' (icArguments), 
            input  '' (icFieldName), 
            input  '' (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '' (icRowid), 
            input  'QadFin-4866':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BMfgAddressListDetail>          
        leave.
    end.                          
    
    /* Store the debtorCode into a variable, since we the code to put in ad_addr */
    assign vcRelationCode2     = tDebtorEndusersToUpdate2.DebtorEnduserCode
           vcRelationType2     = {&ADDRESSTYPECODESYSTEM-ENDUSER}
           tAddress2.tc_status =  tDebtorEndusersToUpdate2.tc_status.

    <Q-128 run DebtorByCodeSharedSetID (all) (Read) (NoCache)
       (input ?, (SharedSetID)
        input tDebtorEndusersToUpdate2.tcDebtorCode, (DebtorCode)
        output dataset tqDebtorByCodeSharedSetID) in BDebtor >
        

    /* Addresses are system wide date, so we need to use the SystemWideToMfg pattern.
     * However, we are processing debtors, which use the SharedSetToMfg pattern.
     * We need to copy the address information (system wide data) to all domains where the 
     * debtor is added. To achieve this we determine all the companies that uses the debtor 
     * shared set, get the domains and for each domain we run MaintainData and follow
     * the SystemWideToMFG pattern.
     */ 
     for first tqDebtorByCodeSharedSetID:
         <Q-4 run CompanyBySharedSet (all) (Read) (NoCache)
            (input '', (CompanyId)
             input tqDebtorByCodeSharedSetID.tiSharedSet_ID, (SharedSetId)
             output dataset tqCompanyBySharedSet) in BCompany >
     end.

    /*
     * Process the Financial Data into each of the domains returned by the above query.   
     */
    DomainLoop:
    for each tqCompanyBySharedSet break by tqCompanyBySharedSet.tcDomainCode:

        if not first-of(tqCompanyBySharedSet.tcDomainCode) then
           next DomainLoop.
        /* save the domain code which currently being processed */
        assign vcDomain2 = tqCompanyBySharedSet.tcDomainCode.

        /* Check if we need to replicate to one domain or to all */  
        <I-20 {bFcOpenInstance
            &CLASS           = "Session"}>
        <M-21 run GetCharacterValue
           (input  'ProcessSharedSetToMFGDomainCode':U (icName), 
            output vcProcessSharedSetToMFGDomain (ocValue), 
            output viSessionReturnStatus (oiReturnStatus)) in Session>
        <I-22 {bFcCloseInstance
            &CLASS           = "Session"}>

        if viSessionReturnStatus <> 0 
        then assign oiReturnStatus = viSessionReturnStatus.
        if viSessionReturnStatus < 0 
        then do :       
            leave endusers.
        end. /* if viFcReturnSuper < 0 */
        if vcProcessSharedSetToMFGDomain <> '':U and
           vcProcessSharedSetToMFGDomain <> ?    and
           vcProcessSharedSetToMFGDomain <> tqCompanyBySharedSet.tcDomainCode
        then next DomainLoop. 
        
        /* ================================================================ */
        /* For new records, check if the address list record already exists */
        /* Either in the database or in the instance.                       */
        /* ================================================================ */
        if tDebtorEndusersToUpdate2.tc_Status = "N":U
        then do:
            /* Check instance */
            if can-find(tls_mstr where
                        tls_mstr.ls_domain = tqCompanyBySharedSet.tcDomainCode          and
                        tls_mstr.ls_addr   = tDebtorEndusersToUpdate2.DebtorEnduserCode and
                        tls_mstr.ls_type   = {&ADDRESSTYPECODESYSTEM-ENDUSER}           and
                        tls_mstr.tc_Status <> "D":U)
            then next DomainLoop.

            /* Check database */
            <Q-23 assign vlFcQueryRecordsAvailable = LsMstrByAddrDomainType (NoCache)
               (input tqCompanyBySharedSet.tcDomainCode, (Domain)
                input tDebtorEndusersToUpdate2.DebtorEndUserCode, (Addr)
                input {&ADDRESSTYPECODESYSTEM-ENDUSER}, (Type)) in BMfgAddressListDetail >
            if vlFcQueryRecordsAvailable <> false
            then next DomainLoop.
        end. /* if tDebtorEndusersToUpdate2.tc_Status = "N":U */


        /* update the MFG/PRO record */
        <M-18 run ProcessMaintainData (output viFcReturnSuper (oiReturnStatus)) in BMfgAddressListDetail>
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave endusers.
    end. /* for each companybysharedset */
end.  /* endusers: */

/* stop  queries */
<Q-42 run DebtorByCodeSharedSetID  (Stop) in BDebtor >
<Q-9 run CompanyBySharedSet  (Stop) in BCompany >

/* If we started the business relation instance then we need to stop it - otherwise just close it */
if iiBusinessRelationInstanceID = 0
then do:
    <I-12 {bFcCloseAndStopInstance
            &CLASS           = "BBusinessRelation"}>    
end.                        
else do:
    <I-13 {bFcCloseInstance
            &CLASS           = "BBusinessRelation"}>    
end.

if oiReturnStatus = -98 or oiReturnStatus > 0
then do:
    <M-19 run ProcessMaintainDataSave (output viFcReturnSuper (oiReturnStatus)) in BMfgAddressListDetail>
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then Return.
end.

/* ======================== */
/* Set ReturnStatus = OK    */
/* ======================== */
if oiReturnStatus = -98
then assign oiReturnStatus = 0.