project QadFinancials > class BDDocument > method GetPossibleAllocationsByBank

Description

Assumption: An invoice can only have one bank row if staged payment condition is used.


Parameters


icAmountOperatorinputcharacter
iiBankNumberIdinputinteger
icBusinessRelationCodeinputcharacter
icCurrencyCodeinputcharacter
icDebtorCodeinputcharacter
icDInvoiceJournalinputcharacter
icDInvoiceReferenceinputcharacter
icPaymentReferenceinputcharacterPaymentReference
idAmountMargininputdecimal
idDInvoiceAmountinputdecimal
iiDInvoiceVoucherinputinteger
iiDInvoiceYearinputinteger
ilAllCompaniesinputlogical
itDDocumentDueDateinputdate
icShipperCodeinputcharacter
icCorporateGroupCodeinputcharacterCorporate Group Code
tDDocumentPossibleAllocationsoutputtemp-table
tDDocPossibleAllocationStagesoutputtemp-table
iiBillYearinputinteger
iiBillNumberinputinteger
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program6/bddocument.p)

if iiBankNumberId = ? or iiBankNumberId = 0
then return.

assign vcDInvoiceShipperInfo = "":U.

if icAmountOperator       = ? then assign icAmountOperator       = "":U.
if icBusinessRelationCode = ? then assign icBusinessRelationCode = "":U.
if icDInvoiceJournal      = ? then assign icDInvoiceJournal      = "":U.
if icDInvoiceReference    = ? then assign icDInvoiceReference    = "":U.
if icPaymentReference     = ? then assign icPaymentReference     = "":U.
if icDebtorCode           = ? then assign icDebtorCode           = "":U.
if icCurrencyCode         = ? then assign icCurrencyCode         = "":U.
if idAmountMargin         = ? then assign idAmountMargin         = 0.
if idDInvoiceAmount       = ? then assign idDInvoiceAmount       = 0.
if iiDInvoiceVoucher      = ? then assign iiDInvoiceVoucher      = 0.
if iiDInvoiceYear         = ? then assign iiDInvoiceYear         = 0.
if ilAllCompanies         = ? then assign ilAllCompanies         = false.
if icShipperCode          = ? then assign icShipperCode          = "":U.
if icCorporateGroupCode   = ? then assign icCorporateGroupCode   = "":U.
if iiBillYear             = ? then assign iiBillYear             = 0.
if iiBillNumber           = ? then assign iiBillNumber           = 0.

if icAmountOperator <> "=":U  and
   icAmountOperator <> "<=":U and
   icAmountOperator <> ">=":U
then do:    

    assign vcMessage = trim(substitute(#T-85'Invalid amount operator. The valid options are &1, &2 and &3.':150(59064)T-85#,'=':U,'<=':U,'>=':U))
           oiReturnStatus = -1.
    <M-8 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-5932':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BDDocument>
    return.
end.

assign vdMinimumAmount = if icAmountOperator = "=":U
                         then idDInvoiceAmount - abs(idAmountMargin)
                         else
                         if icAmountOperator = "<=":U
                         then 0.0001
                         else idDInvoiceAmount
       vdMaximumAmount = if icAmountOperator = "=":U
                         then idDInvoiceAmount + abs(idAmountMargin)
                         else
                         if icAmountOperator = "<=":U
                         then idDInvoiceAmount
                         else 999999999999999.9999
       viA             = 0.
       
empty temp-table tDDocumentPossibleAllocations.
empty temp-table tDDocPossibleAllocationStages.

<Q-12 run BankNumberByIDAllInfo (all) (Read) (NoCache)
   (input iiBankNumberId, (BankNumber_ID)
    input ?, (BankNumberIsDefault)
    output dataset tqBankNumberByIDAllInfo) in BBankNumber>

find first tqBankNumberByIDAllInfo no-error.
if available tqBankNumberByIDAllInfo
then do:
    assign viBankPayFormatId = tqBankNumberByIDAllInfo.tiBankPayFormat_Id.
end.

if icDebtorCode <> "":U and 
   icDebtorCode <> ?
then do:
    <Q-13 run DebtorPrim (all) (Read) (NoCache)
       (input viCompanyId, (CompanyId)
        input icDebtorCode, (DebtorCode)
        input ?, (Debtor_ID)
        output dataset tqDebtorPrim) in BDebtor >
    find first tqDebtorPrim no-error.
    if available tqDebtorPrim
    then do:
        assign viDebtorId = tqDebtorPrim.tiDebtor_ID.
    end.
end.

<Q-1 run DInvoiceBankByAllocationInfo (all) (Read) (NoCache)
   (input ?, (BankPayformatId)
    input icBusinessRelationCode, (BusinessRelationCode)
    input icDInvoiceReference, (DInvoiceDescription)
    input icCurrencyCode, (CurrencyCode)
    input icDebtorCode, (DebtorCode)
    input vdMaximumAmount, (DInvoiceAmountTCMaximum)
    input vdMinimumAmount, (DInvoiceAmountTCMinimum)
    input true, (DInvoiceIsOpen)
    input icPaymentReference, (DInvoiceTSMNumber)
    input icDInvoiceJournal, (JournalCode)
    input iiDInvoiceYear, (PeriodYear)
    input ?, (PeriodYearMinimum)
    input iiDInvoiceVoucher, (Voucher)
    input if ilAllCompanies then ? else viCompanyId, (CompanyId)
    input icShipperCode, (ShipperCode)
    input viDebtorId, (DebtorId)
    input icCorporateGroupCode, (CorporateGroupCode)
    input false, (DInvoiceIsSelected)
    input iiBillYear, (BillYear)
    input iiBillNumber, (BillNumber)
    output dataset tqDInvoiceBankByAllocationInfo) in BDInvoice>

assign vcDInvoiceIDList = ""
       viLengthOfQueryString = 0.

for each tqDInvoiceBankByAllocationInfo where
         tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceDebitTC  <> 0 or
         tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> 0
        break by tqDInvoiceBankByAllocationInfo.tiDInvoice_ID:
    
    if first-of (tqDInvoiceBankByAllocationInfo.tiDInvoice_ID)
    then do:
        if vcDInvoiceIDList = ""
        then do:
            assign vcDInvoiceIDList = string(tqDInvoiceBankByAllocationInfo.tiDInvoice_ID).
        end.
        else do:
            assign vcDInvoiceIDList = vcDInvoiceIDList + "," + string(tqDInvoiceBankByAllocationInfo.tiDInvoice_ID).
        end.
        
        /* 40 is the length of Field Name(DInvoiceVat1.DInvoice_ID*/
        viLengthOfQueryString = viLengthOfQueryString + length(string(tqDInvoiceBankByAllocationInfo.tiDInvoice_ID), "CHARACTER") + 40.

        /*To avoid 32K issue for query string, leave 2000 for query skeleton*/
        if viLengthOfQueryString > 8000 then
        do:

           /*Just Read the DInvoiceVat record which DInvoiceVatIsAbsRet = true*/
           <Q-55 run DInvoiceVatRetByDInvoiceId (first) (Append) (NoCache)
              (input vcDInvoiceIDList, (DInvoiceId)
               output dataset tqDInvoiceVatRetByDInvoiceId) in BDInvoice>

           assign vcDInvoiceIDList = ""
                  viLengthOfQueryString = 0.
        end.  
    end.
end.

if vcDInvoiceIDList <> "" and vcDInvoiceIDList <> ? then
do:
   /*When the length of IDList is less than upper limit(9000), 
     or when there exists a remainder of  the division (a/b), execute the query again.*/
   /*Just Read the DInvoiceVat record which DInvoiceVatIsAbsRet = true*/
   <Q-88 run DInvoiceVatRetByDInvoiceId (first) (Append) (NoCache)
      (input vcDInvoiceIDList, (DInvoiceId)
       output dataset tqDInvoiceVatRetByDInvoiceId) in BDInvoice>  
end.

for each tqDInvoiceBankByAllocationInfo where
         tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceDebitTC  <> 0 or
         tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> 0
         break by tqDInvoiceBankByAllocationInfo.tiDInvoice_ID
               by tqDInvoiceBankByAllocationInfo.tcDInvoiceOpInfoType
               by tqDInvoiceBankByAllocationInfo.tcDInvoiceOpInfoCode:

    assign vlStagedPayments = tqDInvoiceBankByAllocationInfo.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}.

    assign vcDInvoiceShipperInfo = if vcDInvoiceShipperInfo = "":U then tqDInvoiceBankByAllocationInfo.tcDInvoiceOpInfoCode
                                   else vcDInvoiceShipperInfo + ",":U + tqDInvoiceBankByAllocationInfo.tcDInvoiceOpInfoCode.
    
    if first-of (tqDInvoiceBankByAllocationInfo.tiDInvoice_ID)
    then do:

        create tDDocumentPossibleAllocations.

        assign viA                                                     = viA - 1
               tDDocumentPossibleAllocations.DInvoice_ID               = tqDInvoiceBankByAllocationInfo.tiDInvoice_ID
               tDDocumentPossibleAllocations.tcAllocAmountCrDt         = if tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> 0 and
                                                                        tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> ?
                                                                         then {&CREDITDEBITABBREVIATION-DEBIT}
                                                                         else {&CREDITDEBITABBREVIATION-CREDIT}
               tDDocumentPossibleAllocations.tcDInvoiceType            = tqDInvoiceBankByAllocationInfo.tcDInvoiceType
               tDDocumentPossibleAllocations.tiBillYear                = if tqDInvoiceBankByAllocationInfo.tiBillYear <> 0 
                                                                         then tqDInvoiceBankByAllocationInfo.tiBillYear 
                                                                         else ?   
               tDDocumentPossibleAllocations.tiBillNumber              = if tqDInvoiceBankByAllocationInfo.tiBillNumber <> 0 
                                                                         then tqDInvoiceBankByAllocationInfo.tiBillNumber
                                                                         else ?
               tDDocumentPossibleAllocations.tcBillStatus              = tqDInvoiceBankByAllocationInfo.tcBillStatus.

        if tDDocumentPossibleAllocations.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} 
        then assign tDDocumentPossibleAllocations.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}.

        if tDDocumentPossibleAllocations.tcDInvoiceType = {&INVOICETYPE-DEDUCTION}
        then do:
            <Q-35 run GetDeductionDetailByInvoiceID (all) (Read) (NoCache)
               (input ?, (CompanyId)
                input tDDocumentPossibleAllocations.DInvoice_ID, (InvoiceID)
                output dataset tqGetDeductionDetailByInvoiceID) in BDInvoice>
        
            for each tqGetDeductionDetailByInvoiceID no-lock:
                assign tDDocumentPossibleAllocations.tdDeductionTC      = tDDocumentPossibleAllocations.tdDeductionTC + tqGetDeductionDetailByInvoiceID.tdDInvoiceDeductionTC
                       tDDocumentPossibleAllocations.tcDeductionCatCode    = if tDDocumentPossibleAllocations.tcDeductionCatCode = '':U then tqGetDeductionDetailByInvoiceID.tcDeductionCatCode
                                                                          else trim(#T-29'See details':20(648722267)T-29#)
                       tDDocumentPossibleAllocations.tcDInvoiceDeductionStatus = if tDDocumentPossibleAllocations.tcDInvoiceDeductionStatus = {&INVOICEDEDUCTSTATUS-REJECTED} then {&INVOICEDEDUCTSTATUS-REJECTED}
                                                               else if tqGetDeductionDetailByInvoiceID.tcDInvoiceDeductionStatus = {&INVOICEDEDUCTSTATUS-REJECTED} then {&INVOICEDEDUCTSTATUS-REJECTED} 
                                                               else if tDDocumentPossibleAllocations.tcDInvoiceDeductionStatus = '':U then tqGetDeductionDetailByInvoiceID.tcDInvoiceDeductionStatus
                                                               else trim(#T-50'See details':20(648722267)T-50#).
                
            end.
        
        end.

        assign tDDocumentPossibleAllocations.tcBusinessRelationCode    = tqDInvoiceBankByAllocationInfo.tcBusinessRelationCode
               tDDocumentPossibleAllocations.tcCorporateGroupCode      = tqDInvoiceBankByAllocationInfo.tcCorporateGroupCode
               tDDocumentPossibleAllocations.tcCompanyCode             = tqDInvoiceBankByAllocationInfo.tcCompanyCode
               tDDocumentPossibleAllocations.tcCurrencyCode            = tqDInvoiceBankByAllocationInfo.tcCurrencyCode
               tDDocumentPossibleAllocations.tcDivisionCode            = tqDInvoiceBankByAllocationInfo.tcDivisionCode
               tDDocumentPossibleAllocations.tcDebtorCode              = tqDInvoiceBankByAllocationInfo.tcDebtorCode
               tDDocumentPossibleAllocations.tcDInvoiceBalanceCrDt     = if tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> 0 and
                                                                            tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> ?
                                                                         then {&CREDITDEBITABBREVIATION-CREDIT}
                                                                         else {&CREDITDEBITABBREVIATION-DEBIT}
               tDDocumentPossibleAllocations.tcDInvoiceReference       = string(tqDInvoiceBankByAllocationInfo.tiPeriodYear)                     + "/":U +
                                                                         tqDInvoiceBankByAllocationInfo.tcJournalCode                            + "/":U +
                                                                         string(tqDInvoiceBankByAllocationInfo.tiDInvoiceVoucher, "999999999":U) + "/":U +
                                                                         tqDInvoiceBankByAllocationInfo.tcDInvoiceDescription
               tDDocumentPossibleAllocations.tcDInvoiceTSMNumber       = tqDInvoiceBankByAllocationInfo.tcDInvoiceTSMNumber
               tDDocumentPossibleAllocations.tcNewBalanceCrDt          = tDDocumentPossibleAllocations.tcDInvoiceBalanceCrDt
               tDDocumentPossibleAllocations.tdDInvoiceBalance         = if tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> 0 and
                                                                            tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> ?
                                                                         then tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC
                                                                         else tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceDebitTC
               tDDocumentPossibleAllocations.tdDiscountPercentage      = if itDDocumentDueDate <= tqDInvoiceBankByAllocationInfo.ttDInvoiceDiscountDueDate
                                                                         then tqDInvoiceBankByAllocationInfo.tdPaymentConditionPercentage
                                                                         else 0
               tDDocumentPossibleAllocations.tdNewBalance              = tDDocumentPossibleAllocations.tdDInvoiceBalance
               tDDocumentPossibleAllocations.tdVatBaseTC               = if tqDInvoiceBankByAllocationInfo.tdDInvoiceVatBaseDebitTC  <> 0 and tqDInvoiceBankByAllocationInfo.tdDInvoiceVatBaseDebitTC  <> ? then tqDInvoiceBankByAllocationInfo.tdDInvoiceVatBaseDebitTC  else tqDInvoiceBankByAllocationInfo.tdDInvoiceVatBaseCreditTC
               tDDocumentPossibleAllocations.tcVatBaseCrDt             = if tqDInvoiceBankByAllocationInfo.tdDInvoiceVatBaseDebitTC  <> 0 and tqDInvoiceBankByAllocationInfo.tdDInvoiceVatBaseDebitTC  <> ? then {&CREDITDEBITABBREVIATION-DEBIT}                     else {&CREDITDEBITABBREVIATION-CREDIT}
               tDDocumentPossibleAllocations.tdVatTC                   = if tqDInvoiceBankByAllocationInfo.tdDInvoiceVatDebitTC      <> 0 and tqDInvoiceBankByAllocationInfo.tdDInvoiceVatDebitTC      <> ? then tqDInvoiceBankByAllocationInfo.tdDInvoiceVatDebitTC      else tqDInvoiceBankByAllocationInfo.tdDInvoiceVatCreditTC
               tDDocumentPossibleAllocations.tcVatCrDt                 = if tqDInvoiceBankByAllocationInfo.tdDInvoiceVatDebitTC      <> 0 and tqDInvoiceBankByAllocationInfo.tdDInvoiceVatDebitTC      <> ? then {&CREDITDEBITABBREVIATION-DEBIT}                     else {&CREDITDEBITABBREVIATION-CREDIT}
               tDDocumentPossibleAllocations.tiDInvoicePostingId       = tqDInvoiceBankByAllocationInfo.tiPosting_ID
               
               tDDocumentPossibleAllocations.ttDInvoiceDate            = tqDInvoiceBankByAllocationInfo.ttDInvoiceDate
               tDDocumentPossibleAllocations.ttDInvoiceDiscountDueDate = tqDInvoiceBankByAllocationInfo.ttDInvoiceDiscountDueDate
               tDDocumentPossibleAllocations.ttDInvoiceDueDate         = tqDInvoiceBankByAllocationInfo.ttDInvoiceDueDate
               tDDocumentPossibleAllocations.tc_Rowid                  = string(viA)
               tDDocumentPossibleAllocations.tlDiscountTaxAtPayment    = tqDInvoiceBankByAllocationInfo.tlDInvoiceIsDiscTaxAtPaym
               tDDocumentPossibleAllocations.tcDInvoiceDIText          = tqDInvoiceBankByAllocationInfo.tcDInvoiceDIText
               vdInvoiceAmount                                         = if tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> 0 and
                                                                            tqDInvoiceBankByAllocationInfo.tdDInvoiceBalanceCreditTC <> ?
                                                                         then tqDInvoiceBankByAllocationInfo.tdDInvoiceOriginalCreditTC
                                                                         else tqDInvoiceBankByAllocationInfo.tdDInvoiceOriginalDebitTC
                                                                   
               vdDefaultDiscountAmount                                 = 0
               vdDefaultPaymentAmount                                  = 0
               vdInvoiceTotalTC                                        = 0
               vlProposePayment                                        = false
               vlDInvoiceIsWithOneBank                                 = false   
               tDDocumentPossibleAllocations.tdNonDiscAmtTC =  tqDInvoiceBankByAllocationInfo.tdDInvoiceNonDiscAmtTC.
        
        if can-find (first tqDInvoiceVatRetByDInvoiceId where 
                           tqDInvoiceVatRetByDInvoiceId.tiDInvoice_ID = tqDInvoiceBankByAllocationInfo.tiDInvoice_ID)
        then do:
            assign tDDocumentPossibleAllocations.tdVatTC = tqDInvoiceBankByAllocationInfo.tdDInvoiceVatCreditTC - tqDInvoiceBankByAllocationInfo.tdDInvoiceVatDebitTC.
        end.
        
        if vlStagedPayments = false
        then do:
            if last-of (tqDInvoiceBankByAllocationInfo.tiDInvoice_ID)
            then do:
                assign vlDInvoiceIsWithOneBank = true.
            end.
            else do:
                <Q-2 run DInvoiceBankByDInvoiceID (all) (Read) (NoCache)
                   (input tDDocumentPossibleAllocations.DInvoice_ID, (DInvoiceId)
                    output dataset tqDInvoiceBankByDInvoiceID) in BDInvoice >
    
                find tqDInvoiceBankByDInvoiceID no-error.
                if available tqDInvoiceBankByDInvoiceID
                then assign vlDInvoiceIsWithOneBank = true.
            end.

            /* Propose payment if there is only one invoice bank or this is the first payment on the invoice */
            if (vlDInvoiceIsWithOneBank or tDDocumentPossibleAllocations.tdDInvoiceBalance = vdInvoiceAmount) and
            tqDInvoiceBankByAllocationInfo.tcPaymentConditionPaymentTyp <> {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
            then assign vlProposePayment = true.

            if itDDocumentDueDate <= tqDInvoiceBankByAllocationInfo.ttDInvoiceDiscountDueDate
            then do:
                /****************************************************************************************************************  
                   Calculation of Discount Amount 
                   ==============================
                  1) In case of Discount Tax at Payment = YES
                    Discount Amount =  Invoice amount with Tax * 
                                       ((Invoice amount without Tax ?- Non-discountable amount) / Invoice amount without Tax) * 
                                       Discount %  *
                                      (Invoice open amount / Invoice amount with Tax).
                  2) In case of Discount Tax at Payment = NO
                    Discount Amount = (Invoice amount without Tax - Non-discountable amount) * 
                                      Discount %  *
                                     (Invoice open amount / Invoice amount with Tax).
                *****************************************************************************************************************/         
                                                      
                if tDDocumentPossibleAllocations.tlDiscountTaxAtPayment = true
                then assign vdInvoiceDefaultDiscountAmount = vdInvoiceAmount * 
                                                            
                                                             (((vdInvoiceAmount - tDDocumentPossibleAllocations.tdVatTC) - tDDocumentPossibleAllocations.tdNonDiscAmtTC) /
                                                              (vdInvoiceAmount - tDDocumentPossibleAllocations.tdVatTC)) * 
                                                             
                                                             (tqDInvoiceBankByAllocationInfo.tdPaymentConditionPercentage / 100) *
                                                            
                                                             (tDDocumentPossibleAllocations.tdDInvoiceBalance / vdinvoiceAmount).                   
                
                /* The discount calculation for discount at invoice or no tax discount is the same */
                else assign vdInvoiceDefaultDiscountAmount = ((vdInvoiceAmount - tDDocumentPossibleAllocations.tdVatTC) - tDDocumentPossibleAllocations.tdNonDiscAmtTC) * 
                                                             
                                                             (tqDInvoiceBankByAllocationInfo.tdPaymentConditionPercentage / 100) *
                                                             
                                                             (tDDocumentPossibleAllocations.tdDInvoiceBalance / vdinvoiceAmount).
                                                                    
            end.
            else assign vdInvoiceDefaultDiscountAmount = 0.

            assign vdInvoiceDefaultPaymentAmount  = tDDocumentPossibleAllocations.tdDInvoiceBalance - vdInvoiceDefaultDiscountAmount.
        end.  /* Not staged payments */
    end.  /* if first-of(tqDInvoiceBankByAllocationInfo.tiDInvoice_ID) */

    if vlStagedPayments = true
    then do:
        if tDDocumentPossibleAllocations.tcDInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or
           tDDocumentPossibleAllocations.tcDInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
        then assign vdOriginalAmountTC = - abs(tqDInvoiceBankByAllocationInfo.tdDInvoiceOriginalCreditTC - tqDInvoiceBankByAllocationInfo.tdDInvoiceOriginalDebitTC).
        else assign vdOriginalAmountTC = abs(tqDInvoiceBankByAllocationInfo.tdDInvoiceOriginalCreditTC - tqDInvoiceBankByAllocationInfo.tdDInvoiceOriginalDebitTC).
        
        <M-10 run GetPossibleAllocationStages
           (input  tDDocumentPossibleAllocations.DInvoice_ID (iiDInvoiceID), 
            input  tDDocumentPossibleAllocations.tc_Rowid (icParentRowID), 
            input  itDDocumentDueDate (itDDocumentDueDate), 
            input  tDDocumentPossibleAllocations.tcCurrencyCode (icDInvoiceCurrencyCode), 
            input  vdOriginalAmountTC (idInvoiceOriginalAmountTC), 
            input  tDDocumentPossibleAllocations.tdNonDiscAmtTC (idInvoiceNonDiscAmountTC), 
            input  tDDocumentPossibleAllocations.tdVatTC (idInvoiceVatTC), 
            input  tDDocumentPossibleAllocations.tlDiscountTaxAtPayment (ilIsDiscountTaxAtPayment), 
            output tDDocumentPossibleAllocations.tdDefaultPaymentAmount (odInvoiceTotalPaymentAmountTC), 
            output tDDocumentPossibleAllocations.tdDefaultDiscountAmount (odInvoiceTotalDiscountAmountTC), 
            input  tDDocumentPossibleAllocations.tdNonDiscAmtTC (idNonDiscountableAmountTC), 
            output viFcReturnSuper (oiReturnStatus)) in BDDocument>
        if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus  < 0 then return.
    end.  /* Staged payments */
    else do:
        
        if last-of (tqDInvoiceBankByAllocationInfo.tiDInvoice_ID)
        then do:
            if vlProposePayment
            then do:
                
                 assign vdDefaultDiscountAmount = vdDefaultDiscountAmount + vdInvoiceDefaultDiscountAmount *
                                                  tqDInvoiceBankByAllocationInfo.tdDInvoiceBankToPayTC / tDDocumentPossibleAllocations.tdDInvoiceBalance
                        vdDefaultPaymentAmount  = vdDefaultPaymentAmount + vdInvoiceDefaultPaymentAmount *
                                                  tqDInvoiceBankByAllocationInfo.tdDInvoiceBankToPayTC / tDDocumentPossibleAllocations.tdDInvoiceBalance.

                assign tDDocumentPossibleAllocations.tdDefaultDiscountAmount = <M-3 RoundAmount
                                                                                  (input  vdDefaultDiscountAmount (idUnroundedAmount), 
                                                                                   input  ? (iiCurrencyID), 
                                                                                   input  tDDocumentPossibleAllocations.tcCurrencyCode (icCurrencyCode)) in business>
                       tDDocumentPossibleAllocations.tdDefaultPaymentAmount  = <M-4 RoundAmount
                                                                                  (input  vdDefaultPaymentAmount (idUnroundedAmount), 
                                                                                   input  ? (iiCurrencyID), 
                                                                                   input  tDDocumentPossibleAllocations.tcCurrencyCode (icCurrencyCode)) in business>
                       vdInvoiceTotalTC = tDDocumentPossibleAllocations.tdDefaultPaymentAmount +
                                          tDDocumentPossibleAllocations.tdDefaultDiscountAmount.

                /* Rounding difference compensation between dinvoice OpenAmount vs PaymentAmount */
                if vlDInvoiceIsWithOneBank and
                  (vdInvoiceTotalTC <> tDDocumentPossibleAllocations.tdDInvoiceBalance)
                then assign tDDocumentPossibleAllocations.tdDefaultPaymentAmount = tDDocumentPossibleAllocations.tdDefaultPaymentAmount +
                                                                                   tDDocumentPossibleAllocations.tdDInvoiceBalance - vdInvoiceTotalTC.
            end.
            else assign tDDocumentPossibleAllocations.tdDefaultDiscountAmount = 0
                        tDDocumentPossibleAllocations.tdDefaultPaymentAmount  = 0.
        end.
    end.  /* Not staged payments */

    /* Assign shipper code */
    if last-of (tqDInvoiceBankByAllocationInfo.tiDInvoice_ID)
    then do:
        assign tDDocumentPossibleAllocations.tcShipperCode = vcDInvoiceShipperInfo
               vcDInvoiceShipperInfo = "":U.
    end.
end.  /* for each tqDInvoiceBankByAllocationInfo */

/*Cleanup the appended temp-table*/
empty temp-table tqDInvoiceVatRetByDInvoiceId.