ignore virtual transactions when auto-balancing

This commit is contained in:
Simon Michael 2008-10-16 06:52:35 +00:00
parent 9acf11de4d
commit 405f71c389
2 changed files with 18 additions and 8 deletions

View File

@ -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.

View File

@ -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