From 405f71c38968c6c54914cf94ec25706c0264db23 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 16 Oct 2008 06:52:35 +0000 Subject: [PATCH] ignore virtual transactions when auto-balancing --- Ledger/Entry.hs | 5 +++-- Ledger/RawTransaction.hs | 21 +++++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Ledger/Entry.hs b/Ledger/Entry.hs index 05cce0c96..0532223a4 100644 --- a/Ledger/Entry.hs +++ b/Ledger/Entry.hs @@ -23,8 +23,9 @@ showDescription s = printf "%-20s" (elideRight 20 s) isEntryBalanced :: Entry -> Bool isEntryBalanced (Entry {etransactions=ts}) = isZeroAmount sum && numcommodities==1 where - sum = sumLedgerTransactions ts - numcommodities = length $ nub $ map (symbol . commodity . tamount) ts + realts = filter isReal ts + sum = sumLedgerTransactions realts + numcommodities = length $ nub $ map (symbol . commodity . tamount) realts -- | 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/RawTransaction.hs b/Ledger/RawTransaction.hs index a2d042a5a..a8a95a473 100644 --- a/Ledger/RawTransaction.hs +++ b/Ledger/RawTransaction.hs @@ -21,18 +21,27 @@ showRawTransaction t = (showaccountname $ taccount t) ++ " " ++ (showamount $ ta showaccountname = printf "%-22s" . elideAccountName 22 showamount = printf "%12s" . showAmountOrZero --- | Fill in the missing balance in an entry's transactions. There can be --- at most one missing balance, otherwise we'll return Nothing. +-- | Fill in the missing balance in an entry's transactions. Excluding +-- virtual transactions, there should be at most one missing balance, +-- otherwise return Nothing. autofillTransactions :: [RawTransaction] -> Maybe [RawTransaction] autofillTransactions ts = - case (length blanks) of + case (length missingamounts) of 0 -> Just ts 1 -> Just $ map balance ts otherwise -> Nothing where - (normals, blanks) = partition isnormal ts - isnormal t = (symbol $ commodity $ tamount t) /= "AUTO" - balance t = if isnormal t then t else t{tamount = -(sumLedgerTransactions normals)} + (reals, _) = partition isReal ts + (realamounts, missingamounts) = partition hasAmount reals + balance t = if (isReal t) && (not $ hasAmount t) + then t{tamount = -(sumLedgerTransactions realamounts)} + else t + +isReal :: RawTransaction -> Bool +isReal t = rttype t == RegularTransaction + +hasAmount :: RawTransaction -> Bool +hasAmount = ("AUTO" /=) . symbol . commodity . tamount sumLedgerTransactions :: [RawTransaction] -> Amount sumLedgerTransactions = sumAmounts . map tamount