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
, transactionMapPostingAmounts
, transactionAmounts
, transactionNegate
, partitionAndCheckConversionPostings
, transactionAddTags
, transactionAddHiddenAndMaybeVisibleTag
@ -478,10 +479,14 @@ transactionMapPostings f t@Transaction{tpostings=ps} = t{tpostings=map f ps}
transactionMapPostingAmounts :: (MixedAmount -> MixedAmount) -> Transaction -> Transaction
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 = 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.
transactionFile :: Transaction -> FilePath
transactionFile Transaction{tsourcepos} = sourceName $ fst tsourcepos

View File

@ -35,7 +35,9 @@ type EntriesReportItem = Transaction
-- | Select transactions for an entries report.
entriesReport :: ReportSpec -> Journal -> EntriesReport
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)
. filterJournalTransactions (filterQuery (not.queryIsDepth) $ _rsQuery rspec)

View File

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

View File

@ -17,6 +17,7 @@ Flags:
(can unbalance transactions)
all - also round cost amounts to precision
(can unbalance transactions)
--invert display all amounts with reversed sign
--new show only newer-dated transactions added in each
file since last run
-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)
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.
This uses the same deduplication system as the [`import`](#import) command.
(See import's docs for details.)

View File

@ -38,3 +38,21 @@ $ hledger -f- print --depth 1
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
>=