project QadFinancials > class BDebtorShipTo > method ApiMaintainDebtorShipto


Parameters


icDebtorCodeinputcharacter
tShiptoAddressMasterinput-outputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program3/bdebtorshipto.p)

MAIN_BLOCK:
do on error undo, leave:

    assign oiReturnStatus      = -98
           viLocalReturnStatus = 0.    

    /* Clear the instance data */
    <M-1 run ClearData (output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
        
    empty temp-table tApiShiptoAddress.
    empty temp-table tApiShiptoContact.

    <Q-53 run DebtorByCode (all) (Read) (NoCache)
       (input viCompanyID, (CompanyId)
        input icDebtorCode, (DebtorCode)
        output dataset tqDebtorByCode) in BDebtor >

    find first tqDebtorByCode where
               tqDebtorByCode.tcDebtorCode = icDebtorCode
               no-lock no-error.
    /* if no debtor record was found then return an error */
    if not available tqDebtorByCode 
    then do:
        assign vcMessage           = #T-92'Customer ($1) is not defined in the system.':200(31311)T-92#
               viLocalReturnStatus = -1.
       
        <M-3 run SetMessage
            (input  vcMessage (icMessage), 
                     input  icDebtorCode (icArguments), 
                     input  '' (icFieldName), 
                     input  '' (icFieldValue), 
                     input  'E':U (icType), 
                     input  3 (iiSeverity), 
                     input  '' (icRowid), 
                     input  'QadFin-5502':U (icFcMsgNumber), 
                     input  '' (icFcExplanation), 
                     input  '' (icFcIdentification), 
                     input  '' (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
        leave MAIN_BLOCK.
    end.  /* end of if available tqDebtorPrim */
       
    /* Map the Address data from MFG/PRO format to financial format */
    if viBMfgAddress2ID = 0 or
       viBMfgAddress2ID = ?
    then do:
        <I-71 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "true"
            &CLASS              = "BMfgAddress"}>           
        assign vlIsBMfgAddressStartedHr = true
               vlIsBMfgAddressOpenedHr  = true.
    end.
    else if not valid-handle(vhBMfgAddress2Inst)
    then do:
        <I-59 {bFcOpenInstance
            &CLASS           = "BMfgAddress"}>
        assign vlIsBMfgAddressOpenedHr = true.
    end.  /* end if viBMfgAddress2ID */

    <M-6 run MapMfgAddressData
       (input  temp-table tShiptoAddressMaster:default-buffer-handle (ihMfgTable), 
        input  temp-table tApiShiptoAddress:default-buffer-handle (ihFinancialAddressTable), 
        input  temp-table tApiShiptoContact:default-buffer-handle (ihFinancialContactTable), 
        output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0 then leave MAIN_BLOCK.  
       
    /* only run the business relation method if we have at least one address */
    if can-find(first tApiShiptoAddress)
    then do:
        /* Create/update the address in the Businress Relation */
        if viBBusinessRelation1ID = 0 or
           viBBusinessRelation1ID = ?
        then do:
            <I-97 {bFcStartAndOpenInstance
                &ADD-TO-TRANSACTION = "true"
                &CLASS              = "BBusinessRelation"}>
            assign vlIsBBusinessRelationStartedHr = true
                   vlIsBBusinessRelationOpenedHr  = true.
        end.
        else if not valid-handle(vhBBusinessRelation1Inst)
        then do:         
            <I-96 {bFcOpenInstance
                &CLASS           = "BBusinessRelation"}>
            assign vlIsBBusinessRelationOpenedHr = true.
        end.  /* end if viBBusinessRelation1ID */
        
        <M-11 run ApiMaintainAddress
           (input-output tApiShiptoAddress (tApiAddress), 
            input-output tApiShiptoContact (tApiContact), 
            input  tqDebtorByCode.tcBusinessRelationCode (icBusinessRelationCode), 
            input  tqDebtorByCode.tiBusinessRelation_ID (iiBusinessRelationID), 
            output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>                            
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viLocalReturnStatus < 0 then leave MAIN_BLOCK.    
    end.   /* end of can-find */
        
    /* Iterate through all the shiptos */
    shiptos:
    for each tShiptoAddressMaster:   

        /* clear the contacts table */
        <M-43 run ClearData
          (output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>    
        
        if tShiptoAddressMaster.ad_addr <> ""
        then do:
            /* check if the shipto already exists */
            <Q-29 run DebtorShipToPrim (all) (Read) (NoCache)
               (input '', (DebtorShiptoID)
                input  tShiptoAddressMaster.ad_addr, (DebtorShiptoCode)
                input viCompanyId, (CompanyId)
                output dataset tqDebtorShipToPrim) in BDebtorShipTo>
            find first tqDebtorShipToPrim where
                       tqDebtorShipToPrim.tcDebtorShipToCode = tShiptoAddressMaster.ad_addr
                       no-lock no-error.
        end.
        if available tqDebtorShipToPrim
        then do:
            <M-30 run DataLoad
               (input  '' (icRowids), 
                input  tqDebtorShipToPrim.tiDebtorShipTo_ID (icPkeys), 
                input  '' (icObjectIds), 
                input  '' (icFreeform), 
                input  false (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
             
            /* find the record we just loaded */
            find first tDebtorShipTo where
                       tDebtorShipTo.DebtorShipToCode = tqDebtorShipToPrim.tcDebtorShipToCode
                       no-lock no-error.   
            if available tDebtorShipTo
            then assign tDebtorShipTo.tc_Status = "C":U
                        vcActivityCode = "Modify":U.        
        end.
        
        /* if we are changing and we don't have a record raise an error */
        if not available tDebtorShipto
        then do:  
            if tShiptoAddressMaster.tc_Status = "D":U
            then do:
                assign vcMessage      = trim(#T-42'Customer ship-to $1 for customer $2 is not defined in the system.':80(53897)t-42#)
                       oiReturnStatus = 1.
                <M-41 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  tShiptoAddressMaster.ad_addr + chr(2) + icDebtorCode (icArguments), 
                    input  '' (icFieldName), 
                    input  '' (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '' (icRowid), 
                    input  'QadFin-5522':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcREturnSuper (oiReturnStatus)) in BDebtorShipTo>
                leave MAIN_BLOCK.
            end.
            else do:     
                <M-38 run AddDetailLine
                   (input  'DebtorShipto':U (icTable), 
                    input  '' (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>  
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0 then leave MAIN_BLOCK.   
                    
                assign vcActivityCode = "Create":U.

                if tShiptoAddressMaster.ad_addr = "":U
                then do:
                    <M-45 run ApiGenerateShiptoCode
                       (input  tDebtorShipTo.tc_Rowid (icDebtorShipToRowid), 
                        output tShiptoAddressMaster.ad_addr (ocDebtorShiptoCode), 
                        output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
                    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                    if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
                end.
            end. /* end else */
        end.       /* end no shipto record available */
           
         /* if the icStatus = "D" then we need to delete the record */
        if  tShiptoAddressMaster.tc_Status = "D":U
        then do:
            assign vcActivityCode = "Delete":U.
            <M-40 run DataDelete
                   (output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viLocalReturnStatus < 0 then leave MAIN_BLOCK.          
        end.  /* end of  = D*/                                     
        /* if this is a change or a new record - then update the address details */
        else do:
           
            /* Find the financial record that maps to this MFG/PRO record */
            for first tApiShiptoAddress        
                where tApiShiptoAddress.tc_rowid = tShiptoAddressMaster.tc_rowid:       
            end.
            
            /* Copy all the fields */
            if available tApiShiptoAddress
            then do:

                /* Be sure we do not overwrite some key fields */
                assign tApiShiptoAddress.tc_Status      = tDebtorShipTo.tc_Status
                       tApiShiptoAddress.tc_Rowid       = tDebtorShipTo.tc_Rowid
                       tApiShiptoAddress.tc_ParentRowid = tDebtorShipTo.tc_ParentRowid.

                <M-26 run BufferCopy
                   (input  buffer tApiShiptoAddress:handle (ihFrom), 
                    input  buffer tDebtorShipto:handle (ihTo), 
                    output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>

            end. /* if available tApiShiptoAddress */

            assign 
                tDebtorShipto.debtorShiptoCode  = tShiptoAddressMaster.ad_addr                 
                tDebtorShipTo.tcDebtorCode = icDebtorcode 
                tDebtorShipTo.tcBusinessRelationCode = tqDebtorByCode.tcBusinessRelationCode
                tDebtorShipTo.tiBusinessRelation_ID = tqDebtorByCode.tiBusinessRelation_ID
                tDebtorShipto.DebtorShiptoIsDebtor = false
                tDebtorShipTo.SharedSet_ID = tqDebtorByCode.tiSharedSet_ID
                tDebtorShipto.tlUpdateAllSharedAddRecords = false.   
                        
            <Q-76 run DebtorEndUserByID (all) (Read) (NoCache)
               (input tDebtorShipto.DebtorShiptoCode, (DebtorEndUserCode)
                input ?, (DebtorEndUser_ID)
                input viCompanyID, (CompanyId)
                output dataset tqDebtorEndUserByID) in BDebtorEndUser>                 
            
            /* If it already exists as an end user then we need to add a reference to the id */
            for first tqDebtorEndUserByID where
                      tqDebtorEndUserByID.tcDebtorEndUserCode = tDebtorshipto.DebtorshiptoCode:
                
                assign tDebtorShipTo.tcDebtorEndUserCode = tDebtorshipto.DebtorshiptoCode
                       tDebtorShipTo.DebtorEndUser_ID = tqDebtorEndUserByID.tiDebtorEndUser_ID
                       tDebtorShipTo.address_id = tqDebtorEndUserByID.tiAddress_id.
                                                                                
                /* clean up possible address */
                 /* clean up address if it is no longer being used*/                                   
                if not can-find (bApiShiptoAddress where bApiShiptoAddress.Address_id = tApiShiptoAddress.Address_id 
                        and bApiShiptoAddress.tc_rowid <> tApiShiptoAddress.tc_rowid)
                then do:
                    <M-15 run ClearAddressFromInstance
                   (input  tApiShiptoAddress.Address_id (iiAddressID), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                     if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 
                     then assign viLocalReturnStatus = viFcReturnSuper.
                     if viLocalReturnStatus < 0 then leave MAIN_BLOCK. 

                end.                                   
            end.                                         
           
            if available tApiShiptoAddress
            then do:
                assign 
                  tDebtorShipTo.DebtorShiptoName = tApiShiptoAddress.AddressName                  
                  tDebtorShipto.tcAddressCity = tApiShiptoAddress.AddressCity              
                  tDebtorShipto.tcAddressZip = tApiShiptoAddress.AddressZip
                  tDebtorShipto.tcAddressStreet1 = tApiShiptoAddress.AddressStreet1
                  tDebtorShipto.tcAddressStreet2 = tApiShiptoAddress.AddressStreet2
                  tDebtorShipto.tcAddressStreet3 = tApiShiptoAddress.AddressStreet3             
                  tDebtorShipTo.tcAddressTypeCode = tApiShiptoAddress.tcAddressTypeCode.                             
            end.   
                  
            assign  tShiptoAddressMaster.ad_address_id =  tDebtorShipto.Address_ID.                       
                                           
        end.  
        
    end.  /* end of each t_sShiptoAddressMaster */

    /* Validate the component */
    <M-20 run ValidateBC (output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
      
    <M-21 run AdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0 then leave MAIN_BLOCK.

    /* Save the component */
    <M-22 run DataSave (output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
end. /* of MAIN_BLOCK: */

if viLocalReturnStatus < 0
then do:
    <M-7 run StopExternalInstances  (output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
end.

assign oiReturnStatus = viLocalReturnStatus.