save and print entry and transaction comments

This commit is contained in:
Simon Michael 2007-07-04 12:05:54 +00:00
parent 5763a80fda
commit 6dc6186c0d
8 changed files with 84 additions and 54 deletions

View File

@ -30,7 +30,7 @@ isEntryBalanced e = (sumLedgerTransactions . etransactions) e == 0
autofillEntry :: LedgerEntry -> LedgerEntry autofillEntry :: LedgerEntry -> LedgerEntry
autofillEntry e = autofillEntry e =
LedgerEntry (edate e) (estatus e) (ecode e) (edescription e) LedgerEntry (edate e) (estatus e) (ecode e) (edescription e) (ecomment e)
(autofillTransactions (etransactions e)) (autofillTransactions (etransactions e))
-- the print command shows cleaned up ledger file entries, something like: -- the print command shows cleaned up ledger file entries, something like:
@ -49,25 +49,27 @@ showEntry :: LedgerEntry -> String
showEntry e = showEntry e =
unlines $ ["", description] ++ (showtxns $ etransactions e) unlines $ ["", description] ++ (showtxns $ etransactions e)
where where
description = concat [date, status, code, desc] description = concat [date, status, code, desc, comment]
date = showDate $ edate e date = showDate $ edate e
status = if estatus e then " *" else "" status = if estatus e then " *" else ""
code = if (length $ ecode e) > 0 then " "++(printf "%-10s" $ ecode e) else "" code = if (length $ ecode e) > 0 then " "++(printf "%-10s" $ ecode e) else ""
desc = " " ++ (elideRight 20 $ edescription e) desc = " " ++ (elideRight 20 $ edescription e)
comment = if (length $ ecomment e) > 0 then " ; "++(printf "%-20s" $ ecomment e) else ""
showtxns (t1:t2:[]) = [showtxn t1, showtxnnoamt t2] showtxns (t1:t2:[]) = [showtxn t1, showtxnnoamt t2]
showtxns ts = map showtxn ts showtxns ts = map showtxn ts
showtxn t = showacct t ++ " " ++ (showamount $ tamount t) showtxn t = showacct t ++ " " ++ (showamount $ tamount t) ++ (showcomment $ tcomment t)
showtxnnoamt t = showacct t ++ " " showtxnnoamt t = showacct t ++ " " ++ (showcomment $ tcomment t)
showacct t = " " ++ (showaccountname $ taccount t) showacct t = " " ++ (showaccountname $ taccount t)
showamount = printf "%11s" . showAmountRounded showamount = printf "%11s" . showAmountRounded
showaccountname = printf "%-35s" . elideRight 35 showaccountname = printf "%-35s" . elideRight 35
showcomment s = if (length s) > 0 then " ; "++(printf "%-20s" $ elideRight 20 s) else ""
showEntries :: [LedgerEntry] -> String showEntries :: [LedgerEntry] -> String
showEntries = concatMap showEntry showEntries = concatMap showEntry
entrySetPrecision :: Int -> LedgerEntry -> LedgerEntry entrySetPrecision :: Int -> LedgerEntry -> LedgerEntry
entrySetPrecision p (LedgerEntry d s c desc ts) = entrySetPrecision p (LedgerEntry d s c desc comm ts) =
LedgerEntry d s c desc $ map (ledgerTransactionSetPrecision p) ts LedgerEntry d s c desc comm $ map (ledgerTransactionSetPrecision p) ts
-- modifier & periodic entries -- modifier & periodic entries

View File

@ -33,4 +33,4 @@ sumLedgerTransactions :: [LedgerTransaction] -> Amount
sumLedgerTransactions = sum . map tamount sumLedgerTransactions = sum . map tamount
ledgerTransactionSetPrecision :: Int -> LedgerTransaction -> LedgerTransaction ledgerTransactionSetPrecision :: Int -> LedgerTransaction -> LedgerTransaction
ledgerTransactionSetPrecision p (LedgerTransaction a amt) = LedgerTransaction a amt{precision=p} ledgerTransactionSetPrecision p (LedgerTransaction a amt c) = LedgerTransaction a amt{precision=p} c

2
NOTES
View File

@ -3,8 +3,6 @@ hledger project notes
* TO DO * TO DO
** bugs/cleanup ** bugs/cleanup
** ledger features ** ledger features
*** print command
**** need to save & print comments
*** handle mixed amounts, non-money currencies *** handle mixed amounts, non-money currencies
**** handle precision per currency **** handle precision per currency
*** handle time logs *** handle time logs

View File

@ -152,10 +152,22 @@ ledger = do
return $ LedgerFile modifier_entries periodic_entries entries return $ LedgerFile modifier_entries periodic_entries entries
ledgernondatalines :: Parser [String] ledgernondatalines :: Parser [String]
ledgernondatalines = many (ledgerdirective <|> ledgercomment <|> do {whiteSpace1; return []}) ledgernondatalines = many (ledgerdirective <|> ledgercommentline <|> do {whiteSpace1; return []})
ledgercommentline :: Parser String
ledgercommentline = do
char ';'
many spacenonewline
restofline <?> "comment line"
ledgercomment :: Parser String ledgercomment :: Parser String
ledgercomment = char ';' >> restofline <?> "comment" ledgercomment =
try (do
char ';'
many spacenonewline
many (noneOf "\n")
)
<|> return "" <?> "comment"
ledgerdirective :: Parser String ledgerdirective :: Parser String
ledgerdirective = char '!' >> restofline <?> "directive" ledgerdirective = char '!' >> restofline <?> "directive"
@ -183,10 +195,12 @@ ledgerentry = do
date <- ledgerdate date <- ledgerdate
status <- ledgerstatus status <- ledgerstatus
code <- ledgercode code <- ledgercode
description <- anyChar `manyTill` ledgereol description <- many (noneOf ";\n") <?> "description"
comment <- ledgercomment
restofline
transactions <- ledgertransactions transactions <- ledgertransactions
ledgernondatalines ledgernondatalines
return $ autofillEntry $ LedgerEntry date status code description transactions return $ autofillEntry $ LedgerEntry date status code description comment transactions
ledgerdate :: Parser String ledgerdate :: Parser String
ledgerdate = do ledgerdate = do
@ -213,9 +227,10 @@ ledgertransaction = do
account <- ledgeraccount account <- ledgeraccount
amount <- ledgeramount amount <- ledgeramount
many spacenonewline many spacenonewline
ledgereol comment <- ledgercomment
many ledgercomment restofline
return (LedgerTransaction account amount) many ledgercommentline
return (LedgerTransaction account amount comment)
-- account names may have single spaces in them, and are terminated by two or more spaces -- account names may have single spaces in them, and are terminated by two or more spaces
ledgeraccount :: Parser String ledgeraccount :: Parser String
@ -240,7 +255,7 @@ ledgeramount =
striptrailingpoint = reverse . dropWhile (=='.') . reverse striptrailingpoint = reverse . dropWhile (=='.') . reverse
ledgereol :: Parser String ledgereol :: Parser String
ledgereol = ledgercomment <|> do {newline; return []} ledgereol = do {newline; return []}
spacenonewline :: Parser Char spacenonewline :: Parser Char
spacenonewline = satisfy (\c -> c `elem` " \v\f\t") spacenonewline = satisfy (\c -> c `elem` " \v\f\t")

View File

@ -65,7 +65,7 @@ parseEqual parsed other =
transaction1_str = " expenses:food:dining $10.00\n" transaction1_str = " expenses:food:dining $10.00\n"
transaction1 = LedgerTransaction "expenses:food:dining" (dollars 10) transaction1 = LedgerTransaction "expenses:food:dining" (dollars 10) ""
entry1_str = "\ entry1_str = "\
\2007/01/28 coopportunity\n\ \2007/01/28 coopportunity\n\
@ -74,9 +74,9 @@ entry1_str = "\
\\n" --" \\n" --"
entry1 = entry1 =
(LedgerEntry "2007/01/28" False "" "coopportunity" (LedgerEntry "2007/01/28" False "" "coopportunity" ""
[LedgerTransaction "expenses:food:groceries" (Amount (getcurrency "$") 47.18 2), [LedgerTransaction "expenses:food:groceries" (Amount (getcurrency "$") 47.18 2) "",
LedgerTransaction "assets:checking" (Amount (getcurrency "$") (-47.18) 2)]) LedgerTransaction "assets:checking" (Amount (getcurrency "$") (-47.18) 2) ""])
entry2_str = "\ entry2_str = "\
\2007/01/27 * joes diner\n\ \2007/01/27 * joes diner\n\
@ -211,62 +211,74 @@ ledger7 = LedgerFile
[] []
[ [
LedgerEntry { LedgerEntry {
edate="2007/01/01", estatus=False, ecode="*", edescription="opening balance", edate="2007/01/01", estatus=False, ecode="*", edescription="opening balance", ecomment="",
etransactions=[ etransactions=[
LedgerTransaction {taccount="assets:cash", LedgerTransaction {taccount="assets:cash",
tamount=Amount {currency=(getcurrency "$"), quantity=4.82, precision=2}}, tamount=Amount {currency=(getcurrency "$"), quantity=4.82, precision=2},
tcomment=""},
LedgerTransaction {taccount="equity:opening balances", LedgerTransaction {taccount="equity:opening balances",
tamount=Amount {currency=(getcurrency "$"), quantity=(-4.82), precision=2}} tamount=Amount {currency=(getcurrency "$"), quantity=(-4.82), precision=2},
tcomment=""}
] ]
} }
, ,
LedgerEntry { LedgerEntry {
edate="2007/02/01", estatus=False, ecode="*", edescription="ayres suites", edate="2007/02/01", estatus=False, ecode="*", edescription="ayres suites", ecomment="",
etransactions=[ etransactions=[
LedgerTransaction {taccount="expenses:vacation", LedgerTransaction {taccount="expenses:vacation",
tamount=Amount {currency=(getcurrency "$"), quantity=179.92, precision=2}}, tamount=Amount {currency=(getcurrency "$"), quantity=179.92, precision=2},
tcomment=""},
LedgerTransaction {taccount="assets:checking", LedgerTransaction {taccount="assets:checking",
tamount=Amount {currency=(getcurrency "$"), quantity=(-179.92), precision=2}} tamount=Amount {currency=(getcurrency "$"), quantity=(-179.92), precision=2},
tcomment=""}
] ]
} }
, ,
LedgerEntry { LedgerEntry {
edate="2007/01/02", estatus=False, ecode="*", edescription="auto transfer to savings", edate="2007/01/02", estatus=False, ecode="*", edescription="auto transfer to savings", ecomment="",
etransactions=[ etransactions=[
LedgerTransaction {taccount="assets:saving", LedgerTransaction {taccount="assets:saving",
tamount=Amount {currency=(getcurrency "$"), quantity=200, precision=2}}, tamount=Amount {currency=(getcurrency "$"), quantity=200, precision=2},
tcomment=""},
LedgerTransaction {taccount="assets:checking", LedgerTransaction {taccount="assets:checking",
tamount=Amount {currency=(getcurrency "$"), quantity=(-200), precision=2}} tamount=Amount {currency=(getcurrency "$"), quantity=(-200), precision=2},
tcomment=""}
] ]
} }
, ,
LedgerEntry { LedgerEntry {
edate="2007/01/03", estatus=False, ecode="*", edescription="poquito mas", edate="2007/01/03", estatus=False, ecode="*", edescription="poquito mas", ecomment="",
etransactions=[ etransactions=[
LedgerTransaction {taccount="expenses:food:dining", LedgerTransaction {taccount="expenses:food:dining",
tamount=Amount {currency=(getcurrency "$"), quantity=4.82, precision=2}}, tamount=Amount {currency=(getcurrency "$"), quantity=4.82, precision=2},
tcomment=""},
LedgerTransaction {taccount="assets:cash", LedgerTransaction {taccount="assets:cash",
tamount=Amount {currency=(getcurrency "$"), quantity=(-4.82), precision=2}} tamount=Amount {currency=(getcurrency "$"), quantity=(-4.82), precision=2},
tcomment=""}
] ]
} }
, ,
LedgerEntry { LedgerEntry {
edate="2007/01/03", estatus=False, ecode="*", edescription="verizon", edate="2007/01/03", estatus=False, ecode="*", edescription="verizon", ecomment="",
etransactions=[ etransactions=[
LedgerTransaction {taccount="expenses:phone", LedgerTransaction {taccount="expenses:phone",
tamount=Amount {currency=(getcurrency "$"), quantity=95.11, precision=2}}, tamount=Amount {currency=(getcurrency "$"), quantity=95.11, precision=2},
tcomment=""},
LedgerTransaction {taccount="assets:checking", LedgerTransaction {taccount="assets:checking",
tamount=Amount {currency=(getcurrency "$"), quantity=(-95.11), precision=2}} tamount=Amount {currency=(getcurrency "$"), quantity=(-95.11), precision=2},
tcomment=""}
] ]
} }
, ,
LedgerEntry { LedgerEntry {
edate="2007/01/03", estatus=False, ecode="*", edescription="discover", edate="2007/01/03", estatus=False, ecode="*", edescription="discover", ecomment="",
etransactions=[ etransactions=[
LedgerTransaction {taccount="liabilities:credit cards:discover", LedgerTransaction {taccount="liabilities:credit cards:discover",
tamount=Amount {currency=(getcurrency "$"), quantity=80, precision=2}}, tamount=Amount {currency=(getcurrency "$"), quantity=80, precision=2},
tcomment=""},
LedgerTransaction {taccount="assets:checking", LedgerTransaction {taccount="assets:checking",
tamount=Amount {currency=(getcurrency "$"), quantity=(-80), precision=2}} tamount=Amount {currency=(getcurrency "$"), quantity=(-80), precision=2},
tcomment=""}
] ]
} }
] ]

View File

@ -9,7 +9,7 @@ import LedgerEntry
import LedgerFile import LedgerFile
instance Show TimeLogEntry where instance Show TimeLogEntry where
show t = printf "%s %s %s" (show $ tcode t) (tdatetime t) (tcomment t) show t = printf "%s %s %s" (show $ tlcode t) (tldatetime t) (tlcomment t)
instance Show TimeLog where instance Show TimeLog where
show tl = printf "TimeLog with %d entries" $ length $ timelog_entries tl show tl = printf "TimeLog with %d entries" $ length $ timelog_entries tl
@ -30,16 +30,17 @@ entriesFromTimeLogEntries [clockin,clockout] =
estatus = True, estatus = True,
ecode = "", ecode = "",
edescription = accountname, edescription = accountname,
ecomment = "",
etransactions = [ etransactions = [
LedgerTransaction accountname amount, LedgerTransaction accountname amount "",
LedgerTransaction "TIME" (-amount) LedgerTransaction "TIME" (-amount) ""
]} ]}
] ]
where where
accountname = tcomment clockin accountname = tlcomment clockin
intime = tdatetime clockin intime = tldatetime clockin
indate = dateFrom $ tdatetime clockin indate = dateFrom $ tldatetime clockin
outtime = tdatetime clockout outtime = tldatetime clockout
amount = hours 0 -- read $ outtime - intime amount = hours 0 -- read $ outtime - intime
entriesFromTimeLogEntries many = entriesFromTimeLogEntries many =

View File

@ -10,7 +10,7 @@ import Currency
flattenEntry :: LedgerEntry -> [Transaction] flattenEntry :: LedgerEntry -> [Transaction]
flattenEntry (LedgerEntry d _ _ desc ts) = [Transaction d desc (taccount t) (tamount t) | t <- ts] flattenEntry (LedgerEntry d _ _ desc _ ts) = [Transaction d desc (taccount t) (tamount t) | t <- ts]
transactionSetPrecision :: Int -> Transaction -> Transaction transactionSetPrecision :: Int -> Transaction -> Transaction
transactionSetPrecision p (Transaction d desc a amt) = Transaction d desc a amt{precision=p} transactionSetPrecision p (Transaction d desc a amt) = Transaction d desc a amt{precision=p}
@ -55,12 +55,12 @@ showTransactionsWithBalances ts b =
showTransactionDescriptionAndBalance :: Transaction -> Amount -> String showTransactionDescriptionAndBalance :: Transaction -> Amount -> String
showTransactionDescriptionAndBalance t b = showTransactionDescriptionAndBalance t b =
(showEntryDescription $ LedgerEntry (date t) False "" (description t) []) (showEntryDescription $ LedgerEntry (date t) False "" (description t) "" [])
++ (showLedgerTransaction $ LedgerTransaction (account t) (amount t)) ++ (showBalance b) ++ (showLedgerTransaction $ LedgerTransaction (account t) (amount t) "") ++ (showBalance b)
showTransactionAndBalance :: Transaction -> Amount -> String showTransactionAndBalance :: Transaction -> Amount -> String
showTransactionAndBalance t b = showTransactionAndBalance t b =
(replicate 32 ' ') ++ (showLedgerTransaction $ LedgerTransaction (account t) (amount t)) ++ (showBalance b) (replicate 32 ' ') ++ (showLedgerTransaction $ LedgerTransaction (account t) (amount t) "") ++ (showBalance b)
showBalance :: Amount -> String showBalance :: Amount -> String
showBalance b = printf " %12s" (showAmountRoundedOrZero b) showBalance b = printf " %12s" (showAmountRoundedOrZero b)

View File

@ -53,7 +53,8 @@ type AccountName = String
-- a line item in a ledger entry -- a line item in a ledger entry
data LedgerTransaction = LedgerTransaction { data LedgerTransaction = LedgerTransaction {
taccount :: AccountName, taccount :: AccountName,
tamount :: Amount tamount :: Amount,
tcomment :: String
} deriving (Eq) } deriving (Eq)
-- a ledger entry, with two or more balanced transactions -- a ledger entry, with two or more balanced transactions
@ -62,6 +63,7 @@ data LedgerEntry = LedgerEntry {
estatus :: Bool, estatus :: Bool,
ecode :: String, ecode :: String,
edescription :: String, edescription :: String,
ecomment :: String,
etransactions :: [LedgerTransaction] etransactions :: [LedgerTransaction]
} deriving (Eq) } deriving (Eq)
@ -79,9 +81,9 @@ data PeriodicEntry = PeriodicEntry {
-- we also parse timeclock.el timelogs -- we also parse timeclock.el timelogs
data TimeLogEntry = TimeLogEntry { data TimeLogEntry = TimeLogEntry {
tcode :: Char, tlcode :: Char,
tdatetime :: DateTime, tldatetime :: DateTime,
tcomment :: String tlcomment :: String
} deriving (Eq,Ord) } deriving (Eq,Ord)
data TimeLog = TimeLog { data TimeLog = TimeLog {