From 8d70ed87eaa548ae5a30f3556e6e0d8ed91e9524 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 15 Oct 2008 02:11:30 +0000 Subject: [PATCH] small cleanups --- Ledger/Amount.hs | 12 +++++++----- Ledger/Commodity.hs | 8 +++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Ledger/Amount.hs b/Ledger/Amount.hs index ddcf5049d..0d447199f 100644 --- a/Ledger/Amount.hs +++ b/Ledger/Amount.hs @@ -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 diff --git a/Ledger/Commodity.hs b/Ledger/Commodity.hs index 7a1f44e18..242ac1493 100644 --- a/Ledger/Commodity.hs +++ b/Ledger/Commodity.hs @@ -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)