| 1 |
{-| |
| 2 |
|
| 3 |
The @payees@ command lists all unique payees (description part before a |) seen in transactions, sorted alphabetically. |
| 4 |
|
| 5 |
-} |
| 6 |
|
| 7 |
{-# LANGUAGE MultiWayIf #-} |
| 8 |
{-# LANGUAGE OverloadedStrings #-} |
| 9 |
{-# LANGUAGE ScopedTypeVariables #-} |
| 10 |
{-# LANGUAGE TemplateHaskell #-} |
| 11 |
|
| 12 |
module Hledger.Cli.Commands.Payees ( |
| 13 |
payeesmode |
| 14 |
,payees |
| 15 |
) where |
| 16 |
|
| 17 |
import Data.Text.IO qualified as T |
| 18 |
import System.Console.CmdArgs.Explicit |
| 19 |
|
| 20 |
import Hledger |
| 21 |
import Hledger.Cli.CliOptions |
| 22 |
import Data.List ((\\)) |
| 23 |
import Data.List.Extra (nubSort) |
| 24 |
|
| 25 |
|
| 26 |
-- | Command line options for this command. |
| 27 |
payeesmode = hledgerCommandMode |
| 28 |
$(embedFileRelative "Hledger/Cli/Commands/Payees.txt") |
| 29 |
[flagNone ["used"] (setboolopt "used") "list payees used" |
| 30 |
,flagNone ["declared"] (setboolopt "declared") "list payees declared" |
| 31 |
,flagNone ["undeclared"] (setboolopt "undeclared") "list payees used but not declared" |
| 32 |
,flagNone ["unused"] (setboolopt "unused") "list payees declared but not used" |
| 33 |
,flagNone ["find"] (setboolopt "find") "list the first payee matched by the first argument (a case-insensitive infix regexp)" |
| 34 |
] |
| 35 |
cligeneralflagsgroups1 |
| 36 |
hiddenflags |
| 37 |
([], Just $ argsFlag "[QUERY..]") |
| 38 |
|
| 39 |
-- | The payees command. |
| 40 |
payees :: CliOpts -> Journal -> IO () |
| 41 |
payees opts@CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=query}} j = do |
| 42 |
let |
| 43 |
-- XXX matchesPayeeWIP is currently an alias for matchesDescription, not sure if it matters |
| 44 |
matchedused = dbg5 "matchedused" $ nubSort $ map transactionPayee $ filter (matchesTransaction query) $ jtxns j |
| 45 |
matcheddeclared = dbg5 "matcheddeclared" $ nubSort $ filter (matchesPayeeWIP query) $ journalPayeesDeclared j |
| 46 |
matchedunused = dbg5 "matchedunused" $ nubSort $ matcheddeclared \\ matchedused |
| 47 |
matchedundeclared = dbg5 "matchedundeclared" $ nubSort $ matchedused \\ matcheddeclared |
| 48 |
matchedall = dbg5 "matchedall" $ nubSort $ matcheddeclared ++ matchedused |
| 49 |
found = dbg5 "found" $ findMatchedByArgument rawopts "payee" all' |
| 50 |
where all' = nubSort $ map transactionPayee (jtxns j) <> journalPayeesDeclared j |
| 51 |
mapM_ T.putStrLn $ case declarablesSelectorFromOpts opts of |
| 52 |
Nothing -> matchedall |
| 53 |
Just Used -> matchedused |
| 54 |
Just Declared -> matcheddeclared |
| 55 |
Just Undeclared -> matchedundeclared |
| 56 |
Just Unused -> matchedunused |
| 57 |
Just Find -> [found] |
| 58 |
|