From 0cfbced1650d080148d3358f4ab93357b0803935 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 17 Oct 2008 03:32:00 +0000 Subject: [PATCH] and allow parsing of mixed-commodity entries, as long as we don't have to balance them --- Ledger/Entry.hs | 5 ++--- Ledger/Ledger.hs | 2 +- Ledger/RawTransaction.hs | 14 +++++++++----- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Ledger/Entry.hs b/Ledger/Entry.hs index 1dbacdf4d..1306db343 100644 --- a/Ledger/Entry.hs +++ b/Ledger/Entry.hs @@ -21,11 +21,10 @@ showDate d = printf "%-10s" d showDescription s = printf "%-20s" (elideRight 20 s) isEntryBalanced :: Entry -> Bool -isEntryBalanced (Entry {etransactions=ts}) = isZeroAmount sum && numcommodities==1 +isEntryBalanced (Entry {etransactions=ts}) = isZeroMixedAmount sum where - realts = filter isReal ts sum = sumRawTransactions realts - numcommodities = length $ nub $ map (symbol . commodity . tamount) realts + realts = filter isReal ts -- | Fill in a missing balance in this entry, if there is one, -- or raise an error if there is more than one. diff --git a/Ledger/Ledger.hs b/Ledger/Ledger.hs index 9324917d5..d73da71ad 100644 --- a/Ledger/Ledger.hs +++ b/Ledger/Ledger.hs @@ -40,7 +40,7 @@ cacheLedger l = (Map.fromList [(account $ head g, g) | g <- groupedts]) (Map.fromList [(a,[]) | a <- anames]) txnsof = (txnmap !) - subacctsof a = filter (isAccountNamePrefixOf a) anames + subacctsof a = filter (a `isAccountNamePrefixOf`) anames subtxnsof a = concat [txnsof a | a <- [a] ++ subacctsof a] balmap = Map.union (Map.fromList [(a, (sumTransactions $ subtxnsof a)) | a <- anames]) diff --git a/Ledger/RawTransaction.hs b/Ledger/RawTransaction.hs index 25e75496f..7dfcf0fb6 100644 --- a/Ledger/RawTransaction.hs +++ b/Ledger/RawTransaction.hs @@ -26,16 +26,20 @@ showRawTransaction t = (showaccountname $ taccount t) ++ " " ++ (showamount $ ta -- otherwise return Nothing. autofillTransactions :: [RawTransaction] -> Maybe [RawTransaction] autofillTransactions ts = - case (length missingamounts) of + case (length withmissingamounts) of 0 -> Just ts 1 -> Just $ map balance ts otherwise -> Nothing where (reals, _) = partition isReal ts - (realamounts, missingamounts) = partition hasAmount reals + (withrealamounts, withmissingamounts) = partition hasAmount reals balance t = if (isReal t) && (not $ hasAmount t) - then t{tamount = -(sumRawTransactions realamounts)} + then t{tamount = -otherssimpletotal} else t + otherstotal = sumRawTransactions withrealamounts + otherssimpletotal + | length otherstotal == 1 = head otherstotal + | otherwise = error "sorry, can't balance a mixed-commodity entry yet" isReal :: RawTransaction -> Bool isReal t = rttype t == RegularTransaction @@ -43,5 +47,5 @@ isReal t = rttype t == RegularTransaction hasAmount :: RawTransaction -> Bool hasAmount = ("AUTO" /=) . symbol . commodity . tamount -sumRawTransactions :: [RawTransaction] -> Amount -sumRawTransactions = sumAmounts . map tamount +sumRawTransactions :: [RawTransaction] -> MixedAmount +sumRawTransactions = normaliseMixedAmount . map tamount