project QadFinancials > class BCreditorReport > method CreditorWithholdingTax

report procedure

Description

main procedure for Creditor Withholding Tax report


Parameters


icLanguageCodeinputcharacter
tFilterinputtemp-table
dcrCreditorWithholdingTaxoutputdataset
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program1/bcreditorreport.p)

empty temp-table tqCreditorWithholdingTax.

define buffer bAllWHTByInvoice for tqGetAllWHTByInvoiceID. 
define buffer bInvoiceWHT for tqGetAllInvoiceForWHT. 

<M-73 run GetReportLabels
   (input  'CreditorWithholdingTax':U (icReportName), 
    input  icLanguageCode (icLanguageCode), 
    input  tFilter (tFilter), 
    output tqHeader (tqHeader), 
    output tqFilter (tqFilter), 
    output tqText (tqText), 
    output oiReturnStatus (oiReturnStatus)) in BCreditorReport>
    
if oiReturnStatus <> 0
then return.                             

<M-64 run SetDataItemsBasedOnFilterTT  (output viFcReturnSuper (oiReturnStatus)) in BCreditorReport>

/* Initialize some default values */
if viFromVoucherFilter1 = 0 then assign viFromVoucherFilter1 = ?.
if viToVoucherFilter1 = 0 then assign viToVoucherFilter1 = ?.
if vcFromCreditorCodeFilter1 = "" then assign vcFromCreditorCodeFilter1 = ?.
if vcToCreditorCodeFilter1 = "" then assign vcToCreditorCodeFilter1 = ?.
if vcFromJournalCodeFilter1 = "" then assign vcFromJournalCodeFilter1 = ?.
if vcToJournalCodeFilter1 = "" then assign vcToJournalCodeFilter1 = ?.
if vcCRCompanyFilter = ? or vcCRCompanyFilter = "" then assign vcCRCompanyFilter = vcCompanyCode.
if vcCurrencyCodeFilter1 = ? or vcCurrencyCodeFilter1 = "" then assign vcCurrencyCodeFilter1 = vcCompanyLC.
if vlPrintExpectedTaxAmtFilter then assign  viFromAccPeriodFilter1 = ?.

assign viCompanyIDLocal = 0.

find first tqCompanyByDomainActive where 
           tqCompanyByDomainActive.tcCompanyCode = vcCRCompanyFilter
           no-lock no-error.
if available tqCompanyByDomainActive             
then assign viCompanyIDLocal = tqCompanyByDomainActive.tiCompany_ID.

if viCompanyIDLocal <= 0
then return.

/* check the combination of the threshold filter and the include expected tax filter */
if vdThreshold <> 0 and 
   vdThreshold <> ? 
then do:
    if vlPrintExpectedTaxAmtFilter = true
    then do:
        assign vcMessage = #T-29'You can't set the filter include expected tax to true when the threshold filter is filled':255(863347386)T-29#
               oiReturnStatus = -1.
        <M-75 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-274508':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCreditorReport>
        return.
    end.
end.

/* check if the currency exists */  
<Q-71 run CurrencyPrim (all) (Read) (NoCache)
   (input vcCurrencyCodeFilter1, (CurrencyCode)
    input ?, (Currency_ID)
    output dataset tqCurrencyPrim) in BCurrency >
find first tqCurrencyPrim where
           tqCurrencyPrim.tcCurrencyCode = vcCurrencyCodeFilter1
           no-lock no-error.
if not available tqCurrencyPrim 
then do:
    assign oiReturnStatus = -1.
    <M-4 run SetMessage
       (input  #T-43'Please fill in a valid currency code.':255(934914366)T-43# (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'qadfin-498132':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCreditorReport>
    return.
end.

/* Main query */
assign vlisService           = (if vdThreshold = ? then ? else true).
if vlPrintExpectedTaxAmtFilter = true
then assign vcCDocumentStatusList = {&WHTSTATUS-DUE} + "," + {&WHTSTATUS-INIT}.
else assign vcCDocumentStatusList = {&WHTSTATUS-DUE}. 
       
<Q-25 run WHTForPayments (all) (Read) (NoCache)
   (input viCompanyIDLocal, (CompanyId)
    input viAccYearFilter1, (Year)
    input viFromAccPeriodFilter1, (Period)
    input vcFromCreditorCodeFilter1, (CreditorFrom)
    input vcToCreditorCodeFilter1, (CredtiorTill)
    input vcFromJournalCodeFilter1, (JournalFrom)
    input vcToJournalCodeFilter1, (JournalTill)
    input viFromVoucherFilter1, (VoucherFrom)
    input viToVoucherFilter1, (VoucherTill)
    input vcCDocumentStatusList, (CDocumentStatusList)
    input vcDomainCode, (Domaincode)
    input vlisService, (isService)
    output dataset tqWHTForPayments) in BCreditorReport>

for each tqWHTForPayments
         break by tqWHTForPayments.tcCInvoiceReference
               by tqWHTForPayments.tcVatCode:

    create tqCreditorWithholdingTax.
    buffer-copy tqWHTForPayments to tqCreditorWithholdingTax.
    
    assign tqCreditorWithholdingTax.tiFromAccPeriodFilter1 = viFromAccPeriodFilter1
           tqCreditorWithholdingTax.tcCurrencyCodeFilter1  = vcCurrencyCodeFilter1.
    
    <Q-42 run CurrencyById (all) (Read) (NoCache)
       (input tqWHTForPayments .tiCInvoiceCurrency_ID, (CurrencyId)
        output dataset tqCurrencyById) in BCurrency>
    
    find first tqCurrencyById where 
               tqCurrencyById.tiCurrency_ID = tqWHTForPayments.tiCInvoiceCurrency_ID
               no-lock no-error.
    if available tqCurrencyById
    then do:
        if tqCurrencyById.tcCurrencyCode = vcCurrencyCodeFilter1
        then assign vdExchangeRateTCtoRC         = 1
                    vdExchangeRateScaleFactor    = 1
                    vdVATExchangeRateTCtoRC      = 1
                    vdVATExchangeRateScaleFactor = 1.
        else do:
            /* Get VAT Exchangerate from Transaction Currency to Reporting Currency */
            <M-18 run GetExchangeRate
               (input  ? (iiCompanyID), 
                input  tqWHTForPayments.tiCInvoiceCurrency_ID (iiFromCurrencyID), 
                input  ? (icFromCurrencyCode), 
                input  ? (iiToCurrencyID), 
                input  vcCurrencyCodeFilter1 (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeID), 
                input  {&EXCHANGERATETYPE-VAT} (icExchangeRateTypeCode), 
                input  today (itValidityDate), 
                output vdVATExchangeRateTCtoRC (odExchangeRate), 
                output vdVATExchangeRateScaleFactor (odExchangeScaleFactor), 
                output oiReturnStatus (oiReturnStatus)) in BCreditorReport>
           if oiReturnStatus < 0 
           then return.

           /* Get Exchangerate from Transaction Currency to Reporting Currency */
           <M-2 run GetExchangeRate
              (input  ? (iiCompanyID), 
               input  tqWHTForPayments.tiCInvoiceCurrency_ID (iiFromCurrencyID), 
               input  ? (icFromCurrencyCode), 
               input  ? (iiToCurrencyID), 
               input  vcCurrencyCodeFilter1 (icToCurrencyCode), 
               input  ? (iiExchangeRateTypeID), 
               input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
               input  today (itValidityDate), 
               output vdExchangeRateTCtoRC (odExchangeRate), 
               output vdExchangeRateScaleFactor (odExchangeScaleFactor), 
               output oiReturnStatus (oiReturnStatus)) in BCreditorReport>
           if oiReturnStatus < 0 
           then return.

        end. /* tqCurrencyById.tcCurrencyCode = vcCurrencyCodeFilter1 */
    end. /* if available tqCurrencyById */
    
    assign vdExchangeRateTCtoRC    = vdExchangeRateTCtoRC * vdExchangeRateScaleFactor
           vdVATExchangeRateTCtoRC = vdVATExchangeRateTCtoRC * vdVATExchangeRateScaleFactor.
    
    if tqWHTForPayments.tcWHTChargeType = {&WHTCHARGETYPE-FEE} 
    then assign tqCreditorWithholdingTax.tdWHTFees       = (tqWHTForPayments.tdWHTChargeAmtCreditTC - tqWHTForPayments.tdWHTChargeAmtDebitTC) 
                                                         * vdExchangeRateTCtoRC
                tqCreditorWithholdingTax.tdWHTExpenseAmt = 0. 
    
    if tqWHTForPayments.tcWHTChargeType = {&WHTCHARGETYPE-EXPENSE}
    then assign tqCreditorWithholdingTax.tdWHTFees       = 0
                tqCreditorWithholdingTax.tdWHTExpenseAmt = (tqWHTForPayments.tdWHTChargeAmtCreditTC - tqWHTForPayments.tdWHTChargeAmtDebitTC) 
                                                         * vdExchangeRateTCtoRC. 
          
             
    assign tqCreditorWithholdingTax.tdWHTTax            = (tqWHTForPayments.tdWHTVATAmtCreditTC - tqWHTForPayments.tdWHTVATAmtDebitTC) * vdExchangeRateTCtoRC 
           tqCreditorWithholdingTax.tdWHTAmt            = (tqWHTForPayments.tdWHTAmtCreditTC - tqWHTForPayments.tdWHTAmtDebitTC) * vdVATExchangeRateTCtoRC 
           tqCreditorWithholdingTax.tdWHTInvoiceTotal   = (tqWHTForPayments.tdCInvoiceOriginalCreditTC - tqWHTForPayments.tdCInvoiceOriginalDebitTC) * vdExchangeRateTCtoRC 
           tqCreditorWithholdingTax.tdWHTTotalPaid      = (tqWHTForPayments.tdWHTPaidAmtDebitTC - tqWHTForPayments.tdWHTPaidAmtCreditTC) * vdExchangeRateTCtoRC 
           tqCreditorWithholdingTax.tdWHTTaxableFees    = <M-38 RoundAmount
                                                             (input  (tqWHTForPayments.tdWHTTaxableFeesCreditTC - tqWHTForPayments.tdWHTTaxableFeesDebitTC) * vdExchangeRateTCtoRC (idUnroundedAmount), 
                                                              input  ? (iiCurrencyID), 
                                                              input  vcCurrencyCodeFilter1 (icCurrencyCode)) in BCreditorReport> 
           tqCreditorWithholdingTax.tdWHTToPay          = (tqWHTForPayments.tdWHTRetainedAmtCreditTC - tqWHTForPayments.tdWHTRetainedAmtDebitTC) * vdExchangeRateTCtoRC
           tqCreditorWithholdingTax.tcCInvoiceReference = string(tqWHTForPayments.tiCInvoicePostingYear) 
                                                        + "/" 
                                                        + tqWHTForPayments.tcJournalCode + "/" + string(tqWHTForPayments.tiCInvoiceVoucher, "999999999")
           vdInvoiceTotal                               = tqCreditorWithholdingTax.tdWHTInvoiceTotal.
end. /* for each tqWHTForPayments */


/*Append the dummy record*/
if vlPrintExpectedTaxAmtFilter 
then do:             
    <Q-53 run GetAllInvoiceForWHT (all) (Read) (NoCache)
       (input viCompanyIDLocal, (CompanyId)
        input vcFromCreditorCodeFilter1, (FromCreditor)
        input vcToCreditorCodeFilter1, (ToCreditor)
        input viAccYearFilter1, (PostingYear)
        input vccdocumentstatuslist, (CDocumentStatusList)
        input viAccYearFilter1, (Year)
        input viFromAccPeriodFilter1, (Period)
        input vcFromJournalCodeFilter1, (FromJournal)
        input vcToJournalCodeFilter1, (ToJournal)
        input viFromVoucherFilter1, (FromVoucher)
        input viToVoucherFilter1, (ToVoucher)
        output dataset tqGetAllInvoiceForWHT) in BCreditorReport>

    InvoiceLoop:
    for each tqGetAllInvoiceForWHT 
             break by tqGetAllInvoiceForWHT.tiCInvoice_ID:
    
        if first-of(tqGetAllInvoiceForWHT.tiCInvoice_ID) 
        then do:      
            assign vhFcComponent = ?.
            <Q-7 run CurrencyById (all) (Read) (NoCache)
               (input tqGetALLInvoiceForWHT.tiCInvoiceCurrency_ID, (CurrencyId)
                output dataset tqCurrencyById) in BCurrency>
    
            find first tqCurrencyById where
                       tqCurrencyById.tiCurrency_ID = tqGetALLInvoiceForWHT.tiCInvoiceCurrency_ID
                       no-lock no-error.       
            if available tqCurrencyById
            then do:
                if tqCurrencyById.tcCurrencyCode = vcCurrencyCodeFilter1
                then assign vdExchangeRateTCtoRC      = 1
                            vdExchangeRateScaleFactor = 1.
                else do:
                    /* Get Accounting Exchangerate from Transaction Currency to Reporting Currency */
                    <M-15 run GetExchangeRate
                       (input  ? (iiCompanyID), 
                        input  tqGetAllInvoiceForWHT.tiCInvoiceCurrency_ID (iiFromCurrencyID), 
                        input  ? (icFromCurrencyCode), 
                        input  ? (iiToCurrencyID), 
                        input  vcCurrencyCodeFilter1 (icToCurrencyCode), 
                        input  ? (iiExchangeRateTypeID), 
                        input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                        input  today (itValidityDate), 
                        output vdExchangeRateTCtoRC (odExchangeRate), 
                        output vdExchangeRateScaleFactor (odExchangeScaleFactor), 
                        output oiReturnStatus (oiReturnStatus)) in BCreditorReport>
                    if oiReturnStatus < 0 
                    then return.

                end. /* tqCurrencyById.tcCurrencyCode = vcCurrencyCodeFilter1 */
            end. /* if available tqCurrencyById */
          
            assign vdExchangeRateTCtoRC = vdExchangeRateTCtoRC * vdExchangeRateScaleFactor.
            
            <Q-28 run GetPaymentIDByInvoice (all) (Read) (NoCache)
               (input tqGetAllInvoiceForWHT.tiCInvoice_ID, (InvoiceID)
                output dataset tqGetPaymentIDByInvoice) in BCreditorReport>
                
            find first tqGetPaymentIDByInvoice where 
                       tqGetPaymentIDByInvoice.tdCDocumentInvoiceXRefWHTAmtTC <> 0 and
                       tqGetPaymentIDByInvoice.tdCDocumentInvoiceXRefWHTAmtTC <> ?
                       no-lock no-error.
            
            <Q-50 run GetBankingEntryByInvoice (all) (Read) (NoCache)
               (input viCompanyIDLocal, (CompanyId)
                input tqGetAllInvoiceForWHT.tiCInvoice_ID, (CInvoiceID)
                output dataset tqGetBankingEntryByInvoice) in BCreditorReport>
                
            find first tqGetBankingEntryByInvoice where
                       tqGetBankingEntryByInvoice.tdBankStateAllocWHTAmtTC <> 0 and
                       tqGetBankingEntryByInvoice.tdBankStateAllocWHTAmtTC <> ?
                       no-lock no-error.                       
        
            /*Unpaid*/
            if not available tqGetPaymentIDByInvoice and not available tqGetBankingEntryByInvoice
            then do:
                for each bInvoiceWHT where 
                         bInvoiceWHT.tiCInvoice_ID = tqGetAllInvoiceForWHT.tiCInvoice_ID:
               
                    /*Fee*/
                    if bInvoiceWHT.tcVatCode <> "" 
                    then do:
                        create tqCreditorWithholdingTax.
                        assign tqCreditorWithholdingTax.tcCreditorCode        = bInvoiceWHT.tcCreditorCode 
                               tqCreditorWithholdingTax.tcVatCode             = bInvoiceWHT.tcVatCode 
                               tqCreditorWithholdingTax.tiCInvoice_ID         = bInvoiceWHT.tiCInvoice_ID
                               tqCreditorWithholdingTax.tcVatDescription      = bInvoiceWHT.tcVatDescription
                               tqCreditorWithholdingTax.tcCInvoiceReference   =  string(bInvoiceWHT.tiCInvoicePostingYear) 
                                                                              + "/" + bInvoiceWHT.tcJournalCode 
                                                                              + "/" + string(bInvoiceWHT.tiCInvoiceVoucher, "999999999")
                               tqCreditorWithholdingTax.ttCInvoiceDate        = bInvoiceWHT.ttCInvoiceDate
                               tqCreditorWithholdingTax.tdWHTFees             = (bInvoiceWHT.tdCInvoiceWHTFeeCreditTC - bInvoiceWHT.tdCInvoiceWHTFeeDebitTC)
                                                                              * vdExchangeRateTCtoRC 
                               tqCreditorWithholdingTax.tdWHTExpenseAmt       = 0
                               tqCreditorWithholdingTax.tdWHTInvoiceTotal     = (bInvoiceWHT.tdCInvoiceOriginalCreditTC - bInvoiceWHT.tdCInvoiceOriginalDebitTC)
                                                                              * vdExchangeRateTCtoRC          
                               tqCreditorWithholdingTax.tlIsExpectedTax       = yes
                               tqCreditorWithholdingTax.tiCInvoicePostingYear = bInvoiceWHT.tiCInvoicePostingYear
                               tqCreditorWithholdingTax.tcJournalCode         = bInvoiceWHT.tcJournalCode
                               tqCreditorWithholdingTax.tiCInvoiceVoucher     = bInvoiceWHT.tiCInvoiceVoucher
                               tqCreditorWithholdingTax.tdWHTPercentage       = bInvoiceWHT.tdtx2_tax_pct
                               tqCreditorWithholdingTax.tdWHTAmt              = (bInvoiceWHT.tdCInvoiceWHTAmtCreditTC - bInvoiceWHT.tdCInvoiceWHTAmtDebitTC)
                                                                              * vdExchangeRateTCtoRC
                               tqCreditorWithholdingTax.tdWHTTaxableFees      = <M-3 RoundAmount
                                                                                   (input  bInvoiceWHT.tdtx2_pct_recv * tqCreditorWithholdingTax.tdWHTFees / 100 (idUnroundedAmount), 
                                                                                    input  ? (iiCurrencyID), 
                                                                                    input  vcCurrencyCodeFilter1 (icCurrencyCode)) in BCreditorReport> 
                               tqCreditorWithholdingTax.tdWHTTax              = ((bInvoiceWHT.tdCInvoiceVatCreditTC - bInvoiceWHT.tdCInvoiceVatDebitTC) 
                                                                              * vdExchangeRateTCtoRC)
                                                                              * (tqCreditorWithholdingTax.tdWHTFees 
                                                                              / ((bInvoiceWHT.tdCInvoiceVatBaseCreditTC - bInvoiceWHT.tdCInvoiceVatBaseDebitTC)
                                                                              * vdExchangeRateTCtoRC)) .
                        if tqCreditorWithholdingTax.tdWHTTax = ? 
                        then assign tqCreditorWithholdingTax.tdWHTTax = 0.

                    end. /* bInvoiceWHT.tcVatCode <> ""  */

                    /* Expense*/ 
                    if (bInvoiceWHT.tdCInvoiceWHTExpenseCreditTC - bInvoiceWHT.tdCInvoiceWHTExpenseDebitTC) <> 0
                    then do:
                        create tqCreditorWithholdingTax.
                        assign tqCreditorWithholdingTax.tcCreditorCode        = bInvoiceWHT.tcCreditorCode
                               tqCreditorWithholdingTax.tiCInvoice_ID         = bInvoiceWHT.tiCInvoice_ID
                               tqCreditorWithholdingTax.tcVatCode             = bInvoiceWHT.tcVatCode 
                               tqCreditorWithholdingTax.tcVatDescription      = bInvoiceWHT.tcVatDescription
                               tqCreditorWithholdingTax.tcCInvoiceReference   = string(bInvoiceWHT.tiCInvoicePostingYear) 
                                                                              + "/" + bInvoiceWHT.tcJournalCode 
                                                                              + "/" + string(bInvoiceWHT.tiCInvoiceVoucher, "999999999")
                               tqCreditorWithholdingTax.ttCInvoiceDate        = bInvoiceWHT.ttCInvoiceDate
                               tqCreditorWithholdingTax.tdWHTFees             = 0
                               tqCreditorWithholdingTax.tdWHTExpenseAmt       = (bInvoiceWHT.tdCInvoiceWHTExpenseCreditTC - bInvoiceWHT.tdCInvoiceWHTExpenseDebitTC)
                                                                              * vdExchangeRateTCtoRC 
                               tqCreditorWithholdingTax.tdWHTInvoiceTotal     = (bInvoiceWHT.tdCInvoiceOriginalCreditTC - bInvoiceWHT.tdCInvoiceOriginalDebitTC)
                                                                              * vdExchangeRateTCtoRC  
                               tqCreditorWithholdingTax.tlIsExpectedTax       = yes
                               tqCreditorWithholdingTax.tiCInvoicePostingYear = bInvoiceWHT.tiCInvoicePostingYear
                               tqCreditorWithholdingTax.tcJournalCode         = bInvoiceWHT.tcJournalCode
                               tqCreditorWithholdingTax.tiCInvoiceVoucher     = bInvoiceWHT.tiCInvoiceVoucher
                               tqCreditorWithholdingTax.tdWHTPercentage       = 0
                               tqCreditorWithholdingTax.tdWHTAmt              = 0
                               tqCreditorWithholdingTax.tdWHTTax              = ((bInvoiceWHT.tdCInvoiceVatCreditTC - bInvoiceWHT.tdCInvoiceVatDebitTC) 
                                                                              * vdExchangeRateTCtoRC)
                                                                              * (tqCreditorWithholdingTax.tdWHTExpenseAmt
                                                                              / ((bInvoiceWHT.tdCInvoiceVATBaseCreditTC - bInvoiceWHT.tdCInvoiceVATBaseDebitTC) 
                                                                              * vdExchangeRateTCtoRC)).
                        if tqCreditorWithholdingTax.tdWHTTax = ? 
                        then assign tqCreditorWithholdingTax.tdWHTTax = 0.
                    end. /* (bInvoiceWHT.tdCInvoiceWHTExpenseCreditTC - bInvoiceWHT.tdCInvoiceWHTExpenseDebitTC) <> 0 */
                end. /* for each bInvoiceWHT */ 
            end. /*if not available(tqGetPaymentIDByInvoice) then*/ 
            /*Partial or Full Paild*/
            else do:
    
                /*Get all WHT(no whtcode filter) by Invoice ID*/
                <Q-81 run GetAllWHTByInvoiceID (all) (Read) (NoCache)
                   (input viCompanyIDLocal, (CompanyId)
                    input tqGetAllInvoiceForWHT.tiCInvoice_ID, (InvoiceID)
                    input ?, (VatID)
                    output dataset tqGetAllWHTByInvoiceID) in BCreditorReport >
               
                assign vdWHTTotal = 0.
                for each tqGetAllWHTByInvoiceID:
                   assign vdWHTTotal = vdWHTTotal + (tqGetAllWHTByInvoiceID.tdWHTAmtCreditTC - tqGetAllWHTByInvoiceID.tdWHTAmtDebitTC).
                end.
    
                if vdWHTTotal <> (tqGetAllInvoiceForWHT.tdCInvoiceWHTTotAmtCreditTC - tqGetAllInvoiceForWHT.tdCInvoiceWHTTotAmtDebitTC) 
                then do:
                    /*Partial Paid*/
    
                    /*For each wht code*/
                    for each tqGetAllWHTByInvoiceID 
                        break by tqGetAllWHTByInvoiceID.tiVat_ID:
                       
                        if first-of(tqGetAllWHTByInvoiceID.tiVat_ID) 
                        then do:
                            assign vdFeeTotalByWHTCode      = 0
                                   vdExpenseTotalByWHTCode  = 0
                                   vdWHTAmtByWHTCode        = 0
                                   vdVatAmtByWHTCodeFee     = 0
                                   vdVatAmtByWHTCodeExpense = 0.
    
                            for each bAllWHTByInvoice where 
                                     bAllWHTByInvoice.tiVat_ID = tqGetALLWHTByInvoiceID.tiVat_ID:
                                if bAllWHTByInvoice.tcWHTChargeType = {&WHTCHARGETYPE-FEE} 
                                then assign vdFeeTotalByWHTCode  = vdFeeTotalByWHTCode 
                                                                 + (bAllWHTByInvoice.tdWHTChargeAmtCreditTc - bAllWHTByInvoice.tdWHTChargeAmtDebitTC)
                                            vdVatAmtByWHTCodeFee = vdVatAmtByWHTCodeFee 
                                                                 + (bAllWHTByInvoice.tdWHTVatAmtCreditTC - bAllWHTByInvoice.tdWHTVatAmtDebitTC).
                   
                                if bAllWHTByInvoice.tcWHTChargeType = {&WHTCHARGETYPE-EXPENSE} 
                                then assign vdExpenseTotalByWHTCode  = vdExpenseTotalByWHTCode 
                                                                     + (bAllWHTByInvoice.tdWHTChargeAmtCreditTC - bAllWHTByInvoice.tdWHTChargeAmtDebitTC)
                                            vdVatAmtByWHTCodeExpense = vdVatAmtByWHTCodeExpense 
                                                                     + (bAllWHTByInvoice.tdWHTVatAmtCreditTC - bAllWHTByInvoice.tdWHTVatAmtDebitTC).
                            
                                assign vdWHTAmtByWHTCode = vdWHTAmtByWHTCode + (bAllWHTByInvoice.tdWHTAmtCreditTC - bAllWHTByInvoice.tdWHTAmtDebitTC).
                            end. /* for each bAllWHTByInvoice */

                            /*Fee*/
                            if tqGetAllWHTByInvoiceID.tcVatCode <> ""  and tqGetAllWHTByInvoiceID.tcVatCode <> "00000000" 
                            then do: 
                                create tqCreditorWithholdingTax.
                                assign tqCreditorWithholdingTax.tcCreditorCode        = tqGetAllInvoiceForWHT.tcCreditorCode
                                       tqCreditorWithholdingTax.tiCInvoice_ID         = tqGetAllInvoiceForWHT.tiCInvoice_ID
                                       tqCreditorWithholdingTax.tcVatCode             = tqGetAllWHTByInvoiceID.tcVatCode
                                       tqCreditorWithholdingTax.tcVatDescription      = tqGetAllWHTByInvoiceID.tcVatDescription
                                       tqCreditorWithholdingTax.tcCInvoiceReference   = string(tqGetAllInvoiceForWHT.tiCInvoicePostingYear) 
                                                                                      + "/" + tqGetAllWHTByInvoiceID.tcJournalCode 
                                                                                      + "/" + string(tqGetAllWHTByInvoiceID.tiCInvoiceVoucher, "999999999")
                                       tqCreditorWithholdingTax.ttCInvoiceDate        = tqGetAllInvoiceForWHT.ttCInvoiceDate
                                       tqCreditorWithholdingTax.tdWHTFees             = (tqGetAllWHTByInvoiceID.tdCInvoiceWHTFeeCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceWHTFeeDebitTC 
                                                                                      - vdFeeTotalByWHTCode ) * vdExchangeRateTCtoRC 
                                       tqCreditorWithholdingTax.tdWHTExpenseAmt       = 0
                                       tqCreditorWithholdingTax.tdWHTInvoiceTotal     = (tqGetAllWHTByInvoiceID.tdCInvoiceOriginalCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceOriginalDebitTC)
                                                                                      * vdExchangeRateTCtoRC 
                                       tqCreditorWithholdingTax.tlIsExpectedTax       = yes
                                       tqCreditorWithholdingTax.tiCInvoicePostingYear = tqGetAllInvoiceForWHT.tiCInvoicePostingYear
                                       tqCreditorWithholdingTax.tcJournalCode         = tqGetAllInvoiceForWHT.tcJournalCode
                                       tqCreditorWithholdingTax.tiCInvoiceVoucher     = tqGetAllInvoiceForWHT.tiCInvoiceVoucher
                                       tqCreditorWithholdingTax.tdWHTPercentage       = tqGetAllWHTByInvoiceID.tdtx2_tax_pct
                                       tqCreditorWithholdingTax.tdWHTAmt              = (tqGetAllWHTByInvoiceID.tdCInvoiceWHTAmtCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceWHTAmtDebitTC 
                                                                                      - vdWHTAmtByWHTCode) * vdExchangeRateTCtoRC 
                                       tqCreditorWithholdingTax.tdWHTTaxableFees      = <M-12 RoundAmount
                                                                                           (input  tqGetAllWHTByInvoiceID.tdtx2_pct_recv * tqCreditorWithholdingTax.tdWHTFees / 100 (idUnroundedAmount), 
                                                                                            input  ? (iiCurrencyID), 
                                                                                            input  vcCurrencyCodeFilter1 (icCurrencyCode)) in BCreditorReport>
                                       tqCreditorWithholdingTax.tdWHTTax              = ((((tqGetAllWHTByInvoiceID.tdCInvoiceVatCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceVatDebitTC) * vdExchangeRateTCtoRC)
                                                                                      * (tqGetAllWHTByInvoiceID.tdCInvoiceWHTFeeCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceWHTFeeDebitTC) 
                                                                                      / (tqGetAllWHTByInvoiceID.tdCInvoiceVATBaseCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceVATBaseDebitTC)) - vdVatAmtByWHTCodeFee) * vdExchangeRateTCtoRC.
                                if tqCreditorWithholdingTax.tdWHTTax = ? 
                                then assign tqCreditorWithholdingTax.tdWHTTax = 0.
                            end. /* if tqGetAllWHTByInvoiceID.tcVatCode <> ""  and tqGetAllWHTByInvoiceID.tcVatCode <> "00000000"  */

                            /* Expense*/ 
                            if (tqGetAllWHTByInvoiceID.tdCInvoiceWHTExpenseCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceWHTExpenseDebitTC) <> 0 
                            then do:
                                create tqCreditorWithholdingTax.
                                assign tqCreditorWithholdingTax.tcCreditorCode        = tqGetAllInvoiceForWHT.tcCreditorCode
                                       tqCreditorWithholdingTax.tiCInvoice_ID         = tqGetAllInvoiceForWHT.tiCInvoice_ID
                                       tqCreditorWithholdingTax.tcVatCode             = tqGetAllWHTByInvoiceID.tcVatCode
                                       tqCreditorWithholdingTax.tcVatDescription      = tqGetAllWHTByInvoiceID.tcVatDescription
                                       tqCreditorWithholdingTax.tcCInvoiceReference   = string(tqGetAllInvoiceForWHT.tiCInvoicePostingYear) 
                                                                                      + "/" + tqGetAllWHTByInvoiceID.tcJournalCode 
                                                                                      + "/" + string(tqGetAllWHTByInvoiceID.tiCInvoiceVoucher, "999999999")
                                       tqCreditorWithholdingTax.ttCInvoiceDate        = tqGetAllInvoiceForWHT.ttCInvoiceDate
                                       tqCreditorWithholdingTax.tdWHTFees             = 0
                                       tqCreditorWithholdingTax.tdWHTExpenseAmt       = (tqGetAllWHTByInvoiceID.tdCInvoiceWHTExpenseCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceWHTExpenseDebitTC 
                                                                                      - vdExpenseTotalByWHTCode) * vdExchangeRateTCtoRC 
                                       tqCreditorWithholdingTax.tdWHTInvoiceTotal     = (tqGetAllWHTByInvoiceID.tdCInvoiceOriginalCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceOriginalDebitTC)
                                                                                      * vdExchangeRateTCtoRC  
                                       tqCreditorWithholdingTax.tlIsExpectedTax       = yes
                                       tqCreditorWithholdingTax.tiCInvoicePostingYear = tqGetAllInvoiceForWHT.tiCInvoicePostingYear
                                       tqCreditorWithholdingTax.tcJournalCode         = tqGetAllInvoiceForWHT.tcJournalCode
                                       tqCreditorWithholdingTax.tiCInvoiceVoucher     = tqGetAllInvoiceForWHT.tiCInvoiceVoucher
                                       tqCreditorWithholdingTax.tdWHTPercentage       = 0
                                       tqCreditorWithholdingTax.tdWHTAmt              = 0
                                       tqCreditorWithholdingTax.tdWHTTax              = ((((tqGetAllWHTByInvoiceID.tdCInvoiceVatCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceVatDebitTC) * vdExchangeRateTCtoRC)
                                                                                      * (tqGetAllWHTByInvoiceID.tdCInvoiceWHTExpenseCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceWHTExpenseDebitTC) 
                                                                                      / (tqGetAllWHTByInvoiceID.tdCInvoiceVATBaseCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceVATBaseDebitTC)) - vdVatAmtByWHTCodeExpense) * vdExchangeRateTCtoRC.
                                if tqCreditorWithholdingTax.tdWHTTax = ? 
                                then assign tqCreditorWithholdingTax.tdWHTTax = 0.
                            end. /* (tqGetAllWHTByInvoiceID.tdCInvoiceWHTExpenseCreditTC - tqGetAllWHTByInvoiceID.tdCInvoiceWHTExpenseDebitTC) <> 0 */
                        end. /* first-of(tqGetAllWHTByInvoiceID.tiVat_ID) */
                    end. /* for each tqGetAllWHTByInvoiceID */
                end. /* vdWHTTotal <> (tqGetAllInvoiceForWHT.tdCInvoiceWHTTotAmtCreditTC - tqGetAllInvoiceForWHT.tdCInvoiceWHTTotAmtDebitTC) */
                else do:
                /*Full Paid*/
                   next InvoiceLoop.
                end.
            end. /*Partial or Full Paid*/
        end. /* first-of(tqGetAllInvoiceForWHT.tiCInvoice_ID) */  
    end. /*for each tqGetAllInvoiceForWHT beak by tqGetAllInvoiceForWHT.tiCInvoice_ID:*/
end. /* if vlPrintExpectedTaxAmt */    

/* delete all the records of the supplier where the invoice total is above the threshold and when the parameter include expected tax is set to false */
if vdThreshold <> 0
then do:
    /* delete all records where the invoice total wht is below a certain threshold */
    for each tqCreditorWithholdingTax
        no-lock
        break by tqCreditorWithholdingTax.tcCreditorCode
              by tqCreditorWithholdingTax.tcCInvoiceReference:
        
        /*initialize */
        if first-of(tqCreditorWithholdingTax.tcCreditorCode)
        then assign vdtotalPaidByCreditor  = 0.
    
        assign vdtotalPaidByCreditor = vdtotalPaidByCreditor + tqCreditorWithholdingTax.tdWHTTotalPaid.  
        /* delete */
        if last-of(tqCreditorWithholdingTax.tcCreditorCode)
        then do:
            if vdtotalPaidByCreditor > vdThreshold
            then do:
                for each btqCreditorWithholdingTax where
                         btqCreditorWithholdingTax.tcCreditorCode = tqCreditorWithholdingTax.tcCreditorCode:
                    delete btqCreditorWithholdingTax .
                end.
            end.  /* vdtotalPaidByCreditor < vdTreshold */  
        end. /* last-of(tqCreditorWithholdingTax.tcCreditorCode) */
    end. /* for each tqCreditorWithholdingTax */
end. /* if not vlPrintExpectedTaxAmtFilter  */