project QadFinancials > class BMfgAddress > method MaintainDataEndUser


Parameters


tDebtorEndUsersToUpdateinputtemp-table
iiBusinessRelationInstanceIDinputinteger
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BMfgEndUser.AdditionalUpdates


program code (program1/bmfgaddress.p)

assign oiReturnStatus = -98.
   
<M-25 run ClearData (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
/* clear tAddress tt*/
empty temp-table tAddress.
empty temp-table tContactsToUpdate.

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

/* start queries */
<Q-33 run DebtorEndUserByID   (Start) in BDebtorEndUser >
<Q-10 run CompanyBySharedSet  (Start) in BCompany >

/* store the instance id of the Business Relation */
assign viBBusinessRelation7ID = iiBusinessRelationInstanceID.

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

/* Loop through all the end users being updated */
/* delete contain is handled by relation to eu_mstr.*/
assign vlAddressToProcess = false.
enduser:
for each tDebtorEndUsersToUpdate
    where tDebtorEndUsersToUpdate.tc_status = "N":U
       or tDebtorEndUsersToUpdate.tc_status = "C":U
       or tDebtorEndUsersToUpdate.tc_status = "D":U:
       
       /* clear the tAddres temp table */
      empty temp-table tAddress.
       
    /* If we are changing a shipto we only want to process the change if the adddress id has changed */
    if tDebtorEndUsersToUpdate.tc_status = "C":U         
    then do:            
        <Q-24 run DebtorEndUserByID (all) (Read) (NoCache)
           (input '', (DebtorEndUserCode)
            input tDebtorEndUsersToUpdate.debtorenduser_id, (DebtorEndUser_ID)
            input ?, (CompanyId)
            output dataset tqDebtorEndUserByID) in BDebtorEndUser >
        for first tqDebtorEndUserByID:          
            if tDebtorEndUsersToUpdate.address_id = tqDebtorEndUserByID.tiAddress_ID             
            then next enduser.
        end. 
    end.    

    /* If we get this far then it means we have an end user address details to process */
    assign vlAddressToProcess = true.

    <M-14 run GetAddressWithID
       (input  tDebtorEndUsersToUpdate.address_id (iiAddressID), 
        input-output tAddress (tApiAddress), 
        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>       

    <M-38 run GetContactByAddress
       (input  tDebtorEndUsersToUpdate.address_id (iiAddressID), 
        input-output tContactsToUpdate (tApiContact), 
        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
    
    if  tDebtorEndUsersToUpdate.DebtorEndUserIsDebtor
    then do:
        for first tAddress 
            where tAddress.Address_ID = tDebtorEndUsersToUpdate.Address_ID:
            
            <Q-5 run DebtorByIDAllInfo (all) (Read) (NoCache)
               (input ?, (CompanyId)
                input tDebtorEndUsersToUpdate.Debtor_ID, (DebtorID)
                output dataset tqDebtorByIDAllInfo) in BDebtor >
            
            for first tqDebtorByIDAllInfo
                where tqDebtorByIDAllInfo.tiDebtor_ID =  tDebtorEndUsersToUpdate.Debtor_ID:
            end.
            
            if available tqDebtorByIDAllInfo
            then do:       
                assign tAddress.AddressIsTaxable     = tqDebtorByIDAllInfo.tlDebtorIsTaxable
                       tAddress.AddressIsTaxIncluded = tqDebtorByIDAllInfo.tlDebtorIsTaxIncluded
                       tAddress.AddressIsTaxInCity   = tqDebtorByIDAllInfo.tlDebtorIsTaxInCity
                       tAddress.AddressTaxIDFederal  = tqDebtorByIDAllInfo.tcDebtorTaxIDFederal
                       tAddress.AddressTaxIDState    = tqDebtorByIDAllInfo.tcDebtorTaxIDState
                       tAddress.AddressTaxIDMisc1    = tqDebtorByIDAllInfo.tcDebtorTaxIDMisc1
                       tAddress.AddressTaxIDMisc2    = tqDebtorByIDAllInfo.tcDebtorTaxIDMisc2
                       tAddress.AddressTaxIDMisc3    = tqDebtorByIDAllInfo.tcDebtorTaxIDMisc3
                       tAddress.TxzTaxZone           = tqDebtorByIDAllInfo.tcTxzTaxZone
                       tAddress.TxclTaxCls           = tqDebtorByIDAllInfo.tcTxclTaxCls
                       tAddress.TxuTaxUsage          = tqDebtorByIDAllInfo.tcTxuTaxUsage.  
            end. /*IF AVAILABLE tqDebtorByIDAllInfo*/
        
        end. /*FOR FIRST tAddress*/
    end. /*IF tDebtorEndUsersToUpdate.tcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-ENDUSER}*/    

    /* find the tAddress record we just created */
    for first tAddress 
        where tAddress.address_id = tDebtorEndUsersToUpdate.address_id:
    end.  
    
    /* We should always be able to find an address but in case we don't then raise an error */
    if not available tAddress   
    then do:
        assign 
            vcMessageText =#T-16'The address details are not defined in the system.':35(31629)T-16#
            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-4864':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
        leave enduser.
    end.     
    
    /* Overrite the AdddresName in this case so that the Debtor end user name will be used for the ad_name value */
    assign tAddress.AddressName =   tDebtorEndUsersToUpdate.DebtorEndUserName.
    
    /* Store the Debtor code into a variable, it will be populated in ad_addr */
    assign vcRelationCode = tDebtorEndUsersToUpdate.DebtorEndUsercode.                      
           
    if tDebtorEndUsersToUpdate.DebtorEndUserIsDebtor
    then
        assign
            vcRelationType = "CUSTOMER":U
            tAddress.tc_status =  "C":U.       
    else if tDebtorEndUsersToUpdate.DebtorEndUserIsShipTo
    then
        assign
            vcRelationType = "SHIP-TO":U    
            tAddress.tc_status =  "C":U.    
    else
        assign
            vcRelationType = {&ADDRESSTYPECODESYSTEM-ENDUSER}
            tAddress.tc_status =  tDebtorEndUsersToUpdate.tc_status. /*t_sDebtorsToUpdate.tc_status. */.
                
    /* Addresses are system wide date, so we need to use the SystemWideToMfg pattern.
     * However, we are processing Creditors, which use the SharedSetToMfg pattern.
     * We need to copy the address information (system wide data) to all domains where the 
     * creditor is added. To achieve this we determine all the companies that uses the creditor 
     * shared set, get the domains and for each domain we run MaintainData and follow
     * the SystemWideToMFG pattern.
     */      
     if tDebtorEndUsersToUpdate.tc_status = "D":U 
     then
        assign vcRelatedCustomer = "".
     else
        assign vcRelatedCustomer = tDebtorEndUsersToUpdate.tcDebtorCode.
    <Q-3 run CompanyBySharedSet (all) (Read) (NoCache)
       (input '', (CompanyId)
        input tDebtorEndUsersToUpdate.SharedSet_ID, (SharedSetId)
        output dataset tqCompanyBySharedSet) in BCompany >
     
    /*
    * Process the Financial Data into each of the domains returned by the above query.   
    */
    DomainLoop:
    for each tqCompanyBySharedSet where 
             tqCompanyBySharedSet.tlDomainPropertyIsSetupCompl = true
             break by tqCompanyBySharedSet.tcDomainCode:

        if not first-of(tqCompanyBySharedSet.tcDomainCode) then
           next DomainLoop.

         /* find the tAddress record we just created */
        for first tAddress 
            where tAddress.address_id = tDebtorEndUsersToUpdate.address_id:
        end.

        /* save the domain code which is currently being processed */
        assign vcDomain = tqCompanyBySharedSet.tcDomainCode.
       
        /* Check if we need to replicate to one domain or to all */  
        <I-28 {bFcOpenInstance
             &CLASS           = "Session"}>
        <M-29 run GetCharacterValue
           (input  'ProcessSharedSetToMFGDomainCode':U (icName), 
            output vcProcessSharedSetToMFGDomain (ocValue), 
            output viSessionReturnStatus (oiReturnStatus)) in Session>
        <I-30 {bFcCloseInstance
             &CLASS           = "Session"}>

        if viSessionReturnStatus <> 0 
        then assign oiReturnStatus = viSessionReturnStatus.
        if viSessionReturnStatus < 0 
        then do :
            leave enduser.
        end. /* if viFcReturnSuper < 0 */
        if vcProcessSharedSetToMFGDomain <> '':U and
           vcProcessSharedSetToMFGDomain <> ?    and
           vcProcessSharedSetToMFGDomain <> tqCompanyBySharedSet.tcDomainCode
        then next DomainLoop. 

        if tAddress.tc_status = "D":U
        then do:
             /* check if address already exists */
             <Q-36 run AddressListDetailByDomainCode (all) (Read) (NoCache)
                (input tDebtorEndUsersToUpdate.DebtorEndUserCode, (AddressCode)
                 input vcDomain, (domain)
                 output dataset tqAddressListDetailByDomainCode) in BMfgAddressListDetail >
             for first tqAddressListDetailByDomainCode
                 where tqAddressListDetailByDomainCode.tcls_addr =  tDebtorEndUsersToUpdate.DebtorEndUserCode
                   and tqAddressListDetailByDomainCode.tcls_domain = vcDomain
                   and tqAddressListDetailByDomainCode.tcls_type <> "enduser":U:
                next domainloop.
             end.
        end. /* end new debtor */
               
        /* Check if this address already exists */
        if tAddress.tc_Status = "N":U
        then do:
            <Q-37 assign vlFcQueryRecordsAvailable = AdMstrByDomainByCode (NoCache)
               (input tqCompanyBySharedSet.tcDomainCode, (Domain)
                input tDebtorEndUsersToUpdate.DebtorEndUserCode, (Code)) in BMfgAddress >
            if vlFcQueryRecordsAvailable <> false or
              (can-find(first tad_mstr where
                              tad_mstr.ad_domain = tqCompanyBySharedSet.tcDomainCode and
                              tad_mstr.ad_addr   = tDebtorEndUsersToUpdate.DebtorEndUserCode))
            then assign tAddress.tc_Status = "C":U.
        end.

        /* update the MFG/PRO record */
        <M-26 run ProcessMaintainData (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave enduser.      
          
    end.  /* end of for each companybysharedset  */
end.

/* stop queries */
<Q-34 run DebtorEndUserByID (Stop) in BDebtorEndUser >
<Q-22 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-19 {bFcCloseAndStopInstance
         &CLASS           = "BBusinessRelation"}>
end.                        
else do:
    <I-20 {bFcCloseInstance
         &CLASS           = "BBusinessRelation"}>
end.

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

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