small cleanups

This commit is contained in:
Simon Michael 2008-10-15 02:11:30 +00:00
parent 3dc5f54f39
commit 8d70ed87ea
2 changed files with 12 additions and 8 deletions

View File

@ -97,17 +97,19 @@ instance Num Amount where
-- commodity.)
amountop :: (Double -> Double -> Double) -> Amount -> Amount -> Amount
amountop op a@(Amount ac aq) b@(Amount bc bq) =
Amount bc ((quantity $ toCommodity bc a) `op` bq)
Amount bc ((quantity $ convertAmountTo bc a) `op` bq)
-- | Convert an amount to the specified commodity using the appropriate
-- exchange rate.
convertAmountTo :: Commodity -> Amount -> Amount
convertAmountTo c2 (Amount c1 q) = Amount c2 (q * conversionRate c1 c2)
-- | Sum a list of amounts. This is still needed because a final zero
-- amount will discard the sum's commodity.
sumAmounts :: [Amount] -> Amount
sumAmounts = sum . filter (not . isZeroAmount)
toCommodity :: Commodity -> Amount -> Amount
toCommodity newc (Amount oldc q) =
Amount newc (q * (conversionRate oldc newc))
nullamt = Amount (comm "") 0
-- temporary value for partial entries
autoamt = Amount (Commodity {symbol="AUTO",side=L,spaced=False,comma=False,precision=0,rate=1}) 0

View File

@ -1,8 +1,9 @@
{-|
A 'Commodity' is a symbol and a conversion rate relative to the
dollar. Commodity symbols are parsed from the ledger file, rates are
currently hard-coded.
A 'Commodity' is a symbol representing a currency or some other kind of
thing we are tracking, and some settings that tell how to display amounts
of the commodity. For the moment, commodities also include a hard-coded
conversion rate relative to the dollar.
-}
module Ledger.Commodity
@ -36,6 +37,7 @@ defaultcommoditiesmap = Map.fromList [(symbol c :: String, c :: Commodity) | c <
comm :: String -> Commodity
comm symbol = Map.findWithDefault (error "commodity lookup failed") symbol defaultcommoditiesmap
-- | Find the conversion rate between two commodities.
conversionRate :: Commodity -> Commodity -> Double
conversionRate oldc newc = (rate newc) / (rate oldc)