{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
module Hledger.Cli.Commands.Payees (
payeesmode
,payees
) where
import qualified Data.Set as S
import qualified Data.Text.IO as T
import System.Console.CmdArgs.Explicit as C
import Hledger
import Hledger.Cli.CliOptions
payeesmode :: Mode RawOpts
payeesmode = CommandDoc
-> [Flag RawOpts]
-> [(CommandDoc, [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Payees.txt")
[forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone [CommandDoc
"declared"] (CommandDoc -> RawOpts -> RawOpts
setboolopt CommandDoc
"declared") CommandDoc
"show payees declared with payee directives"
,forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone [CommandDoc
"used"] (CommandDoc -> RawOpts -> RawOpts
setboolopt CommandDoc
"used") CommandDoc
"show payees referenced by transactions"
]
[(CommandDoc, [Flag RawOpts])
generalflagsgroup1]
[Flag RawOpts]
hiddenflags
([], forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ CommandDoc -> Arg RawOpts
argsFlag CommandDoc
"[QUERY]")
payees :: CliOpts -> Journal -> IO ()
payees :: CliOpts -> Journal -> IO ()
payees CliOpts{rawopts_ :: CliOpts -> RawOpts
rawopts_=RawOpts
rawopts, reportspec_ :: CliOpts -> ReportSpec
reportspec_=ReportSpec{_rsQuery :: ReportSpec -> Query
_rsQuery=Query
query}} Journal
j = do
let
decl :: Bool
decl = CommandDoc -> RawOpts -> Bool
boolopt CommandDoc
"declared" RawOpts
rawopts
used :: Bool
used = CommandDoc -> RawOpts -> Bool
boolopt CommandDoc
"used" RawOpts
rawopts
matcheddeclaredpayees :: Set Payee
matcheddeclaredpayees = forall a. Ord a => [a] -> Set a
S.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (Query -> Payee -> Bool
matchesPayeeWIP Query
query) forall a b. (a -> b) -> a -> b
$ Journal -> [Payee]
journalPayeesDeclared Journal
j
matchedusedpayees :: Set Payee
matchedusedpayees = forall a. Ord a => [a] -> Set a
S.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Transaction -> Payee
transactionPayee forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (Query -> Transaction -> Bool
matchesTransaction Query
query) forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
payees' :: Set Payee
payees' =
if | Bool
decl Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
used -> Set Payee
matcheddeclaredpayees
| Bool -> Bool
not Bool
decl Bool -> Bool -> Bool
&& Bool
used -> Set Payee
matchedusedpayees
| Bool
otherwise -> Set Payee
matcheddeclaredpayees forall a. Semigroup a => a -> a -> a
<> Set Payee
matchedusedpayees
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Payee -> IO ()
T.putStrLn Set Payee
payees'