project QadFinancials > class BBankImportLine > method GetInvoicesByBankImpLine

Description

This method gets matched invoices based on the information of Bank import Line.


Parameters


ilincludeAllEntitiesinputlogicalInclude Invoices in All Entities
iiCompanyIdinputintegerCompany ID
tInvoiceXrefoutputtemp-tableInvoiceXref allocation records when creating customer document to pay matched invoices.
iiBankNumberIdinputintegerBank Number ID
iiCurrencyIdinputintegerCurrency ID
icPaymentFormatinputcharacterPayment Format
icBankImpLineInvRefListinputcharacter
odUnBalancedAmountoutputdecimal
icNoInvoiceActioninputcharacter
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankImportLine.ProcessBankImpLineNewDDocument


program code (program6/bbankimportline.p)

/* **************************************************************************************************** */

/* =================================================================================================== */
/* if using custom matching logic, then return directly without executing standard matching logic.     */
/* =================================================================================================== */
if vlUseCustomMatching = yes
then return.

assign oiReturnStatus     = -98
       vdAccumulateAmount = 0
       viInvoicesCount    = 0
       vcParam            = '':U
       vcCriteria         = '':U
       odUnBalancedAmount = 0.

empty temp-table tInvoiceXref.       
empty temp-table tInvoiceStageXref.       
empty temp-table tDInvoiceStageDetails.       
empty temp-table tDInvoiceByDebtorRefTemp.
empty temp-table tqDInvoiceByDebtorRef.

if not available tBankImpLine
then do:
    assign vcMessage = trim(#T-9'Missing definition of imported bank line.':255(67965)T-9#).
    <M-8 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'S':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'QadFin-7680':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>
    assign oiReturnStatus = -1.
    return.
end.

/* =================================================================================================== */
/* Initialize Unallocated balance amount if payconf action is Prepayment                               */
/* =================================================================================================== */
if icNoInvoiceAction = {&PAYCONFNOINVOICEACT-CREATE-PREPAY}
then assign odUnBalancedAmount   = tBankImpLine.BankImpLineAmountTC.

/* =================================================================================================== */
/* Get Invoice base to check the reference which is supplied                                           */
/* =================================================================================================== */
if icBankImpLineInvRefList <> "":U and
   icBankImpLineInvRefList <> ?
then do:
    for each tBankImpLineDet:  /* actually only one record in these */
            
        REPEAT viRefListCount = 1 TO NUM-ENTRIES(tBankImpLineDet.BankImpLineInvRefList):

            /* first search on payment reference : that is unique for every entity,   */
            /* but because we don't have an index on it yet, we add the debtor_id too */
            if tBankImpLineDet.BankImpLinePaymentRef <> ? and 
               tBankImpLineDet.BankImpLinePaymentRef <> ""
            then do:
                <Q-41 run DInvoiceByDebtorRef (all) (Read) (NoCache)
                   (input if ilincludeAllEntities then ? else iicompanyId, (CompanyId)
                    input tBankImpLineDet.Debtor_ID, (DebtorId)
                    input ?, (DInvoiceRefList)
                    input true, (DInvoiceIsOpen)
                    input ?, (DebitTCAmount)
                    input ?, (BankNumberId)
                    input iiCurrencyId, (CurrencyId)
                    input ?, (Voucher)
                    input tBankImpLineDet.BankImpLinePaymentRef, (DInvoiceTSMNumber)
                    output dataset tqDInvoiceByDebtorRef) in BDInvoice>
                    
                assign vcCriteria = "DInvoiceTSMNumber = " + tBankImpLineDet.BankImpLinePaymentRef.

            end. /* if tBankImpLineDet.BankImpLinePaymentRef <> ? and .. */
              
            /* message viRefListCount "," ENTRY(viRefListCount,icBankImpLineInvRefList). */
            /* Get invoice by ref to build the tqDInvoiceByDebtorRef */
            /* case1: Ref Format: journalcode and voucher. eg (split them ) as QA requests, we ignore it*/
            /* case2: Ref Format: voucher */

            assign viDInvoiceVoucher = integer(ENTRY(viRefListCount,tBankImpLineDet.BankImpLineInvRefList)) NO-ERROR.
            if error-status:error
            then do:
                assign viDInvoiceVoucher = 0.
            end.

            if viDInvoiceVoucher <> 0 and not can-find(first tqDInvoiceByDebtorRef) then
            do:
                <Q-21 run DInvoiceByDebtorRef (all) (Read) (NoCache)
                   (input if ilincludeAllEntities then ? else iicompanyId, (CompanyId)
                    input tBankImpLineDet.Debtor_ID, (DebtorId)
                    input ?, (DInvoiceRefList)
                    input true, (DInvoiceIsOpen)
                    input ?, (DebitTCAmount)
                    input iiBankNumberId, (BankNumberId)
                    input iiCurrencyId, (CurrencyId)
                    input viDInvoiceVoucher, (Voucher)
                    input ?, (DInvoiceTSMNumber)
                    output dataset tqDInvoiceByDebtorRef) in BDInvoice>
                    
                assign vcCriteria = if vcCriteria <> '' then vcCriteria + ";Voucher = " + string(viDInvoiceVoucher)
                                    else "Voucher = " + string(viDInvoiceVoucher).
            end.
            /* case3: Ref Format: DIText */
            if (viDInvoiceVoucher = 0 or  viDInvoiceVoucher = ?) and not can-find(first tqDInvoiceByDebtorRef) then
            do: 
                /* Search for Invoices - Case1 - including banknumber */
                <Q-1 run DInvoiceByDebtorRef (all) (Read) (NoCache)
                   (input if ilincludeAllEntities then ? else iicompanyId, (CompanyId)
                    input tBankImpLineDet.Debtor_ID, (DebtorId)
                    input ENTRY(viRefListCount,tBankImpLineDet.BankImpLineInvRefList), (DInvoiceRefList)
                    input true, (DInvoiceIsOpen)
                    input ?, (DebitTCAmount)
                    input iiBankNumberId, (BankNumberId)
                    input iiCurrencyId, (CurrencyId)
                    input ?, (Voucher)
                    input ?, (DInvoiceTSMNumber)
                    output dataset tqDInvoiceByDebtorRef) in BDInvoice>
                    
                assign vcCriteria = if vcCriteria <> '' then vcCriteria + ";DInvoiceRefList = " + tBankImpLineDet.BankImpLineInvRefList
                                    else "DInvoiceRefList = " + tBankImpLineDet.BankImpLineInvRefList.
            end.
            /* store all the result to temp table and after loop, retrieve all the data to table tqDInvoiceByDebtorRef. */

            for each tqDInvoiceByDebtorRef:
                create tDinvoiceByDebtorRefTemp.
                buffer-copy tqDInvoiceByDebtorRef to tDinvoiceByDebtorRefTemp.
            end.
            empty temp-table tqDInvoiceByDebtorRef.
        end. /* end of repeate for reflist.*/
        
        for each tDinvoiceByDebtorRefTemp:
            create tqDInvoiceByDebtorRef.
            buffer-copy tDinvoiceByDebtorRefTemp to tqDInvoiceByDebtorRef.
        end.

        /* case4: All invoices: including banknumber */
        if not can-find(first tqDInvoiceByDebtorRef) then
        do:
            <Q-51 run DInvoiceByDebtorRef (all) (Read) (NoCache)
               (input if ilincludeAllEntities then ? else iicompanyId, (CompanyId)
                input tBankImpLineDet.Debtor_ID, (DebtorId)
                input ?, (DInvoiceRefList)
                input true, (DInvoiceIsOpen)
                input ?, (DebitTCAmount)
                input iiBankNumberId, (BankNumberId)
                input iiCurrencyId, (CurrencyId)
                input ?, (Voucher)
                input ?, (DInvoiceTSMNumber)
                output dataset tqDInvoiceByDebtorRef) in BDInvoice>
        end.
        else do:
            find first tqDInvoiceByDebtorRef no-error.
            if available tqDInvoiceByDebtorRef                   
            then assign vcCriteria = vcCriteria + ";DebtorCode = " + tqDInvoiceByDebtorRef.tcDebtorCode + 
                                     ";CurrencyCode = " + tqDInvoiceByDebtorRef.tcCurrencyCode + 
                                     ";BankNumber = " + tqDInvoiceByDebtorRef.tcBankNumber.
        end.
            
        /* ========================================================================================== */
        /* If there are No open invoices on this bank number, we give a message to the user           */
        /* ========================================================================================== */
        find first tqDInvoiceByDebtorRef where
                   tqDInvoiceByDebtorRef.tiBankNumber_ID  = iiBankNumberId and
                   tqDInvoiceByDebtorRef.tiDebtor_ID      = tBankImpLineDet.Debtor_ID and
                   tqDInvoiceByDebtorRef.tlDInvoiceIsOpen = true 
                   no-error.
            
        if not available tqDInvoiceByDebtorRef
        then do:        
            /* not found the IsAvailableDInvoiceByDebtor. */
            <Q-77 run DInvoiceByDebtorRef (all) (Read) (NoCache)
               (input if ilincludeAllEntities then ? else iicompanyId, (CompanyId)
                input tBankImpLineDet.Debtor_ID, (DebtorId)
                input ?, (DInvoiceRefList)
                input true, (DInvoiceIsOpen)
                input ?, (DebitTCAmount)
                input ?, (BankNumberId)
                input iiCurrencyId, (CurrencyId)
                input ?, (Voucher)
                input ?, (DInvoiceTSMNumber)
                output dataset tqDInvoiceByDebtorRef) in BDInvoice>
    
            for each tqDInvoiceByDebtorRef where
                     tqDInvoiceByDebtorRef.tiDebtor_ID      = tBankImpLineDet.Debtor_ID and
                     tqDInvoiceByDebtorRef.tlDInvoiceIsOpen = true:
                 assign viInvoicesCount = viInvoicesCount + 1.
            end.
    
            if viInvoicesCount = 0
            then do:
                assign vcMessage = trim(substitute(#T-54'There are no open invoices for customer &1':255(582333512)T-54#,
                                                       tBankImpLineDet.BankImpLineDebtorCode)).
                <M-88 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  'BIL':U + tBankImpLineDet.tc_Rowid (icRowid), 
                    input  'qadfin-66820':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>
                
                assign oiReturnStatus = -1
                       tBankImpLineDet.BankImpLineProcessedStatus = {&BANKIMPPROCSTATUS-PROCESSED-ERROR}.
                return.
            end.
            else do:
                assign vcMessage = trim(substitute(#T-35'The open invoices for customer &1 do not have a bank number that corresponds with Own Bank Number &2 and Payment Format &3':255(119552570)T-35#,
                                                       tBankImpLineDet.BankImpLineDebtorCode, tBankImpLineDet.BankImpLineOwnBankNumber,icPaymentFormat)).
                <M-90 run SetMessage
                    (input  vcMessage (icMessage), 
                     input  '':U (icArguments), 
                     input  '':U (icFieldName), 
                     input  '':U (icFieldValue), 
                     input  'E':U (icType), 
                     input  3 (iiSeverity), 
                     input  'BIL':U + tBankImpLineDet.tc_Rowid (icRowid), 
                     input  'qadfin-262229':U (icFcMsgNumber), 
                     input  '':U (icFcExplanation), 
                     input  '':U (icFcIdentification), 
                     input  '':U (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>
                
                assign oiReturnStatus = -1
                       tBankImpLineDet.BankImpLineProcessedStatus = {&BANKIMPPROCSTATUS-PROCESSED-ERROR}.
                empty temp-table tqDInvoiceByDebtorRef.
                return.
            end.    
        end. /* if not available tqDInvoiceByDebtorRef */
        else do:
            find first tqDInvoiceByDebtorRef no-error.
            if available tqDInvoiceByDebtorRef                   
            then assign vcCriteria = "DebtorCode = " + tqDInvoiceByDebtorRef.tcDebtorCode + 
                                     ";CurrencyCode = " + tqDInvoiceByDebtorRef.tcCurrencyCode + 
                                     ";BankNumber = " + tqDInvoiceByDebtorRef.tcBankNumber.
        end.
        
        /* ===================================================================== */
        /* Allocate invoice amount for partical payment                          */
        /* ===================================================================== */
        assign vdTotalAllocatedAmount = 0.
        for each tqDInvoiceByDebtorRef where
                     tBankImpLineDet.BankImpLineInvRefList matches '*' + string(tqDInvoiceByDebtorRef.tiDInvoiceVoucher,'999999999') + '*':U AND
                     tBankImpLineDet.BankImpLineInvRefList matches '*' + tqDInvoiceByDebtorRef.tcJournalCode + '*'
                     by tqDInvoiceByDebtorRef.ttDInvoiceDueDate:
                     
                        
            find first tDInvoiceStageDetails where 
            tDInvoiceStageDetails.tiDInvoiceId = tqDInvoiceByDebtorRef.tiDInvoice_ID 
            no-error.

            if not available tDInvoiceStageDetails 
            then do:
                 assign vdInvoiceAmount  =  abs(tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC - tqDInvoiceByDebtorRef.tdDInvoiceOriginalCreditTC)
                        vdInvoiceBalance =  abs(tqDInvoiceByDebtorRef.tdDInvoiceBalanceDebitTC - tqDInvoiceByDebtorRef.tdDInvoiceBalanceCreditTC - vdInvoiceAllocatedAmount).
                        vdVatTC          =  if tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC <> 0 and tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC <> ?
                                            then tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC 
                                            else tqDInvoiceByDebtorRef.tdDInvoiceVatCreditTC.  
                 assign vdInvoiceAllocatedAmount = 0.   
                 for each bBankImportLine where 
                          bBankImportLine.tlIsProcessed     = true and 
                          bBankImportLine.BankImpLineAction = {&BANKIMPORTACTION-CREATEDDOCUMENT}:
                              
                      <M-43 run GetAllocAmountByInvoice
                         (input  tqDInvoiceByDebtorRef.tiDInvoice_ID (iiInvoiceID), 
                          input  bBankImportLine.DDocument_ID (iiDDocumentID), 
                          input-output vdInvoiceAllocatedAmount (bdAllocAmount), 
                          output viFcReturnSuper (oiReturnStatus)) in BDDocument>
                 end.
                 /* Calculate the discount amount */    
                 if tqDInvoiceByDebtorRef.tdPaymentConditionPercentage <> 0  and
                     tqDInvoiceByDebtorRef.ttDInvoiceDiscountDueDate >= tBankImpLine.BankImpLineValueDate
                 then do:
                     assign vdDiscountBase  =   vdInvoiceBalance.
                    /* if discount is set in the incoming file use that amount */
                     if tBankImpLineDet.BankImpLineDiscAmtTC > 0 
                     then assign vdDiscountAmount = tBankImpLineDet.BankImpLineDiscAmtTC.                
                     else assign vdDiscountAmount = if tqDInvoiceByDebtorRef.tlDInvoiceIsDiscTaxAtPaym = true 
                                                    then vdDiscountBase * tqDInvoiceByDebtorRef.tdPaymentConditionPercentage / 100
                                                    else (vdInvoiceBalance - vdVatTC * vdInvoiceBalance / vdInvoiceAmount) * 
                                                         tqDInvoiceByDebtorRef.tdPaymentConditionPercentage / 100.                                              
                
                     assign vdInvoicePaymentAmount =   vdInvoiceBalance - vdDiscountAmount.
                                                             
                 end.
                   else do:
                     assign vdDiscountAmount       = 0
                            vdInvoicePaymentAmount = vdInvoiceBalance.
                 end.  
                 if (tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> 0 and tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> ?)
                 then assign vdTotalAllocatedAmount = vdTotalAllocatedAmount + ( vdInvoicePaymentAmount *
                                                           ((tqDInvoiceByDebtorRef.tdDInvoiceBankToPayTC - vdInvoiceAllocatedAmount) /
                                                             vdInvoiceBalance) )  .
                 else assign vdTotalAllocatedAmount = vdTotalAllocatedAmount -  ( vdInvoicePaymentAmount *
                                                           ((tqDInvoiceByDebtorRef.tdDInvoiceBankToPayTC - vdInvoiceAllocatedAmount) /
                                                             vdInvoiceBalance) )  .
                                     

        
                  assign vdTotalAllocatedAmount =  <M-95 RoundAmount
                                                      (input  vdTotalAllocatedAmount (idUnroundedAmount), 
                                                       input  ? (iiCurrencyID), 
                                                       input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine> .
               
            end. /* if not available tDInvoiceStageDetails */  
        end. /*   for each tqDInvoiceByDebtorRef */
            
        assign vdAccumulateAmount = 0.
        /* ===================================================================== */
        /* 0 - Check on DInvoiceTSMNumber (Payment Reference)                    */
        /* ===================================================================== */
        if tBankImpLineDet.BankImpLinePaymentRef <> '' and
           tBankImpLineDet.BankImpLinePaymentRef <> ?
        then do:
            for each tqDInvoiceByDebtorRef where
                     tqDInvoiceByDebtorRef.tcDInvoiceTSMNumber = tBankImpLineDet.BankImpLinePaymentRef
                     by tqDInvoiceByDebtorRef.ttDInvoiceDueDate: 
                /* record before accumlate amount */
                assign vdInvoiceAllocatedAmount = 0.
                    
                /* check whether the invoice balance is not enough, since in the batch, invoice balance will be changed.*/
                for each bBankImportLine where 
                         bBankImportLine.tlIsProcessed     = true and 
                         bBankImportLine.BankImpLineAction = {&BANKIMPORTACTION-CREATEDDOCUMENT}:
                       <M-26 run GetAllocAmountByInvoice
                          (input  tqDInvoiceByDebtorRef.tiDInvoice_ID (iiInvoiceID), 
                           input  bBankImportLine.DDocument_ID (iiDDocumentID), 
                           input-output vdInvoiceAllocatedAmount (bdAllocAmount), 
                           output viFcReturnSuper (oiReturnStatus)) in BDDocument>
                end.
                <M-48 run GetInvoicesByBankImpLineXref
                   (input-output vdAccumulateAmount (bdAccumulateAmount), 
                    input  False (ilIdentifiedBy10DigitCode), 
                    input  'TRACE0':U (icTrace), 
                    input  vdInvoiceAllocatedAmount (idAllocatedInvoiceAmount), 
                    output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>    
            end. /* for each tqDInvoiceByDebtorRef tqDInvoiceByDebtorRef.tcDInvoiceTSMNumber = tBankImpLineDet.BankImpLinePaymentRef */
        end. /* if tBankImpLineDet.BankImpLinePaymentRef <> '' */
        
        /* Find invoice by Voucher and daybook */
        find first tInvoiceXref no-error.
        if not available tInvoiceXref or
           vdAccumulateAmount <> tBankImpLineDet.BankImpLineAmountTC
        then do:  
            /* ===================================================================== */
            /* 1 - Check on DInvoiceVoucher and the JournalCode                      */
            /* ===================================================================== */
            for each tqDInvoiceByDebtorRef where
                     tBankImpLineDet.BankImpLineInvRefList matches '*' + string(tqDInvoiceByDebtorRef.tiDInvoiceVoucher,'999999999') + '*':U AND
                     tBankImpLineDet.BankImpLineInvRefList matches '*' + tqDInvoiceByDebtorRef.tcJournalCode + '*'
                     by tqDInvoiceByDebtorRef.ttDInvoiceDueDate: 
                /* record before accumlate amount */
                assign vdInvoiceAllocatedAmount = 0.
                                         
                for each bBankImportLine where bBankImportLine.tlIsProcessed = true 
                                            and bBankImportLine.BankImpLineAction = {&BANKIMPORTACTION-CREATEDDOCUMENT}:
                       <M-86 run GetAllocAmountByInvoice
                          (input  tqDInvoiceByDebtorRef.tiDInvoice_ID (iiInvoiceID), 
                           input  bBankImportLine.DDocument_ID (iiDDocumentID), 
                           input-output vdInvoiceAllocatedAmount (bdAllocAmount), 
                           output viFcReturnSuper (oiReturnStatus)) in BDDocument>
                end.
                <M-68 run GetInvoicesByBankImpLineXref
                   (input-output vdAccumulateAmount (bdAccumulateAmount), 
                    input  False (ilIdentifiedBy10DigitCode), 
                    input  'TRACE1':U (icTrace), 
                    input  vdInvoiceAllocatedAmount (idAllocatedInvoiceAmount), 
                    output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>    
            end. /* for each tqDInvoiceByDebtorRef break by tqDInvoiceByDebtorRef.tiDInvoice_ID: */
        end.
                    
        /* Could not find invoice by Voucher and daybook then by voucher only */
        find first tInvoiceXref no-error.
        if not available tInvoiceXref or
           vdAccumulateAmount <> tBankImpLineDet.BankImpLineAmountTC
        then do:  
            /* ===================================================================== */
            /* 2 - Check on DInvoiceVoucher                                          */
            /* ===================================================================== */
            for each tqDInvoiceByDebtorRef where
                     tBankImpLineDet.BankImpLineInvRefList matches '*' + string(tqDInvoiceByDebtorRef.tiDInvoiceVoucher,'999999999') + '*':U
                     by tqDInvoiceByDebtorRef.ttDInvoiceDueDate:
                /* record before accumlate amount */
                assign vdInvoiceAllocatedAmount = 0.
                 
                 /* check whether the invoice balance is not enough, since in the batch, invoice balance will be changed.*/
                 for each bBankImportLine where bBankImportLine.tlIsProcessed = true 
                                            and bBankImportLine.BankImpLineAction = {&BANKIMPORTACTION-CREATEDDOCUMENT}:
                       <M-27 run GetAllocAmountByInvoice
                          (input  tqDInvoiceByDebtorRef.tiDInvoice_ID (iiInvoiceID), 
                           input  bBankImportLine.DDocument_ID (iiDDocumentID), 
                           input-output vdInvoiceAllocatedAmount (bdAllocAmount), 
                           output viFcReturnSuper (oiReturnStatus)) in BDDocument>
                 end.
                 <M-52 run GetInvoicesByBankImpLineXref
                    (input-output vdAccumulateAmount (bdAccumulateAmount), 
                     input  False (ilIdentifiedBy10DigitCode), 
                     input  'TRACE2':U (icTrace), 
                     input  vdInvoiceAllocatedAmount (idAllocatedInvoiceAmount), 
                     output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>    
            end. /* each tqDInvoiceByDebtorRef */
        end.
    
        /* Could not find invoice by voucher then by DInvoiceDIText */
        find first tInvoiceXref no-error.
        if not available tInvoiceXref or
           vdAccumulateAmount <> tBankImpLineDet.BankImpLineAmountTC
        then do:   
                 
            /* ===================================================================== */
            /* 3 - Check on DInvoiceDIText                                           */
            /* ===================================================================== */
            for each tqDInvoiceByDebtorRef where
                     tBankImpLineDet.BankImpLineInvRefList matches '*' + tqDInvoiceByDebtorRef.tcDInvoiceDIText + '*':U
                     by tqDInvoiceByDebtorRef.ttDInvoiceDueDate:
                 /* record before accumlate amount */
                assign vdInvoiceAllocatedAmount = 0.
                /* check whether the invoice balance is not enough, since in the batch, invoice balance will be changed.*/
                for each bBankImportLine where bBankImportLine.tlIsProcessed = true 
                                            and bBankImportLine.BankImpLineAction = {&BANKIMPORTACTION-CREATEDDOCUMENT}:
                    <M-50 run GetAllocAmountByInvoice
                       (input  tqDInvoiceByDebtorRef.tiDInvoice_ID (iiInvoiceID), 
                        input  bBankImportLine .DDocument_ID (iiDDocumentID), 
                        input-output vdInvoiceAllocatedAmount (bdAllocAmount), 
                        output viFcReturnSuper (oiReturnStatus)) in BDDocument>
                end.                  
                <M-42 run GetInvoicesByBankImpLineXref
                   (input-output vdAccumulateAmount (bdAccumulateAmount), 
                    input  False (ilIdentifiedBy10DigitCode), 
                    input  'TRACE3':U (icTrace), 
                    input  vdInvoiceAllocatedAmount (idAllocatedInvoiceAmount), 
                    output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>                
            end. /* for each tqDInvoiceByDebtorRef */
        end.
            
        /* ===================================================================== */
        /* 4 - Check on PaySelPayRef                                             */
        /* ===================================================================== */
        <Q-74 run DPaySelPayRefByCode (all) (Read) (NoCache)
           (input tBankImpLineDet.BankImpLineInvRefList, (Code)
            input ?, (CompanyId)
            output dataset tqDPaySelPayRefByCode) in BDPaymentSelection>
        
        find first tqDPaySelPayRefByCode where 
                   tqDPaySelPayRefByCode.tcDPaySelPayRefCode = tBankImpLineDet.BankImpLineInvRefList 
                   no-error.

        if available tqDPaySelPayRefByCode 
        then do:   
            /* create the tDInvoiceStageDetails if it is a staged payment */                            
            if tqDPaySelPayRefByCode.tcDPaySelLineObjectType = {&PAYMENTSELECTIONTYPE-INVOICE}
            then do:
                find first tqDInvoiceByDebtorRef where 
                           tqDInvoiceByDebtorRef.tiDInvoice_ID = tqDPaySelPayRefByCode.tiDPaySelLineParentObject_ID
                           no-error.
                if available tqDInvoiceByDebtorRef   
                then do:
                    if tqDPaySelPayRefByCode.tiDPaySelLineStg_id <> 0 and tqDPaySelPayRefByCode.tiDPaySelLineStg_id <> ?
                    then do:
                        create tDInvoiceStageDetails.
                        assign tDInvoiceStageDetails.tcDPaySelpayRefCode = tqDPaySelPayRefByCode.tcDPaySelPayRefCode
                               tDInvoiceStageDetails.tiDInvoiceId = tqDPaySelPayRefByCode.tiDPaySelLineParentObject_ID
                               tDInvoiceStageDetails.tiDInvoiceStageID = tqDPaySelPayRefByCode.tiDInvoiceStage_ID.
                     end. /* if tqDPaySelPayRefByCode.tiDPaySelLineStg_id */
                    
                    assign vdInvoiceAllocatedAmount = 0.

                    /* check whether the invoice balance is not enough, since in the batch, invoice balance will be changed.*/
                    for each bBankImportLine where bBankImportLine.tlIsProcessed = true 
                                                and bBankImportLine.BankImpLineAction = {&BANKIMPORTACTION-CREATEDDOCUMENT}:
                           <M-61 run GetAllocAmountByInvoice
                              (input  tqDInvoiceByDebtorRef.tiDInvoice_ID (iiInvoiceID), 
                               input  bBankImportLine.DDocument_ID (iiDDocumentID), 
                               input-output vdInvoiceAllocatedAmount (bdAllocAmount), 
                               output viFcReturnSuper (oiReturnStatus)) in BDDocument>
                    end.
                    <M-11 run GetInvoicesByBankImpLineXref
                       (input-output vdAccumulateAmount (bdAccumulateAmount), 
                        input  true (ilIdentifiedBy10DigitCode), 
                        input  'TRACE4':U (icTrace), 
                        input  vdInvoiceAllocatedAmount (idAllocatedInvoiceAmount), 
                        output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>
                end. /* if available tqDInvoiceByDebtorRef */    

            end. /* if tqDPaySelPayRefByCode.tcDPaySelLineObjectType = {&PAYMENTSELECTIONTYPE-INVOICE} */    
        end.  /* if available tqDPaySelPayRefByCode */
                     
        /* =================================================== */
        /* match the invoice to open invoices                  */
        /* =================================================== */
        if vdAccumulateAmount <> tBankImpLineDet.BankImpLineAmountTC
        then do:        
            assign vcMessage = trim(substitute( #T-6'The total open balance (&1) of the invoices you selected does not equal the amount (&2) of this transaction line.':255(457855476)T-6#,
                                                vdAccumulateAmount,tBankImpLineDet.BankImpLineAmountTC)).
            <M-7 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  'BIL':U + tBankImpLineDet.tc_Rowid (icRowid), 
                input  'QadFin-7669':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>
                
            if icNoInvoiceAction = {&PAYCONFNOINVOICEACT-CREATE-PREPAY} and
               vdAccumulateAmount < tBankImpLineDet.BankImpLineAmountTC          
            then do:
                assign odUnBalancedAmount   = tBankImpLineDet.BankImpLineAmountTC - vdAccumulateAmount.
            end.
            else empty temp-table tInvoiceXref.

            assign oiReturnStatus = -1
                   tBankImpLineDet.BankImpLineProcessedStatus = {&BANKIMPPROCSTATUS-PROCESSED-ERROR}.
        end.
    end. /* for each tBankImpLineDet */
    if oiReturnStatus = -1 then return.
end. /* if tBankImpLine.BankImpLineInvRefList <> "":U and tBankImpLine.BankImpLineInvRefList <> ? */

/* ================================================================================================================= */
/* If we can not get invoices by invoice reference then get Invoice by open amonunt of the Bank Import Line          */
/* ================================================================================================================= */
find first tInvoiceXref no-error.

if not available tInvoiceXref
then do:
    <Q-2 run DInvoiceByDebtorRef (all) (Read) (NoCache)
       (input if ilIncludeAllEntities then ? else iiCompanyId, (CompanyId)
        input tBankImpLine.Debtor_ID, (DebtorId)
        input ?, (DInvoiceRefList)
        input true, (DInvoiceIsOpen)
        input ?, (DebitTCAmount)
        input iiBankNumberId, (BankNumberId)
        input iiCurrencyId, (CurrencyId)
        input ?, (Voucher)
        input ?, (DInvoiceTSMNumber)
        output dataset tqDInvoiceByDebtorRef) in BDInvoice>
    
    find first tqDInvoiceByDebtorRef no-error.
    if available tqDInvoiceByDebtorRef 
    then assign vcCriteria = "DebtorCode = " + tqDInvoiceByDebtorRef.tcDebtorCode + 
                             ";CurrencyCode = " + tqDInvoiceByDebtorRef.tcCurrencyCode + 
                             ";BankNumber = " + tqDInvoiceByDebtorRef.tcBankNumber.

    for each tqDInvoiceByDebtorRef 
        by tqDInvoiceByDebtorRef.ttDInvoiceDueDate:
        assign vdInvoiceAllocatedAmount = 0.

        /* check whether the invoice balance is not enough, since in the batch, invoice balance will be changed.*/
        for each bBankImportLine where bBankImportLine.tlIsProcessed = true 
                                                and bBankImportLine.BankImpLineAction = {&BANKIMPORTACTION-CREATEDDOCUMENT}:
            <M-37 run GetAllocAmountByInvoice
               (input  tqDInvoiceByDebtorRef.tiDInvoice_ID (iiInvoiceID), 
                input  bBankImportLine.DDocument_ID (iiDDocumentID), 
                input-output vdInvoiceAllocatedAmount (bdAllocAmount), 
                output viFcReturnSuper (oiReturnStatus)) in BDDocument>
        end.
        find first tInvoiceXref where 
                   tInvoiceXref.tiDInvoiceId = tqDInvoiceByDebtorRef.tiDInvoice_ID 
                   no-error.
        if not available tInvoiceXref
        then do:
            assign vdInvoiceAmount = abs(tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC - tqDInvoiceByDebtorRef.tdDInvoiceOriginalCreditTC)
                   vdInvoiceBalance= abs(tqDInvoiceByDebtorRef.tdDInvoiceBalanceDebitTC - tqDInvoiceByDebtorRef.tdDInvoiceBalanceCreditTC - vdInvoiceAllocatedAmount)
                   vdVatTC         = if tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC <> 0 and tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC <> ?
                                     then tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC else tqDInvoiceByDebtorRef.tdDInvoiceVatCreditTC.

            /* Calculate the discount amount */
            if tqDInvoiceByDebtorRef.tdPaymentConditionPercentage <> 0
            then do:
                assign vdDiscountBase         =   vdInvoiceBalance 
                       vdDiscountAmount       =   if tqDInvoiceByDebtorRef.tlDInvoiceIsDiscTaxAtPaym = true then vdDiscountBase *
                                                  tqDInvoiceByDebtorRef.tdPaymentConditionPercentage / 100
                                                  else (vdInvoiceBalance - vdVatTC * vdInvoiceBalance / vdInvoiceAmount) * 
                                                  tqDInvoiceByDebtorRef.tdPaymentConditionPercentage / 100
                       vdInvoicePaymentAmount =   vdInvoiceBalance - vdDiscountAmount.
                                                 
            end.
            else do:
                assign vdDiscountAmount       = 0
                       vdInvoicePaymentAmount = vdInvoiceBalance.
            end.
        end. /* if first-of (tqDInvoiceByDebtorRef.tiDInvoice_ID) */
        
        assign vdDefaultDiscountTC = vdDiscountAmount *
                                     ((tqDInvoiceByDebtorRef.tdDInvoiceBankToPayTC - vdInvoiceAllocatedAmount) /
                                     vdInvoiceBalance)
               vdDefaultPaymentTC  =  vdInvoicePaymentAmount *
                                     ((tqDInvoiceByDebtorRef.tdDInvoiceBankToPayTC - vdInvoiceAllocatedAmount) /
                                      vdInvoiceBalance).
        /* Roundind the amount */
        assign vdDefaultDiscountTC = <M-12 RoundAmount
                                        (input  vdDefaultDiscountTC (idUnroundedAmount), 
                                         input  ? (iiCurrencyID), 
                                         input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine>
               vdDefaultPaymentTC  = <M-13 RoundAmount
                                        (input  vdDefaultPaymentTC (idUnroundedAmount), 
                                         input  ? (iiCurrencyID), 
                                         input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine>.

        if vdDefaultPaymentTC = tBankImpLine.BankImpLineAmountTC
        then do:
            find first tInvoiceXref where 
                       tInvoiceXref.tiDInvoiceBankId = tqDInvoiceByDebtorRef.tiDInvoiceBank_ID 
                       no-error.
            if not available tInvoiceXref
            then do:
                create tInvoiceXref.
                assign tInvoiceXref.tiDInvoiceId             = tqDInvoiceByDebtorRef.tiDInvoice_ID
                       tInvoiceXref.tiDInvoiceBankId         = tqDInvoiceByDebtorRef.tiDInvoiceBank_ID
                       tInvoiceXref.tdInvoiceRefDiscTC       = vdDefaultDiscountTC
                       tInvoiceXref.tdInvoiceRefAlloTC       = vdDefaultPaymentTC + tInvoiceXref.tdInvoiceRefDiscTC
                       tInvoiceXref.tcInvoiceRefCurrencyCode = tqDInvoiceByDebtorRef.tcCurrencyCode
                       tInvoiceXref.tcAmountCrDt             = if tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> 0 and
                                                               tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> ?
                                                               then {&CREDITDEBITABBREVIATION-CREDIT}
                                                               else {&CREDITDEBITABBREVIATION-DEBIT}.
                leave.
            end. /* if not available tInvoiceXref */
        end. /* if vdDefaultPaymentTC = tBankImpLine.BankImpLineAmountTC */
    end. /* for each tqDInvoiceByDebtorRef by tqDInvoiceByDebtorRef.ttDInvoiceDueDate:*/
end. /* if not available tInvoiceXref */

/* =================================================================================================== */
/* If we can not get invoice by reference or exactly amount then Get Invoice(s) of debtor and try to   */
/* calculate open amount from oldest one to match paid amount                                          */
/* =================================================================================================== */
find first tInvoiceXref no-error.
if not available tInvoiceXref
then do:
    <Q-3 run DInvoiceByDebtorRef (all) (Read) (NoCache)
       (input if ilincludeAllEntities then ? else iiCompanyId, (CompanyId)
        input tBankImpLine.Debtor_ID, (DebtorId)
        input ?, (DInvoiceRefList)
        input true, (DInvoiceIsOpen)
        input ?, (DebitTCAmount)
        input iiBankNumberId, (BankNumberId)
        input iiCurrencyId, (CurrencyId)
        input ?, (Voucher)
        input ?, (DInvoiceTSMNumber)
        output dataset tqDInvoiceByDebtorRef) in BDInvoice>
    
    find first tqDInvoiceByDebtorRef no-error.
    if available tqDInvoiceByDebtorRef 
    then assign vcCriteria = "DebtorCode = " + tqDInvoiceByDebtorRef.tcDebtorCode + 
                             ";CurrencyCode = " + tqDInvoiceByDebtorRef.tcCurrencyCode + 
                             ";BankNumber = " + tqDInvoiceByDebtorRef.tcBankNumber.

    for each tqDInvoiceByDebtorRef 
        by tqDInvoiceByDebtorRef.ttDInvoiceDueDate:
        assign vdInvoiceAllocatedAmount = 0.

        /* check whether the invoice balance is not enough, since in the batch, invoice balance will be changed.*/
        for each bBankImportLine where bBankImportLine.tlIsProcessed = true 
                                                and bBankImportLine.BankImpLineAction = {&BANKIMPORTACTION-CREATEDDOCUMENT}:
            <M-32 run GetAllocAmountByInvoice
               (input  tqDInvoiceByDebtorRef.tiDInvoice_ID (iiInvoiceID), 
                input  bBankImportLine.DDocument_ID (iiDDocumentID), 
                input-output vdInvoiceAllocatedAmount (bdAllocAmount), 
                output viFcReturnSuper (oiReturnStatus)) in BDDocument>
        end.        
        find first tInvoiceXref where 
                   tInvoiceXref.tiDInvoiceId = tqDInvoiceByDebtorRef.tiDInvoice_ID 
                   no-error.

        if not available tInvoiceXref
        then do:
            assign vdInvoiceAmount = abs(tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC - 
                                         tqDInvoiceByDebtorRef.tdDInvoiceOriginalCreditTC)
                   vdInvoiceBalance= abs(tqDInvoiceByDebtorRef.tdDInvoiceBalanceDebitTC - 
                                         tqDInvoiceByDebtorRef.tdDInvoiceBalanceCreditTC - vdInvoiceAllocatedAmount )
                   vdVatTC         = if tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC <> 0 and tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC <> ?
                                     then tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC else tqDInvoiceByDebtorRef.tdDInvoiceVatCreditTC.

            if tqDInvoiceByDebtorRef.tdPaymentConditionPercentage <> 0
            then do:
                assign vdDiscountBase         =   vdInvoiceBalance  
                       vdDiscountAmount       =   if tqDInvoiceByDebtorRef.tlDInvoiceIsDiscTaxAtPaym = true then vdDiscountBase *
                                                  tqDInvoiceByDebtorRef.tdPaymentConditionPercentage / 100
                                                  else (vdInvoiceBalance - vdVatTC * vdInvoiceBalance / vdInvoiceAmount) * 
                                                  tqDInvoiceByDebtorRef.tdPaymentConditionPercentage / 100
                       vdInvoicePaymentAmount =   vdInvoiceBalance - vdDiscountAmount.
                                     
            end.
            else do:
                assign vdDiscountAmount       = 0
                       vdInvoicePaymentAmount = vdInvoiceBalance.
            end.
        end. /* if first-of (tqDInvoiceByDebtorRef.tiDInvoice_ID) */

        assign vdDefaultDiscountTC = vdDiscountAmount *
                                     ((tqDInvoiceByDebtorRef.tdDInvoiceBankToPayTC - vdInvoiceAllocatedAmount) /
                                     vdInvoiceBalance)
               vdDefaultPaymentTC  =  vdInvoicePaymentAmount *
                                     ((tqDInvoiceByDebtorRef.tdDInvoiceBankToPayTC - vdInvoiceAllocatedAmount) /
                                      vdInvoiceBalance).
        /* Roundind the amount */
        assign vdDefaultDiscountTC = <M-14 RoundAmount
                                        (input  vdDefaultDiscountTC (idUnroundedAmount), 
                                         input  ? (iiCurrencyID), 
                                         input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine>
               vdDefaultPaymentTC  = <M-15 RoundAmount
                                        (input  vdDefaultPaymentTC (idUnroundedAmount), 
                                         input  ? (iiCurrencyID), 
                                         input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine>.

        find first tInvoiceXref where 
                   tInvoiceXref.tiDInvoiceBankId = tqDInvoiceByDebtorRef.tiDInvoiceBank_ID 
                   no-error.
        if not available tInvoiceXref
        then do:
            create tInvoiceXref.
            assign tInvoiceXref.tiDInvoiceId             = tqDInvoiceByDebtorRef.tiDInvoice_ID
                   tInvoiceXref.tiDInvoiceBankId         = tqDInvoiceByDebtorRef.tiDInvoiceBank_ID
                   tInvoiceXref.tdInvoiceRefDiscTC       = vdDefaultDiscountTC
                   tInvoiceXref.tdInvoiceRefAlloTC       = vdDefaultPaymentTC +  vdDefaultDiscountTC
                   tInvoiceXref.tcInvoiceRefCurrencyCode = tqDInvoiceByDebtorRef.tcCurrencyCode
                   tInvoiceXref.tcAmountCrDt             = if tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> 0 and
                                                              tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> ?
                                                           then {&CREDITDEBITABBREVIATION-CREDIT}
                                                           else {&CREDITDEBITABBREVIATION-DEBIT}.
            /* Accumulata the amount */
            assign vdAccumulateAmount = vdAccumulateAmount + vdDefaultPaymentTC.
            
            /* if AccumulatAmount equals amount in bank import Line then leave */
            if vdAccumulateAmount = tBankImpLine.BankImpLineAmountTC 
            then leave.
            else 
            /* if AccumulatAmount more than  amount in bank import Line then empty the temp-table and leave */
            if vdAccumulateAmount > tBankImpLine.BankImpLineAmountTC
            then do:
                empty temp-table tInvoiceXref.
                leave.
            end.
        end. /* find first tInvoiceXref where tInvoiceXref.tiDInvoiceBankId = tqDInvoiceByDebtorRef.tiDInvoiceBank_ID no-error. */
    end. /*  for each tqDInvoiceByDebtorRef break by tqDInvoiceByDebtorRef.tiDInvoice_ID by tqDInvoiceByDebtorRef.ttDInvoiceDueDate:*/
    
    if vdAccumulateAmount <> tBankImpLine.BankImpLineAmountTC  
    then do:
        if icNoInvoiceAction = {&PAYCONFNOINVOICEACT-CREATE-PREPAY} and
            vdAccumulateAmount < tBankImpLine.BankImpLineAmountTC
        then do:
            assign odUnBalancedAmount    =  tBankImpLine.BankImpLineAmountTC - vdAccumulateAmount
                   oiReturnStatus        = -1.
        end.
        else empty temp-table tInvoiceXref.
    end.
end. /* if not available tInvoiceXref */

/* =================================================================================================== */
/* Raise the error if can not find invoices of the customer                                            */
/* =================================================================================================== */
find first tInvoiceXref no-error.
if not available tInvoiceXref
then do:
    assign vcMessage = trim( #T-4'There are no open invoices for this customer.':255(68527)T-4#).
    <M-5 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'QadFin-7590':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>
    assign oiReturnStatus = -1.
end.
else do:
    assign vcParam = '':U.
    for each tInvoiceXref:
        find first tqDInvoiceByDebtorRef where tqDInvoiceByDebtorRef.tiDInvoice_ID = tInvoiceXref.tiDInvoiceId no-error.
        if available tqDInvoiceByDebtorRef
        then assign vcParam = vcParam + "Inv(InvoiceNbr=" + tqDInvoiceByDebtorRef.tcDInvoiceDIText +
                              ";Year=" + string(tqDInvoiceByDebtorRef.tiDInvoicePostingYear) + 
                              ";JournalCode=" + tqDInvoiceByDebtorRef.tcJournalCode + 
                              ";Voucher=" + string(tqDInvoiceByDebtorRef.tiDInvoiceVoucher)+ 
                              ";CurrencyCode=" + tqDInvoiceByDebtorRef.tcCurrencyCode + ");".
    end.
end.

/* Create processing info */
<M-36 run CreateBankImpLineProcessInfo
   (input  'DInvoiceByDebtorRef':U (icProcessName), 
    input  #T-77'Get customer invoice info':255(337140028)T-77# (icProcessDesc), 
    input  if oiReturnStatus = -1 then '':U else vcParam (icProcessResult), 
    input  if oiReturnStatus = -1 then '':U else vcCriteria (icProcessParam), 
    input  'GetInvoicesByBankImpLine':U (icParentRowName), 
    input  oiReturnStatus (iiProcessStatus), 
    output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>

if oiReturnStatus = -98 
then assign oiReturnStatus = 0
            odUnBalancedAmount = 0.