From 6dc6186c0dc9dadeda1935ac2ec8c21edea42481 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 4 Jul 2007 12:05:54 +0000 Subject: [PATCH] save and print entry and transaction comments --- LedgerEntry.hs | 14 ++++++----- LedgerTransaction.hs | 2 +- NOTES | 2 -- Parse.hs | 31 +++++++++++++++++------- Tests.hs | 56 +++++++++++++++++++++++++++----------------- TimeLog.hs | 15 ++++++------ Transaction.hs | 8 +++---- Types.hs | 10 ++++---- 8 files changed, 84 insertions(+), 54 deletions(-) diff --git a/LedgerEntry.hs b/LedgerEntry.hs index f32dcdc8a..bcd55bbcf 100644 --- a/LedgerEntry.hs +++ b/LedgerEntry.hs @@ -30,7 +30,7 @@ isEntryBalanced e = (sumLedgerTransactions . etransactions) e == 0 autofillEntry :: LedgerEntry -> LedgerEntry 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)) -- the print command shows cleaned up ledger file entries, something like: @@ -49,25 +49,27 @@ showEntry :: LedgerEntry -> String showEntry e = unlines $ ["", description] ++ (showtxns $ etransactions e) where - description = concat [date, status, code, desc] + description = concat [date, status, code, desc, comment] 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) + comment = if (length $ ecomment e) > 0 then " ; "++(printf "%-20s" $ ecomment e) else "" showtxns (t1:t2:[]) = [showtxn t1, showtxnnoamt t2] showtxns ts = map showtxn ts - showtxn t = showacct t ++ " " ++ (showamount $ tamount t) - showtxnnoamt t = showacct t ++ " " + showtxn t = showacct t ++ " " ++ (showamount $ tamount t) ++ (showcomment $ tcomment t) + showtxnnoamt t = showacct t ++ " " ++ (showcomment $ tcomment t) showacct t = " " ++ (showaccountname $ taccount t) showamount = printf "%11s" . showAmountRounded showaccountname = printf "%-35s" . elideRight 35 + showcomment s = if (length s) > 0 then " ; "++(printf "%-20s" $ elideRight 20 s) else "" showEntries :: [LedgerEntry] -> String showEntries = concatMap showEntry entrySetPrecision :: Int -> LedgerEntry -> LedgerEntry -entrySetPrecision p (LedgerEntry d s c desc ts) = - LedgerEntry d s c desc $ map (ledgerTransactionSetPrecision p) ts +entrySetPrecision p (LedgerEntry d s c desc comm ts) = + LedgerEntry d s c desc comm $ map (ledgerTransactionSetPrecision p) ts -- modifier & periodic entries diff --git a/LedgerTransaction.hs b/LedgerTransaction.hs index de5e08a31..6a9fd07ec 100644 --- a/LedgerTransaction.hs +++ b/LedgerTransaction.hs @@ -33,4 +33,4 @@ sumLedgerTransactions :: [LedgerTransaction] -> Amount sumLedgerTransactions = sum . map tamount 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 diff --git a/NOTES b/NOTES index 14815adc2..1f8355539 100644 --- a/NOTES +++ b/NOTES @@ -3,8 +3,6 @@ hledger project notes * TO DO ** bugs/cleanup ** ledger features -*** print command -**** need to save & print comments *** handle mixed amounts, non-money currencies **** handle precision per currency *** handle time logs diff --git a/Parse.hs b/Parse.hs index 699b84df8..64cbc4804 100644 --- a/Parse.hs +++ b/Parse.hs @@ -152,10 +152,22 @@ ledger = do return $ LedgerFile modifier_entries periodic_entries entries 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 = char ';' >> restofline "comment" +ledgercomment = + try (do + char ';' + many spacenonewline + many (noneOf "\n") + ) + <|> return "" "comment" ledgerdirective :: Parser String ledgerdirective = char '!' >> restofline "directive" @@ -183,10 +195,12 @@ ledgerentry = do date <- ledgerdate status <- ledgerstatus code <- ledgercode - description <- anyChar `manyTill` ledgereol + description <- many (noneOf ";\n") "description" + comment <- ledgercomment + restofline transactions <- ledgertransactions ledgernondatalines - return $ autofillEntry $ LedgerEntry date status code description transactions + return $ autofillEntry $ LedgerEntry date status code description comment transactions ledgerdate :: Parser String ledgerdate = do @@ -213,9 +227,10 @@ ledgertransaction = do account <- ledgeraccount amount <- ledgeramount many spacenonewline - ledgereol - many ledgercomment - return (LedgerTransaction account amount) + comment <- ledgercomment + restofline + many ledgercommentline + return (LedgerTransaction account amount comment) -- account names may have single spaces in them, and are terminated by two or more spaces ledgeraccount :: Parser String @@ -240,7 +255,7 @@ ledgeramount = striptrailingpoint = reverse . dropWhile (=='.') . reverse ledgereol :: Parser String -ledgereol = ledgercomment <|> do {newline; return []} +ledgereol = do {newline; return []} spacenonewline :: Parser Char spacenonewline = satisfy (\c -> c `elem` " \v\f\t") diff --git a/Tests.hs b/Tests.hs index b116a8a92..b4dd03bb1 100644 --- a/Tests.hs +++ b/Tests.hs @@ -65,7 +65,7 @@ parseEqual parsed other = transaction1_str = " expenses:food:dining $10.00\n" -transaction1 = LedgerTransaction "expenses:food:dining" (dollars 10) +transaction1 = LedgerTransaction "expenses:food:dining" (dollars 10) "" entry1_str = "\ \2007/01/28 coopportunity\n\ @@ -74,9 +74,9 @@ entry1_str = "\ \\n" --" entry1 = - (LedgerEntry "2007/01/28" False "" "coopportunity" - [LedgerTransaction "expenses:food:groceries" (Amount (getcurrency "$") 47.18 2), - LedgerTransaction "assets:checking" (Amount (getcurrency "$") (-47.18) 2)]) + (LedgerEntry "2007/01/28" False "" "coopportunity" "" + [LedgerTransaction "expenses:food:groceries" (Amount (getcurrency "$") 47.18 2) "", + LedgerTransaction "assets:checking" (Amount (getcurrency "$") (-47.18) 2) ""]) entry2_str = "\ \2007/01/27 * joes diner\n\ @@ -211,62 +211,74 @@ ledger7 = LedgerFile [] [ LedgerEntry { - edate="2007/01/01", estatus=False, ecode="*", edescription="opening balance", + edate="2007/01/01", estatus=False, ecode="*", edescription="opening balance", ecomment="", etransactions=[ 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", - tamount=Amount {currency=(getcurrency "$"), quantity=(-4.82), precision=2}} + tamount=Amount {currency=(getcurrency "$"), quantity=(-4.82), precision=2}, + tcomment=""} ] } , LedgerEntry { - edate="2007/02/01", estatus=False, ecode="*", edescription="ayres suites", + edate="2007/02/01", estatus=False, ecode="*", edescription="ayres suites", ecomment="", etransactions=[ 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", - tamount=Amount {currency=(getcurrency "$"), quantity=(-179.92), precision=2}} + tamount=Amount {currency=(getcurrency "$"), quantity=(-179.92), precision=2}, + tcomment=""} ] } , 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=[ 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", - tamount=Amount {currency=(getcurrency "$"), quantity=(-200), precision=2}} + tamount=Amount {currency=(getcurrency "$"), quantity=(-200), precision=2}, + tcomment=""} ] } , LedgerEntry { - edate="2007/01/03", estatus=False, ecode="*", edescription="poquito mas", + edate="2007/01/03", estatus=False, ecode="*", edescription="poquito mas", ecomment="", etransactions=[ 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", - tamount=Amount {currency=(getcurrency "$"), quantity=(-4.82), precision=2}} + tamount=Amount {currency=(getcurrency "$"), quantity=(-4.82), precision=2}, + tcomment=""} ] } , LedgerEntry { - edate="2007/01/03", estatus=False, ecode="*", edescription="verizon", + edate="2007/01/03", estatus=False, ecode="*", edescription="verizon", ecomment="", etransactions=[ 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", - tamount=Amount {currency=(getcurrency "$"), quantity=(-95.11), precision=2}} + tamount=Amount {currency=(getcurrency "$"), quantity=(-95.11), precision=2}, + tcomment=""} ] } , LedgerEntry { - edate="2007/01/03", estatus=False, ecode="*", edescription="discover", + edate="2007/01/03", estatus=False, ecode="*", edescription="discover", ecomment="", etransactions=[ 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", - tamount=Amount {currency=(getcurrency "$"), quantity=(-80), precision=2}} + tamount=Amount {currency=(getcurrency "$"), quantity=(-80), precision=2}, + tcomment=""} ] } ] diff --git a/TimeLog.hs b/TimeLog.hs index 3ecaff3c5..9224e6393 100644 --- a/TimeLog.hs +++ b/TimeLog.hs @@ -9,7 +9,7 @@ import LedgerEntry import LedgerFile 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 show tl = printf "TimeLog with %d entries" $ length $ timelog_entries tl @@ -30,16 +30,17 @@ entriesFromTimeLogEntries [clockin,clockout] = estatus = True, ecode = "", edescription = accountname, + ecomment = "", etransactions = [ - LedgerTransaction accountname amount, - LedgerTransaction "TIME" (-amount) + LedgerTransaction accountname amount "", + LedgerTransaction "TIME" (-amount) "" ]} ] where - accountname = tcomment clockin - intime = tdatetime clockin - indate = dateFrom $ tdatetime clockin - outtime = tdatetime clockout + accountname = tlcomment clockin + intime = tldatetime clockin + indate = dateFrom $ tldatetime clockin + outtime = tldatetime clockout amount = hours 0 -- read $ outtime - intime entriesFromTimeLogEntries many = diff --git a/Transaction.hs b/Transaction.hs index 886775c39..4064dec36 100644 --- a/Transaction.hs +++ b/Transaction.hs @@ -10,7 +10,7 @@ import Currency 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 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 t b = - (showEntryDescription $ LedgerEntry (date t) False "" (description t) []) - ++ (showLedgerTransaction $ LedgerTransaction (account t) (amount t)) ++ (showBalance b) + (showEntryDescription $ LedgerEntry (date t) False "" (description t) "" []) + ++ (showLedgerTransaction $ LedgerTransaction (account t) (amount t) "") ++ (showBalance b) showTransactionAndBalance :: Transaction -> Amount -> String 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 b = printf " %12s" (showAmountRoundedOrZero b) diff --git a/Types.hs b/Types.hs index d4fd2adab..cd9ee820f 100644 --- a/Types.hs +++ b/Types.hs @@ -53,7 +53,8 @@ type AccountName = String -- a line item in a ledger entry data LedgerTransaction = LedgerTransaction { taccount :: AccountName, - tamount :: Amount + tamount :: Amount, + tcomment :: String } deriving (Eq) -- a ledger entry, with two or more balanced transactions @@ -62,6 +63,7 @@ data LedgerEntry = LedgerEntry { estatus :: Bool, ecode :: String, edescription :: String, + ecomment :: String, etransactions :: [LedgerTransaction] } deriving (Eq) @@ -79,9 +81,9 @@ data PeriodicEntry = PeriodicEntry { -- we also parse timeclock.el timelogs data TimeLogEntry = TimeLogEntry { - tcode :: Char, - tdatetime :: DateTime, - tcomment :: String + tlcode :: Char, + tldatetime :: DateTime, + tlcomment :: String } deriving (Eq,Ord) data TimeLog = TimeLog {