module Hledger.Cli.Print (
printmode
,print'
,tests_Hledger_Cli_Print
)
where
import Data.List
import System.Console.CmdArgs.Explicit
import Test.HUnit
import Text.CSV
import Hledger
import Hledger.Cli.Options
import Hledger.Cli.Utils
printmode = (defCommandMode $ ["print"] ++ aliases) {
modeHelp = "show transaction entries" `withAliases` aliases
,modeGroupFlags = Group {
groupUnnamed = outputflags
,groupHidden = []
,groupNamed = [generalflagsgroup1]
}
}
where aliases = []
print' :: CliOpts -> Journal -> IO ()
print' opts@CliOpts{reportopts_=ropts} j = do
d <- getCurrentDay
let q = queryFromOpts d ropts
fmt = outputFormatFromOpts opts
(render, ropts') = case fmt of
"csv" -> ((++"\n") . printCSV . entriesReportAsCsv, ropts{accountlistmode_=ALFlat})
_ -> (entriesReportAsText, ropts)
writeOutput opts $ render $ entriesReport ropts' q j
entriesReportAsText :: EntriesReport -> String
entriesReportAsText items = concatMap showTransactionUnelided items
entriesReportAsCsv :: EntriesReport -> CSV
entriesReportAsCsv items =
concat $
([["nth","date","date2","status","code","description","comment","account","amount","commodity","credit","debit","status","posting-comment"]]:).snd $
mapAccumL (\n e -> (n + 1, transactionToCSV n e)) 0 items
transactionToCSV :: Integer -> Transaction -> CSV
transactionToCSV n t =
map (\p -> show n:date:date2:status:code:description:comment:p)
(concatMap postingToCSV $ tpostings t)
where
description = tdescription t
date = showDate (tdate t)
date2 = maybe "" showDate (tdate2 t)
status = show $ tstatus t
code = tcode t
comment = chomp $ strip $ tcomment t
postingToCSV :: Posting -> CSV
postingToCSV p =
map (\(a@(Amount {aquantity=q,acommodity=c})) ->
let a_ = a{acommodity=""} in
let amount = showAmount a_ in
let commodity = c in
let credit = if q < 0 then showAmount $ negate a_ else "" in
let debit = if q > 0 then showAmount a_ else "" in
account:amount:commodity:credit:debit:status:comment:[])
amounts
where
Mixed amounts = pamount p
status = show $ pstatus p
account = showAccountName Nothing (ptype p) (paccount p)
comment = chomp $ strip $ pcomment p
tests_Hledger_Cli_Print = TestList []