project QadFinancials > class BJournalEntry > method AutoUpdatePostingVatVatGroups

Description

AutoUpdatePostingVatVatGroups: remoting non-api method that updates the VatGroup on a number of PostingVat records based on some selection createria.


Parameters


icVatCodeinputcharacterTax code
icInputOutputinputcharacterThe value of this parmeter shoudl either be input, either output. Use preprocessors in {&VATINOUT-OUTPUTS}
iiFromVatYearinputintegerFromVatYear
iiFromVatPeriodinputintegerFromVatPeriod
iiToVatYearinputintegerToVatYear
iiToVatPeriodinputintegerToVatPeriod
tVatGroupUpdateResultoutputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program4/bjournalentry.p)

/* ============================================================================================ */
/* AutoUpdatePostingVatVatGroups: remoting-enabled, non-api method that updates the VatGroup_ID */
/* on a number of PostingVat records in the current domain based on some selection createria.   */
/* ============================================================================================ */


/* ===================================== */
/* Initialisation and Exception handling */
/* ===================================== */
assign oiReturnStatus = -98.
empty temp-table tVatGroupUpdateResult.

/* =============================================================================================================== */
/* Validate: no transaction should be active at this moment because this method needs to do several commits itself */
/* =============================================================================================================== */
if viTransactionID <> 0 and
   viTransactionID <> ?
then do :
    assign oiReturnStatus = -1
           vcMessage      = trim(substitute(#T-1'Internal error: the system cannot update the posting VAT groups when a transaction is already active.':255(65625)t-1#)).
    <M-2 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-7228':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    Return.
end. /* if viTransactionID <> 0 and */

/* ============= */
/* Start Queries */
/* Start Block   */
/* ============= */
<Q-40 run VATGroupPrim  (Start) in BVATGroup >
MAJORBLOCK: DO :

    /* ============== */
    /* Validate input */
    /* ============== */
    if icVatCode = ? then assign icVatCode = "":U.
    if iiFromVatYear = ? then assign iiFromVatYear = 0.
    if iiFromVatPeriod = ? then assign iiFromVatPeriod = 0.
    if iiToVatYear = ? then assign iiToVatYear = 0.
    if iiToVatPeriod = ? then assign iiToVatPeriod = 0.
    if icInputOutput = ? then assign icInputOutput = "":U.
    /* Input - output should have a valid value or be empty */
    if icInputOutput <> "":U              and
       icInputOutput <> {&VATINOUT-INPUT} and 
       icInputOutput <> {&VATINOUT-OUTPUT}
    then do :
        assign viLocalReturnStatus = -1
               vcMessage           = trim(substitute(#T-4'Invalid input: the system cannot update the posting VAT groups unless the input-output parameter is completed &1.':255(65632)t-4#,icInputOutput)).
        <M-3 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-7229':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    end. /* if icInputOutput <> "" or */
    /* From shoudl be < To */
    if iiFromVatYear <> 0 and 
       iiToVatYear   <> 0 and 
       (iiFromVatYear * 100 + iiFromVatPeriod) > (iiToVatYear * 100 + iiToVatPeriod)
    then do :
       assign viLocalReturnStatus = -1
               vcMessage           = trim(substitute(#T-6'The From Year period &1/&2 must be before the To Year period &3/&4.':255(65630)t-6#,string(iiFromVatYear),string(iiFromVatPeriod),string(iiToVatYear),string(iiToVatPeriod))).
        <M-5 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-7230':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    end. /* if iiFromVatYear <> 0 and  */
    /* Vat should exist when filled */
    if icVatCode <> "":U 
    then do :
        <Q-7 run VATPrim (all) (Read) (NoCache)
           (input ?, (VatId)
            input icVatCode, (VatCode)
            input viDomainID, (DomainId)
            input ?, (DomainCode)
            input icInputOutput, (VatInOut)
            output dataset tqVATPrim) in BVAT >
        Find first tqVATPrim no-lock no-error. /* Use the first option as icInputOutput can be empty and then we have multiple ones */
        if not available tqVATPrim
        then do :
            assign viLocalReturnStatus = -1
                   vcMessage           = trim(substitute(#T-9'The tax combination of code &1 and In-Out &2 should reference an existing Tax record in the current domain.':255(65634)t-9#,icVatCode,icInputOutput)).
            <M-8 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-7231':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        end. /* if not available tqVATPrim */
        else do :
            Find tqVATPrim no-lock no-error. /* omit the first option as icInputOutput can be empty and then we have multiple ones */
            if available tqVATPrim
            then assign viResolvedVatIID = tqVATPrim.tiVat_ID.
        end. /* if available tqVATPrim */
    end. /* if icVatCode <> "":U  */
    /* From should exist (in some cy) when year and period are filled */
    if iiFromVatYear   <> 0 and 
       iiFromVatPeriod <> 0 
    then do :
        <Q-10 assign vlFcQueryRecordsAvailable = VATPeriodPrim (NoCache)
           (input ?, (CompanyId)
            input ?, (VATPeriodID)
            input iiFromVatYear, (Year)
            input iiFromVatPeriod, (Period)) in BVATPeriod >
        if vlFcQueryRecordsAvailable = false
        then do :
            assign viLocalReturnStatus = -1
                   vcMessage           = trim(substitute(#T-12'The From Year-Period combination &1/&2 should reference an existing tax period when both are entered.':255(65639)t-12#,string(iiFromVatYear),string(iiFromVatPeriod))).
            <M-11 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-7232':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        end. /* if vlFcQueryRecordsAvailable = false */
    end. /* if iiFromVatYear   <> 0 and  */
    /* To should exist (in some cy) when year and period are filled */
    if iiToVatYear   <> 0 and 
       iiToVatPeriod <> 0 
    then do :
        <Q-13 assign vlFcQueryRecordsAvailable = VATPeriodPrim (NoCache)
           (input ?, (CompanyId)
            input ?, (VATPeriodID)
            input iiToVatYear, (Year)
            input iiToVatPeriod, (Period)) in BVATPeriod >
        if vlFcQueryRecordsAvailable = false
        then do :
            assign viLocalReturnStatus = -1
                   vcMessage           = trim(substitute(#T-15'The To Year-Period &1/&2 combination should reference an existing tax period when both are entered.':255(65640)t-15#,string(iiFromVatYear),string(iiFromVatPeriod))).
            <M-14 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-7233':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        end. /* if vlFcQueryRecordsAvailable = false */
    end. /* if iiToVatYear   <> 0 and  */
    if viLocalReturnStatus < 0 
    then Leave MAJORBLOCK.
    
    /* =============================================================================================================== */
    /* Perform a query on PostingVat+VatPeriod that will return all postingvat records that fit the selection criteria */
    /* =============================================================================================================== */
    assign viFromVatPeriodYearPeriod = (iiFromVatYear * 100 + iiFromVatPeriod) 
           viToVatPeriodYearPeriod   = ((if iiToVatYear = 0 then 9999 else iiToVatYear) * 100 + (if iiToVatPeriod = 0 then 99 else iiToVatPeriod)).
    <Q-16 run PostingVatForUpdateVatGroup (all) (Read) (NoCache)
       (input ?, (CompanyId)
        input icVatCode, (VatCode)
        input viResolvedVatIID, (VatID)
        input icInputOutput, (VatInOut)
        input viFromVatPeriodYearPeriod, (FromVatPeriodYearPeriod)
        input viToVatPeriodYearPeriod, (ToVatPeriodYearPeriod)
        output dataset tqPostingVatForUpdateVatGroup) in BJournalEntry >
    
    /* =========================================================== */
    /* Go through all these postingvat-records with a break-by on: */
    /* JournalTypeCode+VatID+VatRuleID+VatIsAbsorbed+VatIsRetained */
    /* because the new VatGroup for this combination is the same   */
    /* =========================================================== */
    assign viNewVatGroupID = 0. /* default value */
    for each tqPostingVatForUpdateVatGroup                     
        no-lock 
        break by tqPostingVatForUpdateVatGroup.tcJournalTypeCode
              by tqPostingVatForUpdateVatGroup.tiVat_ID
              by tqPostingVatForUpdateVatGroup.tiVatRuleSequence
              by tqPostingVatForUpdateVatGroup.tlVatIsAbsorbed
              by tqPostingVatForUpdateVatGroup.tlVatIsRetained :
        
        /* ============= */
        /* Start Block   */
        /* ============= */
        MINORBLOCK: DO :
            
            /* ================================================= */
            /* Create an output record with default = error      */
            /* Clear the tFcMessages to make sure we start clean */
            /* ================================================= */
            empty temp-table tFcMessages.
            create tVatGroupUpdateResult.
            assign tVatGroupUpdateResult.tcTopic  = string(tqPostingVatForUpdateVatGroup.tiPostingYear) + "/":U + tqPostingVatForUpdateVatGroup.tcJournalCode + "/":U + string(tqPostingVatForUpdateVatGroup.tiPostingVoucher,"999999999":U).
            if not first-of (tqPostingVatForUpdateVatGroup.tlVatIsRetained) and 
               viNewVatGroupID = ? /* This indicates that the new VatGroup could not be retrieved */
            then assign tVatGroupUpdateResult.tcResult = trim(#T-19'Cannot retrieve the new tax group for this combination: see the previous error messages.':255(65660)t-19#)
                        viMinorBlockReturnStatus       = -3.
            else assign tVatGroupUpdateResult.tcResult = trim(#T-30'Internal error: unknown error occurred.':255(65643)t-30#)
                        viMinorBlockReturnStatus       = 0.
            
            /* ======================================================================================= */
            /* Call the method that returns the new VatGroupID only once for every unique combination  */
            /* We can then use this new VatGroupID to update the lines that differ from this new value */
            /* ======================================================================================= */
            if first-of (tqPostingVatForUpdateVatGroup.tlVatIsRetained)
            then do :
                assign viNewVatGroupID = ?. /* This indicates that the new VatGroup could not be retrieved */
                <M-17 run GetPostingVatVatGroup
                   (input  tqPostingVatForUpdateVatGroup.tcJournalTypeCode (icJournalTypeCode), 
                    input  tqPostingVatForUpdateVatGroup.tcVatCode (icVatCode), 
                    input  tqPostingVatForUpdateVatGroup.tiVatRuleSequence (iiVatRuleSequence), 
                    input  tqPostingVatForUpdateVatGroup.tiInvoiceVatGroup_ID (iiInvoiceVatGroupID), 
                    input  tqPostingVatForUpdateVatGroup.tiCreditNoteVatGroup_ID (iiCreditNoteVatGroupID), 
                    input  tqPostingVatForUpdateVatGroup.tiAbsorbedInvoiceVatGroup_ID (iiAbsorbedInvoiceVatGroupID), 
                    input  tqPostingVatForUpdateVatGroup.tiAbsorbedCreditNVatGroup_ID (iiAbsorbedCreditNVatGroupID), 
                    input  tqPostingVatForUpdateVatGroup.tiRetainVoucherVatGroup_ID (iiRetainVoucherVatGroupID), 
                    input  tqPostingVatForUpdateVatGroup.tiRetainCVoucherVatGroup_ID (iiRetainCVoucherVatGroupID), 
                    input  tqPostingVatForUpdateVatGroup.tlVatIsAbsorbed (ilVatIsAbsorbed), 
                    input  tqPostingVatForUpdateVatGroup.tlVatIsRetained (ilVatIsRetained), 
                    input  tqPostingVatForUpdateVatGroup.tiPostingYear (iiPostingYear), 
                    input  tqPostingVatForUpdateVatGroup.tiPostingVoucher (iiPostingVoucher), 
                    input  tqPostingVatForUpdateVatGroup.tiPostingLineSequence (iiPostingLineSequence), 
                    input  tqPostingVatForUpdateVatGroup.tcGLCode (icPostingLineGLCode), 
                    input  '':U (icPostingVatTcRowid), 
                    output viNewVatGroupID (oiDeterminedVatGroupID), 
                    output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
                if viFcReturnSuper <> 0
                then assign viMinorBlockReturnStatus = viFcReturnSuper.
                /* ============================= */
                /* Leave block in case of errors */
                /* ============================= */
                if viFcReturnSuper < 0 or 
                   viNewVatGroupID = 0 or
                   viNewVatGroupID = ?
                then do :
                    assign viNewVatGroupID                = ? /* This indicates that the new VatGroup could not be retrieved */
                           tVatGroupUpdateResult.tcResult = trim(#T-20'Cannot retrieve the new tax group for this combination.':255(65642)t-20#) + chr(10) + 
                                                            trim(substitute(#T-32'Journal Type: &1':255(65650)T-32#,(if tqPostingVatForUpdateVatGroup.tcJournalTypeCode = ? then "?":U else string(tqPostingVatForUpdateVatGroup.tcJournalTypeCode)))) + chr(10) + 
                                                            trim(substitute(#T-33'Tax Code: &1':255(65655)T-33#,(if tqPostingVatForUpdateVatGroup.tcVatCode = ? then "?":U else string(tqPostingVatForUpdateVatGroup.tcVatCode)))) + chr(10) + 
                                                            trim(substitute(#T-34'Tax Rule Sequence: &1':255(65656)T-34#,(if tqPostingVatForUpdateVatGroup.tiVatRuleSequence = ? then "?":U else string(tqPostingVatForUpdateVatGroup.tiVatRuleSequence)))) + chr(10) + 
                                                            trim(substitute(#T-35'Absorbed Tax: &1':255(65653)T-35#,(if tqPostingVatForUpdateVatGroup.tlVatIsAbsorbed = ? then "?":U else string(tqPostingVatForUpdateVatGroup.tlVatIsAbsorbed)))) + chr(10) + 
                                                            trim(substitute(#T-36'Retained Tax: &1':255(65654)T-36#,(if tqPostingVatForUpdateVatGroup.tlVatIsRetained = ? then "?":U else string(tqPostingVatForUpdateVatGroup.tlVatIsRetained)))) + chr(10) + 
                                                            trim(substitute(#T-37'Returned Tax Group ID: &1':255(65657)T-37#,(if viNewVatGroupID = ? then "?":U else string(viNewVatGroupID)))).
                    if viMinorBlockReturnStatus >= 0
                    then assign viMinorBlockReturnStatus = -3.
                    Leave MINORBLOCK.
                end. /* if viFcReturnSuper < 0 */
                /* ============================================================== */
                /* Query the VatGroupCode based on the ID: used in the output-tt  */
                /* Query uses Append and result-caching                           */
                /* ============================================================== */
                Find first tqVATGroupPrim where 
                           tqVATGroupPrim.tiVatGroup_ID = viNewVatGroupID
                           no-lock no-error.
                if not available tqVATGroupPrim 
                then do :
                    <Q-25 run VATGroupPrim (all) (Append) (Cache)
                       (input viNewVatGroupID, (VatGroupId)
                        input ?, (VatGroupCode)
                        output dataset tqVATGroupPrim) in BVATGroup >
                    Find first tqVATGroupPrim where 
                               tqVATGroupPrim.tiVatGroup_ID = viNewVatGroupID
                               no-lock no-error.
                end. /* if not available tqVATGroupPrim */
                if available tqVATGroupPrim
                then assign vcNewVatGroupCode = tqVATGroupPrim.tcVatGroupCode.
                else assign tVatGroupUpdateResult.tcResult = trim(substitute(#T-31'Corruption detected: cannot find the VAT group based on the VatGroupID &1.':255(65646)t-31#,string(viNewVatGroupID)))
                            viMinorBlockReturnStatus       = -3.
            end. /* if first-of (tqPostingVatForUpdateVatGroup.tlVatIsRetained) */
            
            /* ========================================================================================= */
            /* If the current VatGroupID on the PostingVat is already correct                            */
            /* Then remove the output record and go to the next one (set Return=9999)                    */
            /* Else use a write-direct to update PostingVat.VatGroup_ID (this is transaction on its own) */
            /* ========================================================================================= */
            if viNewVatGroupID <> 0 and 
               viNewVatGroupID <> ? 
            then do :
                if viNewVatGroupID <> tqPostingVatForUpdateVatGroup.tiVatGroup_ID
                then do :
                    assign vcWhereClause = "for each PostingVat where " + 
                                           "         PostingVat.PostingVat_ID = ":U + string(tqPostingVatForUpdateVatGroup.tiPostingVat_ID).
                    <M-21 run StartPersistence
                      (output vhFcComponent (ohPersistence), 
                       output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
                    if viFcReturnSuper <> 0
                    then assign viMinorBlockReturnStatus  = viFcReturnSuper.
                    if viFcReturnSuper < 0 
                    then Leave MINORBLOCK.
                    <M-26 run WriteDirect
                       (input  'PostingVat':U (icTableName), 
                        input  vcWhereClause (icPrepare), 
                        input  'VatGroup_ID':U (icFieldList), 
                        input  'i':U (icFieldListDataTypes), 
                        input  string(viNewVatGroupID) (icAbsolute), 
                        input  '':U (icIncremental), 
                        input  {&TARGETPROCEDURE} (ihClass), 
                        input  vcUserLogin (icUserLogin), 
                        output viFcReturnSuper (oiReturnStatus)) in persistence>
                    if viFcReturnSuper <> 0
                    then assign viMinorBlockReturnStatus  = viFcReturnSuper.
                    if viFcReturnSuper < 0 
                    then Leave MINORBLOCK.
                end. /* if viNewVatGroupID <> tqPostingVatForUpdateVatGroup.tiVatGroup_ID */
                else assign viMinorBlockReturnStatus = 9999. /* Inidcates that no changes are needed and that the output-tt record can be removed */
            end. /* if viNewVatGroupID <> 0 and */
        
        /* ============= */
        /* End Block     */
        /* ============= */
        END. /* MINORBLOCK */
        
        /* ================================================================= */
        /* Error Handling: keep track and store any errors/warnings occured  */
        /* ================================================================= */
        if viMinorBlockReturnStatus = 9999
        then delete tVatGroupUpdateResult.
        else do :
            if viMinorBlockReturnStatus = 0
            then assign tVatGroupUpdateResult.tcResult = trim(substitute(#T-22'Correctly processed: VAT Group &3 is changed into &1 &2.':255(65658)t-22#,vcNewVatGroupCode,string(viNewVatGroupID),tqPostingVatForUpdateVatGroup.tcVatGroupCode)).
            else do :
                /* Add general result to the result temp-table */
                if viMinorBlockReturnStatus > 0
                then assign tVatGroupUpdateResult.tcResult = trim(#T-23'Warning occurred.':255(65659)t-23#) + ": ":U + string(viMinorBlockReturnStatus) + chr(10) + tVatGroupUpdateResult.tcResult.
                else assign tVatGroupUpdateResult.tcResult = trim(#T-24'Error occurred.':255(65600)t-24#) + ": ":U + string(viMinorBlockReturnStatus) + chr(10) + tVatGroupUpdateResult.tcResult.
                /* Add the messages to the result temp-table */
                for each tFcMessages : 
                    assign tVatGroupUpdateResult.tcResult = tVatGroupUpdateResult.tcResult + chr(10) + chr(10) + 
                                                            tFcMessages.tcFcMsgNumber + "; ":U + tFcMessages.tcFcMessage.
                    
                end. /* for each tFcMessages */
                /* Put the return of this single transaction to the return of the method */
                if viMinorBlockReturnStatus < viLocalReturnStatus
                then assign viLocalReturnStatus = viMinorBlockReturnStatus.
            end. /* if viMinorBlockReturnStatus <> 0 */
        end. /* not if viMinorBlockReturnStatus = 9999 */
        
    end. /* for each tqPostingVatForUpdateVatGroup */
    
/* ============= */
/* End Block     */
/* End Queries   */
/* ============= */
END. /* MAJORBLOCK */
<Q-39 run VATGroupPrim  (Stop) in BVATGroup >

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