diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index 67492600ec0e..a937021f4a86 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -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 diff --git a/hledger-lib/Hledger/Reports/EntriesReport.hs b/hledger-lib/Hledger/Reports/EntriesReport.hs index 827d015486f1..de6f81cb8235 100644 --- a/hledger-lib/Hledger/Reports/EntriesReport.hs +++ b/hledger-lib/Hledger/Reports/EntriesReport.hs @@ -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) diff --git a/hledger/Hledger/Cli/Commands/Print.hs b/hledger/Hledger/Cli/Commands/Print.hs index 36966398bd43..f0cff4465b0d 100644 --- a/hledger/Hledger/Cli/Commands/Print.hs +++ b/hledger/Hledger/Cli/Commands/Print.hs @@ -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 diff --git a/hledger/Hledger/Cli/Commands/Print.md b/hledger/Hledger/Cli/Commands/Print.md index bf1869239fd5..018d9861bce7 100644 --- a/hledger/Hledger/Cli/Commands/Print.md +++ b/hledger/Hledger/Cli/Commands/Print.md @@ -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.) diff --git a/hledger/test/print/print.test b/hledger/test/print/print.test index 1b2c850d2459..0ef75c301d38 100644 --- a/hledger/test/print/print.test +++ b/hledger/test/print/print.test @@ -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 + +>= + + +