X-Git-Url: https://git.cyclocoop.org/?p=tool%2Fhledger.git;a=blobdiff_plain;f=hledger-print-csv.hs;fp=hledger-print-csv.hs;h=0000000000000000000000000000000000000000;hp=cac402abcd62b252b9d581fec6a713ae7bb72c32;hb=707b0a621cd3c97d5d7e22e855272b3f865a2d56;hpb=30e9b83c521f5149836e1ef0b544676370721efe diff --git a/hledger-print-csv.hs b/hledger-print-csv.hs deleted file mode 100755 index cac402a..0000000 --- a/hledger-print-csv.hs +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env runhaskell -{-| -hledger-print-csv [-f JOURNALFILE] - -Print matched journal entries as CSV -Reads the default or specified journal. -|-} - -import Hledger.Cli -import Text.CSV -import Data.Char (isSpace) -import Data.List (mapAccumL) - -argsmode :: Mode RawOpts -argsmode = (defCommandMode ["print-csv"]) - { modeHelp = "print matched journal entries as CSV" - , modeGroupFlags = Group - { groupNamed = - [ ("Input",inputflags) - , ("Reporting",reportflags) - , ("Misc",helpflags) - ] - , groupUnnamed = [] - , groupHidden = [] - } - } - -chomp :: String -> String -chomp = reverse . dropWhile isSpace . reverse . dropWhile isSpace - -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 = if pstatus p then "*" else "" - account = showAccountName Nothing (ptype p) (paccount p) - comment = chomp $ pcomment p - -postingsToCSV :: [Posting] -> CSV -postingsToCSV ps = - concatMap postingToCSV ps - -transactionToCSV :: Integer -> Transaction -> CSV -transactionToCSV n t = - map (\p -> show n:date:date2:status:code:description:comment:p) - (postingsToCSV (tpostings t)) - where - description = tdescription t - date = showDate (tdate t) - date2 = maybe "" showDate (tdate2 t) - status = if tstatus t then "*" else "" - code = tcode t - comment = chomp $ tcomment t - -main :: IO () -main = do - opts <- getCliOpts argsmode - withJournalDo opts $ - \CliOpts{reportopts_=ropts} j -> do - d <- getCurrentDay - let ropts_ = ropts{flat_=True} - let q = queryFromOpts d ropts_ - putStrLn $ printCSV $ 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 $ - entriesReport ropts_ q j