From 1324fe3a41737af06774f7b52c90a704a6bdfd4a Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 22 Sep 2013 22:32:28 -0700 Subject: [PATCH] new hledger-balance-csv and hledger-register-csv examples in extra, generating CSV output --- extra/hledger-balance-csv.hs | 32 ++++++++++++++++++++ extra/hledger-register-csv.hs | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100755 extra/hledger-balance-csv.hs create mode 100755 extra/hledger-register-csv.hs diff --git a/extra/hledger-balance-csv.hs b/extra/hledger-balance-csv.hs new file mode 100755 index 000000000..116eb9fe3 --- /dev/null +++ b/extra/hledger-balance-csv.hs @@ -0,0 +1,32 @@ +#!/usr/bin/env runhaskell +{-| +hledger-balance-csv [OPTIONS] [ARGS] + +Show a balance report as CSV. +-} + +import Hledger +import Hledger.Cli +import System.Console.CmdArgs.Explicit +import Text.CSV + + +argsmode = + (defCommandMode ["balance-csv"]) { + modeHelp = "show matched postings accounts and their balances as CSV" + ,modeGroupFlags = Group { + groupNamed = [] + ,groupUnnamed = inputflags ++ reportflags ++ helpflags + ,groupHidden = [] + } + } + +main = getCliOpts argsmode >>= printBalanceCsv + +printBalanceCsv opts = withJournalDo opts $ + \CliOpts{reportopts_=ropts} j -> do + d <- getCurrentDay + let (items,_) = accountsReport ropts (queryFromOpts d ropts) j + putStrLn $ printCSV $ + ["account","balance"] : + [[a, showMixedAmountWithoutPrice b] | (a, _, _, b) <- items] diff --git a/extra/hledger-register-csv.hs b/extra/hledger-register-csv.hs new file mode 100755 index 000000000..95237dc15 --- /dev/null +++ b/extra/hledger-register-csv.hs @@ -0,0 +1,57 @@ +#!/usr/bin/env runhaskell +{-| +hledger-register-csv [OPTIONS] [ARGS] + +Show a register report as CSV. +-} + +module Main +where + +import Hledger +import Hledger.Cli +import System.Console.CmdArgs.Explicit +import Text.CSV + + +argsmode :: Mode RawOpts +argsmode = (defCommandMode ["register-csv"]) { + modeHelp = "show matched postings and running total as CSV" + ,modeGroupFlags = Group { + groupNamed = [ + ("Input",inputflags) + ,("Reporting",reportflags) + ,("Misc",helpflags) + ] + ,groupUnnamed = [] + ,groupHidden = [] + } + } + +main :: IO () +main = do + opts <- getCliOpts argsmode + withJournalDo opts $ + \CliOpts{reportopts_=ropts} j -> do + d <- getCurrentDay + putStrLn $ printCSV $ postingsReportAsCsv $ postingsReport ropts (queryFromOpts d ropts) j + +postingsReportAsCsv :: PostingsReport -> CSV +postingsReportAsCsv (_,is) = + ["date","description","account","amount","balance"] + : + map postingsReportItemAsCsvRecord is + +postingsReportItemAsCsvRecord :: PostingsReportItem -> Record +postingsReportItemAsCsvRecord (_, _, p, b) = [date,desc,acct,amt,bal] + where + date = showDate $ postingDate p + desc = maybe "" tdescription $ ptransaction p + acct = bracket $ paccount p + where + bracket = case ptype p of + BalancedVirtualPosting -> (\s -> "["++s++"]") + VirtualPosting -> (\s -> "("++s++")") + _ -> id + amt = showMixedAmountWithoutPrice $ pamount p + bal = showMixedAmountWithoutPrice b