project QadFinancials > class BCInvoice > method ValidateComponentPostWHT

validation procedure

Description

This method evaluates all CInvoiceWHT records


Parameters


iiMfgProMajorVersioninputinteger
iiMfgProMinorVersioninputinteger
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ValidateComponentPost


program code (program5/bcinvoice.p)

/* ======================================================================== */
/* IMPORTANT: This method is run from ValidateComponentPost                 */
/* The code in this method is nested within the for each t_sCInvoice        */
/* This can only work when both methods are defined in the same segment (5) */
/* ======================================================================== */

assign vdTotalFees            = 0
       vdTotalExpenses        = 0
       vdTotalInvoiceAmount   = 0
       vdTotalFeesAndExpenses = 0
       vdTotalTaxAmount       = 0.
       
/* =============================================================== */
/* Only take actions if the INVOICE is enabled for withholding tax */
/* =============================================================== */
if t_sCInvoice.CInvoiceIsWHT
then do :
    
    /* =========================================================== */
    /* Only actions if the supplier is enabled for withholding tax */
    /* =========================================================== */
    <Q-77 run CreditorAllInfo (all) (Read) (NoCache)
       (input t_sCInvoice.Company_ID, (CompanyId)
        input t_sCInvoice.Creditor_ID, (Creditor_ID)
        output dataset tqCreditorAllInfo) in BCreditor >   
    find first tqCreditorAllInfo 
         where tqCreditorAllInfo.tiCreditor_ID = t_sCInvoice.Creditor_ID no-error.
    if available tqCreditorAllInfo
    then do:
       if tqCreditorAllInfo.tlCreditorIsWHT <> true
       then do:
           assign vcMessage      = trim(subst(#T-69'Supplier &1 is not enabled for withholding tax':255(502425280)T-69#, string(tqCreditorAllInfo.tcCreditorCode)))
                  oiReturnStatus = -1.
           <M-11 run SetMessage
              (input  vcMessage (icMessage), 
               input  '':U (icArguments), 
               input  't_sCInvoice.CreditorCode':U (icFieldName), 
               input  tqCreditorAllInfo.tcCreditorCode (icFieldValue), 
               input  'E':U (icType), 
               input  1 (iiSeverity), 
               input  t_sCInvoice.tc_Rowid (icRowid), 
               input  'qadfin-810317':U (icFcMsgNumber), 
               input  '':U (icFcExplanation), 
               input  '':U (icFcIdentification), 
               input  '':U (icFcContext), 
               output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
       end. /* if tqCreditorAllInfo.tlCreditorIsWHT <> true */ 
    end. /* if available tqCreditorAllInfo */
    
    /* ===================================================== */
    /* Go through all underlaying WHT details of the invoice */
    /* ===================================================== */
    assign vlManualyEnteredCIWHTExists = false.
    for each t_sCInvoiceWHT where
             t_sCInvoiceWHT.tc_ParentRowid = t_sCInvoice.tc_Rowid and
             t_sCInvoiceWHT.tc_Status      <> "D":U:
        
        /* ================================================== */
        /* Check if at least on WHT line was entered mnanualy */
        /* ================================================== */
        if (t_sCInvoiceWHT.CInvoiceVat_ID = 0 or 
            t_sCInvoiceWHT.CInvoiceVat_ID = ?) 
           /* and tCInvoiceWHT.CInvoiceWHTIsOrigFromAPM = false */
        then assign vlManualyEnteredCIWHTExists = true.
                     
        /********************************************************************************/
        /* Check tax code exists (Only do this for wht records which have a fee amount) */
        /********************************************************************************/
        if not (t_sCInvoiceWHT.CInvoiceWHTFeeCreditTC = 0 and
                t_sCInvoiceWHT.CInvoiceWHTFeeDebitTC  = 0)
        then do:           
            <Q-87 assign vlFcQueryRecordsAvailable = MfgTaxCodeByTaxCode (NoCache)
               (input t_sCInvoiceWHT.tcVatCode, (TaxCode)
                input t_sCInvoiceWHT.tcDomainCode, (DomainCode)
                input ?, (DiscountAtPayment)
                input ?, (IsTaxByLine)) in BMfgTaxCode>
            if vlFcQueryRecordsAvailable = false 
            then do:
                assign  vcMessage       = trim(subst(#T-74'Withholding tax code &1 is not defined in domain &2.':255(65341)T-74#, t_sCInvoiceWHT.tcVatCode, t_sCInvoiceWHT.tcDomainCode))
                        oiReturnStatus  = -1.
                <M-64 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  'tCInvoiceWHT.tcVatCode':U (icFieldName), 
                    input  t_sCInvoiceWHT.tcVatCode (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  t_sCInvoiceWHT.tc_Rowid (icRowid), 
                    input  'qadfin-581455':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            end. /* if vlFcQueryRecordsAvailable = false */
            
            if iiMfgProMajorVersion >= 3
            and iiMfgProMinorVersion > 6
            then do:
            
                /*************************************/
                /* Check tax type is withholding tax */
                /*************************************/    
                <Q-24 assign vlFcQueryRecordsAvailable = MfgTaxTypeByTaxTypeV01 (NoCache)
                   (input t_sCInvoiceWHT.CInvoiceWHTTaxType, (TaxTypeCode)
                    input true, (TaxTypeIsWHT)) in BMfgTaxType>    
                if vlFcQueryRecordsAvailable = false
                then do:
                    assign  vcMessage       = trim(subst(#T-66'Tax type &1 must be a designated withholding tax type':255(600341100)T-66#, t_sCInvoiceWHT.CInvoiceWHTTaxType))
                            oiReturnStatus  = -1.
                    <M-8 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  't_sCInvoiceWHT.CInvoiceWHTTaxType':U (icFieldName), 
                        input  t_sCInvoiceWHT.CInvoiceWHTTaxType (icFieldValue), 
                        input  'E':U (icType), 
                        input  3 (iiSeverity), 
                        input  t_sCInvoiceWHT.tc_Rowid (icRowid), 
                        input  'qadfin-597106':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                end. /* if vlFcQueryRecordsAvailable = false */  
           end. /* end version check */
        end. /* if not t_sCInvoiceWHT.CInvoiceWHTFeeCreditTC = 0 */           
        
        /********************************************************************/
        /* Fees and expenses cannot all be zero amounts for withholding tax */
        /* This message is only for non-initial invoices                    */
        /********************************************************************/   
        If (t_sCInvoice.CInvoiceIsInitialStatus <> true) and
           (t_sCInvoiceWHT.CInvoiceWHTFeeDebitTC = 0 or
            t_sCInvoiceWHT.CInvoiceWHTFeeDebitTC = ?) and
           (t_sCInvoiceWHT.CInvoiceWHTFeeCreditTC = 0 or
            t_sCInvoiceWHT.CInvoiceWHTFeeCreditTC = ?) and
           (t_sCInvoiceWHT.CInvoiceWHTExpenseDebitTC = 0 or
            t_sCInvoiceWHT.CInvoiceWHTExpenseDebitTC = ?) and
           (t_sCInvoiceWHT.CInvoiceWHTExpenseCreditTC = 0 or
            t_sCInvoiceWHT.CInvoiceWHTExpenseCreditTC = ?)
        then do:
            assign  vcMessage       = trim(#T-16'The withholding tax record cannot have zero fees and expenses':255(424491914)T-16#)
                    oiReturnStatus  = -1.
            <M-47 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  't_sCInvoiceWHT.tcVatCode':U (icFieldName), 
                input  t_sCInvoiceWHT.tcVatCode (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  t_sCInvoiceWHT.tc_Rowid (icRowid), 
                input  'qadfin-363700':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>                
        end. /* if (t_sCInvoiceWHT.CInvoiceWHTFeeDebitTC = 0 */
        
        /* ===================================================== */
        /* Check the type of the invoice against debti-credit    */
        /* ===================================================== */
        if ((t_sCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or
             t_sCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}) and
            (t_sCInvoiceWHT.CInvoiceWHTFeeDebitTC <> 0 or
             t_sCInvoiceWHT.CInvoiceWHTExpenseDebitTC <> 0))
        then do:
            assign  vcMessage       = trim(#T-75'You cannot have debit wht amounts on an invoice or invoice correction':255(53219512)T-75#)
                    oiReturnStatus  = -1.
            <M-48 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-313375':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>                
        end. /* if t_sCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} */  
        if ((t_sCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} or
             t_sCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}) and
            (t_sCInvoiceWHT.CInvoiceWHTFeeCreditTC <> 0 or
             t_sCInvoiceWHT.CInvoiceWHTExpenseCreditTC <> 0))
        then do:
            assign  vcMessage       = trim(#T-70'You cannot have credit wht amounts on a credit note or credit note correction':255(414653977)T-70#)
                    oiReturnStatus  = -1.
            <M-35 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-113216':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>                
        end. /* if t_sCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} */         
        
        /* ===================================================== */
        /* Sun the Fees and the Expenses                         */
        /* ===================================================== */
        assign vdTotalFees         = vdTotalFees + (t_sCInvoiceWHT.CInvoiceWHTFeeCreditTC - t_sCInvoiceWHT.CInvoiceWHTFeeDebitTC)      
               vdTotalExpenses     = vdTotalExpenses + (t_sCInvoiceWHT.CInvoiceWHTExpenseCreditTC - t_sCInvoiceWHT.CInvoiceWHTExpenseDebitTC).               
        
    end. /* for each t_sCInvoiceWHT */
    
    /* ============= */
    /* Sum the taxes */
    /* ============= */
    assign vdTotalTaxAmount = 0.
    for each t_sCInvoiceVat where 
             t_sCInvoiceVat.tc_ParentRowid  = t_sCInvoice.tc_Rowid and 
             t_sCInvoiceVat.tc_Status      <> "D":U
             no-lock :
        assign vdTotalTaxAmount = vdTotalTaxAmount + t_sCInvoiceVat.CInvoiceVatVatCreditTC - t_sCInvoiceVat.CInvoiceVatVatDebitTC.
    end. /* for each t_sCInvoiceVat */ 

    /* ======================================================================================================================================================= */ 
    /* Checks between the WHT-totals and the amounts on the invoice itself;                                                                                    */
    /* 1. IF at least one CInvoiceWHT has been entered manually (meaning tCInvoiceWHT.CInvoiceVat_ID is emtpy or tCInvoiceWHT.CInvoiceWHTIsOrigFromAPM = true) */
    /*    and the total-invoice-amount (amount on first tabpage) minus the sum of all taxes of the invoice (tax-amounts in the tax-grid) is less than the sum  */
    /*    of all WHT-Fees and WHT-Expenses on the WHT-grid of the invoice                                                                                      */
    /*    THEN a warning should be raised                                                                                                                      */
    /* ======================================================================================================================================================= */ 
    assign vdTotalInvoiceAmount    = Absolute(t_sCInvoice.CInvoiceOriginalCreditTC - t_sCInvoice.CInvoiceOriginalDebitTC)
           vdTotalTaxAmount        = Absolute(vdTotalTaxAmount)
           vdTotalFeesAndExpenses  = Absolute(vdTotalFees + vdTotalExpenses).
    if vlManualyEnteredCIWHTExists = true and 
       vdTotalFeesAndExpenses > (vdTotalInvoiceAmount - vdTotalTaxAmount)
    then do:
        assign vcMessage = trim(substitute(#T-81'The total fees and expenses for withholding tax &1 cannot exceed the taxable base amount &2 of the invoice (&3).':255(838533439)T-81#, 
                                           string(vdTotalFeesAndExpenses), string(vdTotalInvoiceAmount - vdTotalTaxAmount),
                                           string(t_sCInvoice.CInvoicePostingYear) + "/":U + t_sCInvoice.tcJournalCode + "/":U + string(t_sCInvoice.CInvoiceVoucher))) + chr(10) + 
                           trim(substitute(#T-80'Invoice original credit TC: &1':255(188695809)T-80#,t_sCInvoice.CInvoiceOriginalCreditTC)) + chr(10) + 
                           trim(substitute(#T-41'Invoice original Debit TC: &1':255(447126913)T-41#,t_sCInvoice.CInvoiceOriginalDebitTC)) + chr(10) + 
                           trim(substitute(#T-5'Total Tax amount: &1':255(337970332)T-5#,vdTotalTaxAmount)) + chr(10) + 
                           trim(substitute(#T-22'Fee total: &1':255(313000360)T-22#,vdTotalFees)) + chr(10) + 
                           trim(substitute(#T-88'Expense Total: &1':255(130530043)T-88#,vdTotalExpenses)).
        
        /* to be sure that you get an error on banking entry iso warning, because that gives problems */
        if oiReturnStatus >= 0
        then assign oiReturnStatus = +1.
        <M-42 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  if oiReturnStatus = +1 then 'W':U else 'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-773886':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    end. /* if vlManualyEnteredCIWHTExists = true and  */
    
end. /* if t_sCInvoice.CInvoiceIsWHT */