project QadFinancials > class BPaymentSelection > method GetToPayObjectsForCInvoice


Parameters


icPaymentSelectionDueinputcharacter
ilShowAllinputlogical
itDueDateinputdate
icCurrencyCodeinputcharacter
icDivisionCodeinputcharacter
icBusinessRelationCodeinputcharacter
icFromBusinessRelationCodeinputcharacter
icToBusinessRelationCodeinputcharacter
ilBusinessRelationIsIntercoinputlogical
icPaymentGroupCodeinputcharacter
icCountryCodeinputcharacter
itPaySelDateinputdate
iiBankPayFormatIDinputinteger
ilAllEntitiesinputlogical
ilIsWithBanksinputlogical
icCorporateGroupCodeinputcharacterGroup Name
iiMaxNumberOfLinesToReturninputintegerMaxNumberOfLinesToReturn: If this parameter is filled (meaning it is not zero and not null) then the output parameter tProposedLine will not contain more lines than the number filled in here
iiCInvoiceIDinputintegerIf this param is filled then it means only data of that single invoice will be retrieved
iiPaySel_IDinputintegerPayment Selection ID
icBankNumberFormattedinputcharacterFormatted bank number
icPaymentReferenceinputcharacterPayment Reference
itDisCountRefDueDateinputdate
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BPaymentSelection.ApiGetToPayObjectsV01
method BPaymentSelection.ApiGetToPayObjectsV02
method BPaymentSelection.ApiGetToPayObjectsV03
method BPaymentSelection.ApiGetToPayObjectsV04


program code (program3/bpaymentselection.p)

/* ========================= */
/* Set default return status */
/* ========================= */
assign oiReturnStatus  = -98.

if icBankNumberFormatted = ? then assign icBankNumberFormatted = "":U.
 
/* ============================================================================================= */
/* Empty the temp-table that will be used to retrieve the wht data for certain supplier invoices */
/* ============================================================================================= */
empty temp-table tDefaultWHTDataForPaySel.

/* Get Creditor Invoices and Credit Notes and corresponding bank info */
assign vcInvoiceType  = {&INVOICETYPE-INVOICE} + ",":U + {&INVOICETYPE-CREDITNOTE} + ",":U + 
                        {&INVOICETYPE-PREPAYMENT} + ",":U + {&INVOICETYPE-INVOICECORRECTION} + ",":U +
                        {&INVOICETYPE-CREDITNOTECORRECTION} + ",":U + {&INVOICETYPE-ADJUSTMENT}.

/* If no max-number-of-records-to-retrieve was specified then set the max to 999999 */
if iiMaxNumberOfLinesToReturn = 0 or 
   iiMaxNumberOfLinesToReturn = ?
then assign iiMaxNumberOfLinesToReturn = 999999.

/* Get Creditor Shared Set for current entity */
<Q-27 run CompanySSByCompanyIDCodeSSType (all) (Read) (NoCache)
   (input viCompanyId, (CompanyId)
    input {&SHAREDSETTYPE-CREDITOR}, (SharedSetType)
    input ?, (CompanyCode)
    output dataset tqCompanySSByCompanyIDCodeSSType) in BCompany>

find first tqCompanySSByCompanyIDCodeSSType where
           tqCompanySSByCompanyIDCodeSSType.tiCompany_ID        = viCompanyId               and
           tqCompanySSByCompanyIDCodeSSType.tcSharedSetTypeCode = {&SHAREDSETTYPE-CREDITOR}
           no-error.

if available tqCompanySSByCompanyIDCodeSSType 
then assign viCreditorSharedSetID = tqCompanySSByCompanyIDCodeSSType.tiSharedSet_ID.

/* Query the invoices */  
<Q-1 run CInvoiceBankForPaymentV01 (firstiiMaxNumberOfLinesToReturn) (Read) (NoCache)
   (input if ilAllEntities then ? else viCompanyId, (CompanyId)
    input iiBankPayFormatID, (BankPayFormat_ID)
    input true, (IsInvoiceApproved)
    input iiCInvoiceID, (CInvoice_ID)
    input vcInvoiceType, (CInvoiceType)
    input false, (IsLockPayment)
    input true, (IsOpen)
    input false, (IsSelected)
    input icCurrencyCode, (CurrencyCode)
    input icDivisionCode, (DivisionCode)
    input icPaymentGroupCode, (PaymentGroupCode)
    input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
    input icBusinessRelationCode, (BusinessRelationCode)
    input icCountryCode, (CountryCode)
    input ilIsWithBanks, (IsWithBank)
    input ilBusinessRelationIsInterco, (BusinessRelationIsInterco)
    input icCorporateGroupCode, (CorporateGroupCode)
    input icFromBusinessRelationCode, (FromBusinessRelationCode)
    input icToBusinessRelationCode, (ToBusinessRelationCode)
    input icPaymentReference, (PaymentReference)
    input viCreditorSharedSetID, (CreditorSharedSetID)
    output dataset tqCInvoiceBankForPaymentV01) in BCInvoice>
    
/* Get PaySelLine info of other bank number in current invoice */
if iiPaySel_ID <> ? and iiPaySel_ID <> 0
then do:
    <Q-64 run PaySelLineByPaySel (all) (Read) (NoCache)
       (input iiPaySel_ID, (PaySel_ID)
        input ?, (PaySelLine_ID)
        output dataset tqPaySelLineByPaySel) in BPaymentSelection>
        
    for each tqPaySelLineByPaySel
        where tqPaySelLineByPaySel.tiPaySel_ID = iiPaySel_ID:

        <Q-58 run CInvoiceBankForPaymentOthers (all) (Read) (NoCache)
           (input if ilAllEntities then ? else viCompanyId, (CompanyId)
            input iiBankPayFormatID, (BankPayFormat_ID)
            input true, (IsInvoiceApproved)
            input tqPaySelLineByPaySel.tiPaySelLineParentObject_ID, (CInvoice_ID)
            input vcInvoiceType, (CInvoiceType)
            input false, (IsLockPayment)
            input true, (IsOpen)
            input true, (IsSelected)
            input icCurrencyCode, (CurrencyCode)
            input icDivisionCode, (DivisionCode)
            input icPaymentGroupCode, (PaymentGroupCode)
            input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
            input icBusinessRelationCode, (BusinessRelationCode)
            input icCountryCode, (CountryCode)
            input ilIsWithBanks, (IsWithBank)
            input ilBusinessRelationIsInterco, (BusinessRelationIsInterco)
            input icCorporateGroupCode, (CorporateGroupCode)
            input icFromBusinessRelationCode, (FromBusinessRelationCode)
            input icToBusinessRelationCode, (ToBusinessRelationCode)
            input tqPaySelLineByPaySel.tiBankNumber_ID, (BankNumber_ID)
            output dataset tqCInvoiceBankForPaymentOthers) in BCInvoice>
        
        for each tqCInvoiceBankForPaymentOthers:
            if not can-find(first tqPaySelLineByPaySel 
                where tqPaySelLineByPaySel.tiPaySelLineParentObject_ID = tqCInvoiceBankForPaymentOthers.tiCInvoice_ID and
                      tqPaySelLineByPaySel.tiBankNumber_ID = tqCInvoiceBankForPaymentOthers.tiBankNumber_ID)          and
               not can-find(first tqCInvoiceBankForPaymentV01
                where tqCInvoiceBankForPaymentV01.tc_rowid = tqCInvoiceBankForPaymentOthers.tc_rowid)
            then do: 
                create tqCInvoiceBankForPaymentV01.
                buffer-copy tqCInvoiceBankForPaymentOthers to tqCInvoiceBankForPaymentV01.
            end.
        end.
    end.        
end.

MAIN_BLOCK: 
DO:
/* ============================================================================================ */
/* for withholding taxes                                                                        */
/* fill the temp-table that will be used to retrieve the wht data for certain supplier invoices */
/* when filled, call bcinvoice to retrieve all data                                             */
/* after calling bcinvoice, make sure you copy the fields of the temp-table in the correct      */
/* fields of tCDocumentPossibleAllocations                                                      */
/* ============================================================================================ */
for each tqCInvoiceBankForPaymentV01 
break by tqCInvoiceBankForPaymentV01.tiCInvoice_ID:

    if first-of(tqCInvoiceBankForPaymentV01.tiCInvoice_ID) and
       tqCInvoiceBankForPaymentV01.tlCompanyPropertyIsWHT and
       tqCInvoiceBankForPaymentV01.tlCInvoiceIsWHT
    then do:
        create tDefaultWHTDataForPaySel.
        assign tDefaultWHTDataForPaySel.tiCInvoiceId        = tqCInvoiceBankForPaymentV01.tiCInvoice_ID
               tDefaultWHTDataForPaySel.tiCInvoiceCompanyID = tqCInvoiceBankForPaymentV01.tiCompany_ID.
    end. /* end first-of(tqCInvoiceBankForPaymentV01.tiCInvoice_ID) .. */
end. /* end for each tqCInvoiceBankForPaymentV01 .. */

/* =========================================================== */
/* retrieve the default wht data of the used supplier invoices */
/* =========================================================== */
if can-find(first tDefaultWHTDataForPaySel)
then do:
    if viBCInvoicePaySelID = 0 or
       viBCInvoicePaySelID = ?
    then do:
        <I-31 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION   = "true"
             &CLASS                = "BCInvoice"}>
        assign vlBCInvIsStartedFromPaySel = true.
    end. /* end if viBCInvoicePaySelID = 0 .. */
    else do:
        <I-71 {bFcOpenInstance
             &CLASS           = "BCInvoice"}>
    end. /* end else if viBCInvoicePaySelID = 0 */
    
    <M-62 run DefaultWHTDataBasedOnCInvoice
       (input-output tDefaultWHTDataForPaySel (tDefaultWHTData), 
        output viExternalReturnStatus (oiReturnStatus)) in BCInvoice>
    
    <I-93 {bFcCloseInstance
         &CLASS           = "BCInvoice"}>
         
    if viExternalReturnStatus <> 0
    then assign oiReturnStatus = viExternalReturnStatus.
    
    if viExternalReturnStatus < 0
    then leave MAIN_BLOCK.
end. /* end can-find(first tDefaultWHTDataForPaySel) */

/* ====================== */
/* Populate default lines */
/* ====================== */
for each tqCInvoiceBankForPaymentV01
    break by tqCInvoiceBankForPaymentV01.tiCInvoice_ID:
    
    /* ========================================================================== */
    /* Skip the ones with an incorrect bankpayformat comparing against the filter */
    /* ========================================================================== */
    if tqCInvoiceBankForPaymentV01.tiBankNumber_ID <> 0 and
       tqCInvoiceBankForPaymentV01.tiBankPayFormat_ID <> iiBankPayFormatId
    then next.
    
    if abs(tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC) = abs(tqCInvoiceBankForPaymentV01.tdCInvoiceBalanceTC) 
    then next.
    
    /* ========================================================================= */
    /* The proposed line may not be selected if the invoice does not have a bank */
    /* ========================================================================= */    
    if tqCInvoiceBankForPaymentV01.tiBankNumber_ID = 0
    then assign vlProposedLineIsSelected = false.
    else if tqCInvoiceBankForPaymentV01.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
    then assign vlProposedLineIsSelected = true.
    else do:
        if icPaymentSelectionDue = {&PAYMENTSELECTION-DUE-ALL}
        then assign vlProposedLineIsSelected = true. 
        else do:
            assign vlProposedLineIsSelected = false.
            case icPaymentSelectionDue:
                when {&PAYMENTSELECTION-DUE-DUE}
                then assign vlProposedLineIsSelected = if tqCInvoiceBankForPaymentV01.ttCInvoiceDueDate <= itDueDate then true else false.
                when {&PAYMENTSELECTION-DUE-DUEORFINDISC}
                then if tqCInvoiceBankForPaymentV01.ttCInvoiceDueDate <= itDueDate or
                        (itDisCountRefDueDate <> ? and
                         tqCInvoiceBankForPaymentV01.ttCInvoiceDiscountDueDate <= itDisCountRefDueDate and
                         tqCInvoiceBankForPaymentV01.ttCInvoiceDiscountDueDate >= itDueDate and
                         tqCInvoiceBankForPaymentV01.tdPaymentConditionPercentage <> 0) or
                         (itDisCountRefDueDate = ? and                         
                         tqCInvoiceBankForPaymentV01.ttCInvoiceDiscountDueDate >= itDueDate and
                         tqCInvoiceBankForPaymentV01.tdPaymentConditionPercentage <> 0)
                     then do:
                         assign vlProposedLineIsSelected = true.
                     end.    
                when {&PAYMENTSELECTION-DUE-NONE}
                then assign vlProposedLineIsSelected = false.
                otherwise assign vlProposedLineIsSelected = true.
            end case.
            if not ilShowAll and
               not vlProposedLineIsSelected 
            then next.
        end. /* end else if icPaymentSelectionDue = {&PAYMENTSELECTION-DUE-ALL} */
    end.  /* banknuber_ID <> 0 */


    /* ================================================================================================ */
    /* Check, if this is the first payment of the invoice and whether there is only one bank entry line */
    /* ================================================================================================ */
    if first-of(tqCInvoiceBankForPaymentV01.tiCInvoice_ID)
    then do:
	 /* Get invoice amounts */
        <M-70 run GetCInvoiceAmounts
           (input  ? (icTransactionObjectType), 
            input  tqCInvoiceBankForPaymentV01.tdCInvoiceOriginalDebitTC (idInvoiceOriginalDebit), 
            input  tqCInvoiceBankForPaymentV01.tdCInvoiceOriginalCreditTC (idInvoiceOriginalCredit), 
            input  tqCInvoiceBankForPaymentV01.tdCInvoiceBalanceDebitTC (idInvoiceBalanceDebit), 
            input  tqCInvoiceBankForPaymentV01.tdCInvoiceBalanceCreditTC (idInvoiceBalanceCredit), 
            input  tqCInvoiceBankForPaymentV01.tdCInvoiceVatBaseDebitTC (idInvoiceVatBaseDebit), 
            input  tqCInvoiceBankForPaymentV01.tdCInvoiceVatBaseCreditTC (idInvoiceVatBaseCredit), 
            input  tqCInvoiceBankForPaymentV01.tdCInvoiceVatDebitTC (idInvoiceVatDebit), 
            input  tqCInvoiceBankForPaymentV01.tdCInvoiceVatCreditTC (idInvoiceVatCredit), 
            input  tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC (idInvoiceHoldAmount), 
            output vdOriginalInvoiceAmountTC (odOriginalInvoiceAmount), 
            output vdOriginalInvoiceAmountNetTC (odOriginalInvoiceAmountNet), 
            output vcOriginalInvoiceAmountSign (ocOriginalInvoiceAmountSign), 
            output vcOriginalInvAmntDebitCredit (ocOriginalInvAmntDebitCreditAbbr), 
            output vdInvoiceVat (odInvoiceVat), 
            output vdInvoiceVatBase (odInvoiceVatBase), 
            output vdOpenInvoiceAmountTC (odOpenBalInvAmount), 
            output vcOpenInvoiceAmntDebitCredit (ocOpenBalInvAmntDebitCreditAbbr), 
            output vdOpenInvoiceAmountSigned (odOpenBalInvAmntSigned), 
            output vcPaymentAmntDebitCredit (ocPaymentAmntDebitCreditAbbr), 
            output vcDiscountAmntDebitCredit (ocDiscountAmntDebitCreditAbbr), 
            output viFcReturnSuper (oiReturnStatus)) in BPaymentSelection>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then return.  
        
        assign vlCInvoiceIsWithOneBank     = false
               vlCInvoiceHasOtherPayFormat = false
               vlProposePayment            = false
               vdInvoiceTotalTC            = 0.

        /* =============================================================================================================== */
        /* if this is the first payment and there is only one bank number -> banknumber balance is same as invoice balance */
        /* =============================================================================================================== */
        if vdOpenInvoiceAmountTC = vdOriginalInvoiceAmountTC                       and
           vdOpenInvoiceAmountTC = tqCInvoiceBankForPaymentV01.tdCInvoiceBankToPayTC
        then assign vlProposePayment            = true
                    vlCInvoiceIsWithOneBank     = true
                    vlCInvoiceOtherBankNrForm   = false.
        else do:
            /* ========================================================================= */
            /* Check in the database whether this payment will be automatically proposed */
            /* ========================================================================= */
            <Q-5 run CInvoiceBankByCInvoiceID (all) (Read) (NoCache)
               (input tqCInvoiceBankForPaymentV01.tiCInvoice_ID, (CInvoiceId)
                output dataset tqCInvoiceBankByCInvoiceID) in BCInvoice>
            /* ===================================================== */
            /* Check if the Invoice has only one CInvoiceBank record */
            /* ===================================================== */
            if can-find( tqCInvoiceBankByCInvoiceID where
                         tqCInvoiceBankByCInvoiceID.tiCInvoice_ID = tqCInvoiceBankForPaymentV01.tiCInvoice_ID)
            then assign vlCInvoiceIsWithOneBank = true.
            /* ============================================================================= */
            /* Check if the Invoice has also CInvoiceBank with different Bank Payment Format */
            /* ============================================================================= */
            if can-find(first tqCInvoiceBankByCInvoiceID where
                              tqCInvoiceBankByCInvoiceID.tiCInvoice_ID       = tqCInvoiceBankForPaymentV01.tiCInvoice_ID and
                              tqCInvoiceBankByCInvoiceID.tiBankPayFormat_ID <> iiBankPayFormatID)
            then assign vlCInvoiceHasOtherPayFormat = true.
            /* ================================================================================================================= */
            /* Check if the Invoice has also CInvoiceBank with same Bank Payment Format but with different formatted bank number */
            /* ================================================================================================================= */
            if icBankNumberFormatted <> "":U and
               can-find(first tqCInvoiceBankByCInvoiceID where
                              tqCInvoiceBankByCInvoiceID.tiCInvoice_ID         = tqCInvoiceBankForPaymentV01.tiCInvoice_ID and
                              tqCInvoiceBankByCInvoiceID.tiBankPayFormat_ID    = iiBankPayFormatID and
                              tqCInvoiceBankByCInvoiceID.tcBankNumberFormatted <> icBankNumberFormatted)
            then assign vlCInvoiceOtherBankNrForm = true.
            else assign vlCInvoiceOtherBankNrForm = false.

            /* ============================================================================================= */
            /* Propose payment if there is only one invoice bank or this is the first payment on the invoice */
            /* ============================================================================================= */
            if (vlCInvoiceIsWithOneBank or vdOpenInvoiceAmountTC = vdOriginalInvoiceAmountTC) and
                tqCInvoiceBankForPaymentV01.tcPaymentConditionPaymentTyp <> {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
            then assign vlProposePayment = true.
        end. /* else if vdOpenInvoiceAmountTC = vdOriginalInvoiceAmountTC  and */
    end.  /* if first-of(tqCInvoiceBankForPaymentV01.tiCInvoice_ID) */
    
    if tqCInvoiceBankForPaymentV01.tdCInvoiceBankToPayTC = 0    and
       vlCInvoiceHasOtherPayFormat                    = true 
    then next.    
    /* ============================================================================ */
    /* Replace unknown value for tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC */
    /* ============================================================================ */
    if tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC = ? 
    then assign tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC = 0.    
    
    /* ==================== */
    /* create proposed line */
    /* ==================== */
    find tProposedLine where
         tProposedLine.tiObject_ID     = tqCInvoiceBankForPaymentV01.tiCInvoice_ID and
         tProposedLine.tiBankNumber_ID = tqCInvoiceBankForPaymentV01.tiBankNumber_ID
         no-error.
    if available tProposedLine
    then next.

    create tProposedLine.
    assign tProposedLine.tc_Status                    = "N":U
           tProposedLine.tiProposedLine_ID            = viProposedLineID
           tProposedLine.tiObject_ID                  = tqCInvoiceBankForPaymentV01.tiCInvoice_ID
           tProposedLine.tcBusinessRelationCode       = tqCInvoiceBankForPaymentV01.tcBusinessRelationCode
           tProposedLine.tcBusinessRelationName1      = tqCInvoiceBankforPaymentV01.tcBusinessRelationName1
           tProposedLine.tcCreditorCode               = tqCInvoiceBankForPaymentV01.tcCreditorCode
           tProposedLine.tiMaster_ID                  = tqCInvoiceBankForPaymentV01.tiCreditor_ID
           tProposedLine.tcDivisionCode               = tqCInvoiceBankForPaymentV01.tcDivisionCode
           tProposedLine.tcInvoiceCurrencyCode        = tqCInvoiceBankForPaymentV01.tcCurrencyCode
           tProposedLine.tcTSM                        = tqCInvoiceBankForPaymentV01.tcCInvoiceTSMNumber
           tProposedLine.tcInternalNumber             = string(tqCInvoiceBankForPaymentV01.tiPeriodYear) + "/":U +
                                                        tqCInvoiceBankForPaymentV01.tcJournalCode + "/":U +
                                                        string(tqCInvoiceBankForPaymentV01.tiCInvoiceVoucher,"9999999999":U)
           tProposedLine.tcExternalNumber             = tqCInvoiceBankForPaymentV01.tcCInvoiceReference
           tProposedLine.ttDiscountDueDate            = tqCInvoiceBankForPaymentV01.ttCInvoiceDiscountDueDate
           tProposedLine.tdDiscountPercentage         = if itDueDate <= tqCInvoiceBankForPaymentV01.ttCInvoiceDiscountDueDate
                                                        then tqCInvoiceBankForPaymentV01.tdPaymentConditionPercentage
                                                        else 0
           tProposedLine.ttDueDate                    = tqCInvoiceBankForPaymentV01.ttCInvoiceDueDate
           tProposedLine.ttInvoiceDate                = tqCInvoiceBankForPaymentV01.ttCInvoiceDate
           tProposedLine.tiInvoiceCompany_ID          = tqCInvoiceBankForPaymentV01.tiCompany_ID
           tProposedLine.tdInvoiceRate                = tqCInvoiceBankForPaymentV01.tdCInvoiceExchangeRate
           tProposedLine.tdInvoiceRateScale           = tqCInvoiceBankForPaymentV01.tdCinvoiceRateScale
           tProposedLine.tdCInvoiceOriginalTC         = vdOriginalInvoiceAmountTC
           tProposedLine.tdOriginalInvoiceAmountTC    = vdOriginalInvoiceAmountTC
           tProposedLine.tdOriginalInvoiceAmountNetTC = vdOriginalInvoiceAmountNetTC
           tProposedLine.tcOriginInvAmntSign          = vcOriginalInvoiceAmountSign
           tProposedLine.tcOriginInvAmntDebitCreditAbbr = vcOriginalInvAmntDebitCredit
           /* needed so that he doesn't run GetToPayObjectsCheck anymore */
           tProposedLine.tlIsMultiple                 = vlCInvoiceOtherBankNrForm
           tProposedLine.tdCInvoiceBankToPayTC        = tqCInvoiceBankForPaymentV01.tdCInvoiceBankToPayTC
           tProposedLine.tdOpenInvoiceAmountTC        = vdOpenInvoiceAmountTC
           tProposedLine.tdOpenPaymentInvoiceAmountTC = tProposedLine.tdOpenInvoiceAmountTC
           tProposedLine.tcOpenInvAmntDebitCreditAbbr = vcOpenInvoiceAmntDebitCredit
           tProposedLine.tdOpenAmountTCSigned         = if vcOpenInvoiceAmntDebitCredit = vcCreditTR
                                                        then - tProposedLine.tdOpenInvoiceAmountTC
                                                        else tProposedLine.tdOpenInvoiceAmountTC
           tProposedLine.tcPaymentAmntDebitCreditAbbr = vcPaymentAmntDebitCredit
           tProposedLine.tcPaymentAmntBCDebitCreditAbbr = tProposedLine.tcPaymentAmntDebitCreditAbbr
           tProposedLine.tcDiscountAmntDebitCreditAbbr = vcDiscountAmntDebitCredit
           tProposedLine.tlProposedLineIsSelected     = vlProposedLineIsSelected
           tProposedLine.tlProposedLineCanBeSelected  = false
           tProposedLine.tiAdditionNumber             = viNumberOfAdditions
           viProposedLineID                           = viProposedLineID + 1
           tProposedLine.tiBankNumber_ID              = tqCInvoiceBankForPaymentV01.tiBankNumber_ID
           tProposedLine.tcBankAccountNumber          = (if tqCInvoiceBankForPaymentV01.tcBankNumberFormatted <> ? and
                                                            tqCInvoiceBankForPaymentV01.tcBankNumberFormatted <> '':U
                                                         then tqCInvoiceBankForPaymentV01.tcBankNumberFormatted
                                                         else tqCInvoiceBankForPaymentV01.tcBankNumber)
           tProposedLine.tcBankAccountExtension       = tqCInvoiceBankForPaymentV01.tcBankNumberExtension
           tProposedLine.tcValidationMask             = tqCInvoiceBankForPaymentV01.tcBankNumberValidation  
           tProposedLine.tcCorporateGroupCode         = tqCInvoiceBankForPaymentV01.tcCorporateGroupCode
           tProposedLine.tdHoldAmountTC               = (if tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC = ? then 0 else tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC)
           tProposedLine.tcInvoiceCompanyCode         = tqCInvoiceBankForPaymentV01.tcCompanyCode
           tProposedLine.tdCInvoiceVatTC              = vdInvoiceVat
           tProposedLine.tdCInvoiceVatBaseTC          = vdInvoiceVatBase
           tProposedLine.tlCInvoiceIsDiscTaxAtPaym    = tqCInvoiceBankForPaymentV01.tlCInvoiceIsDiscTaxAtPaym           
           tProposedLine.tdNonDiscAmtTC               = tqCInvoiceBankForPaymentV01.tdCInvoiceNonDiscAmtTC
           /* if not wht is used, then give initial value */
           tProposedLine.tdWHTTCLCExchangeRate        = 1
           tProposedLine.tdWHTTCLCRateScale           = 1
           tProposedLine.tdWHTTCCCExchangeRate        = 1
           tProposedLine.tdWHTTCCCRateScale           = 1
           tProposedLine.tlCreditorIsWHT              = tqCInvoiceBankForPaymentV01.tlCreditorIsWHT.
 
    /* ===================================== */
    /* everything related to withholding tax */
    /* ===================================== */
    if tqCInvoiceBankForPaymentV01.tlCompanyPropertyIsWHT and
       tqCInvoiceBankForPaymentV01.tlCInvoiceIsWHT
    then do:
        /* ============================================================================================== */
        /* search the temp-table record that contains the default wht data for a certain supplier invoice */
        /* make sure you copy the fields of the temp-table in the correct fields of tProposedLine         */
        /* ============================================================================================== */
        find first tDefaultWHTDataForPaySel where
                   tDefaultWHTDataForPaySel.tiCInvoiceId = tqCInvoiceBankForPaymentV01.tiCInvoice_ID
                   no-lock no-error.
        if available tDefaultWHTDataForPaySel
        then assign tProposedLine.tdCInvoiceTotWHTBaseTC        = tDefaultWHTDataForPaySel.tdCInvoiceTotalWHTTaxFeeTC
                    tProposedLine.tdCInvoiceWHTUnpaidAmtTC      = tDefaultWHTDataForPaySel.tdUnpaidWHTAmountTC
                    tProposedLine.tlUpdateTax                   = tDefaultWHTDataForPaySel.tlUpdateTaxIsAllowed
                    tProposedLine.tdCInvoiceWHTAmtTC            = tDefaultWHTDataForPaySel.tdWHTAmountTC
                    tProposedLine.tdCInvoiceTotWHTTC            = tDefaultWHTDataForPaySel.tdCInvoiceTotalWHTAmountTC
                    tProposedLine.tdTotWHTAmtTC                 = tDefaultWHTDataForPaySel.tdWHTAmountTC
                    tProposedLine.tdInitiallyAllocatedWHTAmntTC = tDefaultWHTDataForPaySel.tdInitiallyAllocatedWHTAmountTC
                    tProposedLine.tdWHTTaxableFeeTC             = tDefaultWHTDataForPaySel.tdCInvoiceTotalWHTTaxFeeTC
                    tProposedLine.tdWHTAmtTC                    = tDefaultWHTDataForPaySel.tdWHTAmountTC
                    tProposedLine.tdWHTAmtLC                    = tDefaultWHTDataForPaySel.tdWHTAmountLC
                    tProposedLine.tdWHTAmtCC                    = tDefaultWHTDataForPaySel.tdWHTAmountCC
                    tProposedLine.tdWHTTCLCExchangeRate         = tDefaultWHTDataForPaySel.tdWHTTCLCExchangeRate
                    tProposedLine.tdWHTTCLCRateScale            = tDefaultWHTDataForPaySel.tdWHTTCLCRateScale
                    tProposedLine.tdWHTTCCCExchangeRate         = tDefaultWHTDataForPaySel.tdWHTTCCCExchangeRate
                    tProposedLine.tdWHTTCCCRateScale            = tDefaultWHTDataForPaySel.tdWHTTCCCRateScale
                    tProposedLine.tdWHTPerc                     = if tDefaultWHTDataForPaySel.tdCInvoiceTotalWHTTaxFeeTC <> 0
                                                                  then abs((tDefaultWHTDataForPaySel.tdWHTAmountTC / 
                                                                            tDefaultWHTDataForPaySel.tdCInvoiceTotalWHTTaxFeeTC) * 100)
                                                                  else 0
                    tProposedLine.tdOpenInvoiceAmountTC         = tProposedLine.tdOpenInvoiceAmountTC - tProposedLine.tdInitiallyAllocatedWHTAmntTC
                    tProposedLine.tdOpenPaymentInvoiceAmountTC  = tProposedLine.tdOpenInvoiceAmountTC - tProposedLine.tdWHTAmtTC
                    tProposedLine.tdOpenAmountTCSigned          = if vcOpenInvoiceAmntDebitCredit = vcCreditTR
                                                                  then - tProposedLine.tdOpenInvoiceAmountTC
                                                                  else tProposedLine.tdOpenInvoiceAmountTC.

        /* ======================================================================================== */
        /* Adjust the balance of the invoice about Wht amount, which is allocated, but not yet paid */
        /* just take the right part of it, needed when you have multiple bank records               */
        /* with the same own banknumber but with a different supplier bank number                   */
        /* ======================================================================================== */
        /* There is only one CInvoiceBank record of the invoice */
        if not vlCInvoiceIsWithOneBank 
        then do:
            /* ========================================================================= *
             * Calculate amount of the WHT tax, that can be allocated with this payment. *
             * Calculation is controlled by the Open balance of the CInvoiceBank         *
             * record and by the sequence of the CInvoiceBank record. Generally all Wht  *
             * should be allocated by the first CInvoiceBank record                      *
             * ========================================================================= */
            <M-19 run GetToPayObjectsForCInvoiceWhtMultBanks
               (input  tProposedLine.tdInitiallyAllocatedWHTAmntTC (idInitiallyAllocatedWHTAmntTC), 
                input-output tProposedLine.tdCInvoiceWHTAmtTC (bdCInvoiceAmountWhtTC), 
                input-output tProposedLine.tdCInvoiceBankToPayTC (bdCInvoiceBankToPayTC), 
                output viExternalReturnStatus (oiReturnStatus)) in BPaymentSelection>
            if viExternalReturnStatus <> 0 then assign oiReturnStatus = viExternalReturnStatus.
            if viExternalReturnStatus <  0 then leave MAIN_BLOCK.
            
            /* if the calculated amount was changed, we have to also change other amounts */
            if tProposedLine.tdCInvoiceWHTAmtTC <> tDefaultWHTDataForPaySel.tdWHTAmountTC
            then assign vdRatio                  = tProposedLine.tdCInvoiceWHTAmtTC / tDefaultWHTDataForPaySel.tdWHTAmountTC
                        tProposedLine.tdWHTAmtTC = tProposedLine.tdCInvoiceWHTAmtTC
                        tProposedLine.tdWHTAmtLC = tProposedLine.tdWHTAmtLC * vdRatio
                        tProposedLine.tdWHTAmtCC = tProposedLine.tdWHTAmtCC * vdRatio
                        tProposedLine.tdWHTAmtLC = <M-66 RoundAmount
                                                      (input  tProposedLine.tdWHTAmtLC (idUnroundedAmount), 
                                                       input  viCompanyLCId (iiCurrencyID), 
                                                       input  ? (icCurrencyCode)) in BPaymentSelection>
                        tProposedLine.tdWHTAmtCC = <M-72 RoundAmount
                                                      (input  tProposedLine.tdWHTAmtCC (idUnroundedAmount), 
                                                       input  viCompanyCCId (iiCurrencyID), 
                                                       input  ? (icCurrencyCode)) in BPaymentSelection>.
                                                       
            /* because of the WHT disturbution with prioritizing of the first CInvoiceBank, it can happen *
             * there is nothing on the proposed line to be payed. So delete it                            */
            if tProposedLine.tdCInvoiceBankToPayTC = 0
            then do:
                delete tProposedLine.
                next.
            end.
        end.
        else assign tProposedLine.tdCInvoiceBankToPayTC = tProposedLine.tdCInvoiceBankToPayTC - tProposedLine.tdInitiallyAllocatedWHTAmntTC.
    end. /* if tqCInvoiceBankForPaymentV01.tlCompanyPropertyIsWHT and */

    if tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-INVOICE} or   /* Creditor Invoice */
       tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION} /* creditor invoice correction */
    then assign tProposedLine.tcObjectType                = if tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-INVOICE}
                                                            then {&PAYMENTSELECTIONTYPE-INVOICE}
                                                            else {&PAYMENTSELECTIONTYPE-INVOICECORR}
                tProposedLine.tlProposedLineCanBeSelected = true.

    if tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or /* Creditor Credit Note */
       tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} /* creditor credit note correction */
    then do:
        assign tProposedLine.tcObjectType = if tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE}
                                            then {&PAYMENTSELECTIONTYPE-CREDITNOTE}
                                            else {&PAYMENTSELECTIONTYPE-CREDITNOTECORR}.
                                            
        if tqCInvoiceBankForPaymentV01.tlCreditorIsIndividualPaymnt
        then assign tProposedLine.tlProposedLineIsSelected = false.
        else assign tProposedLine.tlProposedLineCanBeSelected = true.
    end. /* end if tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or .. */
        
    if tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-ADJUSTMENT}
    then do :
        assign tProposedLine.tcObjectType = {&PAYMENTSELECTIONTYPE-ADJUSTMENT}.
        if tqCInvoiceBankForPaymentV01.tlCreditorIsIndividualPaymnt
        then assign tProposedLine.tlProposedLineIsSelected = false.
        else assign tProposedLine.tlProposedLineCanBeSelected = true.                          
    end. /* if tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-ADJUSTMENT */
    
    /* Existing Prepayment */
    if tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-PREPAYMENT}
    then do:
        assign tProposedLine.tcObjectType = {&PAYMENTSELECTIONTYPE-PREPAYMENTINV}.
        if tqCInvoiceBankForPaymentV01.tlCreditorIsIndividualPaymnt
        then assign tProposedLine.tlProposedLineIsSelected = false.
        else assign tProposedLine.tlProposedLineCanBeSelected = true.  
    end. /* end if tqCInvoiceBankForPaymentV01.tcCInvoiceType = {&INVOICETYPE-PREPAYMENT} */

    /* ============================================ */
    /* Calculate invoice level payment and discount */
    /* ============================================ */

    /* ================================================================================================================== */
    /* 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 tProposedLine.tcObjectType             <> {&PAYMENTSELECTIONTYPE-PREPAYMENTINV} and
       tProposedLine.tlProposedLineIsSelected =  true                                  and
           itPaySelDate                       <> ?                                     and 
           itPaySelDate                       <= tProposedLine.ttDiscountDueDate       and
           tProposedLine.tdDiscountPercentage <> 0                                     and
           tProposedLine.tdDiscountPercentage <> ?
    then assign vdDiscountBase                         = if tqCInvoiceBankForPaymentV01.tlCInvoiceIsDiscTaxAtPaym
                                                         then vdOriginalInvoiceAmountTC * 
                                                              ((tProposedLine.tdOriginalInvoiceAmountNetTC - tProposedLine.tdNonDiscAmtTC) / 
                                                               tProposedLine.tdOriginalInvoiceAmountNetTC) *
                                                              (tProposedLine.tdOpenInvoiceAmountTC / vdOriginalInvoiceAmountTC)
                                                         else ((tProposedLine.tdOriginalInvoiceAmountNetTC - tProposedLine.tdNonDiscAmtTC) *
                                                              (tProposedLine.tdOpenInvoiceAmountTC / vdOriginalInvoiceAmountTC))
                vdInvoiceDiscountAmount                = vdDiscountBase * tProposedLine.tdDiscountPercentage / 100
                vdInvoiceDiscountAmount                = <M-44 RoundAmount
                                                            (input  vdInvoiceDiscountAmount (idUnroundedAmount), 
                                                             input  ? (iiCurrencyID), 
                                                             input  tProposedLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
                vdInvoicePaymentAmount                 = tProposedLine.tdOpenInvoiceAmountTC - tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC - vdInvoiceDiscountAmount - tProposedLine.tdWHTAmtTC
                tProposedLine.tlProposedLineIsSelected = if (abs(tProposedLine.tdOpenInvoiceAmountTC - vdInvoiceDiscountAmount) > tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC) 
                                                         then tProposedLine.tlProposedLineIsSelected
                                                         else false
                tProposedLine.tiBankNumber_ID          = if tProposedLine.tlProposedLineCanBeSelected
                                                         then tProposedLine.tiBankNumber_ID
                                                         else 0.
    else assign vdDiscountBase                         = if tqCInvoiceBankForPaymentV01.tlCInvoiceIsDiscTaxAtPaym
                                                         then vdOriginalInvoiceAmountTC * 
                                                              ((tProposedLine.tdOriginalInvoiceAmountNetTC - tProposedLine.tdNonDiscAmtTC) / 
                                                               tProposedLine.tdOriginalInvoiceAmountNetTC) *
                                                              (tProposedLine.tdOpenInvoiceAmountTC / vdOriginalInvoiceAmountTC)
                                                         else ((tProposedLine.tdOriginalInvoiceAmountNetTC - tProposedLine.tdNonDiscAmtTC) *
                                                              (tProposedLine.tdOpenInvoiceAmountTC / vdOriginalInvoiceAmountTC))
                vdInvoiceDiscountAmount                = vdDiscountBase * (tProposedLine.tdDiscountPercentage) / 100
                vdInvoiceDiscountAmount                = <M-25 RoundAmount
                                                            (input  vdInvoiceDiscountAmount (idUnroundedAmount), 
                                                             input  ? (iiCurrencyID), 
                                                             input  tProposedLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>                                                               
                vdInvoicePaymentAmount                 = tProposedLine.tdOpenInvoiceAmountTC - tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC - vdInvoiceDiscountAmount - tProposedLine.tdWHTAmtTC
                tProposedLine.tlProposedLineIsSelected = if(abs(tProposedLine.tdOpenInvoiceAmountTC) > tqCInvoiceBankForPaymentV01.tdCInvoiceHoldAmountTC) 
                                                         then tProposedLine.tlProposedLineIsSelected
                                                         else false
                tProposedLine.tiBankNumber_ID          = if tProposedLine.tlProposedLineCanBeSelected 
                                                         then tProposedLine.tiBankNumber_ID
                                                         else 0.

    /* =============== */
    /* staged payments */
    /* =============== */
    if tqCInvoiceBankForPaymentV01.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} 
    then do:
        <M-9 run CreateProposedStagedLines
           (input  tqCInvoiceBankForPaymentV01.tiCInvoice_ID (iiCInvoiceID), 
            input  tqCInvoiceBankForPaymentV01.tiBankNumber_ID (iiBankNumberID), 
            input  itDueDate (itDueDate), 
            input  icPaymentSelectionDue (icPaymentSelectionDue), 
            input  tProposedLine.tcInvoiceCurrencyCode (icCInvoiceCurrencyCode), 
            input  tProposedLine.tdOriginalInvoiceAmountTC (idOriginalInvoiceAmountTC), 
            input  tProposedLine.tdOriginalInvoiceAmountNetTC (idOriginalInvoiceAmountNetTC), 
            input  tProposedLine.tdOpenInvoiceAmountTC (idOpenInvoiceAmountTC), 
            input  tProposedLine.tdWHTAmtTC (idWhtAmountTC), 
            input  tProposedLine.tdNonDiscAmtTC (idNonDiscountableAmountTC), 
            input  tProposedLine.tlProposedLineIsSelected (ilIsProposePayment), 
            input  tProposedLine.tlCInvoiceIsDiscTaxAtPaym (ilIsDiscountTaxAtPayment), 
            output tProposedLine.tdPaymentAmountTC (odLineTotalPaymentAmount), 
            output tProposedLine.tdDiscountTC (odLineTotalDiscountAmount), 
            output tProposedLine.tdWHTAmtTC (odLineTotalWhtAmount), 
            output vlDummy (olIsAllStagesSelected), 
            output tProposedLineStage (tProposedLineStage), 
            input  itDisCountRefDueDate (itDisCountRefDueDate), 
            output viExternalReturnStatus (oiReturnStatus)) in BPaymentSelection>
        
        if viExternalReturnStatus <> 0
        then assign oiReturnStatus = viExternalReturnStatus.
        if viExternalReturnStatus < 0
        then leave MAIN_BLOCK.

        assign tProposedLine.tdCInvoiceDiscountTC     = tProposedLine.tdDiscountTC
               tProposedLine.tdAllocatedAmount        = tProposedLine.tdPaymentAmountTC + tProposedLine.tdDiscountTC + tProposedLine.tdWHTAmtTC
               tProposedLine.tlProposedLineIsSelected = (tProposedLine.tdAllocatedAmount <> 0).
    end. /* end if tqCInvoiceBankForPaymentV01.tcPaymentConditionPaymentTyp */
    else do:
        /* =========================================================================================================== *
         * If there is multiple bank accounts for the Invoice, we have to pro-rate calculated values calculated for    *
         * whole invoice to one bank account                                                                           *
         * =========================================================================================================== */
        if not vlCInvoiceIsWithOneBank
        then assign vdRatio = tProposedLine.tdCInvoiceBankToPayTC / tProposedLine.tdOpenInvoiceAmountTC
                    vdInvoiceDiscountAmount = vdInvoiceDiscountAmount * vdRatio
                    vdInvoiceDiscountAmount = <M-17 RoundAmount
                                                 (input  vdInvoiceDiscountAmount (idUnroundedAmount), 
                                                  input  ? (iiCurrencyID), 
                                                  input  tProposedLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>.

        /* ============================== */
        /* Calculate payment and discount */
        /* ============================== */        
        assign tProposedLine.tdCInvoiceDiscountTC = vdInvoiceDiscountAmount.
                
        if tProposedLine.tlProposedLineIsSelected and
           vlProposePayment
        then assign tProposedLine.tdDiscountTC             = vdInvoiceDiscountAmount 
                    tProposedLine.tdPaymentAmountTC        = tProposedLine.tdCInvoiceBankToPayTC - tProposedLine.tdDiscountTC - tProposedLine.tdWHTAmtTC - tProposedLine.tdHoldAmountTC
                    tProposedLine.tdAllocatedAmount        = tProposedLine.tdPaymentAmountTC + tProposedLine.tdDiscountTC + tProposedLine.tdWHTAmtTC.
        else assign tProposedLine.tdPaymentAmountTC        = 0
                    tProposedLine.tdDiscountTC             = 0
                    tProposedLine.tdAllocatedAmount        = 0.
    end. /* end else if tqCInvoiceBankForPaymentV01.tcPaymentConditionPaymentTyp */
end.  /* end for each tqCInvoiceBankForPaymentV01 */

/* ================================================================== */
/* Make sure the Selected is false in case the CanBeSelected is false */
/* ================================================================== */
for each tProposedLine:
    assign tProposedLine.tlProposedLineIsSelected = (tProposedLine.tdAllocatedAmount <> 0).
end. /* for each tProposedLine */

/* ========================================================== */
/* Skip suppliers with debit balance during payment selection */
/* ========================================================== */
for each tProposedLine where 
         tProposedLine.tlProposedLineIsSelected = true
         break by tProposedLine.tcCreditorCode
               by tProposedLine.tcInvoiceCurrencyCode
               by tProposedLine.tiBankNumber_ID:

    if first-of(tProposedLine.tiBankNumber_ID)
    then assign vdPaymentAmt = 0.

    assign vdPaymentAmt = vdPaymentAmt
                        + (if tProposedLine.tcPaymentAmntDebitCreditAbbr = vcDebitTR
                           then tProposedLine.tdCInvoiceBankToPayTC - tProposedLine.tdCInvoiceDiscountTC - tProposedLine.tdWHTAmtTC
                           else - (tProposedLine.tdCInvoiceBankToPayTC - tProposedLine.tdCInvoiceDiscountTC - tProposedLine.tdWHTAmtTC)).
 
    if last-of(tProposedLine.tiBankNumber_ID)
    then do:
        for each btProposedLine where 
                 btProposedLine.tlProposedLineIsSelected = true and
                 btProposedLine.tcCreditorCode           = tProposedLine.tcCreditorCode and
                 btProposedLine.tiBankNumber_ID          = tProposedLine.tiBankNumber_ID and
                 btProposedLine.tcInvoiceCurrencyCode    = tProposedLine.tcInvoiceCurrencyCode:
            
            if vdPaymentAmt <= 0
            then assign btProposedLine.tlProposedLineIsSelected = false
                        btProposedLine.tdAllocatedAmount        = 0
                        btProposedLine.tdPaymentAmountTC        = 0
                        btProposedLine.tdDiscountTC             = 0.
            else if btProposedLine.tlProposedLineCanBeSelected = true
                 then assign btProposedLine.tlProposedLineIsSelected = true.
                                 
        end. /* for each btProposedLine where */
    end. /* if last-of(tProposedLine.tiBankNumber_ID) */
end. /* for each tProposedLine */

END. /* end MAIN_BLOCK: */

/* ========================== */
/* Set default return-status  */
/* ========================== */
if oiReturnStatus = -98
then assign oiReturnStatus = 0.