hledger/Entry.hs

82 lines
2.7 KiB
Haskell
Raw Normal View History

module Entry
where
import Utils
2007-07-02 18:54:36 +04:00
import Types
import Transaction
2007-07-04 13:28:07 +04:00
import Amount
2007-07-04 13:28:07 +04:00
instance Show Entry where show = showEntryDescription
2007-07-03 03:41:07 +04:00
2007-07-04 13:28:07 +04:00
-- for 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
-- ... ... ...
2007-07-04 13:28:07 +04:00
-- datewidth = 10
-- descwidth = 20
-- acctwidth = 22
-- amtwidth = 11
-- balwidth = 12
2007-07-04 13:28:07 +04:00
showEntryDescription e = (showDate $ edate e) ++ " " ++ (showDescription $ edescription e) ++ " "
showDate d = printf "%-10s" d
showDescription s = printf "%-20s" (elideRight 20 s)
isEntryBalanced :: Entry -> Bool
isEntryBalanced e = (sumTransactions . etransactions) e == 0
autofillEntry :: Entry -> Entry
autofillEntry e =
Entry (edate e) (estatus e) (ecode e) (edescription e)
(autofillTransactions (etransactions e))
2007-07-04 13:28:07 +04:00
-- the print command shows cleaned up ledger file entries, something like:
--
-- yyyy/mm/dd[ *][ CODE] description......... [ ; comment.............]
-- account name 1..................... ...$amount1[ ; comment.............]
-- account name 2..................... ..$-amount1[ ; comment.............]
--
-- codewidth = 10
-- descwidth = 20
-- acctwidth = 35
-- amtwidth = 11
-- commentwidth = 20
showEntry :: Entry -> String
showEntry e =
unlines $ ["", description] ++ (showtxns $ etransactions e)
where
description = concat [date, status, code, desc]
date = showDate $ edate e
status = if estatus e then " *" else ""
code = if (length $ ecode e) > 0 then " "++(printf "%-10s" $ ecode e) else ""
desc = " " ++ (elideRight 20 $ edescription e)
showtxns (t1:t2:[]) = [showtxn t1, showtxnnoamt t2]
showtxns ts = map showtxn ts
showtxn t = showacct t ++ " " ++ (showamount $ tamount t)
showtxnnoamt t = showacct t ++ " "
showacct t = " " ++ (showaccountname $ taccount t)
showamount = printf "%11s" . showAmountRounded
showaccountname = printf "%-35s" . elideRight 35
showEntries :: [Entry] -> String
showEntries = concatMap showEntry
entrySetPrecision :: Int -> Entry -> Entry
entrySetPrecision p (Entry d s c desc ts) =
Entry d s c desc $ map (transactionSetPrecision p) ts
-- 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))