project QadFinancials > class TSetStatutoryCurrency > method SetStatutoryCurrency

Description

This method will convert the statutory currency amounts of the specified domain for the selected currency.
It will work directly on the database tables, because of performance issues.


Parameters


icDomainCodeinputcharacter
icNewSCCodeinputcharacter
ilDomainIsStatutoryCurrencyinputlogical
ilPerformUpdatesAnywayinputlogical
ocErrorMessageoutputcharacter
oiDurationInSecondsoutputinteger
ocDurationInTextoutputcharacter
oiPostingLineCounteroutputinteger
olTransactionsDoneoutputlogical
oiReturnStatusoutputinteger


Internal usage


QadFinancials
method BDomain.SetStatutoryCurrency


program code (program1/tsetstatutorycurrency.p)

/* ========================================================================================================================================= */
/* As this is a rather large and complex utility, we will here list the global program-structure so programmers can more easily find back    */
/* what is exactly done where without going through the complete program-code. Please keep this up-to-date when making invasive code-chnages */
/* For every called submethod etc, there is an indention in this overview                                                                    */
/* ========================================================================================================================================= */
/* FUNCTIONAL
Some functional rules about this utility :
    - utility only changes SC - it does not change anything to BC amount of any table
    - utility calculates TC * Rate to get SC. The Matching is an exception where the system takes LC to start from
    - the utility updates PostingHist - so it is needed to run the Account Rebuild. Nevertheless we know there might still be issues so 
      we advise to check consistency of PostingHist and do a rebuild if needed
    - this utility can make new PostingLines for following accounts : ROUND, EXCHANGERATEPROFIT, EXCHANGERATELOSS, VARIANCE ACCOUNTS
*/    
/* ========================================================================================================================================= */
/* TECHNICAL
SetStatutoryCurrencyValidate: All kinds of validation on the input and on the required data in the system
if ilDomainIsStatutoryCurrency = false
then SetStatutoryCurrencyNoStatCurr
        Go through all Fin-tables that hold CC-rates or CC-amounts and assign them to their LC equivalents
        Exception: APMatchingLnPvodCCRate and APMatchingLnPvodCCScale are set to 1 as they are the Rate/Scale between LC and CC 
else SetStatutoryCurrencyBasedOnTC
        SetStatutoryCurrencyBasedOnTCCInvoice
            Assign CC = TC * CCRate in CInvoice and CInvoiceVat
            Assign CC = TC * TaxRate for wht-fields in CInvoice and CInvoiceWHT
        SetStatutoryCurrencyBasedOnTCDInvoice
            Assign CC = TC * CCRate in DInvoice, DInvoiceVat and SInvoiceDeduction
        SetStatutoryCurrencyBasedOnTCCDocument
            Assign CC and CCRate in CDocument based upon the CCRates in the linked CInvoices and DInvoices
            Assign CC = TC * TaxRate for wht-fields in CDocumentInvoiceXRef
            Assign CC in CCollection based upon the linked CDocuments
        SetStatutoryCurrencyBasedOnTCDDocument
            Assign CC and CCRate in DDocument based upon the CCRates in the linked DInvoices
            Assign CC in DCollection based upon the linked DDocuments
        SetStatutoryCurrencyBasedOnTCDPaySel
            Assign CC = TC * TaxRate for the wht-fields in DPaySelLine
        SetStatutoryCurrencyBasedOnTCOIAdjust
            Assign CC = TC * TaxRate for the wht-fields in OIAdjustLn
        SetStatutoryCurrencyBasedOnTCPosting
            For all non-APM posting of the domain :
                PostingClearPreviousBalSCLines: Clear all SC amounts that are incorrect and delete all posting lines previsously generated by this utility
                One of the following is executed:
                    PostingForCDocument
                        Post CCDiff to GLSYSTEMTYPE-ROUND - if one of the postinglines is on a bank-account then Post CCDiff to GLSYSTEMTYPE-EXCHANGEREALPROFIT
                        If it is the cross-cy posting then set all CC and CCRate of the posting to the CC of the C/DInvoice
                        Complex way to get the correct CCRate for every posting-line
                    PostingForDDocument: similar to PostingForCDocument but differrent logic
                    PostingForOIA: similar to PostingForCDocument but differrent logic
                    PostingForBankCashEntry: similar to PostingForCDocument but differrent logic
                    PostingForOthers: similar to PostingForCDocument but differrent logic
                PostingCCBalance: Create additional line to balance whole posting in SC -account differs from the previous executed methods
            end. /* For all non-APM posting of the domain : */
        for each Company of Domains :
            SetStatutoryCurrencyBasedOnTCDet1
                Specific assignments for CC-fields in tables GLOpenItem, ExpNote, ExpNoteLine, BankStateAlloc and  BankStateCollAlloc 
                Assign all CC-fields in the FixedAsset-tables to 0
                Assign all CC-fields in the Revaluation-tables to 0 or delete the revaluation if it was only about CC
            SetStatutoryCurrencyBasedOnTCWht
                Assign all CC-fields in table WHT
            SetStatutoryCurrencyBasedOnTCAPM
                For all APM of the Company + posting of APM: 
                    Assign CC-fields in the Cirec-posting-line based upon the linked CInvoice
                    For APMLines of APM :
                        Assign APMatchingLnPvodCCRate and APMatchingLnPvodCCScale with the rate between LC and CC
                        Assign APMatchingLnReceiptAmtCC based upon the corresponding TC-field and APMatchingLnPvodCCRate and APMatchingLnPvodCCScale and the TC-LC-Rate of the pending-voucher
                        Assign APMatchingLnMatchAmtCC, APMatchingLnVarRateCC and APMatchingLnVarUsageCC based upon the corresponding TC-field and the CCRate of CInvoice 
                        Assign APMatchingLnVarExchRateCC based upon the 3 previously assigned fields
                        Create/update the posting-line for the VarExchRateCC: Debit and Credit can shoft from side and the account used is adapted as well
                        Assign CC-fields on the posting-lines on the RateVar and UsageVar based upon the corresponding TC-field and the CCRate of CInvoice 
                        Assign CC-fields on the posting-lines with the New- and Old-ExpenseAccount based upon the corresponding LC-field and APMatchingLnPvodCCRate and APMatchingLnPvodCCScale and the TC-LC-Rate of the pending-voucher
                        Assign CC-fields on the posting-line with the Receipt-account based upon the corresponding LC-field and APMatchingLnPvodCCRate and APMatchingLnPvodCCScale and the TC-LC-Rate of the pending-voucher
                        SetStatutoryCurrencyBasedOnTCAPMTax: Similar actions for the underlying taxes if the APMLine
                    end. /* For APMLines of APM : */
                    For all PostingLine (and PostingVat) of this APM-Posting that were not yet tackled: 
                        Assign CC-fields based upon TC-field and retrieved CC-Rate
                    end. /* For all PostingLine of this APM-Posting that were not yet tackled: */
                    If Posting is not CC-balanced then create additional posting-line on account GLSYSTEMTYPE-ROUND
                end. /* For all APM of the Company + posting of APM:  */
        end. /* for each Company of Domains : */
     SetStatutoryCurrencyBasedOnTCHist
        Update CC-fields in PostingHist, CHist, CBalance, CHist and CBalance
SetStatutoryCurrencyDomain
    Sets DomainProperty.StatutoryCurrency_ID to the new StatCurr
*/    
/* ========================================================================================================================================= */



/* ===================================== */
/* Exception handling and initialisation */
/* ===================================== */
assign oiReturnStatus         = -98
       viLocalReturnStatus    = 0
       viACExchangeRateTypeID = 0
       viSCExchangeRateTypeID = 0
       viTXExchangeRateTypeID = 0
       vlSCIsFallBack         = false
       vcLogFile              = "StatCurr":U + " ":U + string(Year(today),"9999":U) + "-":U + string(Month(today),"99") + "-":U + string(Day(today),"99") + " ":U + replace(STRING(TIME,"HH:MM:SS"),":":U,"-":U) + ".txt".

/* ==================== */
/* Start Time Recording */
/* ==================== */
assign vtStartDate        = Today
       viStartTime        = Time
       olTransactionsDone = false.
/* === Logging === */
output to value(vcLogFile).
put unformatted STRING(TIME,"HH:MM:SS")
                " Statutory-Currency routine started at ":U STRING(TIME,"HH:MM:SS") " " string(today,"99/99/9999") " with following settings:":U skip 
                "   Domain: ":U icDomainCode skip 
                "   Statutory-Currency enabled: " ilDomainIsStatutoryCurrency skip 
                "   Statutory-Currency: " icNewSCCode skip 
                "   Force updates anyway: " ilPerformUpdatesAnyway skip " " skip.
output close.

/* ==================== */
/* Start Block          */
/* ==================== */
GLOBALBLOCK:
do:

    /* ======================== */
    /* Validations              */
    /* ======================== */
    <M-1 run SetStatutoryCurrencyValidate
       (input  icDomainCode (icDomainCode), 
        input  icNewSCCode (icNewSCCode), 
        input  ilDomainIsStatutoryCurrency (ilDomainIsStatutoryCurrency), 
        input  ilPerformUpdatesAnyway (ilPerformUpdatesAnyway), 
        output ocErrorMessage (ocErrorMessage), 
        output viFcReturnSuper (oiReturnStatus)) in TSetStatutoryCurrency>
    if viFcReturnSuper <> 0
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then leave GLOBALBLOCK.
    assign olTransactionsDone = true.
    /* === Logging === */
    output to value(vcLogFile) append.
    put unformatted 
        STRING(TIME,"HH:MM:SS") 
        " Validations were succesfully executed." skip " " skip.
    output close.
    
    /* ======================================================================= */
    /* When domain is without statutory currency                               */
    /* --> Assign all SC-amount in transaction and history equal to BC amounts */
    /* ======================================================================= */
    if ilDomainIsStatutoryCurrency = false
    then do:
        <M-3 run SetStatutoryCurrencyNoStatCurr
           (input  icDomainCode (icDomainCode), 
            output ocErrorMessage (ocErrorMessage), 
            output oiPostingLineCounter (oiPostingLineCounter), 
            output viFcReturnSuper (oiReturnStatus)) in TSetStatutoryCurrency>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave GLOBALBLOCK.
        /* === Logging === */
        output to value(vcLogFile) append.
        put unformatted 
            STRING(TIME,"HH:MM:SS")
            " All SC-amounts are updated sucessfully with their BC-equivalents." skip " " skip.
        output close.
    end. /* if ilDomainIsStatutoryCurrency = false */
    
    /* ======================================================================= */
    /* Actions when domain is with statutory currency                          */
    /* --> Assign all SC-amount in transaction and history with TC * Rate      */
    /* ======================================================================= */
    else do:
        
        /* =============================================================================================== */
        /* Start Persistence to get the ObjectNumber azs we need to create new records and assign their ID */
        /* =============================================================================================== */
        run StartPersistenceInPool (input {&TARGETPROCEDURE},
                                    output vhPersistence,
                                    output viFcReturnSuper).
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave GLOBALBLOCK.
        
        /* ======================================================================= */
        /* Tackle all non-History data                                             */
        /* ======================================================================= */

        /* === Logging === */
        output to value(vcLogFile) append.
        put unformatted skip 
            STRING(TIME,"HH:MM:SS")
            " TRANS START." skip " " skip.
        output close.

        <M-4 run SetStatutoryCurrencyBasedOnTC
           (input  icDomainCode (icDomainCode), 
            output ocErrorMessage (ocErrorMessage), 
            output oiPostingLineCounter (oiPostingLineCounter), 
            output viFcReturnSuper (oiReturnStatus)) in TSetStatutoryCurrency>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave GLOBALBLOCK.

        /* === Logging === */
        output to value(vcLogFile) append.
        put unformatted skip 
            STRING(TIME,"HH:MM:SS")
            " TRANS"
            " All transactional SC-amounts have now been recalculated." skip " " skip.
        put unformatted skip 
            STRING(TIME,"HH:MM:SS") 
            " TRANS END." skip " " skip.
        put unformatted skip 
            STRING(TIME,"HH:MM:SS")
            " HIST START." skip " " skip.
        output close.

        /* ======================================================================= */
        /* Tackle all History data                                                 */
        /* ======================================================================= */
        <M-5 run SetStatutoryCurrencyBasedOnTCHist
           (input  icDomainCode (icDomainCode), 
            output ocErrorMessage (ocErrorMessage), 
            output viFcReturnSuper (oiReturnStatus)) in TSetStatutoryCurrency>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave GLOBALBLOCK.
        /* === Logging === */
        output to value(vcLogFile) append.
        put unformatted 
            STRING(TIME,"HH:MM:SS")
            " HIST"
            " All historical SC-amounts have now been recalculated." skip " " skip.
        put unformatted skip 
            STRING(TIME,"HH:MM:SS") 
            " HIST END." skip " " skip.
        output close.

    end. /* if ilDomainIsStatutoryCurrency = true */

    /* ============================================ */
    /* Final Part: Set Statutory Currency in Domain */
    /* ============================================ */
    <M-2 run SetStatutoryCurrencyDomain
       (input  icDomainCode (icDomainCode), 
        input  icNewSCCode (icNewSCCode), 
        input  ilDomainIsStatutoryCurrency (ilDomainIsStatutoryCurrency), 
        output ocErrorMessage (ocErrorMessage), 
        output viFcReturnSuper (oiReturnStatus)) in TSetStatutoryCurrency>
    if viFcReturnSuper <> 0
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then leave GLOBALBLOCK.
    /* === Logging === */
    output to value(vcLogFile) append.
    put unformatted 
        STRING(TIME,"HH:MM:SS")
        " The statutory-currency on the domain itself has updated." skip " " skip.
    output close.
    
end. /* GLOBALBLOCK */

/* ==================== */
/* Get Time Recording   */
/* ==================== */
assign oiDurationInSeconds = Time  + (3600 * 24 * (Today - vtStartDate))  - viStartTime
       viDays              = integer(truncate((oiDurationInSeconds / (3600 * 24)), 0))
       viHours             = integer(truncate((oiDurationInSeconds / 3600), 0))
       viMinutes           = integer(truncate((oiDurationInSeconds modulo 3600) / 60, 0))
       viSeconds           = integer(oiDurationInSeconds modulo 60)
       ocDurationInText    = substitute("&1 Days, &2 Hours, &3 Minutes, &4 Seconds", string(viDays), string(viHours), string(viMinutes), string(viSeconds)).

/* ================== */
/* Exception handling */
/* ================== */
assign oiReturnStatus = viLocalReturnStatus.
/* === Logging === */
output to value(vcLogFile) append.
put unformatted 
    STRING(TIME,"HH:MM:SS")
    " The Statutory-Currency routine has been completed now." skip.

if oiReturnStatus = 0
then put unformatted "   All updates have been compelted succesfully.":U skip.
else if oiReturnStatus < 0
     then put unformatted "   Errors occured - return-status: ":U + string(oiReturnStatus) skip.
     else put unformatted "   All updates have been compelted succesfully but warnings were raised - return-status: ":U + string(oiReturnStatus) skip.
put unformatted "   Duration: " ocDurationInText skip 
                "   Duration in seconds: ":U oiDurationInSeconds skip 
                "   Number of posting-lines accessed: " oiPostingLineCounter skip 
                "   Transactions done: ":U olTransactionsDone skip.
if ocErrorMessage <> "":U and ocErrorMessage <> ?
then put unformatted "   Message: ":U + ocErrorMessage skip .
put unformatted skip " " skip.
output close.