From 96e0f70a38122e0ce081d6e7f84180f46566e187 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 8 Nov 2008 21:44:41 +0000 Subject: [PATCH] use the greatest precision detected for a commodity, not the first one --- Ledger/RawLedger.hs | 38 ++++++++++++++++++++++++++++---------- Tests.hs | 2 -- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Ledger/RawLedger.hs b/Ledger/RawLedger.hs index 18ddf78eb..800182006 100644 --- a/Ledger/RawLedger.hs +++ b/Ledger/RawLedger.hs @@ -107,19 +107,37 @@ matchLedgerPatterns forbalancereport pats str = then accountLeafName str else str --- | Give all amounts the display settings of the first one detected in each commodity. +-- | Give all a ledger's amounts their canonical display settings. That +-- is, in each commodity all amounts will use the display settings of the +-- first amount detected, and the greatest precision of all amounts +-- detected. setAmountDisplayPrefs :: RawLedger -> RawLedger setAmountDisplayPrefs l@(RawLedger ms ps es f) = RawLedger ms ps (map fixEntryAmounts es) f where fixEntryAmounts (Entry d s c de co ts pr) = Entry d s c de co (map fixRawTransactionAmounts ts) pr fixRawTransactionAmounts (RawTransaction ac a c t) = RawTransaction ac (fixMixedAmount a) c t fixMixedAmount (Mixed as) = Mixed $ map fixAmount as - fixAmount (Amount c q) = Amount (firstoccurrenceof c) q - allcommodities = map commodity $ concat $ map (amounts . amount) $ rawLedgerTransactions l - firstcommodities = nubBy samesymbol $ allcommodities - samesymbol (Commodity {symbol=s1}) (Commodity {symbol=s2}) = s1==s2 - firstoccurrenceof c@(Commodity {symbol=s}) = - fromMaybe - (error $ "failed to find commodity "++s) -- shouldn't happen - (find (\(Commodity {symbol=sym}) -> sym==s) firstcommodities) - -- XXX actually ledger uses the greatest precision found + fixAmount (Amount c q) = Amount (canonicalcommodity c) q + canonicalcommodity c@(Commodity {symbol=s}) = + (firstoccurrenceof c){precision=maximum $ map precision $ commoditieswithsymbol s} + firstoccurrenceof Commodity{symbol=s} = head $ commoditieswithsymbol s + -- Get ledger's amounts' commodities with a given symbol, in the order parsed. + -- Call with a good symbol or it will fail. + commoditieswithsymbol :: String -> [Commodity] + commoditieswithsymbol s = fromMaybe (error $ "no such commodity "++s) (Map.lookup s commoditiesmap) + where + commoditiesmap :: Map.Map String [Commodity] + commoditiesmap = Map.fromList [(symbol $ head cs,cs) | + cs <- groupBy samesymbol $ rawLedgerCommodities l] + samesymbol :: Commodity -> Commodity -> Bool + samesymbol (Commodity{symbol=s1}) (Commodity{symbol=s2}) = s1==s2 + +rawLedgerAmounts :: RawLedger -> [MixedAmount] +rawLedgerAmounts = map amount . rawLedgerTransactions + +rawLedgerCommodities :: RawLedger -> [Commodity] +rawLedgerCommodities = map commodity . concatMap amounts . rawLedgerAmounts + +rawLedgerPrecisions :: RawLedger -> [Int] +rawLedgerPrecisions = map precision . rawLedgerCommodities + diff --git a/Tests.hs b/Tests.hs index c33c2454d..219025a65 100644 --- a/Tests.hs +++ b/Tests.hs @@ -113,8 +113,6 @@ unittests = TestList [ ] -rawLedgerPrecisions = map precision . rawLedgerCommodities -rawLedgerCommodities rl = concatMap (\(Mixed as) -> map commodity as) $ map amount $ rawLedgerTransactions rl rawLedgerWithAmounts as = RawLedger []