1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 {-# LANGUAGE TupleSections #-}
5 module Hcompta.CLI.Command.GL where
7 import Control.Applicative ((<$>))
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 Data.Foldable (foldr)
13 import Data.Functor.Compose (Compose(..))
14 import qualified Data.List
15 import qualified Data.Map.Strict as Data.Map
16 import qualified Data.Sequence
17 import qualified Data.Text as Text
18 import qualified Data.Text.Lazy as TL
19 import Prelude hiding (foldr)
20 import System.Console.GetOpt
25 import System.Environment as Env (getProgName)
26 import System.Exit (exitWith, ExitCode(..))
27 import qualified System.IO as IO
29 import Hcompta.Account (Account)
30 import Hcompta.Amount (Amount)
31 import qualified Hcompta.Amount as Amount
32 import qualified Hcompta.Amount.Write as Amount.Write
33 import qualified Hcompta.CLI.Args as Args
34 import qualified Hcompta.CLI.Context as Context
35 import qualified Hcompta.CLI.Format.Ledger as CLI.Ledger
36 import qualified Hcompta.CLI.Lang as Lang
37 import qualified Hcompta.CLI.Lib.Leijen.Table as Table
38 import qualified Hcompta.CLI.Write as Write
39 import Hcompta.Date (Date)
40 import qualified Hcompta.Date.Write as Date.Write
41 import qualified Hcompta.Filter as Filter
42 import qualified Hcompta.Filter.Read as Filter.Read
43 import qualified Hcompta.Format.Ledger as Ledger
44 import qualified Hcompta.Format.Ledger.Read as Ledger.Read
45 import qualified Hcompta.Format.Ledger.Write as Ledger.Write
46 import Hcompta.Lib.Leijen (toDoc, ToDoc(..))
47 import qualified Hcompta.Lib.Leijen as W
48 import qualified Hcompta.Lib.TreeMap as Lib.TreeMap
49 import Hcompta.GL (GL(..))
50 import qualified Hcompta.GL as GL
54 { ctx_input :: [FilePath]
55 , ctx_transaction_filter :: Filter.Test_Bool (Filter.Test_Transaction Ledger.Transaction)
56 , ctx_posting_filter :: Filter.Test_Bool (Filter.Test_Posting Ledger.Posting)
63 , ctx_transaction_filter = Filter.Any
64 , ctx_posting_filter = Filter.Any
69 bin <- Env.getProgName
72 , " "++bin++" gl [-t TRANSACTION_FILTER] [-p POSTING_FILTER] GL_FILTER"
74 , usageInfo "OPTIONS" options
77 options :: Args.Options Ctx
80 (NoArg (\_context _ctx -> do
81 usage >>= IO.hPutStr IO.stderr
82 exitWith ExitSuccess))
84 , Option "i" ["input"]
85 (ReqArg (\s _context ctx -> do
86 return $ ctx{ctx_input=s:ctx_input ctx}) "FILE")
87 "read data from given file, multiple uses merge the data as would a concatenation do"
88 , Option "p" ["posting-filter"]
89 (ReqArg (\s context ctx -> do
91 fmap (Filter.And $ ctx_posting_filter ctx) $
92 liftIO $ Filter.Read.read Filter.Read.test_posting s
94 Left ko -> Write.fatal context $ ko
96 return $ ctx{ctx_posting_filter}) "FILTER")
97 "filter at posting level, multiple uses are merged with a logical and"
98 , Option "t" ["transaction-filter"]
99 (ReqArg (\s context ctx -> do
100 ctx_transaction_filter <-
101 fmap (Filter.And $ ctx_transaction_filter ctx) $
102 liftIO $ Filter.Read.read Filter.Read.test_transaction s
104 Left ko -> Write.fatal context $ ko
105 Right ok -> return ok
106 return $ ctx{ctx_transaction_filter}) "FILTER")
107 "filter at transaction level, multiple uses are merged with a logical and"
110 run :: Context.Context -> [String] -> IO ()
111 run context args = do
112 (ctx, text_filters) <- Args.parse context usage options (nil, args)
114 CLI.Ledger.paths context $ ctx_input ctx
117 liftIO $ runExceptT $ Ledger.Read.file path
119 Left ko -> return $ Left (path, ko)
120 Right ok -> return $ Right ok
121 >>= return . Data.Either.partitionEithers
122 case read_journals of
123 (errs@(_:_), _journals) ->
124 (flip mapM_) errs $ \(_path, err) -> do
125 Write.fatal context $ err
128 foldr Filter.And Filter.Any <$> do
129 (flip mapM) text_filters $ \s ->
130 liftIO $ Filter.Read.read
134 Left ko -> Write.fatal context $ ko
135 Right ok -> return ok
136 Write.debug context $ "transaction_filter: " ++ show (ctx_transaction_filter ctx)
137 Write.debug context $ "posting_filter: " ++ show (ctx_posting_filter ctx)
138 Write.debug context $ "gl_filter: " ++ show gl_filter
141 (ctx_transaction_filter ctx)
142 (ctx_posting_filter ctx)
145 style_color <- Write.with_color context IO.stdout
146 W.displayIO IO.stdout $
147 W.renderPretty style_color 1.0 maxBound $ do
150 TL.toStrict . W.displayT .
151 W.renderCompact False .
152 toDoc (Context.lang context) in
154 [ Table.column (title Lang.Message_Account) Table.Align_Left
155 , Table.column (title Lang.Message_Date) Table.Align_Left
156 , Table.column (title Lang.Message_Debit) Table.Align_Right
157 , Table.column (title Lang.Message_Credit) Table.Align_Right
158 , Table.column (title Lang.Message_Running_debit) Table.Align_Right
159 , Table.column (title Lang.Message_Running_credit) Table.Align_Right
160 , Table.column (title Lang.Message_Running_balance) Table.Align_Right
161 , Table.column (title Lang.Message_Description) Table.Align_Left
163 write_gl gl (repeat [])
166 :: Filter.Test_Bool (Filter.Test_Transaction Ledger.Transaction)
167 -> Filter.Test_Bool (Filter.Test_Posting Ledger.Posting)
168 -> Filter.Test_Bool (Filter.Test_GL (Account, Date, Amount.Sum Amount, Amount.Sum Amount))
170 -> GL Ledger.Transaction
181 case Filter.test transaction_filter t of
185 t{ Ledger.transaction_postings =
187 (Data.Foldable.foldMap
189 Data.Map.foldrWithKey
190 (\u a -> (:) p{Ledger.posting_amounts=Data.Map.singleton u a})
192 (Ledger.posting_amounts p)
196 (\ps -> case Data.List.filter (Filter.test posting_filter) ps of
199 (Ledger.transaction_postings t)
203 jr (Compose $ Ledger.journal_transactions j)
208 Lib.TreeMap.map_Maybe_with_Path
209 (\acct expanded_lines ->
210 case Data.Map.mapMaybeWithKey
212 case Data.Foldable.foldMap
214 { GL.gl_line_transaction = _t
215 , GL.gl_line_posting = p
218 if Filter.test gl_filter
221 , Amount.sum $ snd $ Data.Map.elemAt 0 $ Ledger.posting_amounts p
222 , snd . Data.Map.elemAt 0 <$> s
224 then Data.Sequence.singleton line
225 else Data.Sequence.empty
227 m | Data.Sequence.null m -> Nothing
230 (GL.inclusive expanded_lines) of
231 m | Data.Map.null m -> Nothing
237 :: GL Ledger.Transaction
241 flip (Lib.TreeMap.foldr_with_Path
243 flip $ Data.Map.foldrWithKey
245 flip (Data.Foldable.foldr
247 { GL.gl_line_transaction = t
248 , GL.gl_line_posting = p
251 flip (Data.Map.foldrWithKey
253 let ptype = Ledger.Posting_Type_Regular
254 let descr = Ledger.transaction_description t
257 { Table.cell_content = Ledger.Write.account ptype acct
258 , Table.cell_width = Ledger.Write.account_length ptype acct
261 { Table.cell_content = Date.Write.date date
262 , Table.cell_width = Date.Write.date_length date
265 { Table.cell_content = maybe W.empty Amount.Write.amount (Amount.sumable_positive amt)
266 , Table.cell_width = maybe 0 Amount.Write.amount_length (Amount.sumable_positive amt)
269 { Table.cell_content = maybe W.empty Amount.Write.amount (Amount.sumable_negative amt)
270 , Table.cell_width = maybe 0 Amount.Write.amount_length (Amount.sumable_negative amt)
273 { Table.cell_content = maybe W.empty Amount.Write.amount (maybe Nothing (Data.Map.lookup unit) $ Amount.sum_positive s)
274 , Table.cell_width = maybe 0 Amount.Write.amount_length (maybe Nothing (Data.Map.lookup unit) $ Amount.sum_positive s)
277 { Table.cell_content = maybe W.empty Amount.Write.amount (maybe Nothing (Data.Map.lookup unit) $ Amount.sum_negative s)
278 , Table.cell_width = maybe 0 Amount.Write.amount_length (maybe Nothing (Data.Map.lookup unit) $ Amount.sum_negative s)
281 { Table.cell_content = maybe W.empty Amount.Write.amount (Data.Map.lookup unit $ Amount.sum_balance s)
282 , Table.cell_width = maybe 0 Amount.Write.amount_length (Data.Map.lookup unit $ Amount.sum_balance s)
285 { Table.cell_content = toDoc () descr
286 , Table.cell_width = Text.length descr
290 (Ledger.posting_amounts p)