project QadFinancials > class BOpenItemAdjustment > method OIAdjApplyFilterCreditor

Description

submethod of OIAdjApplyFilter; supplier part


Parameters


iiCompanyIdinputintegerEntity ID
itPostingDateinputdatePosting date
icCredDebEmplCodeinputcharacterDebtorCreditorEmployeeCode
Code of the debtor/creditor/employee
icBusinessRelationCodeinputcharacterBusiness Relation Code
icInvoiceNumberReferenceinputcharacterReference of the debtor/creditor/employee invoice
idMinimumAmountinputdecimalMinimum Amount that should fit the Invoice/Collection/Document
idMaximumAmountinputdecimalMaximum Amount that should fit the Invoice/Collection/Document
icCurrencyCodeinputcharacterCurrencyCode that should fit the Invoice/Collection/Document. This is not used in case iiCurrencyID is passed
ilIncludeOverAllocatedinputlogicalIncludeOverAllocated Items
ilIncludeAllCompaniesinputlogicalIncludeAllCompanies
ilIncludeClosedInvoicesinputlogical
iiPeriodYearMinimuminputinteger
iiPeriodYearinputinteger
icJournalCodeinputcharacterDaybook Code
iiVoucherinputintegerVoucher
icCorporateGroupCodeinputcharacter
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BOpenItemAdjustment.OIAdjApplyFilter


program code (program7/bopenitemadjustment.p)

/* State the CreditorID when there is only one creditor with that Code */
assign viCreditorID = ?.
if icCredDebEmplCode <> '':U and 
   icCredDebEmplCode <> ?
then do :
    <Q-93 run CreditorPrim (all) (Read) (NoCache)
       (input ?, (CompanyId)
        input ?, (CreditorId)
        input icCredDebEmplCode, (CreditorCode)
        output dataset tqCreditorPrim) in BCreditor>
    find tqCreditorPrim where /* DON'T USE THE FIRST OPTION!!!!! */
    	 tqCreditorPrim.tcCreditorCode = icCredDebEmplCode 
    	 no-error.
    if available tqCreditorPrim
    then  assign viCreditorID = tqCreditorPrim.tiCreditor_ID.
end. /* if icCredDebEmplCode <> ? and  */

/* Launch query on CI */
 <Q-56 run CInvoiceByAllocationInfoNew (all) (Read) (NoCache)
    (input if ilIncludeAllCompanies then ? else iiCompanyID, (CompanyId)
     input icBusinessRelationCode, (BusinessRelationCode)
     input idMaximumAmount, (CInvoiceAmountTCMaximum)
     input idMinimumAmount, (CInvoiceAmountTCMinimum)
     input ?, (CInvoiceID)
     input (if ilIncludeClosedInvoices = true then ? else true), (CInvoiceIsOpen)
     input false, (CInvoiceIsSelected)
     input {&MOVEMENTTYPE-INITIAL}, (CInvoiceMovementType)
     input icInvoiceNumberReference, (CInvoiceRefererence)
     input ?, (CInvoiceTSMNumber)
     input icCorporateGroupCode, (CorporateGroupCode)
     input icCredDebEmplCode, (CreditorCode)
     input icCurrencyCode, (CurrencyCode)
     input icJournalCode, (JournalCode)
     input iiPeriodYear, (PeriodYear)
     input iiPeriodYearMinimum, (PeriodYearMinimum)
     input iiVoucher, (Voucher)
     input if ilIncludeAllCompanies then true else ?, (CompIsOICrossCy)
     input ?, (ReasonIsLockPayment)
     input viCreditorID, (CreditorID)
     output dataset tqCInvoiceByAllocationInfoNew) 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 tOIAdj                                                                             */
    /* ============================================================================================ */
    for each tqCInvoiceByAllocationInfoNew  
             break by tqCInvoiceByAllocationInfoNew.tiCInvoice_ID:
             
        if first-of(tqCInvoiceByAllocationInfoNew.tiCInvoice_ID) and
           tqCInvoiceByAllocationInfoNew.tlCompanyPropertyIsWHT and
           tqCInvoiceByAllocationInfoNew.tlCInvoiceIsWHT
        then do:
            create tDefaultWHTDataForOIA.
            assign tDefaultWHTDataForOIA.tiCInvoiceID        = tqCInvoiceByAllocationInfoNew.tiCInvoice_ID
                   tDefaultWHTDataForOIA.tiCInvoiceCompanyID = tqCInvoiceByAllocationInfoNew.tiCompany_ID.
        end. /* end first-of(tqCInvoiceByAllocationInfoNew.tiCInvoice_ID) .. */
    end. /* end for each tqCInvoiceByAllocationInfoNew .. */
    
    /* =========================================================== */
    /* retrieve the default wht data of the used supplier invoices */
    /* =========================================================== */
    if can-find(first tDefaultWHTDataForOIA)
    then do:
        if viBCInvoiceInOIAID = 0 or
           viBCInvoiceInOIAID = ?
        then do:
            <I-22 {bFcStartAndOpenInstance
                 &ADD-TO-TRANSACTION   = "true"
                 &CLASS                = "BCInvoice"}>
            assign vlBCInvoiceIsStartedFromOIAdj = true.
        end. /* end if viBCInvoiceInOIAID = 0 or ... */
        else do:
            <I-29 {bFcOpenInstance
                 &CLASS           = "BCInvoice"}>
        end. /* end else if viBCInvoiceInOIAID = 0 or ... */
        <M-79 run DefaultWHTDataBasedOnCInvoice
           (input-output tDefaultWHTDataForOIA (tDefaultWHTData), 
            output viExternalReturnStatus (oiReturnStatus)) in BCInvoice>
        <I-34 {bFcCloseInstance
             &CLASS           = "BCInvoice"}>
        if viExternalReturnStatus <> 0
        then assign oiReturnStatus = viExternalReturnStatus.
        if viExternalReturnStatus < 0
        then leave MAIN_BLOCK.
    end. /* end can-find(first tDefaultWHTDataForOIA */

    /* ============================================ */
    /* Create the records in the output temp-tables */
    /* ============================================ */
    for each tqCInvoiceByAllocationInfoNew no-lock :
        assign vdBalanceTC      = tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceDebitTC - tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC
               vlOverAllocation = (tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalDebitTC  > 0  AND  vdBalanceTC < 0)  OR
                                  (tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalDebitTC  < 0  AND  vdBalanceTC > 0)  OR
                                  (tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalCreditTC > 0  AND  vdBalanceTC > 0)  OR
                                  (tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalCreditTC < 0  AND  vdBalanceTC < 0).
        if ilIncludeOverAllocated  =  false     or
           (ilIncludeOverAllocated = true and
            vlOverAllocation       = true )  
        then do:
            create  tOIAdj.
            assign  viRowidOIAdj                         = viRowidOIAdj + 1
                    tOIAdj.tc_Rowid                      = string(viRowidOIAdj) 
                    tOIAdj.tiCompanyID                   = tqCInvoiceByAllocationInfoNew.tiCompany_ID
                    tOIAdj.tiInvoiceID                   = tqCInvoiceByAllocationInfoNew.tiCInvoice_ID
                    tOIAdj.tcReasonCode                  = tqCInvoiceByAllocationInfoNew.tcReasonCode
                    tOIAdj.tcBusinessRelationCode        = tqCInvoiceByAllocationInfoNew.tcBusinessRelationCode
                    tOIAdj.tcCredDebEmplCode             = tqCInvoiceByAllocationInfoNew.tcCreditorCode
                    tOIAdj.tlCreditorIsWHT               = tqCInvoiceByAllocationInfoNew.tlCreditorIsWHT
                    tOIAdj.tcInvoiceCurrencyCode         = tqCInvoiceByAllocationInfoNew.tcCurrencyCode
                    tOIAdj.tcInvoiceNumberReference      = trim(string(tqCInvoiceByAllocationInfoNew.tiPeriodYear)) + "/":U + trim(tqCInvoiceByAllocationInfoNew.tcJournalCode) + "/":U + string(tqCInvoiceByAllocationInfoNew.tiCInvoiceVoucher,"999999999":U)
                    tOIAdj.tcInvoiceOrigin               = {&INVOICEORIGIN-CREDITOR}
                    tOIAdj.tcInvoiceType                 = tqCInvoiceByAllocationInfoNew.tcCInvoiceType
                    tOIAdj.ttInvoiceNormalDueDate        = tqCInvoiceByAllocationInfoNew.ttCInvoiceDueDate 
                    tOIAdj.ttInvoiceDate                 = tqCInvoiceByAllocationInfoNew.ttCInvoiceDate 
                    tOIAdj.tdInvoiceExchangeRate         = tqCInvoiceByAllocationInfoNew.tdCInvoiceExchangeRate
                    tOIAdj.tdInvoiceExchangeRateScale    = tqCInvoiceByAllocationInfoNew.tdCinvoiceRateScale
                    tOIAdj.tcInvoiceBalanceCrDt          = if tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC <> 0 and
                                                              tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC <> ?
                                                           then {&CREDITDEBITABBREVIATION-CREDIT}                        
                                                           else 
                                                           if tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceDebitTC <> 0 and
                                                              tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceDebitTC <> ?
                                                           then {&CREDITDEBITABBREVIATION-DEBIT}
                                                           else
                                                           if tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalCreditTC <> 0 and
                                                              tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalCreditTC <> ?
                                                           then {&CREDITDEBITABBREVIATION-CREDIT}
                                                           else {&CREDITDEBITABBREVIATION-DEBIT}
                    tOIAdj.tdInvoiceBalanceTC            = if tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC <> 0 and   
                                                              tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC <> ? 
                                                           then tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC 
                                                           else 
                                                           if tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceDebitTC <> 0 and   
                                                              tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceDebitTC <> ? 
                                                           then tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceDebitTC
                                                           else 0
                    tOIAdj.tlIsNewOpenItem               = false
                    tOIAdj.tlIsOverAllocated             = vlOverAllocation
                    tOIAdj.tdAdjustmentAmountLC          = 0
                    tOIAdj.tdAdjustmentAmountTC          = 0
                    tOIAdj.tcAdjustmentCrDt              =  if tOIAdj.tdInvoiceBalanceTC = 0
                                                            then tOIAdj.tcInvoiceBalanceCrDt
                                                            else
                                                            if tOIAdj.tcInvoiceBalanceCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                                            then {&CREDITDEBITABBREVIATION-CREDIT}
                                                            else {&CREDITDEBITABBREVIATION-DEBIT}               
                    tOIAdj.tcCorporateGroupCode          = tqCInvoiceByAllocationInfoNew.tcCorporateGroupCode
                    tOIAdj.tcNewBalanceCrDt              = if tqCInvoiceByAllocationInfoNew.tlCInvoiceIsOpen 
                                                           then tOIAdj.tcInvoiceBalanceCrDt
                                                           else if tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalDebitTC <> 0
                                                                then {&CREDITDEBITABBREVIATION-DEBIT} 
                                                                else {&CREDITDEBITABBREVIATION-CREDIT}
                    tOIAdj.tdNewBalanceTC                = tOIAdj.tdInvoiceBalanceTC
                    tOIAdj.tdCInvoiceHoldAmountTC        = tqCInvoiceByAllocationInfoNew.tdCInvoiceHoldAmountTC
                    tOIAdj.tdAdjustmentExchangeRate      = tqCInvoiceByAllocationInfoNew.tdCInvoiceExchangeRate
                    tOIAdj.tdAdjustmentExchangeRateScale = tqCInvoiceByAllocationInfoNew.tdCInvoiceRateScale
                    tOIAdj.tdAdjustmentCCExchangeRate    = tqCInvoiceByAllocationInfoNew.tdCInvoiceCCRate
                    tOIAdj.tdAdjustmentCCExchangeScale   = tqCInvoiceByAllocationInfoNew.tdCInvoiceCCScale
                    tOIAdj.tlIsDiscountTaxAtPayment      = false
                    tOIAdj.tlIsStagedPaymentCondition    = if tqCInvoiceByAllocationInfoNew.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
                                                           then true
                                                           else false
                    tOIAdj.tdInvoiceDiscountPercentage   = (if tqCInvoiceByAllocationInfoNew.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} or
                                                               tqCInvoiceByAllocationInfoNew.tdPaymentConditionPercentage = ? 
                                                            then 0 
                                                            else tqCInvoiceByAllocationInfoNew.tdPaymentConditionPercentage)
                    vdInvoiceAmount                      = if tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC <> 0 and
                                                              tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC <> ?
                                                           then tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalCreditTC
                                                           else tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalDebitTC
                    tOIAdj.ttInvoiceDiscountDueDate      = tqCInvoiceByAllocationInfoNew.ttCInvoiceDiscountDueDate
                    tOIAdj.tdInvoiceOriginalBalanceTC    = (if tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalCreditTC <> 0 and tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalCreditTC <> ? then tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalCreditTC else tqCInvoiceByAllocationInfoNew.tdCInvoiceOriginalDebitTC)
                    tOIAdj.tiInvoicePostingID            = tqCInvoiceByAllocationInfoNew.tiPosting_ID
                    tOIAdj.tdAdjustmentDiscountAmountTC  = 0
                    tOIAdj.tdAdjustmentDiscountAmountLC  = 0
                    tOIAdj.tdInvoiceBalanceLC            = (if tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditLC <> 0 and tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditLC <> ? then tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditLC else tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceDebitLC)
                    tOIAdj.tcInvoiceControlGLCode        = tqCInvoiceByAllocationInfoNew.tcGLCode
                    tOIAdj.tcCompanyCode                 = tqCInvoiceByAllocationInfoNew.tcCompanyCode
                    tOIAdj.tcCInvoiceReference           = tqCInvoiceByAllocationInfoNew.tcCInvoiceReference
                    tOIAdj.tCInvoiceDescription          = tqCInvoiceByAllocationInfoNew.tcCInvoiceDescription
                    tOIAdj.tdNonDiscountableAmountTC     = tqCInvoiceByAllocationInfoNew.tdCInvoiceNonDiscAmtTC
                    tOIAdj.tdVatTC                       = if tqCInvoiceByAllocationInfoNew.tdCInvoiceVatDebitTC <> 0 and tqCInvoiceByAllocationInfoNew.tdCInvoiceVatDebitTC <> ? then tqCInvoiceByAllocationInfoNew.tdCInvoiceVatDebitTC else tqCInvoiceByAllocationInfoNew.tdCInvoiceVatCreditTC
                    tOIAdj.tdOriginalInvoiceAmountNetTC  = vdInvoiceAmount - tOIAdj.tdVatTC
                    /* if not wht is used, then give initial value */
                    tOIAdj.tlUpdateTax                   = false
                    tOIAdj.tdInitiallyAllocatedWHTAmtTC  = 0
        
                    tOIAdj.tiInvMovementPostingLineID    = 0
                    tOIAdj.tlBusinessRelationIsCompens   = tqCInvoiceByAllocationInfoNew.tlBusinessRelationIsCompens
                    tOIAdj.tlCompIsCompens               = tqCInvoiceByAllocationInfoNew.tlCompanyPropertyIsCompens
                    tOIAdj.tcCompOINetRest               = tqCInvoiceByAllocationInfoNew.tcCompanyPropertyOINetRestr.

            /* State if tax-at-payment is applicable for this invoice - pass the domaincode due to Fin-Operation jion in the query that is not complete */
            <Q-63 assign vlFcQueryRecordsAvailable = CInvoiceVatByAllocationInfo (NoCache)
               (input tqCInvoiceByAllocationInfoNew.tiCInvoice_ID, (CInvoiceID)
                input vcDomainCode, (DomainCode)
                input true, (DiscountTaxAtPayment)) in BCInvoice >
            if vlFcQueryRecordsAvailable <> false
            then assign tOIAdj.tlIsDiscountTaxAtPayment = true.
            
            /* ===================================== */
            /* everything related to withholding tax */
            /* ===================================== */
            if tqCInvoiceByAllocationInfoNew.tlCompanyPropertyIsWHT and
               tqCInvoiceByAllocationInfoNew.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 tOIAdj                   */
                /* ================================================================================================= */
                find first tDefaultWHTDataForOIA where
                           tDefaultWHTDataForOIA.tiCInvoiceId = tqCInvoiceByAllocationInfoNew.tiCInvoice_ID
                           no-lock no-error.
                if available tDefaultWHTDataForOIA
                then do :
                    assign tOIAdj.tdCInvoiceTotWHTBaseTC        = tDefaultWHTDataForOIA.tdCInvoiceTotalWHTTaxFeeTC
                           tOIAdj.tdCInvoiceWHTUnpaidAmtTC      = tDefaultWHTDataForOIA.tdUnpaidWHTAmountTC
                           tOIAdj.tlUpdateTax                   = if tDefaultWHTDataForOIA.tlUpdateTaxIsAllowed = ?
                                                                  then false
                                                                  else tDefaultWHTDataForOIA.tlUpdateTaxIsAllowed
                           tOIAdj.tdCInvoiceTotWHTTC            = tDefaultWHTDataForOIA.tdCInvoiceTotalWHTAmountTC
                           tOIAdj.tdWHTBalanceTC                = tDefaultWHTDataForOIA.tdWHTAmountTC
                           tOIAdj.tdInitiallyAllocatedWHTAmtTC  = tDefaultWHTDataForOIA.tdInitiallyAllocatedWHTAmountTC
                           tOIAdj.tdAdjustmentWHTTaxFeeTC       = tDefaultWHTDataForOIA.tdCInvoiceTotalWHTTaxFeeTC
                           tOIAdj.tdAdjustmentWHTAmountTC       = 0 /* tDefaultWHTDataForOIA.tdWHTAmountTC --> Default WHT should be 0, since nothing is allocated yet. */
                           tOIAdj.tdAdjustmentWHTAmountLC       = 0 /* tDefaultWHTDataForOIA.tdWHTAmountLC --> Default WHT should be 0, since nothing is allocated yet. */
                           tOIAdj.tdAdjustmentWHTLCRate         = tDefaultWHTDataForOIA.tdWHTTCLCExchangeRate
                           tOIAdj.tdAdjustmentWHTLCScale        = tDefaultWHTDataForOIA.tdWHTTCLCRateScale
                           tOIAdj.tdAdjustmentWHTCCRate         = tDefaultWHTDataForOIA.tdWHTTCCCExchangeRate
                           tOIAdj.tdAdjustmentWHTCCScale        = tDefaultWHTDataForOIA.tdWHTTCCCRateScale
                           tOIAdj.tdAdjustmentWHTPerc           = if tDefaultWHTDataForOIA.tdCInvoiceTotalWHTTaxFeeTC <> 0
                                                                  then abs((tDefaultWHTDataForOIA.tdWHTAmountTC / 
                                                                            tDefaultWHTDataForOIA.tdCInvoiceTotalWHTTaxFeeTC) * 100)
                                                                  else 0.
                    /* If the invoice is closed already then the WHT should always be defaulted to zero - regardsless  previous actions on the WHT */
                    if (tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceDebitTC = 0 or
                        tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceDebitTC = ?) and 
                       (tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC = 0 or
                        tqCInvoiceByAllocationInfoNew.tdCInvoiceBalanceCreditTC = ?)
                    then assign tOIAdj.tdCInvoiceTotWHTBaseTC        = 0
                                tOIAdj.tdCInvoiceWHTUnpaidAmtTC      = 0
                                tOIAdj.tdCInvoiceTotWHTTC            = 0
                                tOIAdj.tdWHTBalanceTC                = 0
                                tOIAdj.tdInitiallyAllocatedWHTAmtTC  = 0
                                tOIAdj.tdAdjustmentWHTTaxFeeTC       = 0.
                end. /* if available tDefaultWHTDataForOIA */
                /* ======================================================================================== */
                /* Adjust the balance of the invoice about Wht amount, which is allocated, but not yet paid */
                /* ======================================================================================== */
                assign vdInvoiceAmount                = vdInvoiceAmount - tOIAdj.tdInitiallyAllocatedWHTAmtTC
                       tOIAdj.tdInvoiceBalanceTC      = tOIAdj.tdInvoiceBalanceTC - tOIAdj.tdInitiallyAllocatedWHTAmtTC
                       tOIAdj.tdNewBalanceTC          = tOIAdj.tdInvoiceBalanceTC.
            end. /* if tqCInvoiceBankByAllocationInfo.tlCompanyPropertyIsWHT and */

            /* Create detail records for staged payment conditions */
            if tqCInvoiceByAllocationInfoNew.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} and
               tOIAdj.tcInvoiceType                                       <> {&INVOICETYPE-PREPAYMENT} /* If this is prepayment with the Staged payment condition, no stages are created for it */
            then do:
                assign vdTotalDefaultPaymentAmountTC  = 0
                       vdTotalDefaultDiscountAmountTC = 0
                       vdTotalWhtAmountTC             = 0.
                
                /* Calculate discountable amount of whole invoice */
                if tOIAdj.tlIsDiscountTaxAtPayment
                then assign vdDiscountableInvoiceTC = tOIAdj.tdInvoiceOriginalBalanceTC - tOIAdj.tdNonDiscountableAmountTC.
                else assign vdDiscountableInvoiceTC = tOIAdj.tdInvoiceOriginalBalanceTC - tOIAdj.tdNonDiscountableAmountTC - tOIAdj.tdVatTC.
    
                <Q-41 run CInvoiceStageByDueDate (all) (Read) (NoCache)
                   (input tqCInvoiceByAllocationInfoNew.tiCInvoice_ID, (CInvoice_ID)
                    input ?, (DueDate)
                    output dataset tqCInvoiceStageByDueDate) in BCInvoice >
    
                for each tqCInvoiceStageByDueDate where
                         tqCInvoiceStageByDueDate.tiCInvoice_ID           = tqCInvoiceByAllocationInfoNew.tiCInvoice_ID and
                         tqCInvoiceStageByDueDate.tdCInvoiceStageAmountTC > tqCInvoiceStageByDueDate.tdCInvoiceStageAmtAppliedTC
                         break by tqCInvoiceStageByDueDate.tiCInvoice_ID
                               by tqCInvoiceStageByDueDate.ttCInvoiceStageDueDate:
                    
                    create tOIAdjStage.
                    assign viRowidOIAdj                             = viRowidOIAdj + 1
                           tOIAdjStage.tc_Rowid                     = string(tqCInvoiceStageByDueDate.tc_rowid,"X(20)":U) + string(viRowidOIAdj)
                           tOIAdjStage.tc_ParentRowid               = tOIAdj.tc_Rowid
                           tOIAdjStage.tiInvoiceStageID             = tqCInvoiceStageByDueDate.tiCInvoiceStage_ID
                           tOIAdjStage.tiInvoiceID                  = tqCInvoiceStageByDueDate.tiCInvoice_ID
                           tOIAdjStage.ttDueDate                    = tqCInvoiceStageByDueDate.ttCInvoiceStageDueDate
                           tOIAdjStage.ttDiscountDueDate            = tqCInvoiceStageByDueDate.ttCInvoiceStageDiscDate
                           tOIAdjStage.tdDiscountPercentage         = tqCInvoiceStageByDueDate.tdCInvoiceStagePercent
                           vdDefaultAllocAmountTC                   = tqCInvoiceStageByDueDate.tdCInvoiceStageAmountTC - tqCInvoiceStageByDueDate.tdCInvoiceStageAmtAppliedTC.
    
                    /* Distribute Wht amount to stages */
                    if not last-of(tqCInvoiceStageByDueDate.tiCInvoice_ID)
                    then assign tOIAdjStage.tdAllocatedWHTAmountTC    = tOIAdj.tdCInvoiceTotWHTTC
                                                                      * vdDefaultAllocAmountTC / tOIAdj.tdInvoiceOriginalBalanceTC
                                tOIAdjStage.tdAllocatedWHTAmountTC    = <M-54 RoundAmount
                                                                           (input  tOIAdjStage.tdAllocatedWHTAmountTC (idUnroundedAmount), 
                                                                            input  ? (iiCurrencyID), 
                                                                            input  tqCInvoiceByAllocationInfoNew.tcCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>
                                vdTotalWhtAmountTC                    = vdTotalWhtAmountTC + tOIAdjStage.tdAllocatedWHTAmountTC.
                    else assign tOIAdjStage.tdAllocatedWHTAmountTC    = tOIAdj.tdWHTBalanceTC - vdTotalWhtAmountTC
                                vdTotalWhtAmountTC                    = 0.
                                
                    if tOIAdj.tcInvoiceType <> {&INVOICETYPE-PREPAYMENT} and
                       itPostingDate        <= tqCInvoiceStageByDueDate.ttCInvoiceStageDiscDate
                    then assign tOIAdjStage.tdDefaultDiscountAmountTC = vdDiscountableInvoiceTC 
                                                                      * (vdDefaultAllocAmountTC / tOIAdj.tdInvoiceOriginalBalanceTC)
                                                                      * (tqCInvoiceStageByDueDate.tdCInvoiceStageDiscPerc / 100)
                                tOIAdjStage.tdDefaultDiscountAmountTC = <M-20 RoundAmount
                                                                           (input  tOIAdjStage.tdDefaultDiscountAmountTC (idUnroundedAmount), 
                                                                            input  tqCInvoiceByAllocationInfoNew.tiCInvoiceCurrency_ID (iiCurrencyID), 
                                                                            input  tqCInvoiceByAllocationInfoNew.tcCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>
                                tOIAdjStage.tdDefaultPaymentAmountTC  = vdDefaultAllocAmountTC 
                                                                      - tOIAdjStage.tdDefaultDiscountAmountTC 
                                                                      - tOIAdjStage.tdAllocatedWHTAmountTC.
                    else assign tOIAdjStage.tdDefaultDiscountAmountTC = 0
                                tOIAdjStage.tdDefaultPaymentAmountTC  = vdDefaultAllocAmountTC
                                                                      - tOIAdjStage.tdAllocatedWHTAmountTC.                                                                                             
                                    
                    assign vdTotalDefaultPaymentAmountTC        = vdTotalDefaultPaymentAmountTC  + tOIAdjStage.tdDefaultPaymentAmountTC
                           vdTotalDefaultDiscountAmountTC       = vdTotalDefaultDiscountAmountTC + tOIAdjStage.tdDefaultDiscountAmountTC
                           tOIAdjStage.tdDefaultWHTAmountTC     = tOIAdjStage.tdAllocatedWHTAmountTC
                           tOIAdjStage.tdAllocatedWHTAmountTC   = 0 /* --> Default WHT should be 0, since nothing is allocated yet. */.
                end. /* for each tqCInvoiceStageByDueDate */      
                
                assign tOIAdj.tdDefaultPaymentAmountTC  = vdTotalDefaultPaymentAmountTC
                       tOIAdj.tdDefaultDiscountAmountTC = vdTotalDefaultDiscountAmountTC.                                                            
            end. /* if tqCInvoiceByAllocationInfoNew.tcPaymentConditionPaymentTyp = 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 tOIAdj.tcInvoiceType <> {&INVOICETYPE-PREPAYMENT} and
                   itPostingDate <= tqCInvoiceByAllocationInfoNew.ttCInvoiceDiscountDueDate
                then do:
                    /* Calculate discountable amount of whole invoice */
                    if tOIAdj.tlIsDiscountTaxAtPayment
                    then assign vdDiscountableInvoiceTC = tOIAdj.tdInvoiceBalanceTC - tOIAdj.tdNonDiscountableAmountTC.
                    else assign vdDiscountableInvoiceTC = tOIAdj.tdOriginalInvoiceAmountNetTC - tOIAdj.tdNonDiscountableAmountTC.
                    
                    assign tOIAdj.tdDefaultDiscountAmountTC = vdDiscountableInvoiceTC 
                                                            * (tOIAdj.tdInvoiceBalanceTC / tOIAdj.tdInvoiceOriginalBalanceTC)   /* tOIAdj.tdInvoiceBalanceTC is already decreased by allocated by not payed wht */
                                                            * (tqCInvoiceByAllocationInfoNew.tdPaymentConditionPercentage / 100)
                           tOIAdj.tdDefaultDiscountAmountTC = <M-24 RoundAmount
                                                                 (input  tOIAdj.tdDefaultDiscountAmountTC (idUnroundedAmount), 
                                                                  input  0 (iiCurrencyID), 
                                                                  input  tqCInvoiceByAllocationInfoNew.tcCurrencyCode (icCurrencyCode)) in BApplicationProperty>
                           tOIAdj.tdDefaultPaymentAmountTC  = tOIAdj.tdInvoiceBalanceTC
                                                            - tOIAdj.tdDefaultDiscountAmountTC
                                                            - tOIAdj.tdWHTBalanceTC.
                end.                                                               
                else assign tOIAdj.tdDefaultPaymentAmountTC  = tOIAdj.tdInvoiceBalanceTC - tOIAdj.tdWHTBalanceTC
                            tOIAdj.tdDefaultDiscountAmountTC = 0.
            end. /* if tqCInvoiceByAllocationInfoNew.tcPaymentConditionPaymentTyp <> staged */
        end. /* if Over Allocation */
    end. /* for each tqCInvoiceByAllocationInfo no-lock */
END. /* end MAIN_BLOCK: */