{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
module Hledger.Cli.Commands.Payees (
payeesmode
,payees
) where
import Data.Text qualified as Text
import Data.Text qualified as T
import System.Console.CmdArgs.Explicit
import Hledger
import Hledger.Cli.CliOptions
import Hledger.Cli.Utils (writeOutputWithTitle)
import Data.List ((\\))
import Data.List.Extra (nubSort)
payeesmode :: Mode RawOpts
payeesmode = CommandHelpStr
-> [Flag RawOpts]
-> [(CommandHelpStr, [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Payees.txt")
[[CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"used"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"used") CommandHelpStr
"list payees used"
,[CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"declared"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"declared") CommandHelpStr
"list payees declared"
,[CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"undeclared"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"undeclared") CommandHelpStr
"list payees used but not declared"
,[CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"unused"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"unused") CommandHelpStr
"list payees declared but not used"
,[CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"find"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"find") CommandHelpStr
"list the first payee matched by the first argument (a case-insensitive infix regexp)"
,[CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"directives"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"directives") CommandHelpStr
"show as payee directives, for use in journals"
]
[(CommandHelpStr, [Flag RawOpts])]
cligeneralflagsgroups1
[Flag RawOpts]
hiddenflags
([], Arg RawOpts -> Maybe (Arg RawOpts)
forall a. a -> Maybe a
Just (Arg RawOpts -> Maybe (Arg RawOpts))
-> Arg RawOpts -> Maybe (Arg RawOpts)
forall a b. (a -> b) -> a -> b
$ CommandHelpStr -> Arg RawOpts
argsFlag CommandHelpStr
"[QUERY..]")
payees :: CliOpts -> Journal -> IO ()
payees :: CliOpts -> Journal -> IO ()
payees opts :: CliOpts
opts@CliOpts{rawopts_ :: CliOpts -> RawOpts
rawopts_=RawOpts
rawopts, reportspec_ :: CliOpts -> ReportSpec
reportspec_=ReportSpec{_rsQuery :: ReportSpec -> Query
_rsQuery=Query
query}} Journal
j = do
let
matchedused :: [Text]
matchedused = CommandHelpStr -> [Text] -> [Text]
forall a. Show a => CommandHelpStr -> a -> a
dbg5 CommandHelpStr
"matchedused" ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. Ord a => [a] -> [a]
nubSort ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ (Transaction -> Text) -> [Transaction] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Transaction -> Text
transactionPayee ([Transaction] -> [Text]) -> [Transaction] -> [Text]
forall a b. (a -> b) -> a -> b
$ (Transaction -> Bool) -> [Transaction] -> [Transaction]
forall a. (a -> Bool) -> [a] -> [a]
filter (Query -> Transaction -> Bool
matchesTransaction Query
query) ([Transaction] -> [Transaction]) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
matcheddeclared :: [Text]
matcheddeclared = CommandHelpStr -> [Text] -> [Text]
forall a. Show a => CommandHelpStr -> a -> a
dbg5 CommandHelpStr
"matcheddeclared" ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. Ord a => [a] -> [a]
nubSort ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Query -> Text -> Bool
matchesPayee Query
query) ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ Journal -> [Text]
journalPayeesDeclared Journal
j
matchedunused :: [Text]
matchedunused = CommandHelpStr -> [Text] -> [Text]
forall a. Show a => CommandHelpStr -> a -> a
dbg5 CommandHelpStr
"matchedunused" ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. Ord a => [a] -> [a]
nubSort ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text]
matcheddeclared [Text] -> [Text] -> [Text]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Text]
allused
matchedundeclared :: [Text]
matchedundeclared = CommandHelpStr -> [Text] -> [Text]
forall a. Show a => CommandHelpStr -> a -> a
dbg5 CommandHelpStr
"matchedundeclared" ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. Ord a => [a] -> [a]
nubSort ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text]
matchedused [Text] -> [Text] -> [Text]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Text]
alldeclared
matchedall :: [Text]
matchedall = CommandHelpStr -> [Text] -> [Text]
forall a. Show a => CommandHelpStr -> a -> a
dbg5 CommandHelpStr
"matchedall" ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. Ord a => [a] -> [a]
nubSort ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text]
matcheddeclared [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text]
matchedused
found :: Text
found = CommandHelpStr -> Text -> Text
forall a. Show a => CommandHelpStr -> a -> a
dbg5 CommandHelpStr
"found" (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ RawOpts -> CommandHelpStr -> [Text] -> Text
findMatchedByArgument RawOpts
rawopts CommandHelpStr
"payee" ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. Ord a => [a] -> [a]
nubSort ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text]
allused [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text]
alldeclared
allused :: [Text]
allused = (Transaction -> Text) -> [Transaction] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Transaction -> Text
transactionPayee ([Transaction] -> [Text]) -> [Transaction] -> [Text]
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
alldeclared :: [Text]
alldeclared = Journal -> [Text]
journalPayeesDeclared Journal
j
showp :: Text -> Text
showp Text
p
| CommandHelpStr -> RawOpts -> Bool
boolopt CommandHelpStr
"directives" RawOpts
rawopts = Text
"payee " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> if (Char -> Bool) -> Text -> Bool
Text.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
';') Text
p then Text
"\"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
p Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\"" else Text
p
| Bool
otherwise = Text
p
CliOpts -> Text -> IO ()
writeOutputWithTitle CliOpts
opts (Text -> IO ()) -> Text -> IO ()
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unlines ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
showp ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ case CliOpts -> Maybe DeclarablesSelector
declarablesSelectorFromOpts CliOpts
opts of
Maybe DeclarablesSelector
Nothing -> [Text]
matchedall
Just DeclarablesSelector
Used -> [Text]
matchedused
Just DeclarablesSelector
Declared -> [Text]
matcheddeclared
Just DeclarablesSelector
Undeclared -> [Text]
matchedundeclared
Just DeclarablesSelector
Unused -> [Text]
matchedunused
Just DeclarablesSelector
FindFirst -> [Text
found]