project QadFinancials > class BDebtorEndUser > method ApiCreateDebtorEndUser

Description

This method will create a new DebtorEndUser record based on the address information passed in. If the address data does not already exist for the business relation of owning Debtor (icDebtorCode) then it will be created in the business relation first.


The address data that is passed in is in the mfg/pro ad_mstr format.

If the debtor end user code specificed in the temp-table (tEndUserAddress.ad_addr) is left blank then it will be auto-populated. This is done by calling ApiGenerateEndUserCode api method.


If the debtorendusercode already exists as a shipto then the tDebtorEndUser.DebtorEndUserIsShipto field will be set to true.

If the debtroendusercode already exists as a debtor then the tDebtorEndUser.DebtorEndUserIsDebtor field will be set to true.

The ValidateBC and AdditionalUpdates methods are called from this method.

Finally the data is saved to the db using the DataSave method.


Parameters


icDebtorCodeinputcharacterThe owning Debtor for the DebtorEndUser that is to be created.
tEnduserAddressMasterinput-outputtemp-tableThe address details to be used for the DebtorEndUser creation.

Note: the fields is this table correspond to the ad_mstr table.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program3/bdebtorenduser.p)

MAIN_BLOCK:
do on error undo, leave MAIN_BLOCK:

    assign oiReturnStatus      = -98
           viLocalReturnStatus = 0.    
    
    /* Clear the instance data */
    <M-3 run ClearData (output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
    empty temp-table tApiEnduserAddress.
    empty temp-table tApiEnduserContact.
    
     /* Verify the debtor code passed in points to a valid debtor */
    <Q-34 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-1'Customer ($1) could not be found.':200(31311)T-1#
                viLocalReturnStatus = -1.
         <M-5 run SetMessage
            (input  vcMessage (icMessage), 
                     input  icDebtorCode (icArguments), 
                     input  '' (icFieldName), 
                     input  '' (icFieldValue), 
                     input  'E':U (icType), 
                     input  3 (iiSeverity), 
                     input  '' (icRowid), 
                     input  'QadFin-4731':U (icFcMsgNumber), 
                     input  '' (icFcExplanation), 
                     input  '' (icFcIdentification), 
                     input  '' (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>       
        leave MAIN_BLOCK.
    end.  /* end of if available tqDebtorPrim */
    
    /* Mapp the Address data from MFG/PRO format to financial format */
    if viBMfgAddress9ID = 0 or
       viBMfgAddress9ID = ?
    then do:
        <I-6 {bFcStartAndOpenInstance
                &ADD-TO-TRANSACTION = "true"
                &CLASS              = "BMfgAddress"}>    
        assign vlIsBMfgAddressStartedHr = true
               vlIsBMfgAddressOpenedHr  = true.
    end.
    else do:
        <I-8 {bFcOpenInstance
                &CLASS           = "BMfgAddress"}>                   
        assign vlIsBMfgAddressOpenedHr = true.                
    end.  /* end if viBMfgAddress2ID */
    
    <M-9 run MapMfgAddressData
       (input  temp-table tEnduserAddressMaster:default-buffer-handle (ihMfgTable), 
        input  temp-table tApiEnduserAddress:default-buffer-handle (ihFinancialAddressTable), 
        input  temp-table tApiEnduserContact: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 viBBusinessRelation6ID = 0 or
       viBBusinessRelation6ID = ?
    then do:       
        <I-11 {bFcStartAndOpenInstance
                &ADD-TO-TRANSACTION = "true"
                &CLASS              = "BBusinessRelation"}>
        assign vlIsBBusinessRelationStartedHr = true
               vlIsBBusinessRelationOpenedHr  = true.
    end.
    else do:
        <I-22 {bFcOpenInstance
                &CLASS           = "BBusinessRelation"}>
        assign vlIsBBusinessRelationOpenedHr = true.
    end.  /* end if viBBusinessRelation1ID */
    
    <M-14 run ApiMaintainAddress
       (input-output tApiEndUserAddress (tApiAddress), 
        input-output tApiEnduserContact (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 end users */
    for each tEnduserAddressMaster:   
    
        /* =============================== */
    	/* Set Actvity                     */
    	/* =============================== */
        assign vcActivityCode = "Create":U.
        
        /* Verify that the shipto does not already exist */
        <Q-15 assign vlFcQueryRecordsAvailable = DebtorEndUserPrim (NoCache)
           (input tEnduserAddressMaster.ad_addr, (DebtorEndUserCode)
            input '', (DebtorEndUser_ID)
            input viCompanyId, (CompanyId)) in BDebtorEndUser >
    
        if vlFcQueryRecordsAvailable
        then do:
            assign vcMessage           = #T-23'Customer End User $1 already exists; cannot create':50(50297)T-23#
                   viLocalReturnStatus = -1.
            <M-24 run SetMessage
               (input  vcMessage (icMessage), 
                      input  tEnduserAddressMaster.ad_addr (icArguments), 
                      input  '' (icFieldName), 
                      input  '' (icFieldValue), 
                      input  'E':U (icType), 
                      input  3 (iiSeverity), 
                      input  '' (icRowid), 
                      input  'QadFin-4736':U (icFcMsgNumber), 
                      input  '' (icFcExplanation), 
                      input  '' (icFcIdentification), 
                      input  '' (icFcContext), 
                      output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
             leave MAIN_BLOCK.
        end.  /* end of vlFcQueryRecordsAvailable */
             
        /* Map the MFG/PRO format data passed in to Financial application data */    
        <M-17 run AddDetailLine
           (input  'DebtorEndUser':U (icTable), 
            input  '' (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>       
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viLocalReturnStatus < 0 then leave MAIN_BLOCK.      
    
        if tEnduserAddressMaster.ad_addr = ""
        then do:
            <M-37 run ApiGenerateEndUserCode
              (input  tDebtorEndUser.tc_Rowid (icDebtorEndUserRowid), 
               output tEnduserAddressMaster.ad_addr (ocDebtorEndUserCode), 
               output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
            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 tApiEnduserAddress        
            where tApiEnduserAddress.tc_rowid = tEnduserAddressMaster.tc_rowid:
        end.
            assign tDebtorEndUser.debtorEndUserCode      = tEnduserAddressMaster.ad_addr
             tDebtorEndUser.tcDebtorCode                 = icDebtorcode      
             tDebtorEndUser.tcAddressCity                = tApiEnduserAddress.AddressCity
             tDebtorEndUser.tcAddressZip                 = tApiEnduserAddress.AddressZip
             tDebtorEndUser.tcAddressStreet1             = tApiEnduserAddress.AddressStreet1
             tDebtorEndUser.tcAddressStreet2             = tApiEnduserAddress.AddressStreet2
             tDebtorEndUser.tcAddressStreet3             = tApiEnduserAddress.AddressStreet3
             tDebtorEndUser.address_id                   = tApiEnduserAddress.Address_ID
             tDebtorEndUser.tcAddressTypeCode            = tApiEnduserAddress.tcAddressTypeCode
             tDebtorEndUser.tiAddressBusinessRelation_ID = tApiEnduserAddress.BusinessRelation_ID
             tDebtorEndUser.DebtorEndUserIsDebtor        = false
             tDebtorEndUser.DebtorEndUserIsShipto        = false
             tDebtorEndUser.SharedSet_ID                 = tqDebtorByDebtorIDCode.tisharedset_id
             tDebtorEndUser.tcBusinessRelationCode       = tEnduserAddressMaster.ad_bus_relation
             tDebtorEndUser.DebtorEndUserName            = tApiEnduserAddress.AddressName
             
             tEnduserAddressMaster.ad_address_id =  tApiEnduserAddress.Address_ID.
                
        /* If user specified a code check if its a shipto or debtor */
        if tDebtorEndUser.debtorEndUserCode <> ""
        then do:
            <Q-28 assign vlFcQueryRecordsAvailable = DebtorShipToPrim (NoCache)
               (input ?, (DebtorShiptoID)
                input tDebtorEndUser.DebtorEndUserCode, (DebtorShiptoCode)
                input viCompanyId, (CompanyId)) in BDebtorShipTo>
                                                                   
            if vlFcQueryRecordsAvailable <> false 
            then assign tDebtorEndUser.DebtorEndUserIsShipto = true.
            else do:
                <Q-29 assign vlFcQueryRecordsAvailable = DebtorByCode (NoCache)
                   (input viCompanyId, (CompanyId)
                    input tDebtorEndUser.DebtorEndUserCode, (DebtorCode)) in BDebtor >
                                                           
                if vlFcQueryRecordsAvailable <> false
                then assign tDebtorEndUser.DebtorEndUserIsDebtor = true.
            end.
         end.   
    end.  /* end of each t_sShiptoAddressMaster */
    
    /* Validate the component */
    <M-18 run ValidateBC (output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
    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-19 run AdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
    
    /* Save the component */
    <M-20 run DataSave (output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
end. /* MAIN_BLOCK */

/* ======================================================================= *
 * Error handling                                                          *
 * ======================================================================= */
if viLocalReturnStatus < 0
then do:
    <M-53 run StopExternalInstances  (output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
end.

assign oiReturnStatus = viLocalReturnStatus.