use the greatest precision detected for a commodity, not the first one
This commit is contained in:
parent
5c0ff1daa5
commit
96e0f70a38
@ -107,19 +107,37 @@ matchLedgerPatterns forbalancereport pats str =
|
|||||||
then accountLeafName str
|
then accountLeafName str
|
||||||
else 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 :: RawLedger -> RawLedger
|
||||||
setAmountDisplayPrefs l@(RawLedger ms ps es f) = RawLedger ms ps (map fixEntryAmounts es) f
|
setAmountDisplayPrefs l@(RawLedger ms ps es f) = RawLedger ms ps (map fixEntryAmounts es) f
|
||||||
where
|
where
|
||||||
fixEntryAmounts (Entry d s c de co ts pr) = Entry d s c de co (map fixRawTransactionAmounts ts) pr
|
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
|
fixRawTransactionAmounts (RawTransaction ac a c t) = RawTransaction ac (fixMixedAmount a) c t
|
||||||
fixMixedAmount (Mixed as) = Mixed $ map fixAmount as
|
fixMixedAmount (Mixed as) = Mixed $ map fixAmount as
|
||||||
fixAmount (Amount c q) = Amount (firstoccurrenceof c) q
|
fixAmount (Amount c q) = Amount (canonicalcommodity c) q
|
||||||
allcommodities = map commodity $ concat $ map (amounts . amount) $ rawLedgerTransactions l
|
canonicalcommodity c@(Commodity {symbol=s}) =
|
||||||
firstcommodities = nubBy samesymbol $ allcommodities
|
(firstoccurrenceof c){precision=maximum $ map precision $ commoditieswithsymbol s}
|
||||||
samesymbol (Commodity {symbol=s1}) (Commodity {symbol=s2}) = s1==s2
|
firstoccurrenceof Commodity{symbol=s} = head $ commoditieswithsymbol s
|
||||||
firstoccurrenceof c@(Commodity {symbol=s}) =
|
-- Get ledger's amounts' commodities with a given symbol, in the order parsed.
|
||||||
fromMaybe
|
-- Call with a good symbol or it will fail.
|
||||||
(error $ "failed to find commodity "++s) -- shouldn't happen
|
commoditieswithsymbol :: String -> [Commodity]
|
||||||
(find (\(Commodity {symbol=sym}) -> sym==s) firstcommodities)
|
commoditieswithsymbol s = fromMaybe (error $ "no such commodity "++s) (Map.lookup s commoditiesmap)
|
||||||
-- XXX actually ledger uses the greatest precision found
|
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
|
||||||
|
|
||||||
|
|||||||
2
Tests.hs
2
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 =
|
rawLedgerWithAmounts as =
|
||||||
RawLedger
|
RawLedger
|
||||||
[]
|
[]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user