project QadFinancials > class TConCheck > method RoundAmount

Description

Round the input amount according to the input currency's rounding method; return the string of the amount formatted by the rounding method format


Parameters


bdUnroundedAmountinput-outputdecimal
iiCurrencyIdinputinteger
icCurrencyCodeinputcharacter
ocFormatedAmountoutputcharacter
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method TConCheck.BankEntry
method TConCheck.CPaymentControlGLBalance
method TConCheck.CreditorControlGLBalance
method TConCheck.CrossCompany
method TConCheck.DebtorControlGLBalance
method TConCheck.DPaymentControlGLBalance
method TConCheck.GLTrialBalanceCheck
method TConCheck.UnmatchInvoiceCheck


program code (program1/tconcheck.p)

/* ====================== */
/* Check input parameters */
/* ====================== */
if vcSessionNumericFormat = ? or vcSessionNumericFormat = '':U
then assign vcSessionNumericFormat = session:numeric-format.

if bdUnroundedAmount = ?
then do:
    assign ocFormatedAmount = if vcSessionNumericFormat = "AMERICAN":U then "0.00":U else "0,00":U.
    return.
end.

if (iiCurrencyID   = 0    or iiCurrencyID   = ?) and
   (icCurrencyCode = '':U or icCurrencyCode = ?)
then do:
    assign ocFormatedAmount = string(bdUnroundedAmount, "->>>,>>>,>>>,>>9.99").
    if vcSessionNumericFormat <> session:numeric-format 
    then do:
        assign ocFormatedAmount = trim(replace(replace(replace(ocFormatedAmount,".":U,"#":U),",":U,".":U),"#":U,".":U)).
    end.
    return.
end.   

/* =============================================== */                                                     
/* Check if temp-table if filled.  If not, fill it */
/* =============================================== */
if not can-find(first tqCurrencyRounding)
then do:
    for each Currency no-lock, 
        each RoundingMethod of Currency where
             RoundingMethod.RoundingMethodCode <> '':U and
             RoundingMethod.RoundingMethodCode <> ? no-lock:
        create tqCurrencyRounding.
        assign tqCurrencyRounding.tiCurrencyID              = Currency.Currency_ID
               tqCurrencyRounding.tcCurrencyCode            = Currency.CurrencyCode
               tqCurrencyRounding.tdRoundingMethodUnit      = RoundingMethod.RoundingMethodUnit
               tqCurrencyRounding.tdRoundingMethodThreshold = RoundingMethod.RoundingMethodThreshold
               tqCurrencyRounding.tcFormat = "->>>,>>>,>>>,>>9":U
               vdRemainderAmount = RoundingMethod.RoundingMethodUnit.

        do while vdRemainderAmount - trunc(vdRemainderAmount, 0) <> 0:
            if vdRemainderAmount = RoundingMethod.RoundingMethodUnit then  assign tqCurrencyRounding.tcFormat = tqCurrencyRounding.tcFormat + ".":U.
            assign tqCurrencyRounding.tcFormat = tqCurrencyRounding.tcFormat + "9":U
                   vdRemainderAmount = vdRemainderAmount * 10.  
        end.
    end. /* for each Currency */
end. /* if not can-find(first tqCurrencyRounding) */

MAIN_BLOCK: 
do on error undo, throw: 
    /* ======================================================= */
    /* Leave if no rounding method was linked to this currency */
    /* ======================================================= */
    find first tqCurrencyRounding where
         tqCurrencyRounding.tiCurrencyID   = iiCurrencyID or
         tqCurrencyRounding.tcCurrencyCode = icCurrencyCode
         no-lock no-error.
    if not available tqCurrencyRounding
    then do:
        assign ocFormatedAmount = string(bdUnroundedAmount, "->>>,>>>,>>>,>>9.99").
        leave MAIN_BLOCK.
    end.
    /* ============================ */                                  
    /* Calculate the rounded amount */
    /* ============================ */
    /* Check if amount is negative. Make it positive */
    if bdUnroundedAmount < 0
    then assign vlAmountIsNegative = true
                bdUnroundedAmount  = bdUnroundedAmount * -1.
    else assign vlAmountIsNegative = false.
    
    /* Get the truncated amount */
    assign vdTruncatedAmount = trunc(bdUnroundedAmount / tqCurrencyRounding.tdRoundingMethodUnit, 0) * tqCurrencyRounding.tdRoundingMethodUnit.
    
    /* Get the remainder amount */
    assign vdRemainderAmount = bdUnroundedAmount - vdTruncatedAmount.
    
    if vdRemainderAmount >= tqCurrencyRounding.tdRoundingMethodThreshold
    then assign bdUnroundedAmount = vdTruncatedAmount + tqCurrencyRounding.tdRoundingMethodUnit.
    else assign bdUnroundedAmount = vdTruncatedAmount.
    
    /* Reset the correct sign */
    if vlAmountIsNegative = true
    then assign bdUnroundedAmount = bdUnroundedAmount * -1.

    assign ocFormatedAmount = string(bdUnroundedAmount, tqCurrencyRounding.tcFormat).


END. /* MAIN_BLOCK*/

if vcSessionNumericFormat <> session:numeric-format 
then do:
    assign ocFormatedAmount = trim(replace(replace(replace(ocFormatedAmount,".":U,"#":U),",":U,".":U),"#":U,",":U)).
end.