diff --git a/Ledger/Entry.hs b/Ledger/Entry.hs index 75ce754b0..05cce0c96 100644 --- a/Ledger/Entry.hs +++ b/Ledger/Entry.hs @@ -26,8 +26,13 @@ isEntryBalanced (Entry {etransactions=ts}) = isZeroAmount sum && numcommodities= sum = sumLedgerTransactions ts numcommodities = length $ nub $ map (symbol . commodity . tamount) ts +-- | Fill in a missing balance in this entry, if there is one, +-- or raise an error if there is more than one. autofillEntry :: Entry -> Entry -autofillEntry e@(Entry {etransactions=ts}) = e{etransactions=autofillTransactions ts} +autofillEntry e@(Entry {etransactions=ts}) = e{etransactions=ts'} + where ts' = fromMaybe + (error $ "too many blank transactions in this entry:\n" ++ show e) + (autofillTransactions ts) assertBalancedEntry :: Entry -> Entry assertBalancedEntry e diff --git a/Ledger/RawTransaction.hs b/Ledger/RawTransaction.hs index f55d859f0..a2d042a5a 100644 --- a/Ledger/RawTransaction.hs +++ b/Ledger/RawTransaction.hs @@ -21,12 +21,14 @@ showRawTransaction t = (showaccountname $ taccount t) ++ " " ++ (showamount $ ta showaccountname = printf "%-22s" . elideAccountName 22 showamount = printf "%12s" . showAmountOrZero -autofillTransactions :: [RawTransaction] -> [RawTransaction] +-- | Fill in the missing balance in an entry's transactions. There can be +-- at most one missing balance, otherwise we'll return Nothing. +autofillTransactions :: [RawTransaction] -> Maybe [RawTransaction] autofillTransactions ts = case (length blanks) of - 0 -> ts - 1 -> map balance ts - otherwise -> error "too many blank transactions in this entry" + 0 -> Just ts + 1 -> Just $ map balance ts + otherwise -> Nothing where (normals, blanks) = partition isnormal ts isnormal t = (symbol $ commodity $ tamount t) /= "AUTO"