project QadFinancials > class BDebtorShipTo > method ApiCreateDebtorShipto


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 MAIN_BLOCK:

    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.

     /* Verify the debtor code passed in points to a valid debtor */
    <Q-60 run DebtorByDebtorIDCode (all) (Read) (NoCache)
       (input viCompanyID, (CompanyId)
        input '', (DebtorId)
        input icDebtorCode, (DebtorCode)
        input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
        output dataset tqDebtorByDebtorIDCode) in BDebtor >

    for first tqDebtorByDebtorIDCode:
    end.

    /* if no debtor record was found then return an error */
    if not available tqDebtorByDebtorIDCode 
    then do:
          assign vcMessage           = #T-10'Customer ($1) could not be found.':200(31311)T-10#
                 viLocalReturnStatus = -1.
           
         <M-8 run SetMessage
            (input  vcMessage (icMessage), 
             input  icDebtorCode (icArguments), 
             input  '' (icFieldName), 
             input  '' (icFieldValue), 
             input  'E':U (icType), 
             input  3 (iiSeverity), 
             input  '' (icRowid), 
             input  'QadFin-4633':U (icFcMsgNumber), 
             input  '' (icFcExplanation), 
             input  '' (icFcIdentification), 
             input  '' (icFcContext), 
             output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
       
        leave MAIN_BLOCK.
    end.  /* end of if available tqDebtorPrim */

    /* Mapp the Address data from MFG/PRO format to financial format */
    if viBMfgAddress2ID = 0 or
       viBMfgAddress2ID = ?
    then do:
        <I-45 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION   = "true"
             &CLASS                = "BMfgAddress"}>
        assign vlIsBMfgAddressStartedHr = true
               vlIsBMfgAddressOpenedHr  = true.
    end.
    else if not valid-handle(vhBMfgAddress2Inst)
    then do:
        <I-46 {bFcOpenInstance
             &CLASS           = "BMfgAddress"}>
        assign vlIsBMfgAddressOpenedHr = true.
    end.  /* end if viBMfgAddress2ID */

    <M-47 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.    
        
                        
    /* Create/update the address in the Businress Relation */
    if viBBusinessRelation1ID = 0 or
       viBBusinessRelation1ID = ?
    then do: 
         <I-49 {bFcStartAndOpenInstance
                &ADD-TO-TRANSACTION = "true"
                &CLASS              = "BBusinessRelation"}>
         assign vlIsBBusinessRelationStartedHr = true
                vlIsBBusinessRelationOpenedHr  = true.
    end.
    else if not valid-handle(vhBBusinessRelation1Inst)
    then do:         
        <I-50 {bFcOpenInstance
             &CLASS           = "BBusinessRelation"}>
        assign vlIsBBusinessRelationOpenedHr = true.
    end.  /* end if viBBusinessRelation1ID */

    <M-51 run ApiMaintainAddress
       (input-output tApiShiptoAddress (tApiAddress), 
        input-output tApiShiptoContact (tApiContact), 
        input  tqDebtorByDebtorIDCode.tcBusinessRelationCode (icBusinessRelationCode), 
        input  tqDebtorByDebtorIDCode.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.
    
    /* Iterate through all the shiptos */
    for each tShiptoAddressMaster:   

        /* Verify that the shipto does not already exist */
        <Q-22 assign vlFcQueryRecordsAvailable = DebtorShipToPrim (NoCache)
           (input '', (DebtorShiptoID)
            input  tShiptoAddressMaster.ad_addr, (DebtorShiptoCode)
            input ?, (CompanyId)) in BDebtorShipTo>
        
        /*if it exists then return an error */
        if vlFcQueryRecordsAvailable
        then do:
            viLocalReturnStatus = -1.
            <M-23 run SetMessage
               (input  vcMessage (icMessage), 
              input   tShiptoAddressMaster.ad_addr (icArguments), 
              input  '' (icFieldName), 
              input  '' (icFieldValue), 
              input  'E':U (icType), 
              input  3 (iiSeverity), 
              input  '' (icRowid), 
              input  'QadFin-4660':U (icFcMsgNumber), 
              input  '' (icFcExplanation), 
              input  '' (icFcIdentification), 
              input  '' (icFcContext), 
              output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
            leave MAIN_BLOCK.
         end. /* end of vlFcQueryRecordsAvailable */
        
         /* Map the MFG/PRO format data passed in to Financial application data */    
         <M-40 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. 
        
        if tShiptoAddressMaster.ad_addr = "":U
        then do:
            <M-63 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.
       
        /* Find the financial record that maps to this MFG/PRO record */
        for first tApiShiptoAddress        
            where tApiShiptoAddress.tc_rowid = tShiptoAddressMaster.tc_rowid:       
        end.
        
        assign tDebtorShipto.debtorShiptoCode     = tShiptoAddressMaster.ad_addr
               tDebtorShipTo.tcDebtorCode         = icDebtorcode      
               tDebtorShipto.address_id           = tApiShiptoAddress.Address_ID
               tDebtorShipto.tcAddressTypeCode    = tApiShiptoAddress.tcAddressTypeCode
               tDebtorShipto.DebtorShiptoIsDebtor = false
               tDebtorShipTo.SharedSet_ID         = tqDebtorByDebtorIDCode.tiSharedSet_ID.               
                                    
        <Q-92 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 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-54 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.   
        
        assign tShiptoAddressMaster.ad_address_id = tDebtorShipto.Address_ID.   
                               
    end.  /* end of each t_sShiptoAddressMaster */

    /* Validate the component */
    <M-19 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.

    /* Run any addional updates */
    <M-20 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-21 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-32 run StopExternalInstances  (output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
end.

assign oiReturnStatus = viLocalReturnStatus.