2008-10-01 04:29:58 +04:00
|
|
|
{-|
|
2008-10-03 06:04:15 +04:00
|
|
|
An 'Amount' is some quantity of money, shares, or anything else.
|
|
|
|
|
|
|
|
A simple amount is a currency, quantity pair (where currency can be anything):
|
2007-03-13 07:11:39 +03:00
|
|
|
|
2008-10-01 13:33:05 +04:00
|
|
|
@
|
2007-03-13 07:11:39 +03:00
|
|
|
$1
|
|
|
|
£-50
|
|
|
|
EUR 3.44
|
|
|
|
GOOG 500
|
|
|
|
1.5h
|
2008-10-03 06:04:15 +04:00
|
|
|
90apples
|
2007-03-13 07:11:39 +03:00
|
|
|
0
|
2008-10-01 13:33:05 +04:00
|
|
|
@
|
2007-03-13 07:11:39 +03:00
|
|
|
|
2008-10-03 06:04:15 +04:00
|
|
|
A mixed amount (not yet implemented) is one or more simple amounts:
|
2007-03-13 07:11:39 +03:00
|
|
|
|
2008-10-01 13:33:05 +04:00
|
|
|
@
|
2007-03-13 07:11:39 +03:00
|
|
|
$50, EUR 3, AAPL 500
|
|
|
|
16h, $13.55, oranges 6
|
2008-10-01 13:33:05 +04:00
|
|
|
@
|
2007-03-13 07:11:39 +03:00
|
|
|
|
2008-10-01 13:33:05 +04:00
|
|
|
Currencies may be convertible or not (eg, currencies representing
|
2007-07-12 23:09:39 +04:00
|
|
|
non-money commodities). A mixed amount containing only convertible
|
2008-10-01 13:33:05 +04:00
|
|
|
currencies can be converted to a simple amount. Arithmetic examples:
|
2007-03-13 07:11:39 +03:00
|
|
|
|
2008-10-01 13:33:05 +04:00
|
|
|
@
|
2007-03-13 07:11:39 +03:00
|
|
|
$1 - $5 = $-4
|
|
|
|
$1 + EUR 0.76 = $2
|
|
|
|
EUR0.76 + $1 = EUR 1.52
|
|
|
|
EUR0.76 - $1 = 0
|
|
|
|
($5, 2h) + $1 = ($6, 2h)
|
|
|
|
($50, EUR 3, AAPL 500) + ($13.55, oranges 6) = $67.51, AAPL 500, oranges 6
|
|
|
|
($50, EUR 3) * $-1 = $-53.96
|
|
|
|
($50, AAPL 500) * $-1 = error
|
2008-10-01 13:33:05 +04:00
|
|
|
@
|
2007-03-13 07:11:39 +03:00
|
|
|
-}
|
|
|
|
|
2008-10-03 04:40:06 +04:00
|
|
|
module Ledger.Amount
|
2008-10-01 04:29:58 +04:00
|
|
|
where
|
2008-10-03 04:05:16 +04:00
|
|
|
import Ledger.Utils
|
2008-10-03 04:12:59 +04:00
|
|
|
import Ledger.Types
|
2008-10-03 04:22:17 +04:00
|
|
|
import Ledger.Currency
|
2008-10-01 04:29:58 +04:00
|
|
|
|
2007-03-13 07:11:39 +03:00
|
|
|
tests = runTestTT $ test [
|
|
|
|
show (dollars 1) ~?= "$1.00"
|
2007-07-04 05:38:56 +04:00
|
|
|
,show (hours 1) ~?= "1h" -- currently h1.00
|
2007-03-13 07:11:39 +03:00
|
|
|
]
|
|
|
|
|
2007-07-04 05:38:56 +04:00
|
|
|
instance Show Amount where show = showAmountRounded
|
|
|
|
|
2008-10-03 14:22:43 +04:00
|
|
|
-- | Get the string representation of an amount, rounded to its native precision.
|
|
|
|
-- Unlike ledger, we show the decimal digits even if they are all 0, and
|
|
|
|
-- we always show currency symbols on the left.
|
2007-07-04 05:38:56 +04:00
|
|
|
showAmountRounded :: Amount -> String
|
|
|
|
showAmountRounded (Amount c q p) =
|
2007-07-07 13:19:20 +04:00
|
|
|
(symbol c) ++ ({-punctuatethousands $ -}printf ("%."++show p++"f") q)
|
2007-07-04 05:38:56 +04:00
|
|
|
|
2008-10-03 14:22:43 +04:00
|
|
|
-- | Get the string representation of an amount, rounded, or showing just "0" if it's zero.
|
2007-03-13 07:11:39 +03:00
|
|
|
showAmountRoundedOrZero :: Amount -> String
|
2008-10-03 12:21:35 +04:00
|
|
|
showAmountRoundedOrZero a
|
|
|
|
| isZeroAmount a = "0"
|
|
|
|
| otherwise = showAmountRounded a
|
|
|
|
|
|
|
|
-- | is this amount zero, when displayed with its given precision ?
|
|
|
|
isZeroAmount :: Amount -> Bool
|
|
|
|
isZeroAmount a@(Amount c _ _) = nonzerodigits == ""
|
|
|
|
where
|
|
|
|
nonzerodigits = filter (flip notElem "-+,.0") quantitystr
|
|
|
|
quantitystr = withoutcurrency $ showAmountRounded a
|
|
|
|
withoutcurrency = drop (length $ symbol c)
|
2007-03-13 07:11:39 +03:00
|
|
|
|
2007-07-03 23:01:13 +04:00
|
|
|
punctuatethousands :: String -> String
|
|
|
|
punctuatethousands s =
|
|
|
|
sign ++ (punctuate int) ++ frac
|
|
|
|
where
|
|
|
|
(sign,num) = break isDigit s
|
|
|
|
(int,frac) = break (=='.') num
|
|
|
|
punctuate = reverse . concat . intersperse "," . triples . reverse
|
|
|
|
triples "" = []
|
|
|
|
triples s = [take 3 s] ++ (triples $ drop 3 s)
|
|
|
|
|
2007-03-13 07:11:39 +03:00
|
|
|
instance Num Amount where
|
2007-07-04 05:38:56 +04:00
|
|
|
abs (Amount c q p) = Amount c (abs q) p
|
|
|
|
signum (Amount c q p) = Amount c (signum q) p
|
2008-10-03 14:22:43 +04:00
|
|
|
fromInteger i = Amount (getcurrency "") (fromInteger i) amtintprecision
|
2007-07-04 05:38:56 +04:00
|
|
|
(+) = amountop (+)
|
|
|
|
(-) = amountop (-)
|
|
|
|
(*) = amountop (*)
|
|
|
|
|
2008-10-01 13:33:05 +04:00
|
|
|
-- problem: when an integer is converted to an amount it must pick a
|
2007-07-04 05:38:56 +04:00
|
|
|
-- precision, which we specify here (should be infinite ?). This can
|
|
|
|
-- affect amount arithmetic, in particular the sum of a list of amounts.
|
|
|
|
-- So, we may need to adjust the precision after summing amounts.
|
|
|
|
amtintprecision = 2
|
|
|
|
|
2008-10-01 04:29:58 +04:00
|
|
|
-- | apply op to two amounts, adopting a's currency and lowest precision
|
2007-07-04 05:38:56 +04:00
|
|
|
amountop :: (Double -> Double -> Double) -> Amount -> Amount -> Amount
|
|
|
|
amountop op (Amount ac aq ap) b@(Amount _ _ bp) =
|
|
|
|
Amount ac (aq `op` (quantity $ toCurrency ac b)) (min ap bp)
|
2007-03-13 07:11:39 +03:00
|
|
|
|
|
|
|
toCurrency :: Currency -> Amount -> Amount
|
2007-07-04 05:38:56 +04:00
|
|
|
toCurrency newc (Amount oldc q p) =
|
|
|
|
Amount newc (q * (conversionRate oldc newc)) p
|
|
|
|
|
2008-10-03 14:22:43 +04:00
|
|
|
nullamt = Amount (getcurrency "") 0 2
|