and more code cleanups.

This commit is contained in:
Simon Michael 2008-10-18 04:15:43 +00:00
parent 861e2beb1c
commit a304ad3ca6
2 changed files with 36 additions and 51 deletions

View File

@ -15,33 +15,9 @@ import Ledger.Amount
instance Show Entry where show = showEntry
showEntryDescription e =
(showDate $ edate e) ++ " " ++ (showDescription $ edescription e) ++ " "
showDate d = printf "%-10s" d
showDescription s = printf "%-20s" (elideRight 20 s)
isEntryBalanced :: Entry -> Bool
isEntryBalanced (Entry {etransactions=ts}) = isZeroMixedAmount sum
where
sum = sumRawTransactions 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.
autofillEntry :: Entry -> Entry
autofillEntry e@(Entry {etransactions=ts}) = e{etransactions=ts'}
where ts' = fromMaybe
(error $ "too many missing amounts in this entry, could not auto-balance:\n" ++ show e)
(autofillTransactions ts)
assertBalancedEntry :: Entry -> Entry
assertBalancedEntry e
| isEntryBalanced e = e
| otherwise = error $ "transactions don't balance in:\n" ++ show e
{-|
Helper for the print command which shows cleaned up ledger file
entries, something like:
Show a ledger entry, formatted for the print command. ledger 2.x's
standard format looks like this:
@
yyyy/mm/dd[ *][ CODE] description......... [ ; comment...............]
@ -75,6 +51,28 @@ showEntry e =
showaccountname s = printf "%-34s" s
showcomment s = if (length s) > 0 then " ; "++s else ""
showDate = printf "%-10s"
-- | Raise an error if this entry is not balanced.
assertBalancedEntry :: Entry -> Entry
assertBalancedEntry e
| isEntryBalanced e = e
| otherwise = error $ "transactions don't balance in:\n" ++ show e
isEntryBalanced :: Entry -> Bool
isEntryBalanced (Entry {etransactions=ts}) = isZeroMixedAmount sum
where
sum = sumRawTransactions 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.
autofillEntry :: Entry -> Entry
autofillEntry e@(Entry {etransactions=ts}) = e{etransactions=ts'}
where ts' = fromMaybe
(error $ "too many missing amounts in this entry, could not auto-balance:\n" ++ show e)
(autofillTransactions ts)
-- modifier & periodic entries
instance Show ModifierEntry where

View File

@ -32,35 +32,22 @@ showRegisterReport opts args l = showtxns ts nulltxn nullamt
matchtxn Transaction{account=a} = matchLedgerPatterns False apats a
apats = fst $ parseAccountDescriptionArgs args
-- show transactions, one per line, keeping a running balance
-- show transactions, one per line, with a running balance
showtxns [] _ _ = ""
showtxns (t@Transaction{amount=a}:ts) tprev bal =
(if isZeroAmount a then "" else this) ++ showtxns ts t bal'
where
this = if t `issame` tprev
then showTransactionWithoutDescription t bal'
else showTransactionWithDescription t bal'
this = showtxn (t `issame` tprev) t bal'
issame t1 t2 = entryno t1 == entryno t2
bal' = bal + amount t
showTransactionWithDescription :: Transaction -> Amount -> String
showTransactionWithDescription t b =
(showEntryDescription $ Entry (date t) False "" (description t) "" [] "")
++ (showTransactionFormatted t)
++ (showBalance b)
++ "\n"
showTransactionWithoutDescription :: Transaction -> Amount -> String
showTransactionWithoutDescription t b =
(replicate 32 ' ')
++ (showTransactionFormatted t)
++ (showBalance b)
++ "\n"
showTransactionFormatted :: Transaction -> String
showTransactionFormatted (Transaction eno d desc a amt ttype) =
showRawTransaction $ RawTransaction a amt "" ttype
showBalance :: Amount -> String
showBalance b = printf " %12s" (showAmountOrZero b)
-- show one transaction line, with or without the entry details
showtxn :: Bool -> Transaction -> Amount -> String
showtxn omitdesc t b = entrydesc ++ txn ++ bal ++ "\n"
where
entrydesc = if omitdesc then replicate 32 ' ' else printf "%s %s " date desc
date = showDate $ da
desc = printf "%-20s" $ elideRight 20 de :: String
txn = showRawTransaction $ RawTransaction a amt "" tt
bal = printf " %12s" (showAmountOrZero b)
Transaction{date=da,description=de,account=a,amount=amt,ttype=tt} = t