project QadFinancials > class BWithholdingTax > method CreateAndOrUpdateWHTSub

Description

CreateAndOrUpdateWHTSub: sub-method of CreateAndOrUpdateWHT that will create and/or update the WHT records based on input temp-table that differs from the class-table


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BWithholdingTax.CreateAndOrUpdateWHT


program code (program7/bwithholdingtax.p)

/* ============================================================================================================== */
/* This method is a submethod of CreateAndOrUpdateWHT - see that method for all details                           */
/* ============================================================================================================== */
/* By now, we have loaded all instance-data that needed to be loaded (for all tInvoiceWHTPaymentSub record of the */
/* tInvoiceWHTPayment-record with tInvoiceWHTPayment.tlCreateWHT = false).                                        */
/* Now we can go through the tInvoiceWHTPayment records, read the associated CInvoiceWHT records with all         */
/* related data and calculate all 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                   */
/* ============================================================================================================== */        

/* ==================================== */
/* Exception Handling - Initialisation  */
/* ==================================== */
assign oiReturnStatus           = -98
       viLocalReturnStatus      = 0.

/* =========== */
/* Start block */       
/* =========== */
SUBBLOCK: DO :

    /* =========================================================================================== */
    /* Make sure the input table contains at least one record - if not simply return without error */
    /* =========================================================================================== */
    if not can-find (first tInvoiceWHTPayment)
    then Leave SUBBLOCK.
    
    /* ============================ */
    /* Go through all input records */
    /* ============================ */ 
    for each tInvoiceWHTPayment :
        
        /* ============================================================================================================================= */
        /* Reset the data-items that hold the Total WHT of the invoice (vdCInvoiceTotalWHTAmtTC) and the ProRata (vdProRataAllocatedWHT) */     
        /* ============================================================================================================================= */
        assign vdCInvoiceTotalWHTAmtTC = 0
               vdProRataAllocatedWHT   = 0.
        
        /* =========================================================================================================================================== */
        /* Read once all CInvoiceWHT-records for the CInvoice - complex query with a lot of joins                                                      */
        /* In case this method its caller is called from BCInvoice (tInvoiceWHTPayment.tiCACInvoiceID is filled) then this means we are dealing with a */
        /* supplier-adjustment (one CInvoice is created and linked to an existing CInvoice). In that case there will be no CInvoiceWHT records in the  */
        /* db yet for the new CInvoice as the CInvoice is stored in the same transaction as the one holding this class. The solution in this case is   */
        /* that the CInvoice-related data we need here is taken from input temp-table tInvoiceWHTPaymentCIWHT - in all other cases the data is read    */
        /* from the db. Temp-table tInvoiceWHTPaymentCIWHT and tqCInvoiceWHTForWHTCreation have the same structure so in case there is nothing in the  */
        /* db yet then we will simply copy the records of tInvoiceWHTPaymentCIWHT into tqCInvoiceWHTForWHTCreation so the further program-code can be  */
        /* the same for both situations                                                                                                                */
        /* =========================================================================================================================================== */
        <Q-64 run CInvoiceWHTForWHTCreation (all) (Read) (NoCache)
           (input ?, (CompanyId)
            input tInvoiceWHTPayment.tiCInvoiceID, (CInvoiceId)
            input ?, (UpdateTax)
            output dataset tqCInvoiceWHTForWHTCreation) in BCInvoice>  
        if tInvoiceWHTPayment.tiCACInvoiceID <> 0 and 
           tInvoiceWHTPayment.tiCACInvoiceID <> ? and 
           not can-find (first tqCInvoiceWHTForWHTCreation where
                               tqCInvoiceWHTForWHTCreation.tiCInvoice_ID = tInvoiceWHTPayment.tiCInvoiceID)
        then do :
            for each tInvoiceWHTPaymentCIWHT where 
                     tInvoiceWHTPaymentCIWHT.tiCInvoice_ID = tInvoiceWHTPayment.tiCInvoiceID 
                     no-lock :
                create tqCInvoiceWHTForWHTCreation.
                buffer-copy tInvoiceWHTPaymentCIWHT 
                    to tqCInvoiceWHTForWHTCreation.
            end. /* for each tInvoiceWHTPaymentCIWHT where */
        end. /* if tInvoiceWHTPayment.tiCACInvoiceID <> 0 and  */
        if not can-find (first tqCInvoiceWHTForWHTCreation where
                               tqCInvoiceWHTForWHTCreation.tiCInvoice_ID = tInvoiceWHTPayment.tiCInvoiceID)
        then do :
            assign vcMessage           = trim(substitute(#T-4'Internal error: Unable to find the WHT-details of the supplier-invoice (&1 / &2) for payment &3.':255(198767151)T-4#,tInvoiceWHTPaymentCIWHT.tcCInvoiceReference,string(tInvoiceWHTPayment.tiCInvoiceID),tInvoiceWHTPayment.tcPaymentReference))
                   viLocalReturnStatus = -3.
            <M-92 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-285145':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
            Leave SUBBLOCK.
        end. /* if not can-find (first tqCInvoiceWHTForWHTCreation where */
        
        /* ======================================================= */
        /* Go through them all CInvoiceWHT records of the CInvoice */
        /* ======================================================= */
        for each tqCInvoiceWHTForWHTCreation where
                 tqCInvoiceWHTForWHTCreation.tiCInvoice_ID = tInvoiceWHTPayment.tiCInvoiceID :
            
            /* ================================================================================================================== */
            /* Set once the Total WHT of the invoice and the ProRata holding the Allocated amount aganst the Invoice Amount       */     
            /* In case the pro-rate becomes zero then we can simply leave the block as in that case nothing no actions are needed */
            /* ================================================================================================================== */
            if vdCInvoiceTotalWHTAmtTC = 0 and 
               vdProRataAllocatedWHT   = 0 /* meaning this is the first time we are in the for-each loop for this invoice */
            then do :   
                if tInvoiceWHTPayment.tdAllocatedAmountTC                                                                   = 0 or 
                   tInvoiceWHTPayment.tdAllocatedAmountTC                                                                   = ? or 
                   (tqCInvoiceWHTForWHTCreation.tdCInvoiceOriginalDebitTC - tqCInvoiceWHTForWHTCreation.tdCInvoiceOriginalCreditTC) = 0 or
                   (tqCInvoiceWHTForWHTCreation.tdCInvoiceOriginalDebitTC - tqCInvoiceWHTForWHTCreation.tdCInvoiceOriginalCreditTC) = ?
                then Leave /* Leaving for each tqCInvoiceWHTForWHTCreation */.
                assign vdCInvoiceTotalWHTAmtTC = abs(tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTTotAmtDebitTC - tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTTotAmtCreditTC)
                       vdProRataAllocatedWHT   = abs(tInvoiceWHTPayment.tdAllocatedAmountTC / 
                                                     (tqCInvoiceWHTForWHTCreation.tdCInvoiceOriginalDebitTC - tqCInvoiceWHTForWHTCreation.tdCInvoiceOriginalCreditTC)).
                if vdProRataAllocatedWHT = 0 or 
                   vdProRataAllocatedWHT = ?
                then Leave /* Leaving for each tqCInvoiceWHTForWHTCreation */.
            end. /* if vdCInvoiceTotalWHTAmtTC = 0 and  */
            
            /* ============================================================================= */
            /* Actions for the FEE - note that a single CInvoiceWHT can hold a fee and an    */
            /* expense and if so then this results in 2 WHT records for a single CInvoiceWHT */
            /* ============================================================================= */
            if (tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTFeeDebitTC  <> 0 and 
                tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTFeeDebitTC  <> ?) or 
               (tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTFeeCreditTC <> 0 and 
                tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTFeeCreditTC <> ?)
            then do :
                                
                /* ========================================================================================================================== */
                /* If tInvoiceWHTPayment.tlCreateWHT = false then find the corresponding tWHT as it is loaded already and set its status to C */
                /* If tInvoiceWHTPayment.tlCreateWHT = false then create a new tWHT and assign the logical key                                */ 
                /* ========================================================================================================================== */
                if tInvoiceWHTPayment.tlCreateWHT = false
                then do :
                    find tWHT where
                         tWHT.Company_ID              = tqCInvoiceWHTForWHTCreation.tiCompany_ID        and
                         tWHT.CInvoiceWHT_ID          = tqCInvoiceWHTForWHTCreation.tiCInvoiceWHT_ID    and
                         tWHT.BankStateAlloc_ID       = tInvoiceWHTPayment.tiBankStateAllocID       and
                         tWHT.CDocumentInvoiceXref_ID = tInvoiceWHTPayment.tiCDocumentInvoiceXrefID and 
                         tWHT.OIAdjustLn_ID           = tInvoiceWHTPayment.tiOIAjustLnID            and 
                         tWHT.CACInvoice_ID           = tInvoiceWHTPayment.tiCACInvoiceID           and
                         tWHT.WHTChargeType           = {&WHTCHARGETYPE-FEE}
                         no-error.
                    if not available tWHT  
                    then do :
                        assign vcMessage           = trim(substitute(#T-59'Internal error: Unable to find the WHT-record although is was supposed to exist. Method:=&1.':255(14191205)T-59#,"BWithholdingTax:CreateAndOrUpdateWHTSub":U)) + chr(10) +
                                                     trim(substitute(#T-17'Details:':255(984380627)T-17#)) + chr(10) + 
                                                     trim(substitute("Company-ID: &1":U,string(tqCInvoiceWHTForWHTCreation.tiCompany_ID))) + chr(10) + 
                                                     trim(substitute("CInvoiceWHT-ID: &1":U,string(tqCInvoiceWHTForWHTCreation.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-198665':U (icFcMsgNumber), 
                            input  '':U (icFcExplanation), 
                            input  '':U (icFcIdentification), 
                            input  '':U (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                        leave SUBBLOCK.
                    end. /* if not available tWHT */
                    assign tWHT.tc_Status = "C":U.
                end. /* if tInvoiceWHTPayment.tlCreateWHT = false */
                else do :
                    <M-60 run AddDetailLine
                       (input  'WHT':U (icTable), 
                        input  ? (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                    if viFcReturnSuper < 0 then leave SUBBLOCK.
                    assign tWHT.Company_ID              = tqCInvoiceWHTForWHTCreation.tiCompany_ID
                           tWHT.CInvoiceWHT_ID          = tqCInvoiceWHTForWHTCreation.tiCInvoiceWHT_ID
                           tWHT.BankStateAlloc_ID       = tInvoiceWHTPayment.tiBankStateAllocID    
                           tWHT.CDocumentInvoiceXref_ID = tInvoiceWHTPayment.tiCDocumentInvoiceXrefID
                           tWHT.OIAdjustLn_ID           = tInvoiceWHTPayment.tiOIAjustLnID
                           tWHT.CACInvoice_ID           = tInvoiceWHTPayment.tiCACInvoiceID
                           tWHT.WHTNumber               = tInvoiceWHTPayment.tiWHTNumber
                           tWHT.WHTChargeType           = {&WHTCHARGETYPE-FEE}.
                end. /* Not if tInvoiceWHTPayment.tlCreateWHT = false */
                
                /* ====================================================== */
                /* Call a submethod to do all TC-assignement for the Fees */ 
                /* ====================================================== */
                <M-72 run CreateAndOrUpdateWHTSubFee
                   (input  vdCInvoiceTotalWHTAmtTC (idCInvoiceTotalWHTAmtTC), 
                    input  vdProRataAllocatedWHT (idProRataAllocatedWHT), 
                    output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>                
                if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then Leave SUBBLOCK.
                
                /* ====================================================== */
                /* Call a submethod to do all TC-assignement for the Fees */ 
                /* ====================================================== */
                <M-49 run CreateAndOrUpdateWHTSubAssignLCCC  (output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then Leave SUBBLOCK.
                
                /* ====================================================== */
                /* Call a submethod to do all TC-rounding                 */ 
                /* ====================================================== */
                <M-25 run CreateAndOrUpdateWHTSubRoundTC  (output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then Leave SUBBLOCK.
                
            end. /* if (tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTFeeDebitTC  <> 0 and  */
            
            /* ============================================================================== */
            /* Actions for the Expense - note that a single CInvoiceWHT can hold a fee and an */
            /* expense and if so then this results in 2 WHT records for a single CInvoiceWHT  */
            /* ============================================================================== */
            if (tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTExpenseDebitTC  <> 0 and 
                tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTExpenseDebitTC  <> ?) or 
               (tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTExpenseCreditTC <> 0 and 
                tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTExpenseCreditTC <> ?)
            then do :
                                
                /* ========================================================================================================================== */
                /* If tInvoiceWHTPayment.tlCreateWHT = false then find the corresponding tWHT as it is loaded already and set its status to C */
                /* If tInvoiceWHTPayment.tlCreateWHT = false then create a new tWHT and assign the logical key                                */ 
                /* ========================================================================================================================== */
                if tInvoiceWHTPayment.tlCreateWHT = false
                then do :
                    find tWHT where
                         tWHT.Company_ID              = tqCInvoiceWHTForWHTCreation.tiCompany_ID        and
                         tWHT.CInvoiceWHT_ID          = tqCInvoiceWHTForWHTCreation.tiCInvoiceWHT_ID    and
                         tWHT.BankStateAlloc_ID       = tInvoiceWHTPayment.tiBankStateAllocID       and
                         tWHT.CDocumentInvoiceXref_ID = tInvoiceWHTPayment.tiCDocumentInvoiceXrefID and 
                         tWHT.OIAdjustLn_ID           = tInvoiceWHTPayment.tiOIAjustLnID            and
                         tWHT.CACInvoice_ID           = tInvoiceWHTPayment.tiCACInvoiceID           and
                         tWHT.WHTChargeType           = {&WHTCHARGETYPE-EXPENSE}
                         no-error.
                    if not available tWHT  
                    then do :
                        assign vcMessage           = trim(substitute(#T-24'Internal error: Unable to find the WHT-record although is was supposed to exist. Method:=&1.':255(14191205)T-24#,"BWithholdingTax:CreateAndOrUpdateWHTSub":U)) + chr(10) +
                                                     trim(substitute(#T-14'Details:':255(984380627)T-14#)) + chr(10) + 
                                                     trim(substitute("Company-ID: &1":U,string(tqCInvoiceWHTForWHTCreation.tiCompany_ID))) + chr(10) + 
                                                     trim(substitute("CInvoiceWHT-ID: &1":U,string(tqCInvoiceWHTForWHTCreation.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-65 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-325044':U (icFcMsgNumber), 
                            input  '':U (icFcExplanation), 
                            input  '':U (icFcIdentification), 
                            input  '':U (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                        leave SUBBLOCK.
                    end. /* if not available tWHT */
                    assign tWHT.tc_Status = "C":U.
                end. /* if tInvoiceWHTPayment.tlCreateWHT = false */
                else do :
                    <M-26 run AddDetailLine
                       (input  'WHT':U (icTable), 
                        input  ? (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                    if viFcReturnSuper < 0 then leave SUBBLOCK.
                    assign tWHT.Company_ID              = tqCInvoiceWHTForWHTCreation.tiCompany_ID
                           tWHT.CInvoiceWHT_ID          = tqCInvoiceWHTForWHTCreation.tiCInvoiceWHT_ID
                           tWHT.BankStateAlloc_ID       = tInvoiceWHTPayment.tiBankStateAllocID    
                           tWHT.CDocumentInvoiceXref_ID = tInvoiceWHTPayment.tiCDocumentInvoiceXrefID
                           tWHT.OIAdjustLn_ID           = tInvoiceWHTPayment.tiOIAjustLnID
                           tWHT.CACInvoice_ID           = tInvoiceWHTPayment.tiCACInvoiceID
                           tWHT.WHTNumber               = tInvoiceWHTPayment.tiWHTNumber
                           tWHT.WHTChargeType           = {&WHTCHARGETYPE-EXPENSE}.
                end. /* Not if tInvoiceWHTPayment.tlCreateWHT = false */
                
                /* ========================================================== */
                /* Call a submethod to do all TC-assignement for the Expenses */ 
                /* ========================================================== */
                <M-15 run CreateAndOrUpdateWHTSubExpense
                   (input  vdCInvoiceTotalWHTAmtTC (idCInvoiceTotalWHTAmtTC), 
                    input  vdProRataAllocatedWHT (idProRataAllocatedWHT), 
                    output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>                
                if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then Leave SUBBLOCK.
                
                /* ====================================================== */
                /* Call a submethod to do all TC-assignement for the Fees */ 
                /* ====================================================== */
                <M-22 run CreateAndOrUpdateWHTSubAssignLCCC  (output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then Leave SUBBLOCK.

                /* ====================================================== */
                /* Call a submethod to do all TC-rounding                 */ 
                /* ====================================================== */
                <M-58 run CreateAndOrUpdateWHTSubRoundTC  (output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
                if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then Leave SUBBLOCK.                
            end. /* if (tqCInvoiceWHTForWHTCreation.tdCInvoiceWHTExpenseDebitTC  <> 0 and  */
            
        end. /* for each tqCInvoiceWHTForWHTCreation where */
        
    end. /* for each tInvoiceWHTPayment : */    
        
END. /* SUBBLOCK */

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