project QadFinancials > class BDPaymentSelection > method GetToPayObjects

Description

Get the invoices matching the filter criteria passed in.
Assumption: An invoice can only have one bank row if staged payment condition is used


Parameters


iiPaySelIDinputinteger
icRowidinputcharacterRow ID
icBankCurrencyCodeinputcharacterBank Currency Code
ilIsInitialinputlogical
icBusinessRelationinputcharacterBusiness Relation
icCountryinputcharacterCountry
icCurrencyinputcharacterCurrency
icDivisioninputcharacterDivision
icPaymentGroupinputcharacterPayment Group
icSetSelectedinputcharacterSet the objects as selected by default
icVisibleItemsinputcharacter
ilAllCompaniesinputlogicalInclude all Companies
ilIntercompanyinputlogical
itRefDueDateinputdate
iiBankPayFormatIdinputinteger
ilDInvoiceIsWithBankinputlogical
icCorporateGroupCodeinputcharacter
oiIncSelCountoutputinteger
tDIncSelLineoutputtemp-table
tDIncSelLineStageoutputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFitnesse
method BDPaymentSelection.ApiDPaymentSelectionCreate


program code (program1/bdpaymentselection.p)

if icCorporateGroupCode = ? then assign icCorporateGroupCode = "":U.

if iiBankPayFormatId = ? or iiBankPayFormatId = 0
then return.

assign viDIncSelCount = viDIncSelCount + 1.

empty temp-table tDIncSelLine.
empty temp-table tDIncSelLineStage.
if ilIsInitial /* Load grid dataset for View, for all status */
then do:
    <M-7 run GetToPayObjectsInitialLoad
       (input  icBankCurrencyCode (icCurrencyCode), 
        input  iiPaySelID (iiDPaySelID), 
        input  itRefDueDate (itDPaySelDate), 
        output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>           
end.
if not ilIsInitial
then do:
    if icBankCurrencyCode = vcCompanyLC
    then assign vdBankRate      = 1
                vdBankRateScale = 1.
    else do:
            <M-38 run GetExchangeRate
               (input  ? (iiCompanyID), 
                input  ? (iiFromCurrencyID), 
                input  icBankCurrencyCode (icFromCurrencyCode), 
                input  viCompanyLCId (iiToCurrencyID), 
                input  ? (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeID), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                input  itRefDueDate (itValidityDate), 
                output vdBankRate (odExchangeRate), 
                output vdBankRateScale (odExchangeScaleFactor), 
                output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
            if viFcReturnSuper < 0 or
               oiReturnStatus  = 0
            then assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then return.
    end.  /* end if icBankCurrencyCode = vcCompanyLC */
    assign vcPaymentInvoiceType =  {&INVOICETYPE-INVOICE} + ',':U + {&INVOICETYPE-CREDITNOTE} + ',':U + {&INVOICETYPE-PREPAYMENT} + ',':U
                                   + {&INVOICETYPE-DEDUCTION} + ',':U + {&INVOICETYPE-INVOICECORRECTION} + ',':U + {&INVOICETYPE-CREDITNOTECORRECTION}.
    <Q-8 run DInvoiceByBankPaymentInfo (all) (Read) (NoCache)
       (input if ilAllCompanies then ? else viCompanyId, (CompanyId)
        input vcPaymentInvoiceType, (DInvoiceType)
        input true, (DInvoiceIsOpen)
        input false, (DInvoiceIsSelected)
        input icBusinessRelation, (BusinessRelationCode)
        input ilIntercompany, (BusinessRelationIsInterco)
        input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
        input icCountry, (CountryCode)
        input icPaymentGroup, (PaymentGroupCode)
        input icCurrency, (CurrencyCode)
        input icDivision, (DivisionCode)
        input if ilDInvoiceIsWithBank then iiBankPayFormatId else ?, (BankPayFormatId)
        input ilDInvoiceIsWithBank, (DInvoiceIsWithBank)
        input icCorporateGroupCode, (CorporateGroupCode)
        output dataset tqDInvoiceByBankPaymentInfo) in BDInvoice>
    
    for each tqDInvoiceByBankPaymentInfo
    break by tqDInvoiceByBankPaymentInfo.tiDInvoice_ID:
        if tqDInvoiceByBankPaymentInfo.tiBankNumber_ID <> 0 and
           tqDInvoiceByBankPaymentInfo.tiBankPayFormat_ID <> iiBankPayFormatId
        then next.
    
        if tqDInvoiceByBankPaymentInfo.tiBankNumber_ID = 0
        then assign vlIsSelected = false.
        else do:
            case icSetSelected:
                when {&PAYMENTSELECTION-DUE-ALL}
                then assign vlIsSelected = true.
    
                when {&PAYMENTSELECTION-DUE-DUE}
                then assign vlIsSelected = tqDInvoiceByBankPaymentInfo.ttDInvoiceDueDate <= itRefDueDate.
    
                when {&PAYMENTSELECTION-DUE-DUEORFINDISC}
                then assign vlIsSelected = tqDInvoiceByBankPaymentInfo.ttDInvoiceDueDate         <= itRefDueDate or
                                           tqDInvoiceByBankPaymentInfo.ttDInvoiceDiscountDueDate >= itRefDueDate.
    
                otherwise assign vlIsSelected = false.
            end case.
        end.  /* tqDInvoiceByBankPaymentInfo.tiBankNumber_ID <> 0 */
        
        if not vlIsSelected and
           icVisibleItems = {&INCSELVISIBLEITEM-SHOWSELECTEDONLY}
        then next.
    
        if first-of(tqDInvoiceByBankPaymentInfo.tiDInvoice_ID)
        then do:
            assign vlDInvoiceIsWithOneBank     = false
                   vlDInvoiceHasOtherPayFormat = false
                   vlProposePayment            = false
                   vdInvoiceTotalTC            = 0
                   vdInvoiceAmount             = abs(tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalDebitTC -
                                                     tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalCreditTC)
                   vdInvoiceBalance            = abs(tqDInvoiceByBankPaymentInfo.tdDInvoiceBalanceDebitTC -
                                                     tqDInvoiceByBankPaymentInfo.tdDInvoiceBalanceCreditTC).
    
            <Q-52 run DInvoiceBankByDInvoiceID (all) (Read) (NoCache)
               (input tqDInvoiceByBankPaymentInfo.tiDInvoice_ID, (DInvoiceId)
                output dataset tqDInvoiceBankByDInvoiceID) in BDInvoice>
    
            find tqDInvoiceBankByDInvoiceID no-error.
            if available tqDInvoiceBankByDInvoiceID
            then assign vlDInvoiceIsWithOneBank = true.
    
            /* Propose payment if there is only one invoice bank or this is the first payment on the invoice */        
            if (vlDInvoiceIsWithOneBank or vdInvoiceBalance = vdInvoiceAmount)
            then assign vlProposePayment = true.
    
            if can-find(first tqDInvoiceBankByDInvoiceID where
                              tqDInvoiceBankByDInvoiceID.tiDInvoice_ID = tqDInvoiceByBankPaymentInfo.tiDInvoice_ID and
                              tqDInvoiceBankByDInvoiceID.tiBankPayFormat_ID <> iiBankPayFormatId)
            then assign vlDInvoiceHasOtherPayFormat = true.
    
        /**************************************************************************************************************** 
               Calculation of Discount Base Amount 
               ===================================
              1) In case of Discount Tax at Payment = YES
                Discount Base Amount =  Invoice amount with Tax * 
                                       ((Invoice amount without Tax ?- Non-discountable amount) / Invoice amount without Tax) * 
                                       (Invoice open amount / Invoice amount with Tax).
              2) In case of Discount Tax at Payment = NO
                Discount Base Amount = (Invoice amount without Tax - Non-discountable amount) * 
                                       (Invoice open amount / Invoice amount with Tax).
         *****************************************************************************************************************/  
            
            if itRefDueDate <= tqDInvoiceByBankPaymentInfo.ttDInvoiceDiscountDueDate
            then assign vdDiscountBase          = if tqDInvoiceByBankPaymentInfo.tlDInvoiceIsDiscTaxAtPaym
                                                  then vdInvoiceAmount * 
                                                      
                                                      ((vdInvoiceAmount - abs(tqDInvoiceByBankPaymentInfo.tdDInvoiceVatDebitTC - tqDInvoiceByBankPaymentInfo.tdDInvoiceVatCreditTC)) - tqDInvoiceByBankPaymentInfo.tdDInvoiceNonDiscAmtTC) /
                                                       (vdInvoiceAmount - abs(tqDInvoiceByBankPaymentInfo.tdDInvoiceVatDebitTC - tqDInvoiceByBankPaymentInfo.tdDInvoiceVatCreditTC)) *
                                                      
                                                      (vdInvoiceBalance / vdInvoiceAmount)
                                                  
                                                  else ((vdInvoiceAmount - abs(tqDInvoiceByBankPaymentInfo.tdDInvoiceVatDebitTC - tqDInvoiceByBankPaymentInfo.tdDInvoiceVatCreditTC)) - tqDInvoiceByBankPaymentInfo.tdDInvoiceNonDiscAmtTC) * 
                                                        
                                                        (vdInvoiceBalance / vdinvoiceAmount) 
                                                                                                       
                        vdInvoiceDiscountAmount = vdDiscountBase * tqDInvoiceByBankPaymentInfo.tdPaymentConditionPercentage / 100
                        vdInvoicePaymentAmount  = vdInvoiceBalance - vdInvoiceDiscountAmount.
            else assign vdInvoiceDiscountAmount = 0
                        vdInvoicePaymentAmount  = vdInvoiceBalance.
                        
            /* Invoice Operational Info Code */
            assign vcDInvoiceOpInfoCode = tqDInvoiceByBankPaymentInfo.tcDInvoiceShipperList.
        end. /* if first-of(tqDInvoiceByBankPaymentInfo.tiDInvoice_ID) */
    
        /* Corrections always have negative amounts */
        if tqDInvoiceByBankPaymentInfo.tcDInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or
           tqDInvoiceByBankPaymentInfo.tcDInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
        then assign vdInvoiceAmount         = - vdInvoiceAmount
                    vdInvoiceBalance        = - vdInvoiceBalance
                    vdInvoiceDiscountAmount = - vdInvoiceDiscountAmount
                    vdInvoicePaymentAmount  = - vdInvoicePaymentAmount.
                        
        create tDIncSelLine.
    
        assign 
               tDIncSelLine.tc_status               = "N":U
               tDIncSelLine.tcBusinessRelationCode  = tqDInvoiceByBankPaymentInfo.tcBusinessRelationCode
               tDIncSelLine.tcBusinessRelationName1 = tqDInvoicebyBankPaymentInfo.tcBusinessRelationName1
               tDIncSelLine.tcCorporateGroupCode    = tqdinvoicebybankpaymentinfo.tcCorporateGroupCode
               tDIncSelLine.tcCompanyCode          = tqDInvoiceByBankPaymentInfo.tcCompanyCode
               tDIncSelLine.tcDebtorCode           = tqDInvoiceByBankPaymentInfo.tcDebtorCode
               tDIncSelLine.tcIncSelLineAmountCrDt = if tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalDebitTC <> 0 and
                                                       tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalDebitTC <> ?
                                                    then {&CREDITDEBITABBREVIATION-CREDIT}
                                                    else {&CREDITDEBITABBREVIATION-DEBIT}
               tDIncSelLine.tcInvoiceAmountCrDt    = if tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalDebitTC <> 0 and
                                                       tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalDebitTC <> ?
                                                    then {&CREDITDEBITABBREVIATION-DEBIT}
                                                    else {&CREDITDEBITABBREVIATION-CREDIT}
               tDIncSelLine.tcInvoiceBalanceCrDt   = if tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalDebitTC <> 0 and
                                                       tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalDebitTC <> ?
                                                    then {&CREDITDEBITABBREVIATION-DEBIT}
                                                    else {&CREDITDEBITABBREVIATION-CREDIT}
               tDIncSelLine.tcInvoiceCurrencyCode  = tqDInvoiceByBankPaymentInfo.tcCurrencyCode
               tDIncSelLine.tcInvoiceDescription   = tqDInvoiceByBankPaymentInfo.tcDInvoiceDescription
               tDIncSelLine.tcInvoiceNumber        = if tqDInvoiceByBankPaymentInfo.tcDInvoiceDIText = "" or 
                                                        tqDInvoiceByBankPaymentInfo.tcDInvoiceDIText = ?
                                                     then 
                                                         string(tqDInvoiceByBankPaymentInfo.tiPeriodYear, "9999":U) + "/":U +
                                                         tqDInvoiceByBankPaymentInfo.tcJournalCode                  + "/":U +
                                                         string(tqDInvoiceByBankPaymentInfo.tiDInvoiceVoucher, "999999999":U)
                                                     else
                                                         tqDInvoiceByBankPaymentInfo.tcDInvoiceDIText
               tDIncSelLine.tcInvoiceType          = tqDInvoiceByBankPaymentInfo.tcDInvoiceType
               tDIncSelLine.tc_ParentRowid         = icRowid
               tDIncSelLine.tdDiscountPercentage   = if itRefDueDate <= tqDInvoiceByBankPaymentInfo.ttDInvoiceDiscountDueDate
                                                    then tqDInvoiceByBankPaymentInfo.tdPaymentConditionPercentage
                                                    else 0
               tDIncSelLine.tdInvoiceAmount        = vdInvoiceAmount
               tDIncSelLine.tdInvoiceBalance       = vdInvoiceBalance
               tDIncSelLine.tdInvoiceRate          = tqDInvoiceByBankPaymentInfo.tdDInvoiceExchangeRate
               tDIncSelLine.tdInvoiceRateScale     = tqDInvoiceByBankPaymentInfo.tdDInvoiceRateScale
               tDIncSelLine.tiDInvoiceId           = tqDInvoiceByBankPaymentInfo.tiDInvoice_ID
               tDIncSelLine.tiDomiciliationNumber  = tqDInvoiceByBankPaymentInfo.tiDomiciliationNumber
               tDIncSelLine.tiIncSelCount          = viDIncSelCount
               tDIncSelLine.tlIsSelected           = vlIsSelected
               tDIncSelLine.ttDiscountDueDate      = tqDInvoiceByBankPaymentInfo.ttDInvoiceDiscountDueDate
               tDIncSelLine.ttDueDate              = tqDInvoiceByBankPaymentInfo.ttDInvoiceDueDate              
               tDIncSelLine.ttDefaultDueDate       = tDIncSelLine.ttDueDate                             
               tDIncSelLine.ttInvoiceDate          = tqDInvoiceByBankPaymentInfo.ttDInvoiceDate
               tDIncSelLine.tcBankAccountNumber    = if tqDInvoiceByBankPaymentInfo.tcBankNumberFormatted = "":U or
                                                       tqDInvoiceByBankPaymentInfo.tcBankNumberFormatted = ?
                                                    then tqDInvoiceByBankPaymentInfo.tcBankNumber
                                                    else tqDInvoiceByBankPaymentInfo.tcBankNumberFormatted
               tDIncSelLine.tiBankNumberId         = tqDInvoiceByBankPaymentInfo.tiBankNumber_ID
               viDIncSelLineRowid                  = viDIncSelLineRowid + 1
               tDIncSelLine.tc_Rowid               = string(viDIncSelLineRowid)
               tDIncSelLine.tdNonDiscAmtTC         = tqDInvoiceByBankPaymentInfo.tdDInvoiceNonDiscAmtTC           
               tDIncSelLine.tcPaymentConditionPayType = tqDInvoiceByBankPaymentInfo.tcPaymentConditionPaymentTyp               
               tDIncSelLine.tcIncSelLineStatus     = {&PAYSEL-LINE-STATUS-NEW}
               tDIncSelLine.tcIncSelLineStatusTR   = {&PAYSEL-LINE-STATUS-NEW-TR}
               tDIncSelLine.tdIncSelLineIntRate = tqDInvoiceByBankPaymentInfo.tdPaymentConditionLateInt
               tDIncSelLine.tdDefaultIncSelLineIntRate = tDIncSelLine.tdIncSelLineIntRate
               tDIncSelLine.tcDefaultIncLineStatus = tDIncSelLine.tcIncSelLineStatus
               tDIncSelLine.tcDInvoiceDIText       = tqDInvoiceByBankPaymentInfo.tcDInvoiceDIText
               tDIncSelLine.tiDInvoicePostingYear  = tqDInvoiceByBankPaymentInfo.tiDInvoicePostingYear
               tDIncSelLine.tcJournalCode          = tqDInvoiceByBankPaymentInfo.tcJournalCode
               tDIncSelLine.tiDInvoiceVoucher      = tqDInvoiceByBankPaymentInfo.tiDInvoiceVoucher
               tDIncSelLine.tcShipperInfo          = vcDInvoiceOpInfoCode 
               tDIncSelLine.tdBankRate             = vdBankRate
               tDIncSelLine.tdBankRateScale        = vdBankRateScale.

        if tcInvoiceType = {&INVOICETYPE-DEDUCTION} then
        do:
            tDIncSelLine.tcInvoiceBalanceCrDt   = if tqDInvoiceByBankPaymentInfo.tdDInvoiceBalanceDebitTC <> 0 and
                                                    tqDInvoiceByBankPaymentInfo.tdDInvoiceBalanceDebitTC <> ?
                                                    then {&CREDITDEBITABBREVIATION-DEBIT}
                                                    else {&CREDITDEBITABBREVIATION-CREDIT}.

            tDIncSelLine.tcIncSelLineAmountCrDt = if tqDInvoiceByBankPaymentInfo.tdDInvoiceBalanceDebitTC <> 0 and
                                                       tqDInvoiceByBankPaymentInfo.tdDInvoiceBalanceDebitTC <> ?
                                                    then {&CREDITDEBITABBREVIATION-CREDIT}
                                                    else {&CREDITDEBITABBREVIATION-DEBIT}.
        end.

                /* Staged payments */
        if tqDInvoiceByBankPaymentInfo.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
        then do:
            if tqDInvoiceByBankPaymentInfo.tcDInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or
               tqDInvoiceByBankPaymentInfo.tcDInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
            then assign vdOriginalAmountTC = - abs(tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalCreditTC - tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalDebitTC).
            else assign vdOriginalAmountTC = abs(tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalCreditTC - tqDInvoiceByBankPaymentInfo.tdDInvoiceOriginalDebitTC).

            assign vdVatTC = if tqDInvoiceByBankPaymentInfo.tdDInvoiceVatDebitTC <> 0 and tqDInvoiceByBankPaymentInfo.tdDInvoiceVatDebitTC<> ? 
                             then tqDInvoiceByBankPaymentInfo.tdDInvoiceVatDebitTC      
                             else tqDInvoiceByBankPaymentInfo.tdDInvoiceVatCreditTC.

             <M-84 run GetToPayObjectsStages
                (input  tqDInvoiceByBankPaymentInfo.tiDInvoice_ID (iiDInvoiceID), 
                 input  tDIncSelLine.tc_Rowid (icParentRowID), 
                 input  itRefDueDate (itRefDueDate), 
                 input  tDIncSelLine.tcInvoiceCurrencyCode (icDInvoiceCurrencyCode), 
                 input  vdOriginalAmountTC (idInvoiceOriginalAmountTC), 
                 input  tDIncSelLine.tdNonDiscAmtTC (idNonDiscountableAmountTC), 
                 input  vdVatTC (idInvoiceVatTC), 
                 input  tqDInvoiceByBankPaymentInfo.tlDInvoiceIsDiscTaxAtPaym (ilIsDiscountTaxAtPayment), 
                 input  vlProposePayment (ilIsProposePayment), 
                 input  icSetSelected (icPaymentSelectionDue), 
                 output tDIncSelLine.tdDefaultPaymentAmountTC (odInvoiceTotalPaymentAmountTC), 
                 output tDIncSelLine.tdDefaultDiscountAmountTC (odInvoiceTotalDiscountAmountTC), 
                 output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
              
            if viFcReturnSuper < 0 or oiReturnStatus  = 0
            then assign oiReturnStatus = viFcReturnSuper.
    
            if oiReturnStatus < 0
            then do:
                return.    
            end.
            
            /* If we have no available stages to pay then remove the invoice from the list */                      
            if not can-find(first tDIncSelLineStage where tDIncSelLineStage.tc_parentrowid = tDIncSelLine.tc_rowid)
            then do:
                delete tDIncSelLine.
                next.
            end.
            
            if not tDIncSelLine.tlIsSelected and
               icVisibleItems = {&INCSELVISIBLEITEM-SHOWSELECTEDONLY}
            then do:
                for each tDIncSelLineStage where
                         tDIncSelLineStage.tc_ParentRowid = tDIncSelLine.tc_Rowid:
                    delete tDIncSelLineStage.
                end.
                delete tDIncSelLine.
                next.
            end.
        end. /* Staged payments */
        else if tqDInvoiceByBankPaymentInfo.tiBankNumber_ID <> 0 and
                tqDInvoiceByBankPaymentInfo.tiBankPayFormat_ID = iiBankPayFormatId
        then do:
            if vlProposePayment
            then assign vdInvoiceBankPercentage               = tqDInvoiceByBankPaymentInfo.tdDInvoiceBankToPayTC / tDIncSelLine.tdInvoiceBalance
                        tDIncSelLine.tdDefaultDiscountAmountTC = vdInvoiceDiscountAmount * vdInvoiceBankPercentage
                        tDIncSelLine.tdDefaultPaymentAmountTC  = vdInvoicePaymentAmount * vdInvoiceBankPercentage.
            else assign tDIncSelLine.tdDefaultPaymentAmountTC = 0
                        tDIncSelLine.tdDefaultDiscountAmountTC = 0.
        end.  /* Not staged payments */
        assign tDIncSelLine.tdDefaultDiscountAmountTC = <M-91 RoundAmount
                                                           (input  tDIncSelLine.tdDefaultDiscountAmountTC (idUnroundedAmount), 
                                                            input  '':U (iiCurrencyID), 
                                                            input  tDIncSelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BApplicationProperty>
               tDIncSelLine.tdDefaultPaymentAmountTC  = <M-81 RoundAmount
                                                           (input  tDIncSelLine.tdDefaultPaymentAmountTC (idUnroundedAmount), 
                                                            input  '':U (iiCurrencyID), 
                                                            input  tDIncSelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BApplicationProperty>
               vdInvoiceTotalTC                      = vdInvoiceTotalTC + tDIncSelLine.tdDefaultPaymentAmountTC + tDIncSelLine.tdDefaultDiscountAmountTC.

            
        if not tDIncSelLine.tlIsSelected and
           icVisibleItems = {&INCSELVISIBLEITEM-SHOWSELECTEDONLY}
        then do:
            for each tDIncSelLine where
                     tDIncSelLine.tiDInvoiceId = tqDInvoiceByBankPaymentInfo.tiDInvoice_ID
            break by tDIncSelLine.tdDefaultPaymentAmountTC descending:
                if first(tDIncSelLine.tdDefaultPaymentAmountTC)
                then do:
                    assign tDIncSelLine.tdDefaultPaymentAmountTC = tDIncSelLine.tdDefaultPaymentAmountTC + vdInvoiceBalance - vdInvoiceTotalTC.
                    leave.
                end.
            end.
        end.

        assign vdDInvoiceRateInCurrentCny = tDIncSelLine.tdInvoiceRate
               vdDInvoiceRateScaleInCurrentCny = tDIncSelLine.tdInvoiceRateScale.
        /* When cross-company, if base currency is different or sharedset is different, 
        it cannot use invoice rate, need to get new rate.
        */
        if tDIncSelLine.tcCompanyCode <> vcCompanyCode
        then do:
            /* Get current exchange rate shared set */
            if vcExchangeRateSS = ? or vcExchangeRateSS = '':U
            then do:
                <Q-54 run CompanyCurrSSByIDCodeSSType (all) (Read) (NoCache)
                   (input viCompanyId, (CompanyId)
                    input {&SHAREDSETTYPE-EXCHANGERATE}, (SharedSetType)
                    input ?, (CompanyCode)
                    output dataset tqCompanyCurrSSByIDCodeSSType) in BCompany>
                find first tqCompanyCurrSSByIDCodeSSType no-error.
                if available tqCompanyCurrSSByIDCodeSSType then
                assign vcExchangeRateSS = tqCompanyCurrSSByIDCodeSSType.tcSharedSetCode.
            end. /* End if vcExchangeRateSS = ? or vcExchagneRateSS = '':U */
            /* Get invoice's company base currency and exchangerate sharedset code */
            <Q-80 run CompanyCurrSSByIDCodeSSType (all) (Read) (NoCache)
               (input ?, (CompanyId)
                input {&SHAREDSETTYPE-EXCHANGERATE}, (SharedSetType)
                input tDIncSelLine.tcCompanyCode, (CompanyCode)
                output dataset tqCompanyCurrSSByIDCodeSSType) in BCompany>
            find first tqCompanyCurrSSByIDCodeSSType no-error.
            if available tqCompanyCurrSSByIDCodeSSType then
                assign vcExchangeRateSSInInvoice = tqCompanyCurrSSByIDCodeSSType.tcSharedSetCode
                       viBaseCurrInInvoice       = tqCompanyCurrSSByIDCodeSSType.tiCurrency_ID.
            /* Check if sharedset code different or base currency different, need to get the rate 
               based on the current company.
               bank currency same as invoice currency, directly use invoice rate as bank rate 
            */
            if viBaseCurrInInvoice <> viCompanyLCId or vcExchangeRateSS <> vcExchangeRateSSInInvoice
            then do:
                /* convert the invoice curreny to current base currency */
                <M-76 run GetExchangeRate
                   (input  ? (iiCompanyID), 
                    input  ? (iiFromCurrencyID), 
                    input  tDIncSelLine.tcInvoiceCurrencyCode (icFromCurrencyCode), 
                    input  viCompanyLCId (iiToCurrencyID), 
                    input  ? (icToCurrencyCode), 
                    input  ? (iiExchangeRateTypeID), 
                    input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                    input  itRefDueDate (itValidityDate), 
                    output vdDInvoiceRateInCurrentCny (odExchangeRate), 
                    output vdDInvoiceRateScaleInCurrentCny (odExchangeScaleFactor), 
                    output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
            end. /* End if viBaseCurrInInvoice <> viCompanyLCId or vcExchangeRateSS <> vcExchangeRateSSInInvoice */
            else if icBankCurrencyCode = tDIncSelLine.tcInvoiceCurrencyCode 
                     then assign tDIncSelLine.tdBankRate      = tDIncSelLine.tdInvoiceRate
                                 tDIncSelLine.tdBankRateScale = tDIncSelLine.tdInvoiceRateScale.
        end. /* End Cross company */
        else if icBankCurrencyCode = tDIncSelLine.tcInvoiceCurrencyCode 
        then assign tDIncSelLine.tdBankRate      = tDIncSelLine.tdInvoiceRate
                   tDIncSelLine.tdBankRateScale  = tDIncSelLine.tdInvoiceRateScale.
        if tDIncSelLine.tlIsSelected
        then do:
            if tDIncSelLine.tcInvoiceType <> {&INVOICETYPE-DEDUCTION} then 
                assign tDIncSelLine.tdIncSelLineAmountTC   = tDIncSelLine.tdDefaultPaymentAmountTC
                    tDIncSelLine.tdIncSelLineDiscountTC = tDIncSelLine.tdDefaultDiscountAmountTC
                    tDIncSelLine.tdIncSelLineAmountBC   = <M-29 RoundAmount
                                                             (input  tDIncSelLine.tdIncSelLineAmountTC * vdDInvoiceRateInCurrentCny * vdDInvoiceRateScaleInCurrentCny / (tDIncSelLine.tdBankRate * tDIncSelLine.tdBankRateScale) (idUnroundedAmount), 
                                                              input  '':U (iiCurrencyID), 
                                                              input  icBankCurrencyCode (icCurrencyCode)) in BApplicationProperty>.
            else
                assign tDIncSelLine.tdIncSelLineAmountTC   = tDIncSelLine.tdInvoiceBalance
                    tDIncSelLine.tdIncSelLineDiscountTC = 0
                    tDIncSelLine.tdIncSelLineAmountBC   = <M-31 RoundAmount
                                                             (input  tDIncSelLine.tdIncSelLineAmountTC * vdDInvoiceRateInCurrentCny * vdDInvoiceRateScaleInCurrentCny / (tDIncSelLine.tdBankRate * tDIncSelLine.tdBankRateScale) (idUnroundedAmount), 
                                                              input  '':U (iiCurrencyID), 
                                                              input  icBankCurrencyCode (icCurrencyCode)) in BApplicationProperty>.
        end. /* end if selected. */
        
    end.
    assign oiIncSelCount = viDIncSelCount.
end. /* is not initial load */