project QadFinancials > class BDInvoice > method UpdateDInvoiceBankNumber

Description

update the banknumber of the customer invoice : search payment selection line for the old banknumber, if found, then update that line, subtract the DInvoiceBankToPayTC with the amount of payment selection line AND also create a new line for the new bank number ; if not found, then just update the banknumber_id of the dnvoicebank with the new banknumber_id


Parameters


ilClearDatainputlogicalpass true if you want that bdinvoice is started from scratch again
ilTransferUnallocatedAmountinputlogicalOption to transfer unallocated amount to new bank number
tUpdateDInvoiceBankNumberinputtemp-tabletemp table with updated dinvoice bank numbers in it
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BPaymentSelection.ValidateComponentPreBankPayFormat


program code (program6/bdinvoice.p)

assign oiReturnStatus = -98.

/* first clear the instance, otherwise problems when you execute it twice */
if ilClearData
then do:
    <M-2 run ClearData  (output oiReturnStatus (oiReturnStatus)) in BDInvoice>
    if oiReturnStatus < 0 then return.
end. /* if ilClearData */

for each tUpdateDInvoiceBankNumber:
    if not can-find (first tDInvoice where
                           tDInvoice.DInvoice_ID = tUpdateDInvoiceBankNumber.tiDInvoiceId)
    then assign vcListDInvoiceIds =  if vcListDInvoiceIds = "":U
                                     then string(tUpdateDInvoiceBankNumber.tiDInvoiceId)
                                     else
                                     if lookup(string(tUpdateDInvoiceBankNumber.tiDInvoiceId), vcListDInvoiceIds, chr(4)) = 0
                                     then vcListDInvoiceIds + chr(4) + string (tUpdateDInvoiceBankNumber.tiDInvoiceId)
                                     else vcListDInvoiceIds.
end.
 
if vcListDInvoiceIds <> "":U
then do:
    <M-3 run DataLoad
       (input  '':U (icRowids), 
        input  vcListDInvoiceIds (icPkeys), 
        input  '':U (icObjectIds), 
        input  '':U (icFreeform), 
        input  not ilclearData (ilKeepPrevious), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then do :
            assign vcMessage      = trim(substitute(#T-1'Data could not be loaded: unable to load the list of customer invoices (&1).':100(1795)T-1#, replace(vcListDInvoiceIds, chr(4), ",":U))).
            <M-4 run SetMessage
          (input  vcMessage (icMessage), 
           input  '':U (icArguments), 
           input  '':U (icFieldName), 
           input  '':U (icFieldValue), 
           input  'E':U (icType), 
           input  1 (iiSeverity), 
           input  '':U (icRowid), 
           input  'QadFin-7525':U (icFcMsgNumber), 
           input  '':U (icFcExplanation), 
           input  '':U (icFcIdentification), 
           input  '':U (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            return.
        end. /* if oiReturnStatus < 0 */
    end. /* if viFcReturnSuper <> 0 */
end. /* if vcListDInvoiceIds <> "":U */

for each tUpdateDInvoiceBankNumber:
    find first tDInvoice where
               tDInvoice.DInvoice_ID = tUpdateDInvoiceBankNumber.tiDInvoiceId
               no-error.
    if not available tDInvoice
    then do:
        assign vcMessage = trim(#T-8'The invoice was not found. The system cannot update it.':100(65684)t-8#).
        <M-7 run SetMessage
          (input  vcMessage (icMessage), 
           input  '':U (icArguments), 
           input  '':U (icFieldName), 
           input  '':U (icFieldValue), 
           input  'E':U (icType), 
           input  1 (iiSeverity), 
           input  '':U (icRowid), 
           input  'QadFin-7526':U (icFcMsgNumber), 
           input  '':U (icFcExplanation), 
           input  '':U (icFcIdentification), 
           input  '':U (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        next.
    end.
    
    /* check if you find a payment selection line for the oldbanknumber and not the current record */
    <Q-5 assign vlFcQueryRecordsAvailable = PaySelLineByBankNmbrDInvoice (NoCache)
       (input tUpdateDInvoiceBankNumber.tiOldBankNumberId, (BankNumberId)
        input tUpdateDInvoiceBankNumber.tcCurrentPaymSelLineRowId, (CurrentRowId)
        input viCompanyId, (CompanyId)
        input tUpdateDInvoiceBankNumber.tiDInvoiceId, (DInvoiceId)) in BPaymentSelection >
    if not vlFcQueryRecordsAvailable
    then do:
        /* if not payment selection line is found, just change the dinvoicebank record with new banknumber id */
        find first tDInvoiceBank where
                   tDInvoiceBank.tc_ParentRowid = tDInvoice.tc_Rowid and
                   tDInvoiceBank.tc_Status    <> "D":U and
                   tDInvoiceBank.BankNumber_ID = tUpdateDInvoiceBankNumber.tiOldBankNumberId
                   no-lock no-error.
        if available tDInvoiceBank
        then do:
        /* PWI ToDo if you change amount and payment format, then change amount of old line, and create new line */
            if tDInvoiceBank.DInvoiceBankToPayTC <> tUpdateDInvoiceBankNumber.tdDInvoiceBankToPayTC
            then do:
                assign vdOrigAmountToPay                 = tDInvoiceBank.DInvoiceBankToPayTC
                       tDInvoiceBank.DInvoiceBankToPayTC = if ilTransferUnallocatedAmount
                                                           then 0
                                                           else tDInvoiceBank.DInvoiceBankToPayTC - tUpdateDInvoiceBankNumber.tdDInvoiceBankToPayTC
                       tDInvoiceBank.tlValidatePayment   = false.
                if tDInvoiceBank.tc_Status = "":U
                then assign tDInvoiceBank.tc_Status = "C":U.
                
                assign vlCreate = true.
            end.
            else do: 
                  /* shx Check if there is same bank record already exist for the invoice this scenario could happen when the invoice pay to two different banknumber
                  and one of the banknumbers is the one we want to change */
                find first tDInvoiceBank where
                           tDInvoiceBank.tc_ParentRowid = tDInvoice.tc_Rowid and
                           tDInvoiceBank.tc_Status    <> "D":U and
                           tDInvoiceBank.BankNumber_ID = tUpdateDInvoiceBankNumber.tiNewBankNumberId
                           no-lock no-error.
                /*Same bankNumber found, consolidate to the new banknumber and delete the old one */
                if available tDInvoiceBank
                then do:
                    assign 
                        tDInvoiceBank.DInvoiceBankToPayTC = tDInvoiceBank.DInvoiceBankToPayTC + tUpdateDInvoiceBankNumber.tdDInvoiceBankToPayTC
                        tDInvoiceBank.tlValidatePayment   = false.
                    if tDInvoiceBank.tc_Status = "":U
                    then assign tDInvoiceBank.tc_Status = "C":U.
                    find first tDInvoiceBank where
                               tDInvoiceBank.tc_ParentRowid = tDInvoice.tc_Rowid and
                               tDInvoiceBank.tc_Status    <> "D":U and
                               tDInvoiceBank.BankNumber_ID = tUpdateDInvoiceBankNumber.tiOldBankNumberId
                               no-lock no-error.
                    if available tDInvoiceBank
                    then do:
                        if tDInvoiceBank.tc_Status = "":U
                        then assign tDInvoiceBank.tc_Status = "D":U.
                    end.
                end.
                else do:
                    find first tDInvoiceBank where
                               tDInvoiceBank.tc_ParentRowid = tDInvoice.tc_Rowid and
                               tDInvoiceBank.tc_Status    <> "D":U and
                               tDInvoiceBank.BankNumber_ID = tUpdateDInvoiceBankNumber.tiOldBankNumberId
                               no-lock no-error.
                    assign tDInvoiceBank.BankNumber_ID = tUpdateDInvoiceBankNumber.tiNewBankNumberId.
                    if tDInvoiceBank.tc_Status = "":U
                    then assign tDInvoiceBank.tc_Status = "C":U.

                    /* delete the Payment attributes if Payformat changed */
                    if tUpdateDInvoiceBankNumber.tlPayFormatChanged
                    then do:
                        for each tDInvoiceBankPayCode where 
                                 tDInvoiceBankPayCode.tc_ParentRowid = tDInvoiceBank.tc_Rowid and
                                 tDInvoiceBankPayCode.tc_Status      <> "D":U:
                            
                                if tDInvoiceBankPayCode.tc_Status = "":U
                                then assign tDInvoiceBankPayCode.tc_Status = "D":U.
                        end.
                    end.
                end.
            end.
        end.
        if vlCreate
        then do:
            <M-9 run AddDetailLine
               (input  'DInvoiceBank':U (icTable), 
                input  tDInvoice.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = -98)
            then assign oiReturnStatus = viFcReturnSuper.
    
            assign tDInvoiceBank.DInvoice_ID           = tDInvoice.DInvoice_ID
                   tDInvoiceBank.BankNumber_ID         = tUpdateDInvoiceBankNumber.tiNewBankNumberId
                   tDInvoiceBank.DInvoiceBankToPayTC   = if ilTransferUnallocatedAmount
                                                         then vdOrigAmountToPay
                                                         else tUpdateDInvoiceBankNumber.tdDInvoiceBankToPayTC
                   tDInvoiceBank.tiParentObject_ID     = tDInvoice.Debtor_ID. 
            if not vlQBPC
            then do:
                <Q-10 run BankNumberPayCodeByBankNbrID  (Start) in BBankNumber >
                assign vlQBPC = true.
            end.
            find first tDInvoiceBank where tDInvoiceBank.BankNumber_ID = tUpdateDInvoiceBankNumber.tiNewBankNumberId no-error.
            if available tDInvoiceBank
                then do:
                    <Q-11 run BankNumberPayCodeByBankNbrID (all) (Read) (NoCache)
                       (input tDInvoiceBank.BankNumber_ID, (BankNumberId)
                        output dataset tqBankNumberPayCodeByBankNbrID) in BBankNumber >
                for each tqBankNumberPayCodeByBankNbrID no-lock:
                    <M-13 run AddDetailLine
                       (input  'DInvoiceBankPayCode':U (icTable), 
                        input  tDInvoiceBank.tc_Rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                    if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = -98)
                    then assign oiReturnStatus = viFcReturnSuper.
                    assign tDInvoiceBankPayCode.DInvoiceBank_ID  = tDInvoiceBank.DInvoiceBank_ID
                           tDInvoiceBankPayCode.PayFormatCode_ID = tqBankNumberPayCodeByBankNbrID.tiPayFormatCode_ID
                           tDInvoiceBankPayCode.PayFormatGroup_ID= tqBankNumberPayCodeByBankNbrID.tiPayFormatGroup_ID.
                    
                end.
            end.
        end.
    end.
    else do:
        /* if payment selection line is found, change the amount of the old banknumber, with the
           dinvoicebanktopaytc - payment selection amount AND create new dinvoicebank record with new banknumber id  */
        find first tDInvoiceBank where
                   tDInvoiceBank.tc_ParentRowid = tDInvoice.tc_Rowid and
                   tDInvoiceBank.tc_Status    <> "D":U and
                   tDInvoiceBank.BankNumber_ID = tUpdateDInvoiceBankNumber.tiOldBankNumberId
                   no-lock no-error.
        if available tDInvoiceBank
        then do:
            assign vdOrigAmountToPay                 = tDInvoiceBank.DInvoiceBankToPayTC
                   tDInvoiceBank.DInvoiceBankToPayTC = if ilTransferUnallocatedAmount
                                                       then 0
                                                       else tDInvoiceBank.DInvoiceBankToPayTC - tUpdateDInvoiceBankNumber.tdDInvoiceBankToPayTC.
            if tDInvoiceBank.tc_Status = "":U
            then assign tDInvoiceBank.tc_Status = "C":U.
        end.

        <M-6 run AddDetailLine
           (input  'DInvoiceBank':U (icTable), 
            input  tDInvoice.tc_Rowid (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = -98)
        then assign oiReturnStatus = viFcReturnSuper.

        assign tDInvoiceBank.DInvoice_ID           = tDInvoice.DInvoice_ID
               tDInvoiceBank.BankNumber_ID         = tUpdateDInvoiceBankNumber.tiNewBankNumberId
               tDInvoiceBank.DInvoiceBankToPayTC   = if ilTransferUnallocatedAmount
                                                     then vdOrigAmountToPay
                                                     else tUpdateDInvoiceBankNumber.tdDInvoiceBankToPayTC
               tDInvoiceBank.tiParentObject_ID     = tDInvoice.Debtor_ID.
        if not vlQBPC
        then do:
            <Q-14 run BankNumberPayCodeByBankNbrID  (Start) in BBankNumber >
            assign vlQBPC = true.
        end.
        find first tDInvoiceBank where tDInvoiceBank.BankNumber_ID = tUpdateDInvoiceBankNumber.tiNewBankNumberId no-error.
        if available tDInvoiceBank
            then do:
                <Q-15 run BankNumberPayCodeByBankNbrID (all) (Read) (NoCache)
                   (input tDInvoiceBank.BankNumber_ID, (BankNumberId)
                    output dataset tqBankNumberPayCodeByBankNbrID) in BBankNumber >
            for each tqBankNumberPayCodeByBankNbrID no-lock:
                <M-16 run AddDetailLine
                   (input  'DInvoiceBankPayCode':U (icTable), 
                    input  tDInvoiceBank.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = -98)
                then assign oiReturnStatus = viFcReturnSuper.
                assign tDInvoiceBankPayCode.DInvoiceBank_ID  = tDInvoiceBank.DInvoiceBank_ID
                       tDInvoiceBankPayCode.PayFormatCode_ID = tqBankNumberPayCodeByBankNbrID.tiPayFormatCode_ID
                       tDInvoiceBankPayCode.PayFormatGroup_ID= tqBankNumberPayCodeByBankNbrID.tiPayFormatGroup_ID.
                
            end.
        end.
    end.
end.
if vlQBPC
then do:
    <Q-17 run BankNumberPayCodeByBankNbrID  (Stop) in BBankNumber >
end.

if oiReturnStatus = -98
then assign oiReturnStatus = 0.