and allow parsing of mixed-commodity entries, as long as we don't have to balance them

This commit is contained in:
Simon Michael 2008-10-17 03:32:00 +00:00
parent 17ceb9039b
commit 0cfbced165
3 changed files with 12 additions and 9 deletions

View File

@ -21,11 +21,10 @@ showDate d = printf "%-10s" d
showDescription s = printf "%-20s" (elideRight 20 s) showDescription s = printf "%-20s" (elideRight 20 s)
isEntryBalanced :: Entry -> Bool isEntryBalanced :: Entry -> Bool
isEntryBalanced (Entry {etransactions=ts}) = isZeroAmount sum && numcommodities==1 isEntryBalanced (Entry {etransactions=ts}) = isZeroMixedAmount sum
where where
realts = filter isReal ts
sum = sumRawTransactions realts 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, -- | Fill in a missing balance in this entry, if there is one,
-- or raise an error if there is more than one. -- or raise an error if there is more than one.

View File

@ -40,7 +40,7 @@ cacheLedger l =
(Map.fromList [(account $ head g, g) | g <- groupedts]) (Map.fromList [(account $ head g, g) | g <- groupedts])
(Map.fromList [(a,[]) | a <- anames]) (Map.fromList [(a,[]) | a <- anames])
txnsof = (txnmap !) txnsof = (txnmap !)
subacctsof a = filter (isAccountNamePrefixOf a) anames subacctsof a = filter (a `isAccountNamePrefixOf`) anames
subtxnsof a = concat [txnsof a | a <- [a] ++ subacctsof a] subtxnsof a = concat [txnsof a | a <- [a] ++ subacctsof a]
balmap = Map.union balmap = Map.union
(Map.fromList [(a, (sumTransactions $ subtxnsof a)) | a <- anames]) (Map.fromList [(a, (sumTransactions $ subtxnsof a)) | a <- anames])

View File

@ -26,16 +26,20 @@ showRawTransaction t = (showaccountname $ taccount t) ++ " " ++ (showamount $ ta
-- otherwise return Nothing. -- otherwise return Nothing.
autofillTransactions :: [RawTransaction] -> Maybe [RawTransaction] autofillTransactions :: [RawTransaction] -> Maybe [RawTransaction]
autofillTransactions ts = autofillTransactions ts =
case (length missingamounts) of case (length withmissingamounts) of
0 -> Just ts 0 -> Just ts
1 -> Just $ map balance ts 1 -> Just $ map balance ts
otherwise -> Nothing otherwise -> Nothing
where where
(reals, _) = partition isReal ts (reals, _) = partition isReal ts
(realamounts, missingamounts) = partition hasAmount reals (withrealamounts, withmissingamounts) = partition hasAmount reals
balance t = if (isReal t) && (not $ hasAmount t) balance t = if (isReal t) && (not $ hasAmount t)
then t{tamount = -(sumRawTransactions realamounts)} then t{tamount = -otherssimpletotal}
else t 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 :: RawTransaction -> Bool
isReal t = rttype t == RegularTransaction isReal t = rttype t == RegularTransaction
@ -43,5 +47,5 @@ isReal t = rttype t == RegularTransaction
hasAmount :: RawTransaction -> Bool hasAmount :: RawTransaction -> Bool
hasAmount = ("AUTO" /=) . symbol . commodity . tamount hasAmount = ("AUTO" /=) . symbol . commodity . tamount
sumRawTransactions :: [RawTransaction] -> Amount sumRawTransactions :: [RawTransaction] -> MixedAmount
sumRawTransactions = sumAmounts . map tamount sumRawTransactions = normaliseMixedAmount . map tamount