hledger/Ledger/Entry.hs

95 lines
3.1 KiB
Haskell
Raw Normal View History

{-|
2008-10-15 10:00:10 +04:00
An 'Entry' represents a regular entry in the ledger file. It contains two
or more 'RawTransaction's whose sum must be zero.
-}
2008-10-03 06:37:19 +04:00
module Ledger.Entry
where
import Ledger.Utils
2008-10-03 04:12:59 +04:00
import Ledger.Types
import Ledger.RawTransaction
import Ledger.Amount
2008-10-03 06:37:19 +04:00
instance Show Entry where show = showEntryDescription
2007-07-03 03:41:07 +04:00
{-
Helpers for the register report. A register entry is displayed as two
or more lines like this:
@
date description account amount balance
DDDDDDDDDD dddddddddddddddddddd aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA
aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA
... ... ...
datewidth = 10
descwidth = 20
acctwidth = 22
amtwidth = 11
balwidth = 12
@
-}
2008-10-09 13:25:37 +04:00
showEntryDescription e =
(showDate $ edate e) ++ " " ++ (showDescription $ edescription e) ++ " "
showDate d = printf "%-10s" d
showDescription s = printf "%-20s" (elideRight 20 s)
2008-10-03 06:37:19 +04:00
isEntryBalanced :: Entry -> Bool
2008-10-03 12:21:35 +04:00
isEntryBalanced = isZeroAmount . sumLedgerTransactions . etransactions
2008-10-03 06:37:19 +04:00
autofillEntry :: Entry -> Entry
autofillEntry e@(Entry _ _ _ _ _ ts _) =
2007-07-11 12:15:58 +04:00
let e' = e{etransactions=autofillTransactions ts} in
case (isEntryBalanced e') of
True -> e'
False -> (error $ "transactions don't balance in " ++ show e)
{-|
Helper for the print command which shows cleaned up ledger file
entries, something like:
@
yyyy/mm/dd[ *][ CODE] description......... [ ; comment...............]
account name 1..................... ...$amount1[ ; comment...............]
account name 2..................... ..$-amount1[ ; comment...............]
2007-07-04 13:28:07 +04:00
pcodewidth = no limit -- 10
pdescwidth = no limit -- 20
pacctwidth = 35 minimum, no maximum
pamtwidth = 11
pcommentwidth = no limit -- 22
@
-}
2008-10-03 06:37:19 +04:00
showEntry :: Entry -> String
2007-07-04 13:28:07 +04:00
showEntry e =
2008-06-28 09:07:09 +04:00
unlines $ [precedingcomment ++ description] ++ (showtxns $ etransactions e) ++ [""]
2007-07-04 13:28:07 +04:00
where
precedingcomment = epreceding_comment_lines e
description = concat [date, status, code, desc] -- , comment]
2007-07-04 13:28:07 +04:00
date = showDate $ edate e
status = if estatus e then " *" else ""
code = if (length $ ecode e) > 0 then (printf " (%s)" $ ecode e) else ""
desc = " " ++ edescription e
comment = if (length $ ecomment e) > 0 then " ; "++(ecomment e) else ""
2007-07-04 13:28:07 +04:00
showtxns (t1:t2:[]) = [showtxn t1, showtxnnoamt t2]
showtxns ts = map showtxn ts
showtxn t = showacct t ++ " " ++ (showamount $ tamount t) ++ (showcomment $ tcomment t)
showtxnnoamt t = showacct t ++ " " ++ (showcomment $ tcomment t)
2007-07-04 13:28:07 +04:00
showacct t = " " ++ (showaccountname $ taccount t)
2008-10-15 04:37:38 +04:00
showamount = printf "%12s" . showAmount
showaccountname s = printf "%-34s" s
showcomment s = if (length s) > 0 then " ; "++s else ""
2007-07-04 13:28:07 +04:00
-- modifier & periodic entries
instance Show ModifierEntry where
show e = "= " ++ (valueexpr e) ++ "\n" ++ unlines (map show (m_transactions e))
instance Show PeriodicEntry where
show e = "~ " ++ (periodexpr e) ++ "\n" ++ unlines (map show (p_transactions e))