Skip to content

Commit

Permalink
payees: add --used/--declared flags, like accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Jan 18, 2021
1 parent bf328e4 commit 540c659
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
19 changes: 15 additions & 4 deletions hledger/Hledger/Cli/Commands/Payees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Hledger.Cli.Commands.Payees (

import Data.List.Extra (nubSort)
import qualified Data.Text.IO as T
import System.Console.CmdArgs.Explicit as C

import Hledger
import Hledger.Cli.CliOptions
Expand All @@ -25,14 +26,24 @@ import Hledger.Cli.CliOptions
-- | Command line options for this command.
payeesmode = hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Payees.txt")
[]
[flagNone ["declared"] (setboolopt "declared") "show payees declared with payee directives"
,flagNone ["used"] (setboolopt "used") "show payees referenced by transactions"
]
[generalflagsgroup1]
hiddenflags
([], Just $ argsFlag "[QUERY]")

-- | The payees command.
payees :: CliOpts -> Journal -> IO ()
payees CliOpts{reportspec_=rspec} j = do
let ts = entriesReport rspec j
payees = nubSort $ map transactionPayee ts
payees CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{rsQuery=query}} j = do
let
declared = boolopt "declared" rawopts
used = boolopt "used" rawopts
-- XXX matchesPayee is currently an alias for matchesDescription, not sure if it matters
matcheddeclaredpayees = filter (matchesPayeeWIP query) $ journalPayeesDeclared j
matchedusedpayees = map transactionPayee $ filter (matchesTransaction query) $ jtxns j
payees = nubSort $
if | declared && not used -> matcheddeclaredpayees
| not declared && used -> matchedusedpayees
| otherwise -> matcheddeclaredpayees ++ matchedusedpayees
mapM_ T.putStrLn payees
13 changes: 9 additions & 4 deletions hledger/Hledger/Cli/Commands/Payees.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ List the unique payee/payer names that appear in transactions.

_FLAGS

This command lists the unique payee/payer names that appear in transactions,
in alphabetic order.
You can add a query to select a subset of transactions.
The payee/payer is the part of the transaction description before a | character
This command lists unique payee/payer names which have been
declared with payee directives (--declared),
used in transaction descriptions (--used),
or both (the default).

The payee/payer is the part of the transaction description before a | character
(or if there is no |, the whole description).

You can add query arguments to select a subset of transactions. This implies --used.


Example:
```shell
$ hledger payees
Expand Down
26 changes: 20 additions & 6 deletions hledger/test/payees.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@

# basic payees report
<
2018/1/1 foo ; foo:
payee qux

2018/1/1 foo
a

2018/1/2 bar | baz
a

2018/1/1 bar | baz
2018/1/3 foo
a

# declared and used payees, the default
$ hledger -f - payees
bar
foo
>=
qux

# used payees
$ hledger -f - payees --used
bar
foo

# declared payees
$ hledger -f - payees --declared
qux

# filtering transactions by tag
$ hledger -f - payees tag:foo
# payees used in transactions matched by a query
$ hledger -f - payees date:2018-01-03
foo
>=

0 comments on commit 540c659

Please sign in to comment.