1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 {-# LANGUAGE TupleSections #-}
5 module Hcompta.CLI.Command.Journal where
7 import Control.Monad ({-foldM,-} liftM)
8 import Control.Monad.IO.Class (liftIO)
9 import Control.Monad.Trans.Except (runExceptT)
10 import qualified Data.Either
11 import qualified Data.Foldable
12 import System.Console.GetOpt
16 import Data.Monoid ((<>))
17 import Prelude hiding (foldr)
18 import System.Environment as Env (getProgName)
19 import System.Exit (exitWith, ExitCode(..))
20 import qualified System.IO as IO
22 import qualified Hcompta.CLI.Args as Args
23 import qualified Hcompta.CLI.Context as Context
24 import qualified Hcompta.CLI.Format.Ledger as CLI.Ledger
25 import qualified Hcompta.CLI.Write as Write
26 -- import qualified Hcompta.Date as Date
27 import qualified Hcompta.Filter as Filter
28 import qualified Hcompta.Filter.Read as Filter.Read
29 -- import qualified Hcompta.Filter.Reduce as Filter.Reduce
30 import qualified Hcompta.Format.Ledger as Ledger
31 import qualified Hcompta.Format.Ledger.Journal as Ledger.Journal
32 import qualified Hcompta.Format.Ledger.Read as Ledger.Read
33 import qualified Hcompta.Format.Ledger.Write as Ledger.Write
34 import qualified Hcompta.Lib.Leijen as W
35 import qualified Hcompta.Journal as Journal
39 { ctx_input :: [FilePath]
41 , ctx_reduce_date :: Bool
42 , ctx_filter_transaction :: Filter.Simplified
44 (Filter.Filter_Transaction
53 , ctx_reduce_date = True
54 , ctx_filter_transaction = mempty
59 bin <- Env.getProgName
62 , " "++bin++" journal"
63 , " [-i JOURNAL_FILE]"
64 , " [-t TRANSACTION_FILTER]"
65 , " [JOURNAL_FILE] [...]"
67 , usageInfo "OPTIONS" options
70 options :: Args.Options Ctx
73 (NoArg (\_context _ctx -> do
74 usage >>= IO.hPutStr IO.stderr
75 exitWith ExitSuccess))
77 , Option "i" ["input"]
78 (ReqArg (\s _context ctx -> do
79 return $ ctx{ctx_input=s:ctx_input ctx}) "FILE")
80 "read data from given file, multiple uses merge the data as would a concatenation do"
82 (OptArg (\arg context ctx -> do
83 ctx_align <- case arg of
84 Nothing -> return $ True
85 Just "yes" -> return $ True
86 Just "no" -> return $ False
87 Just _ -> Write.fatal context $
88 W.text "--align option expects \"yes\", or \"no\" as value"
89 return $ ctx{ctx_align})
92 {- NOTE: not used so far.
93 , Option "" ["reduce-date"]
94 (OptArg (\arg context ctx -> do
95 ctx_reduce_date <- case arg of
96 Nothing -> return $ True
97 Just "yes" -> return $ True
98 Just "no" -> return $ False
99 Just _ -> Write.fatal context $
100 W.text "--reduce-date option expects \"yes\", or \"no\" as value"
101 return $ ctx{ctx_reduce_date})
103 "use advanced date reducer to speed up filtering"
105 , Option "t" ["transaction-filter"]
106 (ReqArg (\s context ctx -> do
107 ctx_filter_transaction <-
108 liftM ((ctx_filter_transaction ctx <>) . Filter.simplify) $
109 liftIO $ Filter.Read.read Filter.Read.filter_transaction s
111 Left ko -> Write.fatal context $ ko
112 Right ok -> return ok
113 return $ ctx{ctx_filter_transaction}) "FILTER")
114 "filter at transaction level, multiple uses are merged with a logical AND"
117 run :: Context.Context -> [String] -> IO ()
118 run context args = do
119 (ctx, inputs) <- Args.parse context usage options (nil, args)
121 CLI.Ledger.paths context $ ctx_input ctx ++ inputs
124 liftIO $ runExceptT $ Ledger.Read.file
125 (Ledger.Read.context (ctx_filter_transaction ctx) Ledger.journal)
128 Left ko -> return $ Left (path, ko)
129 Right ok -> return $ Right ok
130 >>= return . Data.Either.partitionEithers
131 case read_journals of
132 (errs@(_:_), _journals) ->
133 (flip mapM_) errs $ \(_path, err) -> do
134 Write.fatal context $ err
136 Write.debug context $ "filter: transaction: " ++ show (ctx_filter_transaction ctx)
137 {- NOTE: not used so far
139 if ctx_reduce_date ctx
140 then Filter.Reduce.bool_date <$> ctx_filter_transaction ctx
142 Write.debug context $ "filter: transaction: reducer: " ++ show reducer_date
144 style_color <- Write.with_color context IO.stdout
145 let sty = Ledger.Write.Style
146 { Ledger.Write.style_align = ctx_align ctx
147 , Ledger.Write.style_color
149 let journal = ledger_journal ctx journals
150 Ledger.Write.put sty IO.stdout $ do
151 Ledger.Write.transactions journal
155 -> [Ledger.Journal (Journal.Journal Ledger.Transaction)
157 -> Journal.Journal Ledger.Transaction
158 ledger_journal _ctx journals =
160 (flip $ Ledger.Journal.fold
161 (\Ledger.Journal{Ledger.journal_transactions=j} ->