project QadFinancials > class BBankEntry > method GetPossibleAllocationsCInvoice

Description

this method returns a temp-table containing all allocation possibilities that fit the input criteria of this method., called by UI when pressing Apply on the search criteria window


Parameters


iiCompanyIDinputintegerEntity ID
iiBankStateLineIDinputintegerBankStateLine ID wherefor the allocations need to be proposed
icBankStateLineTcRowidinputcharacterBankStateLine TcRowid wherefor the allocations need to be proposed
icBusinessRelationCodeinputcharacter
icCodeinputcharacterDebtorCreditorEmployeeCode
Code of the debtor/creditor/employee
icReferenceinputcharacterReference of the debtor/creditor/employee
icCurrencyCodeinputcharacterCurrencyCode that should fit the Invoice/Collection/Document. This is not used in case iiCurrencyID is passed
icBankAccountNumberinputcharacterBankAccountNumber that should fit the Creditor-Invoice or the Debtor
ilIncludeAllCompaniesinputlogicalIncludeAllCompanies
iiPeriodYearinputinteger
icJournalCodeinputcharacterDaybook Code
iiVoucherinputintegerVoucher
icBankCurrencyCodeinputcharacterBank Currency Code
itBankStateLineValueDateinputdateValue Date of the Bank State Line
icTSMNumberinputcharacterTSMNumber
tPossibleBankStateAllocoutputtemp-tableTable containing all possibilities that fit the selection critaria.
tPossibleBankStateStageAllocoutputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankEntry.GetPossibleAllocations


program code (program6/bbankentry.p)

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

<Q-30 run CInvoiceByAllocationInfo (all) (Read) (NoCache)
   (input if ilIncludeAllCompanies then ? else iiCompanyID, (CompanyId)
    input icBusinessRelationCode, (BusinessRelationCode)
    input icCode, (CreditorCode)
    input icReference, (CInvoiceRefererence)
    input icCurrencyCode, (CurrencyCode)
    input ?, (CInvoiceID)
    input true, (CInvoiceIsOpen)
    input vdMinimumAmount, (CInvoiceAmountTCMinimum)
    input vdMaximumAmount, (CInvoiceAmountTCMaximum)
    input ?, (PeriodYearMinimum)
    input iiPeriodYear, (PeriodYear)
    input icJournalCode, (JournalCode)
    input iiVoucher, (Voucher)
    input {&MOVEMENTTYPE-INITIAL}, (CInvoiceMovementType)
    input false, (CInvoiceIsSelected)
    input icTSMNumber, (CInvoiceTSMNumber)
    input ?, (CorporateGroupCode)
    input false, (ReasonIsLockPayment)
    input ?, (CompIsOICrossCy)
    output dataset tqCInvoiceByAllocationInfo) in BCInvoice>
    
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 tqCInvoiceByAllocationInfo where
         tqCInvoiceByAllocationInfo.tlCInvoiceIsLockPayment    =  false and
         (tqCInvoiceByAllocationInfo.tdCInvoiceBalanceDebitTC  <> 0     or
          tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> 0)
break by tqCInvoiceByAllocationInfo.tiCInvoice_ID:

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

/* =========================================================== */
/* retrieve the default wht data of the used supplier invoices */
/* =========================================================== */
if can-find(first tDefaultWHTDataForBE)
then do:
    if viBCInvoiceBEID = 0 or
       viBCInvoiceBEID = ?
    then do:
        <I-60 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION   = "yes"
             &CLASS                = "BCInvoice"}>
        assign vlBCInvoiceIsStartedFromBE = true.
    end. /* end if viBCInvoiceBEID = 0 or ... */
    else do:
        <I-73 {bFcOpenInstance
             &CLASS           = "BCInvoice"}>
    end. /* end else if viBCInvoiceBEID = 0 or ... */
    
    <M-78 run DefaultWHTDataBasedOnCInvoice
       (input-output tDefaultWHTDataForBE (tDefaultWHTData), 
        output viExternalReturnStatus (oiReturnStatus)) in BCInvoice>
        
    <I-29 {bFcCloseInstance
         &CLASS           = "BCInvoice"}>
         
    if viExternalReturnStatus <> 0
    then assign oiReturnStatus = viExternalReturnStatus.
    
    if viExternalReturnStatus < 0
    then leave MAIN_BLOCK.
end. /* end can-find(first tDefaultWHTDataForBE */

/* ============================= */
/* Populate possible allocations */
/* ============================= */
for each tqCInvoiceByAllocationInfo  where
         tqCInvoiceByAllocationInfo.tlCInvoiceIsLockPayment    =  false and
         (tqCInvoiceByAllocationInfo.tdCInvoiceBalanceDebitTC  <> 0     or
          tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> 0) 
         no-lock:
    /* =========================================================== */
    /* Check if the banknumber fits the input in case it is filled */
    /* =========================================================== */
    if icBankAccountNumber <> "":U 
    then do :
        <Q-9 assign vlFcQueryRecordsAvailable = BankNumberPrim (NoCache)
           (input ?, (BankNumberId)
            input tqCInvoiceByAllocationInfo.tiCreditor_ID, (ParentObjectId)
            input icBankAccountNumber, (BankNumber)
            input ?, (BankNumberExtension)
            input ?, (BankPayFormatID)) in BBankNumber>
        if vlFcQueryRecordsAvailable = false
        then next.
    end. /* if icBankAccountNumber <> "":U */

    /*  Create output records */
    create tPossibleBankStateAlloc.
    assign tPossibleBankStateAlloc.Company_ID                = tqCInvoiceByAllocationInfo.tiCompany_ID
           tPossibleBankStateAlloc.tcCompanyCode             = tqCInvoiceByAllocationInfo.tcCompanyCode
           tPossibleBankStateAlloc.CInvoice_ID               = tqCInvoiceByAllocationInfo.tiCInvoice_ID
           tPossibleBankStateAlloc.Currency_ID               = ?
           tPossibleBankStateAlloc.tcCurrencyCode            = icBankCurrencyCode
           tPossibleBankStateAlloc.BankStateLine_ID          = iiBankStateLineID
           tPossibleBankStateAlloc.tc_ParentRowid            = icBankStateLineTcRowid
           tPossibleBankStateAlloc.tc_Rowid                  = entry(1,tqCInvoiceByAllocationInfo.tc_rowid)
           tPossibleBankStateAlloc.tlCreditorIsWHT           = tqCInvoiceByAllocationInfo.tlCreditorIsWHT
           tPossibleBankStateAlloc.tcDocBusinessRelationCode = tqCInvoiceByAllocationInfo.tcBusinessRelationCode
           tPossibleBankStateAlloc.tcDocNumberReference      = trim(string(tqCInvoiceByAllocationInfo.tiPeriodYear)) + "/":U + trim(tqCInvoiceByAllocationInfo.tcJournalCode) + "/":U + string(tqCInvoiceByAllocationInfo.tiCInvoiceVoucher,"999999999":U) + "/":U + tqCInvoiceByAllocationInfo.tcCInvoiceReference
           tPossibleBankStateAlloc.ttDocDueDate              = tqCInvoiceByAllocationInfo.ttCInvoiceDueDate
           tPossibleBankStateAlloc.ttDocDiscountDueDate      = tqCInvoiceByAllocationInfo.ttCInvoiceDiscountDueDate
           tPossibleBankStateAlloc.ttDocInvDate              = tqCInvoiceByAllocationInfo.ttCInvoiceDate
           tPossibleBankStateAlloc.tdDocBalance              = if tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> 0 and tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> ? then tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC else tqCInvoiceByAllocationInfo.tdCInvoiceBalanceDebitTC
           tPossibleBankStateAlloc.tdDocOriginal             = abs(tqCInvoiceByAllocationInfo.tdCInvoiceOriginalDebitTC - tqCInvoiceByAllocationInfo.tdCInvoiceOriginalCreditTC)
           tPossibleBankStateAlloc.BankStateAllocOpenBalTC   = if tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> 0 and tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> ? then tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC else tqCInvoiceByAllocationInfo.tdCInvoiceBalanceDebitTC
           tPossibleBankStateAlloc.tcDocBalanceCrDt          = if tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> 0 and tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> ? then {&CREDITDEBITABBREVIATION-CREDIT} else {&CREDITDEBITABBREVIATION-DEBIT}
           tPossibleBankStateAlloc.tdDocVatBase              = abs(tqCInvoiceByAllocationInfo.tdCInvoiceVatBaseDebitTC - tqCInvoiceByAllocationInfo.tdCInvoiceVatBaseCreditTC)
           tPossibleBankStateAlloc.tcDocVatBaseCrDt          = if tqCInvoiceByAllocationInfo.tdCInvoiceVatBaseDebitTC  <> 0 and tqCInvoiceByAllocationInfo.tdCInvoiceVatBaseDebitTC  <> ? then {&CREDITDEBITABBREVIATION-DEBIT}  else {&CREDITDEBITABBREVIATION-CREDIT}
           tPossibleBankStateAlloc.tdDocVat                  = abs(tqCInvoiceByAllocationInfo.tdCInvoiceVatDebitTC - tqCInvoiceByAllocationInfo.tdCInvoiceVatCreditTC)
           tPossibleBankStateAlloc.tcDocVatCrDt              = if tqCInvoiceByAllocationInfo.tdCInvoiceVatDebitTC      <> 0 and tqCInvoiceByAllocationInfo.tdCInvoiceVatDebitTC      <> ? then {&CREDITDEBITABBREVIATION-DEBIT}  else {&CREDITDEBITABBREVIATION-CREDIT}
           tPossibleBankStateAlloc.tcDocCurrencyCode         = tqCInvoiceByAllocationInfo.tcCurrencyCode
           tPossibleBankStateAlloc.tiDocPostingID            = tqCInvoiceByAllocationInfo.tiPosting_ID
           tPossibleBankStateAlloc.tcLocalCurrencyCode       = vcCompanyLC
           tPossibleBankStateAlloc.tdDocExchangeRate         = tqCInvoiceByAllocationInfo.tdCInvoiceExchangeRate
           tPossibleBankStateAlloc.tdDocExchangeRateScale    = tqCInvoiceByAllocationInfo.tdCinvoiceRateScale
           tPossibleBankStateAlloc.tdDocCCExchangeRate       = tqCInvoiceByAllocationInfo.tdCInvoiceCCRate
           tPossibleBankStateAlloc.tdDocCCExchangeRateScale  = tqCInvoiceByAllocationInfo.tdCInvoiceCCScale
           tPossibleBankStateAlloc.tcDocTSMNumber            = tqCInvoiceByAllocationInfo.tcCInvoiceTSMNumber
           tPossibleBankStateAlloc.tcNewBalanceCrDt          = tPossibleBankStateAlloc.tcDocBalanceCrDt
           tPossibleBankStateAlloc.tdNewBalanceTC            = if tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> 0 and  tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> ? then tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC else tqCInvoiceByAllocationInfo.tdCInvoiceBalanceDebitTC
           tPossibleBankStateAlloc.tcAllocAmountCrDt         = (if tPossibleBankStateAlloc.tcDocBalanceCrDt = {&CREDITDEBITABBREVIATION-CREDIT} then {&CREDITDEBITABBREVIATION-DEBIT} else {&CREDITDEBITABBREVIATION-CREDIT})
           /* fields must be filled, otherwise errors on save */
           tPossibleBankStateAlloc.tcCurrDifferenceCrDt      = {&CREDITDEBITABBREVIATION-CREDIT}
           tPossibleBankStateAlloc.tcCCCurrDifferenceCrDt    = {&CREDITDEBITABBREVIATION-CREDIT}
           tPossibleBankStateAlloc.tcCollBalanceCrDt         = {&CREDITDEBITABBREVIATION-CREDIT}
           tPossibleBankStateAlloc.BankStateAllocType        = "?":U
           tPossibleBankStateAlloc.BankStateAllocBankRate    = vdBankRate
           tPossibleBankStateAlloc.BankStateAllocBankRteScale = vdBankRateScale
           tPossibleBankStateAlloc.tcReasonCode              = tqCInvoiceByAllocationInfo.tcReasonCode
           tPossibleBankStateAlloc.tcDInvoiceDIText          = "":U /*tqCInvoiceByAllocationInfo.tcCInvoiceCIText*/
           tPossibleBankStateAlloc.tcInvoiceDescription      = tqCInvoiceByAllocationInfo.tcCInvoiceDescription
           tPossibleBankStateAlloc.tdBankStateAllocOpenBalTCSign = if tPossibleBankStateAlloc.tcDocBalanceCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                                                   then tPossibleBankStateAlloc.BankStateAllocOpenBalTC
                                                                   else (0 - tPossibleBankStateAlloc.BankStateAllocOpenBalTC)
           tPossibleBankStateAlloc.tdDiscountPercentage      = if /*tqCInvoiceByAllocationInfo.tiDiscPaymentCondition_ID <> 0 and tqCInvoiceByAllocationInfo.tiDiscPaymentCondition_ID <> ? and*/
                                                                  itBankStateLineValueDate <= tqCInvoiceByAllocationInfo.ttCInvoiceDiscountDueDate
                                                               then tqCInvoiceByAllocationInfo.tdPaymentConditionPercentage
                                                               else 0
           vdInvoiceAmount                                   = if tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> 0 and
                                                                  tqCInvoiceByAllocationInfo.tdCInvoiceBalanceCreditTC <> ?
                                                               then tqCInvoiceByAllocationInfo.tdCInvoiceOriginalCreditTC
                                                               else tqCInvoiceByAllocationInfo.tdCInvoiceOriginalDebitTC
           tPossibleBankStateAlloc.tdNonDiscAmtTC            = tqCInvoiceByAllocationInfo.tdCInvoiceNonDiscAmtTC
           /* if not wht is used, then give initial value */
           tPossibleBankStateAlloc.tlUpdateTax                   = false
           tPossibleBankStateAlloc.tdInitiallyAllocatedWHTAmntTC = 0
           tPossibleBankStateAlloc.ttDocPostingDate              = tqCInvoiceByAllocationInfo.ttCInvoicePostingDate
           tPossibleBankStateAlloc.tcDocDescription              = tqCInvoiceByAllocationInfo.tcCInvoiceDescription.
                                                               
    do viCounter = num-entries(vcInvoiceTypes,chr(2)) to 1 by -2 :
        if entry(viCounter,vcInvoiceTypes,chr(2)) = tqCInvoiceByAllocationInfo.tcCInvoiceType
        then do :
            /* put untranslatable value in it */
            assign tPossibleBankStateAlloc.BankStateAllocType = entry(viCounter,vcInvoiceTypes,chr(2)) no-error.
            leave.
        end. /* if entry */
    end.

    /* ===================================== */
    /* everything related to withholding tax */
    /* ===================================== */
    if tqCInvoiceByAllocationInfo.tlCompanyPropertyIsWHT and
       tqCInvoiceByAllocationInfo.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 tPossibleBankStateAlloc  */
        /* ================================================================================================= */
        find first tDefaultWHTDataForBE where
                   tDefaultWHTDataForBE.tiCInvoiceId = tqCInvoiceByAllocationInfo.tiCInvoice_ID
                   no-lock no-error.
        if available tDefaultWHTDataForBE
        then assign tPossibleBankStateAlloc.tdCInvoiceTotWHTBaseTC        = tDefaultWHTDataForBE.tdCInvoiceTotalWHTTaxFeeTC
                    tPossibleBankStateAlloc.tdCInvoiceWHTUnpaidAmtTC      = tDefaultWHTDataForBE.tdUnpaidWHTAmountTC
                    tPossibleBankStateAlloc.tlUpdateTax                   = tDefaultWHTDataForBE.tlUpdateTaxIsAllowed
                    tPossibleBankStateAlloc.tdCInvoiceTotWHTTC            = tDefaultWHTDataForBE.tdCInvoiceTotalWHTAmountTC
                    tPossibleBankStateAlloc.tdWHTBalanceTC                = tDefaultWHTDataForBE.tdWHTAmountTC
                    tPossibleBankStateAlloc.tdInitiallyAllocatedWHTAmntTC = tDefaultWHTDataForBE.tdInitiallyAllocatedWHTAmountTC
                    tPossibleBankStateAlloc.BankStateAllocWHTTxFeeTC      = tDefaultWHTDataForBE.tdCInvoiceTotalWHTTaxFeeTC
                    tPossibleBankStateAlloc.BankStateAllocWHTAmtTC        = 0 /* tDefaultWHTDataForBE.tdWHTAmountTC --> Default WHT should be 0, since nothing is allocated yet. */
                    tPossibleBankStateAlloc.BankStateAllocWHTAmtLC        = 0 /* tDefaultWHTDataForBE.tdWHTAmountLC --> Default WHT should be 0, since nothing is allocated yet. */
                    tPossibleBankStateAlloc.BankStateAllocWHTAmtCC        = 0 /* tDefaultWHTDataForBE.tdWHTAmountCC --> Default WHT should be 0, since nothing is allocated yet. */
                    tPossibleBankStateAlloc.BankStateAllocWHTLCRate       = tDefaultWHTDataForBE.tdWHTTCLCExchangeRate
                    tPossibleBankStateAlloc.BankStateAllocWHTLCScale      = tDefaultWHTDataForBE.tdWHTTCLCRateScale
                    tPossibleBankStateAlloc.BankStateAllocWHTCCRate       = tDefaultWHTDataForBE.tdWHTTCCCExchangeRate
                    tPossibleBankStateAlloc.BankStateAllocWHTCCScale      = tDefaultWHTDataForBE.tdWHTTCCCRateScale
                    tPossibleBankStateAlloc.BankStateAllocWHTPerc         = if tDefaultWHTDataForBE.tdCInvoiceTotalWHTTaxFeeTC <> 0
                                                                            then abs((tDefaultWHTDataForBE.tdWHTAmountTC / 
                                                                                      tDefaultWHTDataForBE.tdCInvoiceTotalWHTTaxFeeTC) * 100)
                                                                            else 0.

        /* ======================================================================================== */
        /* Adjust the balance of the invoice about Wht amount, which is allocated, but not yet paid */
        /* ======================================================================================== */
        assign vdInvoiceAmount                                 = vdInvoiceAmount - tPossibleBankStateAlloc.tdInitiallyAllocatedWHTAmntTC
               tPossibleBankStateAlloc.tdDocBalance            = tPossibleBankStateAlloc.tdDocBalance - tPossibleBankStateAlloc.tdInitiallyAllocatedWHTAmntTC
               tPossibleBankStateAlloc.BankStateAllocOpenBalTC = tPossibleBankStateAlloc.BankStateAllocOpenBalTC - tPossibleBankStateAlloc.tdInitiallyAllocatedWHTAmntTC
               tPossibleBankStateAlloc.tdNewBalanceTC          = tPossibleBankStateAlloc.tdDocBalance.
    end. /* if tqCInvoiceBankByAllocationInfo.tlCompanyPropertyIsWHT and */

    /* Miz FIN-2314 ====================================================================================== *
     * Calculate correct amount of LC and CC for the invoice                                               *
     * =================================================================================================== */
    <M-66 run GetPossibleAllocationsCInvoiceCrossCy
       (input  iiCompanyID (iiCompanyID), 
        output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave MAIN_BLOCK.


    assign tPossibleBankStateAlloc.tlDiscountTaxAtPayment = false.
    
    <Q-87 run CInvoiceVatByCInvoiceId (all) (Read) (NoCache)
       (input tqCInvoiceByAllocationInfo.tiCInvoice_ID, (CInvoiceId)
        output dataset tqCInvoiceVatByCInvoiceId) in BCInvoice>
    for each tqCInvoiceVatByCInvoiceId where
             tqCInvoiceVatByCInvoiceId.tiCInvoice_ID = tqCInvoiceByAllocationInfo.tiCInvoice_ID :
         <Q-42 assign vlFcQueryRecordsAvailable = MfgTaxCodeByTaxCode (NoCache)
            (input tqCInvoiceVatByCInvoiceId.tcVatCode, (TaxCode)
             input tqCInvoiceVatByCInvoiceId.tcDomainCode, (DomainCode)
             input true, (DiscountAtPayment)
             input ?, (IsTaxByLine)) in BMfgTaxCode >
         if vlFcQueryRecordsAvailable = yes or vlFcQueryRecordsAvailable = ?
         then do:
            assign tPossibleBankStateAlloc.tlDiscountTaxAtPayment = true.
            leave.
         end.
    end. 
 
    if tqCInvoiceByAllocationInfo.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} or
       tqCInvoiceByAllocationInfo.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
    then assign tPossibleBankStateAlloc.tdDocVat      = tPossibleBankStateAlloc.tdDocVat      * -1
                tPossibleBankStateAlloc.tdDocVatBase  = tPossibleBankStateAlloc.tdDocVatBase  * -1
                tPossibleBankStateAlloc.tdDocOriginal = tPossibleBankStateAlloc.tdDocOriginal * -1.

    /* ================================================== */
    /* Create extra records for staged payment conditions */
    /* ================================================== */
    assign vdTotalWhtAmountTC = tPossibleBankStateAlloc.tdWHTBalanceTC.
    if tqCInvoiceByAllocationInfo.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} 
    then do:
        assign viCount                        = 0
               vdTotalDefaultPaymentAmountTC  = 0
               vdTotalDefaultDiscountAmountTC = 0.
        
        /* Calculate discountable amount of whole invoice */
        if tPossibleBankStateAlloc.tlDiscountTaxAtPayment
        then assign vdDiscountableInvoiceTC = tPossibleBankStateAlloc.tdDocOriginal - tPossibleBankStateAlloc.tdNonDiscAmtTC.
        else assign vdDiscountableInvoiceTC = tPossibleBankStateAlloc.tdDocOriginal - tPossibleBankStateAlloc.tdNonDiscAmtTC - tPossibleBankStateAlloc.tdDocVat.

        <Q-37 run CInvoiceStageByDueDate (all) (Read) (NoCache)
           (input tqCInvoiceByAllocationInfo.tiCInvoice_ID, (CInvoice_ID)
            input ?, (DueDate)
            output dataset tqCInvoiceStageByDueDate) in BCInvoice>
    
        for last tPossibleBankStateStageAlloc:
            assign viCount = integer(tPossibleBankStateStageAlloc.tc_Rowid) no-error.
        end.    
        
        for each tqCInvoiceStageByDueDate where
                 tqCInvoiceStageByDueDate.tiCInvoice_ID = tqCInvoiceByAllocationInfo.tiCInvoice_ID
                 break by tqCInvoiceStageByDueDate.tiCInvoice_ID
                       by tqCInvoiceStageByDueDate.ttCInvoiceStageDueDate:
            
            assign viCount = viCount + 1.

            create tPossibleBankStateStageAlloc.
            
            assign tPossibleBankStateStageAlloc.tc_ParentRowid        = tPossibleBankStateAlloc.tc_Rowid
                   /* Fin-3808 + Fin-2913: This combination of the parent-rowid and the count is needed to avoid that we get duplicate records (tc_rowid) in tDDocPossibleAllocationStages */ 
                   tPossibleBankStateStageAlloc.tc_Rowid              = string(tqCInvoiceStageByDueDate.tc_rowid,"X(20)":U) + string(viCount) 
                   tPossibleBankStateStageAlloc.CInvoiceStage_ID      = tqCInvoiceStageByDueDate.tiCInvoiceStage_ID
                   tPossibleBankStateStageAlloc.tdDefaultDiscountPerc = tqCInvoiceStageByDueDate.tdCInvoiceStageDiscPerc
                   tPossibleBankStateStageAlloc.ttDefaultDueDate      = tqCInvoiceStageByDueDate.ttCInvoiceStageDueDate
                   tPossibleBankStateStageAlloc.ttDefaultDiscDate     = tqCInvoiceStageByDueDate.ttCInvoiceStageDiscDate
                   vdDefaultAllocAmountTC                             = tqCInvoiceStageByDueDate.tdCInvoiceStageAmountTC - tqCInvoiceStageByDueDate.tdCInvoiceStageAmtAppliedTC.
            
            /* Distribute Wht amount to stages */
            if not last-of(tqCInvoiceStageByDueDate.tiCInvoice_ID)
            then assign tPossibleBankStateStageAlloc.BankStateStageAllocWHTTC  = tPossibleBankStateAlloc.tdCInvoiceTotWHTTC
                                                                                 * vdDefaultAllocAmountTC
                                                                                 / tPossibleBankStateAlloc.tdDocOriginal
                        tPossibleBankStateStageAlloc.BankStateStageAllocWHTTC  = <M-56 RoundAmount
                                                                                  (input  tPossibleBankStateStageAlloc.BankStateStageAllocWHTTC (idUnroundedAmount), 
                                                                                   input  ? (iiCurrencyID), 
                                                                                   input  tPossibleBankStateAlloc.tcCurrencyCode (icCurrencyCode)) in BBankEntry>
                        vdTotalWhtAmountTC                                     = vdTotalWhtAmountTC - tPossibleBankStateStageAlloc.BankStateStageAllocWHTTC.
            else assign tPossibleBankStateStageAlloc.BankStateStageAllocWHTTC  = vdTotalWhtAmountTC
                        vdTotalWhtAmountTC                                     = 0.
                        
            if itBankStateLineValueDate <= tqCInvoiceStageByDueDate.ttCInvoiceStageDiscDate
            then assign tPossibleBankStateStageAlloc.tdDefaultDiscountAmountTC = vdDiscountableInvoiceTC * (vdDefaultAllocAmountTC / tPossibleBankStateAlloc.tdDocOriginal)
                                                                               * (tqCInvoiceStageByDueDate.tdCInvoiceStageDiscPerc / 100)
                        tPossibleBankStateStageAlloc.tdDefaultDiscountAmountTC = <M-61 RoundAmount
                                                                                    (input  tPossibleBankStateStageAlloc.tdDefaultDiscountAmountTC (idUnroundedAmount), 
                                                                                     input  tqCInvoiceByAllocationInfo.tiCInvoiceCurrency_ID (iiCurrencyID), 
                                                                                     input  tqCInvoiceByAllocationInfo.tcCurrencyCode (icCurrencyCode)) in BBankEntry>
                        tPossibleBankStateStageAlloc.tdDefaultPaymentAmountTC  = vdDefaultAllocAmountTC 
                                                                                 - tPossibleBankStateStageAlloc.tdDefaultDiscountAmountTC 
                                                                                 - tPossibleBankStateStageAlloc.BankStateStageAllocWHTTC.
            else assign tPossibleBankStateStageAlloc.tdDefaultDiscountAmountTC = 0
                        tPossibleBankStateStageAlloc.tdDefaultPaymentAmountTC  = vdDefaultAllocAmountTC
                                                                                 - tPossibleBankStateStageAlloc.BankStateStageAllocWHTTC.                                                                                             
                            
            assign vdTotalDefaultPaymentAmountTC                         = vdTotalDefaultPaymentAmountTC  + tPossibleBankStateStageAlloc.tdDefaultPaymentAmountTC
                   vdTotalDefaultDiscountAmountTC                        = vdTotalDefaultDiscountAmountTC + tPossibleBankStateStageAlloc.tdDefaultDiscountAmountTC
                   tPossibleBankStateStageAlloc.tdDefaultWhtAmtTC        = tPossibleBankStateStageAlloc.BankStateStageAllocWHTTC
                   tPossibleBankStateStageAlloc.tdDefaultAllocAmountTC   = vdDefaultAllocAmountTC
                   tPossibleBankStateStageAlloc.BankStateStageAllocWHTTC = 0 /* --> Default WHT should be 0, since nothing is allocated yet. */.
        end. /* for each tqCInvoiceStageByDueDate */      
        
        assign tPossibleBankStateAlloc.tdDefaultPaymentAmountTC  = vdTotalDefaultPaymentAmountTC
               tPossibleBankStateAlloc.tdDefaultDiscountAmountTC = vdTotalDefaultDiscountAmountTC.                                     
    end. /* PAYMENTCONDITIONPAYMENTTYPE-STAGED */
    else 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 and with the intially allocated WHT  that is not yet paid - Non-discountable amount) *  */
        /*                      Discount % *                                                                                                        */
        /*                     (Invoice open amount / Invoice amount with Tax and with the intially allocated WHT  that is not yet paid).           */
        /* ======================================================================================================================================== */
        if itBankStateLineValueDate <= tqCInvoiceByAllocationInfo.ttCInvoiceDiscountDueDate
        then do:                    
            assign vdDiscountBaseTC  = if tPossibleBankStateAlloc.tlDiscountTaxAtPayment 
                                       then vdInvoiceAmount *
                                           (((vdInvoiceAmount - tPossibleBankStateAlloc.tdDocVat) - tPossibleBankStateAlloc.tdNonDiscAmtTC) / 
                                            (vdInvoiceAmount - tPossibleBankStateAlloc.tdDocVat)) *
                                             (tPossibleBankStateAlloc.BankStateAllocOpenBalTC / vdInvoiceAmount)
                                      else  ((vdInvoiceAmount + tPossibleBankStateAlloc.tdInitiallyAllocatedWHTAmntTC - tPossibleBankStateAlloc.tdDocVat) - tPossibleBankStateAlloc.tdNonDiscAmtTC) *
                                              (tPossibleBankStateAlloc.BankStateAllocOpenBalTC / (vdInvoiceAmount + tPossibleBankStateAlloc.tdInitiallyAllocatedWHTAmntTC))

                   tPossibleBankStateAlloc.tdDefaultDiscountAmountTC = <M-55 RoundAmount
                                                                          (input  (tqCInvoiceByAllocationInfo.tdPaymentConditionPercentage / 100) * vdDiscountBaseTC (idUnroundedAmount), 
                                                                           input  0 (iiCurrencyID), 
                                                                           input  tPossibleBankStateAlloc.tcDocCurrencyCode (icCurrencyCode)) in BApplicationProperty>
                   tPossibleBankStateAlloc.tdDefaultPaymentAmountTC  = tPossibleBankStateAlloc.BankStateAllocOpenBalTC
                                                                       - tPossibleBankStateAlloc.tdDefaultDiscountAmountTC
                                                                       - tPossibleBankStateAlloc.tdWHTBalanceTC.
        end.                                                               
        else assign tPossibleBankStateAlloc.tdDefaultPaymentAmountTC  = tPossibleBankStateAlloc.BankStateAllocOpenBalTC
                                                                        - tPossibleBankStateAlloc.tdWHTBalanceTC
                    tPossibleBankStateAlloc.tdDefaultDiscountAmountTC = 0.
    end. /* NOT PAYMENTCONDITIONPAYMENTTYPE-STAGED */
end. /* for each tqCInvoiceByAllocationInfo no-lock */
END. /* end MAIN_BLOCK: */

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