project QadFinancials > class BBankImportLine > method GetInvoicesByBankImpLineXref

Description

This method is extended sub-method of the GetInvoiceByBankImpLine methods.
It creates temp tInvoiceXref record for customer payment allocation.

By default, it calcuates InvoiceXref alloc amount and discount amount based on invoice opening balance in full pay case.
However, if calcuated amount < BankImpLineAmount for Debtor Invoice or
calculated amount > BankImpLineAmount for Creditor Invoice
It takes BankImpLineAmount as InvoiceXref alloc amount and assign discount amount as 0.


Parameters


bdAccumulateAmountinput-outputdecimalAccumulated Invoice Allocation Amount
ilIdentifiedBy10DigitCodeinputlogicalSet to true if this invoice was identified by it's unique 10 digit code which is created during Customer Payment Selection Create - Initial
icTraceinputcharacter
idAllocatedInvoiceAmountinputdecimal
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankImportLine.GetInvoicesByBankImpLine


program code (program6/bbankimportline.p)

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

assign vlIsStagedPayment = false.

find first tInvoiceXref where 
           tInvoiceXref.tiDInvoiceId = tqDInvoiceByDebtorRef.tiDInvoice_ID 
           no-error.

if not available tInvoiceXref
then do:
    /* Get the Original and Balance Amounts in TC */
    assign vdInvoiceAmount  = abs(tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC - tqDInvoiceByDebtorRef.tdDInvoiceOriginalCreditTC)
           vdInvoiceBalance = abs(tqDInvoiceByDebtorRef.tdDInvoiceBalanceDebitTC - tqDInvoiceByDebtorRef.tdDInvoiceBalanceCreditTC - idAllocatedInvoiceAmount)
           vdVatTC          = if tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC <> 0 and tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC <> ?
                              then tqDInvoiceByDebtorRef.tdDInvoiceVatDebitTC 
                              else tqDInvoiceByDebtorRef.tdDInvoiceVatCreditTC.    
                                 
    /*if this is paying a stage */
    find first tDInvoiceStageDetails where 
               tDInvoiceStageDetails.tiDInvoiceId = tqDInvoiceByDebtorRef.tiDInvoice_ID 
               no-error.

    if available tDInvoiceStageDetails 
    then do:
          assign vlIsStagedPayment = true.
          <M-81 run GetInvoiceStagesByBankImpLineXref
             (input-output bdAccumulateAmount (bdAccumulateAmount), 
              output vdDiscountAmount (odDiscountAmount), 
              output vdInvoicePaymentAmount (odInvoicePaymentAmount), 
              output viFcReturnSuper (oiReturnStatus)) in BBankImportLine>   

    end. /* if available tDInvoiceStageDetails */
    else do:
        /* 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 
            if not ilIdentifiedby10DigitCode             
            then 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.  
    end. /* else do */    

end. /* if not available tInvoiceXref */

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.tcInvoiceRefCurrencyCode= tqDInvoiceByDebtorRef.tcCurrencyCode
           tInvoiceXref.tcAmountCrDt            = if tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> 0 and
                                                     tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> ?
                                                  then {&CREDITDEBITABBREVIATION-CREDIT}
                                                  else {&CREDITDEBITABBREVIATION-DEBIT}
           tInvoiceXref.tdInvoiceRefDiscTC      = vdDiscountAmount *
                                                 ((tqDInvoiceByDebtorRef.tdDInvoiceBankToPayTC - idAllocatedInvoiceAmount) /
                                                 vdInvoiceBalance)
           tInvoiceXref.tdInvoiceRefDiscTC      = <M-82 RoundAmount
                                                     (input  tInvoiceXref.tdInvoiceRefDiscTC (idUnroundedAmount), 
                                                      input  ? (iiCurrencyID), 
                                                      input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine>
           tInvoiceXref.tdInvoiceRefAlloTC      = vdInvoicePaymentAmount *
                                                 ((tqDInvoiceByDebtorRef.tdDInvoiceBankToPayTC - idAllocatedInvoiceAmount) /
                                                 vdInvoiceBalance)
                                                        
           tInvoiceXref.tdInvoiceRefAlloTC = <M-84 RoundAmount
                                                (input  tInvoiceXref.tdInvoiceRefAlloTC (idUnroundedAmount), 
                                                 input  ? (iiCurrencyID), 
                                                 input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine>
           tInvoiceXref.tdInvoiceRefIntTC = tBankImpLineDet.BankImpLineIntAmtTC
           tInvoiceXref.tdInvoiceRefIntTC = <M-77 RoundAmount
                                               (input  tInvoiceXref.tdInvoiceRefIntTC (idUnroundedAmount), 
                                                input  ? (iiCurrencyID), 
                                                input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine>.
            
    /* do not need to do this calculation if we are paying a staged payment - because this works on the outstanding balance
       of the entire invoice */
   if not vlIsStagedPayment 
    then do:            
        if tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC - tqDInvoiceByDebtorRef.tdDInvoiceOriginalCreditTC > 0
        then do:
            if tBankImpLineDet.BankImpLineAmountTC - tInvoiceXref.tdInvoiceRefAlloTC - tBankImpLineDet.BankImpLineIntAmtTC < 0 
            then do : 
                if vdTotalAllocatedAmount <> 0  and 
                   vdTotalAllocatedAmount - tBankImpLineDet.BankImpLineIntAmtTC <> tBankImpLineDet.BankImpLineAmountTC 
                then do:                                                        
                    assign tInvoiceXref.tdInvoiceRefDiscTC =  tInvoiceXref.tdInvoiceRefDiscTC + tBankImpLine.BankImpLineDiscAmtTC           
                           tInvoiceXref.tdInvoiceRefAlloTC =  tBankImpLineDet.BankImpLineAmountTC - tBankImpLineDet.BankImpLineIntAmtTC
                           tInvoiceXref.tdInvoiceRefAlloTC = <M-34 RoundAmount
                                                                (input  tInvoiceXref.tdInvoiceRefAlloTC (idUnroundedAmount), 
                                                                 input  ? (iiCurrencyID), 
                                                                 input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine>.            
                end.
            end.
        end.
        else if tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC - tqDInvoiceByDebtorRef.tdDInvoiceOriginalCreditTC  < 0
        then do:
            if tBankImpLineDet.BankImpLineAmountTC + tInvoiceXref.tdInvoiceRefAlloTC + tBankImpLineDet.BankImpLineIntAmtTC > 0 
            then do : 
                if vdTotalAllocatedAmount <> 0 and  
                   vdTotalAllocatedAmount -  tBankImpLineDet.BankImpLineIntAmtTC <> tBankImpLineDet.BankImpLineAmountTC  
                then do:
                    assign tInvoiceXref.tdInvoiceRefDiscTC =  tInvoiceXref.tdInvoiceRefDiscTC + tBankImpLine.BankImpLineDiscAmtTC
                           tInvoiceXref.tdInvoiceRefAlloTC = - tBankImpLineDet.BankImpLineAmountTC + tBankImpLineDet.BankImpLineIntAmtTC
                           tInvoiceXref.tdInvoiceRefAlloTC = <M-29 RoundAmount
                                                                (input  tInvoiceXref.tdInvoiceRefAlloTC (idUnroundedAmount), 
                                                                 input  ? (iiCurrencyID), 
                                                                 input  tqDInvoiceByDebtorRef.tcCurrencyCode (icCurrencyCode)) in BBankImportLine>.
                end.                                                 
            end.
        end.
    end. /* if not available tDInvoiceStageDetails */        
                                      
    assign tInvoiceXref.tdInvoiceRefAlloTC = tInvoiceXref.tdInvoiceRefAlloTC + tInvoiceXref.tdInvoiceRefDiscTC.
    
    if (tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> 0 and tqDInvoiceByDebtorRef.tdDInvoiceOriginalDebitTC <> ?)
    then assign bdAccumulateAmount = bdAccumulateAmount + (tInvoiceXref.tdInvoiceRefAlloTC - tInvoiceXref.tdInvoiceRefDiscTC + tInvoiceXref.tdInvoiceRefIntTC).
    else assign bdAccumulateAmount = bdAccumulateAmount - (tInvoiceXref.tdInvoiceRefAlloTC - tInvoiceXref.tdInvoiceRefDiscTC + tInvoiceXref.tdInvoiceRefIntTC).             
    
end. /* if not available tInvoiceXref */