project QadFinancials > class BWithholdingTax > method CreateAndOrUpdateWHT

Description

Create and/or update the WHT records based on input temp-table that differs from the class-table


Parameters


tInvoiceWHTPaymentinputtemp-tabletInvoiceWHTPayment: temp-table used as interface for methods to create or update WHT-record.
Fields:
tiCInvoiceID: Mandatory identifier of the invoice
tdAllocatedAmountTC: Allocated amount of the payment for this invoice - Mandatory
tdDiscountAmountTC: Discount amount of the payment for this invoice
tdWHTTaxableFeeTC: Taxable fee of the payment for this invoice (only for WHT of type 'Fee')
tdWHTAmtTC: wht amount of the payment for this invoice
tlCreateWHT: True in case we need to create a WHT-record, false in case we need to update an existing WHT-record
tiWHTNumber: wht number, mandatory
tcPaymentReference: payment reference, mandatory
tcCurrencyCode: currency code of the payment, mandatory
tiCDocumentInvoiceXrefID: unique identifier of the payment record - either this or tiBankStateAlloc_ID must be filled
tiBankStateAllocID : unique identifier of the bank state alloc record - either this or tiCDocumentInvoiceXref_ID must be filled
ttPostingDate: posting date of the payment - mandatory
tiPaymentAccountingYear: Accounting year that embraces the posting date of the payment - optional
tiPaymentAccountingPeriod: Accounting period that embraces the posting date of the payment - optional
ttSettlementDate: SettlementDate - optional
tcWHTStatus: Status the WHT record should get - mandatory
tlUpdateAllAmounts: Only applicable in case tlCreateWHT is false. When this is true then all amount will be recalculated. Normally this field will only be true in case the AllocatedAmt, DiscountAmt or the PaymentStatus are changed
tdPaymentAccRateTCLC: Accounting tax rate TC-LC on PaymentDate - mandatory
tdPaymentAccScaleTCLC: Accounting tax scale factor TC-LC on PaymentDate - mandatory
tdPaymentAccRateTCCC: Accounting tax rate TC-CC on PaymentDate - mandatory
tdPaymentAccScaleTCCC: Accounting tax scale factor TC-CC on PaymentDate - mandatory
tdPaymentWHTRateTCLC: withholding tax rate TC-LC on PaymentDate - mandatory
tdPaymentWHTScaleTCLC: withholding tax scale factor TC-LC on PaymentDate - mandatory
tdPaymentWHTRateTCCC: withholding tax rate TC-CC on PaymentDate - mandatory
tdPaymentWHTScaleTCCC: withholding tax scale factor TC-CC on PaymentDate - mandatory
tInvoiceWHTPaymentCIWHTinputtemp-tabletInvoiceWHTPaymentCIWHT: temp-table used as interface for methods to create or update WHT-record.

This temp-table should only be filled by callers of this method in case the CInvoiceWHT records are not yet stored in the database at the moment this call is made
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankEntry.AdditionalUpdatesInvWHT
method BCDocument.AdditionalUpdatesWHT
method BCInvoice.AdditionalUpdatesAllCreateCA
method BOpenItemAdjustment.OIAdjCommitSubCInvoice


program code (program7/bwithholdingtax.p)

/* ================================================================================================================================= */    
/* This method should be called from all places where any kind of payment is made against a CInvoice that contains Withholding Taxes */
/* as this method will make sure the WHT is registered properly for later declarion of the WHT to the authorities                    */
/* Description of the interface:                                                                                                     */
/*  tInvoiceWHTPayment: input temp-table used to create or update WHT-records                                                        */
/*  Fields:                                                                                                                          */
/*      tiCInvoiceID: Mandatory identifier of the invoice                                                                            */
/*      tdAllocatedAmountTC: Allocated amount of the payment for this invoice - Mandatory                                            */
/*      tdDiscountAmountTC: Discount amount of the payment for this invoice                                                          */
/*      tdWHTTaxableFeeTC: Taxable fee of the payment for this invoice (only for WHT of type 'Fee')                                  */
/*      tdWHTAmtTC: wht amount of the payment  for this invoice                                                                      */
/*      tlCreateWHT: True in case we need to create a WHT-record, false in case we need to update an existing WHT-record             */
/*      tiWHTNumber: wht number, mandatory in case tlCreateWHT is true                                                               */
/*      tPaymentReference: payment reference, mandatory                                                                              */
/*      tcCurrencyCode: currency code of the payment, mandatory                                                                      */
/*      tiCDocumentInvoiceXrefID: unique ID of the payment record - either this, tiOIAdjustLnID or tiBankStateAllocID must be filled */
/*      tiBankStateAllocID : unique ID of the bank state alloc record - either this, tiOIAdjustLnID, tiCACInvoiceID or               */
/*                           tiCDocumentInvoiceXref_ID                                                                               */
/*                           must be filled                                                                                          */
/*      tiOIAdjustLnID :  unique ID of the open item adjustment line record - either this, tiCACInvoiceID, tiBankStateAllocID or     */
/*                        tiCDocumentInvoiceXref_ID must be filled                                                                   */
/*      tiCACInvoiceID :  unique ID of the supplier-adjustment invoice - either this, tiBankStateAllocID, tiOIAdjustLnID  or         */
/*                        tiCDocumentInvoiceXref_ID must be filled                                                                   */
/*      ttPostingDate: posting date of the payment - mandatory                                                                       */
/*      tiPaymentAccountingYear: Accounting year that embraces the posting date of the payment - optional                            */
/*      tiPaymentAccountingPeriod: Accounting period that embraces the posting date of the payment - optional                        */
/*      ttSettlementDate: SettlementDate - optional                                                                                  */
/*      tcWHTStatus: Status the WHT record should get - mandatory                                                                    */
/*      tlUpdateAllAmounts: Only applicable in case tlCreateWHT is false. When this is true then all amount will be recalculated.    */
/*                          This field will only be true in case the AllocatedAmt, DiscountAmt or the PaymentStatus are changed      */
/*      tdPaymentAccRateTCLC: Accounting tax rate TC-LC on PaymentDate - mandatory                                                   */
/*      tdPaymentAccScaleTCLC: Accounting tax scale factor TC-LC on PaymentDate - mandatory                                          */
/*      tdPaymentAccRateTCCC: Accounting tax rate TC-CC on PaymentDate - mandatory                                                   */
/*      tdPaymentAccScaleTCCC: Accounting tax scale factor TC-CC on PaymentDate - mandatory                                          */
/*      tdPaymentWHTRateTCLC: withholding tax rate TC-LC on PaymentDate - mandatory                                                  */
/*      tdPaymentWHTScaleTCLC: withholding tax scale factor TC-LC on PaymentDate - mandatory                                         */
/*      tdPaymentWHTRateTCCC: withholding tax rate TC-CC on PaymentDate - mandatory                                                  */
/*      tdPaymentWHTScaleTCCC: withholding tax scale factor TC-CC on PaymentDate - mandatory                                         */
/* ================================================================================================================================= */


/* ==================================== */
/* Exception Handling - Initialisation  */
/* ==================================== */
assign oiReturnStatus           = -98
       viLocalReturnStatus      = 0
       vcListWHTIDToLoad        = "":U.

/* =========== */
/* Start block */       
/* =========== */
CREATEUPDATEBLOCK: DO :

    /* ====================================================== */
    /* Make sure the input table contains at least one record */
    /* ====================================================== */
    if not can-find (first tInvoiceWHTPayment)
    then do:          
        assign vcMessage           = trim(substitute(#T-51'Internal Error: No input record available for method &1.':255(9324946)T-51#,"BWithholdingTax:CreateAndOrUpdateWHT":U))
               viLocalReturnStatus = -3.
        <M-90 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-248497':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
        Leave CREATEUPDATEBLOCK.
    end. /* if not can-find (first tInvoiceWHTPayment) */
    
    /* ============================== */
    /* Go through all input records:  */
    /* ============================== */
    for each tInvoiceWHTPayment :
        
        /* ================================================== */
        /* Validate the record content by calling a submethod */
        /* ================================================== */
        <M-86 run CreateAndOrUpdateWHTValidate  (output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
        if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then Leave CREATEUPDATEBLOCK.
        
        /* ================================================================================================================================== */
        /* Compose a list with the records to be loaded. - Note that if this list becomes to long we need to do a DataLoad in between.        */
        /* As the input table of this method in on CInvoice-level and the WHT-records (and thus also the DataLoads) are on CInvoiceWHT-level and even combined with the WHTChargeType,, */
        /* we need to read here the CInvoiceWHT of the CInvoice. Note that later on in the processing we will read CInvoiceWHT once again    */
        /* but with much more details. We know that reading the CInvoiceWHT twice is overhead but that is the only possible solution to make  */
        /* sure we can minimize the number of DataLoad we have to do                                                                          */
        /* Create tInvoiceWHTPaymentSub for the record to be loaded (one for every CInvoiceWHT)                                               */
        /* ================================================================================================================================== */
        if tInvoiceWHTPayment.tlCreateWHT = false                                                                                        
        then do :
            /* Simple query to get the CInvoiceWHT-records for the CInvoice */
            <Q-27 run CInvoiceWHTForIDOnly (all) (Read) (NoCache)
               (input tInvoiceWHTPayment.tiCInvoiceID, (CInvoiceID)
                input ?, (CompanyId)
                output dataset tqCInvoiceWHTForIDOnly) in BCInvoice>
            /* Go thourhg the CInvoiceWHT-records for the CInvoice */                
            for each tqCInvoiceWHTForIDOnly where 
                     tqCInvoiceWHTForIDOnly.tiCInvoice_ID = tInvoiceWHTPayment.tiCInvoiceID :
                /* Tackle this once for the Fee-side and once for the Fee-side: now Fee */
                if (tqCInvoiceWHTForIDOnly.tdCInvoiceWHTFeeCreditTC <> 0 and 
                    tqCInvoiceWHTForIDOnly.tdCInvoiceWHTFeeCreditTC <> ?) or 
                   (tqCInvoiceWHTForIDOnly.tdCInvoiceWHTFeeDebitTC <> 0 and 
                    tqCInvoiceWHTForIDOnly.tdCInvoiceWHTFeeDebitTC <> ?)
                then do :
                    /* Query on WHT to get the prim-key that will be used in the DataLoad later on */
                    <Q-89 run WHTPrim (all) (Read) (NoCache)
                       (input tqCInvoiceWHTForIDOnly.tiCompany_ID, (CompanyID)
                        input tqCInvoiceWHTForIDOnly.tiCInvoiceWHT_ID, (CInvoiceWHTID)
                        input tInvoiceWHTPayment.tiBankStateAllocID, (BankStateAllocID)
                        input tInvoiceWHTPayment.tiCDocumentInvoiceXrefID, (CDocumentInvoiceXRefID)
                        input ?, (WHTID)
                        input {&WHTCHARGETYPE-FEE}, (WHTChargeType)
                        input tInvoiceWHTPayment.tiOIAjustLnID, (OIAdjustLnID)
                        input tInvoiceWHTPayment.tiCACInvoiceID, (CACInvoiceID)
                        output dataset tqWHTPrim) in BWithholdingTax>
                    find tqWHTPrim where
                         tqWHTPrim.tiCompany_ID              = tqCInvoiceWHTForIDOnly.tiCompany_ID         and
                         tqWHTPrim.tiCInvoiceWHT_ID          = tqCInvoiceWHTForIDOnly.tiCInvoiceWHT_ID     and
                         tqWHTPrim.tiBankStateAlloc_ID       = tInvoiceWHTPayment.tiBankStateAllocID       and
                         tqWHTPrim.tiCDocumentInvoiceXref_ID = tInvoiceWHTPayment.tiCDocumentInvoiceXrefID and 
                         tqWHTPrim.tiOIAdjustLn_ID           = tInvoiceWHTPayment.tiOIAjustLnID            and
                         tqWHTPrim.tiCACInvoice_ID           = tInvoiceWHTPayment.tiCACInvoiceID           and 
                         tqWHTPrim.tcWHTChargeType           = {&WHTCHARGETYPE-FEE}
                         no-error.
                    /* If for some reason the caller of this method has marked tlCreateWHT = false but there are no existing records */
                    /* in WHT for some of the underlying CInvoiceWHT-records then we will simply set tlCreateWHT = true, remove the  */
                    /* existing tInvoiceWHTPaymentSub-records for the tInvoiceWHTPayment-record and continue without raising errors  */
                    if not available tqWHTPrim  
                    then do :
                        assign vcMessage           = trim(substitute(#T-93'Internal error: Unable to find the WHT-record although is was supposed to exist. Method:=&1.':255(14191205)T-93#,"BWithholdingTax:CreateAndOrUpdateWHT":U)) + chr(10) +
                                                     trim(substitute(#T-57'Details:':255(984380627)T-57#)) + chr(10) + 
                                                     trim(substitute("Company-ID: &1":U,string(tqCInvoiceWHTForIDOnly.tiCompany_ID))) + chr(10) + 
                                                     trim(substitute("CInvoiceWHT-ID: &1":U,string(tqCInvoiceWHTForIDOnly.tiCInvoiceWHT_ID))) + chr(10) + 
                                                     trim(substitute("BankStateAlloc-ID: &1":U,string(tInvoiceWHTPayment.tiBankStateAllocID))) + chr(10) + 
                                                     trim(substitute("CDocumentInvoiceXref: &1":U,string(tInvoiceWHTPayment.tiCDocumentInvoiceXrefID)))  + chr(10) +
                                                     trim(substitute("OIAdjustLn-ID: &1":U, string(tInvoiceWHTPayment.tiOIAjustLnID))) + chr(10) + 
                                                     trim(substitute("CACInvoice-ID: &1":U, string(tInvoiceWHTPayment.tiCACInvoiceID))) + chr(10) + 
                                                     trim(substitute("Charge-Type: &1":U,{&WHTCHARGETYPE-FEE}))
                               viLocalReturnStatus = -3.
                        <M-46 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-127275':U (icFcMsgNumber), 
                            input  '':U (icFcExplanation), 
                            input  '':U (icFcIdentification), 
                            input  '':U (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                        leave CREATEUPDATEBLOCK.
                    end. /* if not available tqWHTPrim */
                    /* Extend the list of the records to load with the ID of the WHT record */
                    assign vcListWHTIDToLoad = vcListWHTIDToLoad + chr(4) + string(tqWHTPrim.tiWHT_ID).
                end. /* if (tqCInvoiceWHTForIDOnly.tdCInvoiceWHTTaxFeeCreditTC <> 0 and  */
                /* Tackle this once for the Expense-side and once for the Fee-side: now Expense */
                if (tqCInvoiceWHTForIDOnly.tdCInvoiceWHTExpenseCreditTC <> 0 and 
                    tqCInvoiceWHTForIDOnly.tdCInvoiceWHTExpenseCreditTC <> ?) or 
                   (tqCInvoiceWHTForIDOnly.tdCInvoiceWHTExpenseDebitTC <> 0 and 
                    tqCInvoiceWHTForIDOnly.tdCInvoiceWHTExpenseDebitTC <> ?)
                then do :
                    /* Query on WHT to get the prim-key that will be used in the DataLoad later on */
                    <Q-42 run WHTPrim (all) (Read) (NoCache)
                       (input tqCInvoiceWHTForIDOnly.tiCompany_ID, (CompanyID)
                        input tqCInvoiceWHTForIDOnly.tiCInvoiceWHT_ID, (CInvoiceWHTID)
                        input tInvoiceWHTPayment.tiBankStateAllocID, (BankStateAllocID)
                        input tInvoiceWHTPayment.tiCDocumentInvoiceXrefID, (CDocumentInvoiceXRefID)
                        input ?, (WHTID)
                        input {&WHTCHARGETYPE-EXPENSE}, (WHTChargeType)
                        input tInvoiceWHTPayment.tiOIAjustLnID, (OIAdjustLnID)
                        input tInvoiceWHTPayment.tiCACInvoiceID, (CACInvoiceID)
                        output dataset tqWHTPrim) in BWithholdingTax>
                    find tqWHTPrim where
                         tqWHTPrim.tiCompany_ID              = tqCInvoiceWHTForIDOnly.tiCompany_ID         and
                         tqWHTPrim.tiCInvoiceWHT_ID          = tqCInvoiceWHTForIDOnly.tiCInvoiceWHT_ID     and
                         tqWHTPrim.tiBankStateAlloc_ID       = tInvoiceWHTPayment.tiBankStateAllocID       and
                         tqWHTPrim.tiCDocumentInvoiceXref_ID = tInvoiceWHTPayment.tiCDocumentInvoiceXrefID and 
                         tqWHTPrim.tiOIAdjustLn_ID           = tInvoiceWHTPayment.tiOIAjustLnID            and  
                         tqWHTPrim.tiCACInvoice_ID           = tInvoiceWHTPayment.tiCACInvoiceID           and                        
                         tqWHTPrim.tcWHTChargeType           = {&WHTCHARGETYPE-EXPENSE}
                         no-error.
                    /* If for some reason the caller of this method has marked tlCreateWHT = false but there are no existing records */
                    /* in WHT for some of the underlying CInvoiceWHT-records then we will simply set tlCreateWHT = true, remove the  */
                    /* existing tInvoiceWHTPaymentSub-records for the tInvoiceWHTPayment-record and continue without raising errors  */
                    if not available tqWHTPrim  
                    then do :
                        assign vcMessage           = trim(substitute(#T-3'Internal error: Unable to find the WHT-record although is was supposed to exist. Method:=&1.':255(14191205)T-3#,"BWithholdingTax:CreateAndOrUpdateWHT":U)) + chr(10) +
                                                     trim(substitute(#T-23'Details:':255(984380627)T-23#)) + chr(10) + 
                                                     trim(substitute("Company-ID: &1":U,string(tqCInvoiceWHTForIDOnly.tiCompany_ID))) + chr(10) + 
                                                     trim(substitute("CInvoiceWHT-ID: &1":U,string(tqCInvoiceWHTForIDOnly.tiCInvoiceWHT_ID))) + chr(10) + 
                                                     trim(substitute("BankStateAlloc-ID: &1":U,string(tInvoiceWHTPayment.tiBankStateAllocID))) + chr(10) + 
                                                     trim(substitute("CDocumentInvoiceXref: &1":U,string(tInvoiceWHTPayment.tiCDocumentInvoiceXrefID))) + chr(10) + 
                                                     trim(substitute("OIAdjustLn-ID: &1":U, string(tInvoiceWHTPayment.tiOIAjustLnID))) + chr(10) +
                                                     trim(substitute("CACInvoice-ID: &1":U, string(tInvoiceWHTPayment.tiCACInvoiceID))) + chr(10) +
                                                     trim(substitute("Charge-Type: &1":U,{&WHTCHARGETYPE-EXPENSE}))
                               viLocalReturnStatus = -3.
                        <M-63 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-551383':U (icFcMsgNumber), 
                            input  '':U (icFcExplanation), 
                            input  '':U (icFcIdentification), 
                            input  '':U (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                        leave CREATEUPDATEBLOCK.
                    end. /* if not available tqWHTPrim */
                    /* Extend the list of the records to load with the ID of the WHT record */
                    assign vcListWHTIDToLoad = vcListWHTIDToLoad + chr(4) + string(tqWHTPrim.tiWHT_ID).
                end. /* if (tqCInvoiceWHTForIDOnly.tdCInvoiceWHTExpenseCreditTC <> 0 and  */                
            end. /* for each tqCInvoiceWHTForIDOnly where  */ 
            /* Perform a DataLoad in case the list is already becomming too long in the loop and afterwards, reset vcListWHTIDToLoad  */
            if length(vcListWHTIDToLoad,'character':U) > 30000
            then do :
                assign vcListWHTIDToLoad = substring(vcListWHTIDToLoad,2,-1,'character':U).
                <M-98 run DataLoad
                   (input  ? (icRowids), 
                    input  vcListWHTIDToLoad (icPkeys), 
                    input  ? (icObjectIds), 
                    input  ? (icFreeform), 
                    input  true (ilKeepPrevious), 
                    output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                if viFcReturnSuper <> 0 
                then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0
                then leave CREATEUPDATEBLOCK.
                assign vcListWHTIDToLoad = "":U.
            end. /* if length(vcListWHTIDToLoad) > 30000 */
        end. /* if tInvoiceWHTPayment.tlCreateWHT = false */
        
    end. /* end for each tInvoiceWHTPayment */
    
    /* ========================================================= */
    /* Load the WHT Records that were not yet loaded in the loop */
    /* ========================================================= */
    if length(vcListWHTIDToLoad,'character':U) > 1
    then do :
        assign vcListWHTIDToLoad = substring(vcListWHTIDToLoad,2,-1,'character':U).
        <M-7 run DataLoad
           (input  ? (icRowids), 
            input  vcListWHTIDToLoad (icPkeys), 
            input  ? (icObjectIds), 
            input  ? (icFreeform), 
            input  true (ilKeepPrevious), 
            output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
        if viFcReturnSuper <> 0 
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave CREATEUPDATEBLOCK.
    end. /* if length(vcListWHTIDToLoad) > 1 */
    
    /* ============================================================================================================== */
    /* By now, we have loaded all instance-data that needs to be loaded (for all tInvoiceWHTPaymentSub record of the */
    /* tInvoiceWHTPayment-record with tInvoiceWHTPayment.tlCreateWHT = false).                                        */
    /* Now we can go through the tInvoiceWHTPayment and underlying tInvoiceWHTPaymentSub record, read the associated  */
    /* CInvoiceWHT records with all related data and calculate all required amounts and then update the loaded        */
    /* WHT-record or create new WHT records.                                                                          */
    /* We do this in a separate submethod to avoid that this methods gets too large and too complex                   */
    /* ============================================================================================================== */
    <M-8 run CreateAndOrUpdateWHTSub  (output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
    if viFcReturnSuper <> 0 
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then leave CREATEUPDATEBLOCK.
    
END. /* CREATEUPDATEBLOCK */

/* ================== */
/* Exception Handling */
/* ================== */
assign oiReturnStatus = viLocalReturnStatus.