imp:print: support --invert [#2314]

This commit is contained in:
Simon Michael 2025-01-27 05:21:20 -10:00
parent aef59f8edd
commit 3e838e4d0f
5 changed files with 32 additions and 2 deletions

View File

@ -33,6 +33,7 @@ module Hledger.Data.Transaction
, transactionMapPostings , transactionMapPostings
, transactionMapPostingAmounts , transactionMapPostingAmounts
, transactionAmounts , transactionAmounts
, transactionNegate
, partitionAndCheckConversionPostings , partitionAndCheckConversionPostings
, transactionAddTags , transactionAddTags
, transactionAddHiddenAndMaybeVisibleTag , transactionAddHiddenAndMaybeVisibleTag
@ -478,10 +479,14 @@ transactionMapPostings f t@Transaction{tpostings=ps} = t{tpostings=map f ps}
transactionMapPostingAmounts :: (MixedAmount -> MixedAmount) -> Transaction -> Transaction transactionMapPostingAmounts :: (MixedAmount -> MixedAmount) -> Transaction -> Transaction
transactionMapPostingAmounts f = transactionMapPostings (postingTransformAmount f) transactionMapPostingAmounts f = transactionMapPostings (postingTransformAmount f)
-- | All posting amounts from this transactin, in order. -- | All posting amounts from this transaction, in order.
transactionAmounts :: Transaction -> [MixedAmount] transactionAmounts :: Transaction -> [MixedAmount]
transactionAmounts = map pamount . tpostings transactionAmounts = map pamount . tpostings
-- | Flip the sign of this transaction's posting amounts.
transactionNegate :: Transaction -> Transaction
transactionNegate = transactionMapPostingAmounts negate
-- | The file path from which this transaction was parsed. -- | The file path from which this transaction was parsed.
transactionFile :: Transaction -> FilePath transactionFile :: Transaction -> FilePath
transactionFile Transaction{tsourcepos} = sourceName $ fst tsourcepos transactionFile Transaction{tsourcepos} = sourceName $ fst tsourcepos

View File

@ -35,7 +35,9 @@ type EntriesReportItem = Transaction
-- | Select transactions for an entries report. -- | Select transactions for an entries report.
entriesReport :: ReportSpec -> Journal -> EntriesReport entriesReport :: ReportSpec -> Journal -> EntriesReport
entriesReport rspec@ReportSpec{_rsReportOpts=ropts} = entriesReport rspec@ReportSpec{_rsReportOpts=ropts} =
sortBy (comparing $ transactionDateFn ropts) . jtxns sortBy (comparing $ transactionDateFn ropts)
. map (if invert_ ropts then transactionNegate else id)
. jtxns
. journalApplyValuationFromOpts (setDefaultConversionOp NoConversionOp rspec) . journalApplyValuationFromOpts (setDefaultConversionOp NoConversionOp rspec)
. filterJournalTransactions (filterQuery (not.queryIsDepth) $ _rsQuery rspec) . filterJournalTransactions (filterQuery (not.queryIsDepth) $ _rsQuery rspec)

View File

@ -54,6 +54,7 @@ printmode = hledgerCommandMode
,flagNone ["show-costs"] (setboolopt "show-costs") ,flagNone ["show-costs"] (setboolopt "show-costs")
"show transaction prices even with conversion postings" "show transaction prices even with conversion postings"
,roundFlag ,roundFlag
,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign"
,flagNone ["new"] (setboolopt "new") ,flagNone ["new"] (setboolopt "new")
"show only newer-dated transactions added in each file since last run" "show only newer-dated transactions added in each file since last run"
,let arg = "DESC" in ,let arg = "DESC" in

View File

@ -17,6 +17,7 @@ Flags:
(can unbalance transactions) (can unbalance transactions)
all - also round cost amounts to precision all - also round cost amounts to precision
(can unbalance transactions) (can unbalance transactions)
--invert display all amounts with reversed sign
--new show only newer-dated transactions added in each --new show only newer-dated transactions added in each
file since last run file since last run
-m --match=DESC fuzzy search for one recent transaction with -m --match=DESC fuzzy search for one recent transaction with
@ -124,6 +125,9 @@ There are some situations where print's output can become unparseable:
With `-B`/`--cost`, amounts with [costs](https://hledger.org/hledger.html#costs) With `-B`/`--cost`, amounts with [costs](https://hledger.org/hledger.html#costs)
are shown converted to cost. are shown converted to cost.
With `--invert`, posting amounts are shown with their sign flipped.
It could be useful if you have accidentally recorded some transactions with the wrong signs.
With `--new`, print shows only transactions it has not seen on a previous run. With `--new`, print shows only transactions it has not seen on a previous run.
This uses the same deduplication system as the [`import`](#import) command. This uses the same deduplication system as the [`import`](#import) command.
(See import's docs for details.) (See import's docs for details.)

View File

@ -38,3 +38,21 @@ $ hledger -f- print --depth 1
A:AA 0 A A:AA 0 A
>= >=
# ** 4. print --invert flips signs.
<
2025-01-01
a 1 A @ 2 B
b
c 0 C
$ hledger -f- print --invert -x
2025-01-01
a -1 A @ 2 B
b 2 B
c 0 C
>=