function returns decimal
Description
Function to round amounts based on the rounding method linked to the currency.
Parameters
idUnroundedAmount | input | decimal | Unrounded amount |
iiCurrencyID | input | integer | Currency ID |
icCurrencyCode | input | character | Currency Code |
Internal usage
QadFinancials
QadFitnesse
program code (program7/business.p)
define variable oiReturnStatus as integer no-undo. /* for query-usage in function */
/* ====================== */
/* Check input parameters */
/* ====================== */
if idUnroundedAmount = 0 or idUnroundedAmount = ?
then return 0.0.
if (iiCurrencyID = 0 or iiCurrencyID = ?) and
(icCurrencyCode = '':U or icCurrencyCode = ?)
then return idUnroundedAmount.
/* =============================================== */
/* Check if temp-table if filled. If not, fill it */
/* =============================================== */
if not can-find(first tCurrencyRounding)
then do:
<Q-1 run CurrencyByCurrRoundingMethod (all) (Read) (NoCache)
(input 0, (CurrencyID)
input '':U, (CurrencyCode)
input '':U, (RoundingMethodCode)
output dataset tqCurrencyByCurrRoundingMethod) in BCurrency >
for each tqCurrencyByCurrRoundingMethod where
tqCurrencyByCurrRoundingMethod.tcRoundingMethodCode <> '':U and
tqCurrencyByCurrRoundingMethod.tcRoundingMethodCode <> ?:
create tCurrencyRounding.
assign tCurrencyRounding.tiCurrencyID = tqCurrencyByCurrRoundingMethod.tiCurrency_ID
tCurrencyRounding.tcCurrencyCode = tqCurrencyByCurrRoundingMethod.tcCurrencyCode
tCurrencyRounding.tdRoundingMethodUnit = tqCurrencyByCurrRoundingMethod.tdRoundingMethodUnit
tCurrencyRounding.tdRoundingMethodThreshold = tqCurrencyByCurrRoundingMethod.tdRoundingMethodThreshold.
end. /* for each tqCurrencyByCurrRoundingMethod: */
end. /* if not can-find(first tCurrencyRounding) */
/* ======================================================= */
/* Leave if no rounding method was linked to this currency */
/* ======================================================= */
find tCurrencyRounding where
tCurrencyRounding.tiCurrencyID = iiCurrencyID or
tCurrencyRounding.tcCurrencyCode = icCurrencyCode
no-lock no-error.
if not available tCurrencyRounding
then return idUnroundedAmount.
/* ============================ */
/* Calculate the rounded amount */
/* ============================ */
/* Check if amount is negative. Make it positive */
if idUnroundedAmount < 0
then assign vlAmountIsNegative = true
idUnroundedAmount = idUnroundedAmount * -1.
else assign vlAmountIsNegative = false.
/* Get the truncated amount */
assign vdTruncatedAmount = trunc(idUnroundedAmount / tCurrencyRounding.tdRoundingMethodUnit, 0) * tCurrencyRounding.tdRoundingMethodUnit.
/* Get the remainder amount */
assign vdRemainderAmount = idUnroundedAmount - vdTruncatedAmount.
if vdRemainderAmount >= tCurrencyRounding.tdRoundingMethodThreshold
then assign vdRoundedAmount = vdTruncatedAmount + tCurrencyRounding.tdRoundingMethodUnit.
else assign vdRoundedAmount = vdTruncatedAmount.
/* Reset the correct sign */
if vlAmountIsNegative = true
then return vdRoundedAmount * -1.
else return vdRoundedAmount.