From 1a9bfcf5be3fb8eea1ecc373b841787a5843bcc1 Mon Sep 17 00:00:00 2001 From: Henning Thielemann Date: Wed, 23 Oct 2024 11:30:15 +0200 Subject: [PATCH] cli: Commands.Aregister: implement --invert option --- hledger-lib/Hledger/Data/Posting.hs | 7 ++++++- hledger-lib/Hledger/Reports/AccountTransactionsReport.hs | 1 + hledger-lib/Hledger/Reports/PostingsReport.hs | 3 --- hledger/Hledger/Cli/Commands/Aregister.hs | 2 +- hledger/Hledger/Cli/Commands/Aregister.md | 1 + 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index 808bb006a..c68d0fba5 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -58,8 +58,9 @@ module Hledger.Data.Posting ( conversionPostingTagName, costPostingTagName, --- * arithmetic + -- * arithmetic sumPostings, + negatePostingAmount, -- * rendering showPosting, showPostingLines, @@ -387,6 +388,10 @@ accountNamesFromPostings = S.toList . S.fromList . map paccount sumPostings :: [Posting] -> MixedAmount sumPostings = foldl' (\amt p -> maPlus amt $ pamount p) nullmixedamt +-- | Negate amount in a posting. +negatePostingAmount :: Posting -> Posting +negatePostingAmount = postingTransformAmount negate + -- | Strip all prices from a Posting. postingStripCosts :: Posting -> Posting postingStripCosts = postingTransformAmount mixedAmountStripCosts diff --git a/hledger-lib/Hledger/Reports/AccountTransactionsReport.hs b/hledger-lib/Hledger/Reports/AccountTransactionsReport.hs index c3c78eb1f..4c4af9182 100644 --- a/hledger-lib/Hledger/Reports/AccountTransactionsReport.hs +++ b/hledger-lib/Hledger/Reports/AccountTransactionsReport.hs @@ -162,6 +162,7 @@ accountTransactionsReport rspec@ReportSpec{_rsReportOpts=ropts} j thisacctq = it . traceAtWith 5 (("ts4:\n"++).pshowTransactions.map snd) . sortBy (comparing (Down . fst) <> comparing (Down . tindex . snd)) . map (\t -> (transactionRegisterDate wd reportq thisacctq t, t)) + . map (if invert_ ropts then (\t -> t{tpostings = map negatePostingAmount $ tpostings t}) else id) $ jtxns acctJournal pshowTransactions :: [Transaction] -> String diff --git a/hledger-lib/Hledger/Reports/PostingsReport.hs b/hledger-lib/Hledger/Reports/PostingsReport.hs index 41059088b..220e90001 100644 --- a/hledger-lib/Hledger/Reports/PostingsReport.hs +++ b/hledger-lib/Hledger/Reports/PostingsReport.hs @@ -249,9 +249,6 @@ summarisePostingsInDateSpan spn@(DateSpan b e) wd mdepth showempty ps bal = if isclipped a then aibalance else aebalance isclipped a' = maybe False (accountNameLevel a' >=) mdepth -negatePostingAmount :: Posting -> Posting -negatePostingAmount = postingTransformAmount negate - -- tests diff --git a/hledger/Hledger/Cli/Commands/Aregister.hs b/hledger/Hledger/Cli/Commands/Aregister.hs index d2de9f012..b82257b5c 100644 --- a/hledger/Hledger/Cli/Commands/Aregister.hs +++ b/hledger/Hledger/Cli/Commands/Aregister.hs @@ -56,7 +56,7 @@ aregistermode = hledgerCommandMode -- ,flagNone ["average","A"] (setboolopt "average") -- "show running average of posting amounts instead of total (implies --empty)" -- ,flagNone ["related","r"] (setboolopt "related") "show postings' siblings instead" - -- ,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign" + ,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign" ,flagNone ["no-header"] (setboolopt "no-header") "omit header row in table output" ,flagReq ["width","w"] (\s opts -> Right $ setopt "width" s opts) "N" diff --git a/hledger/Hledger/Cli/Commands/Aregister.md b/hledger/Hledger/Cli/Commands/Aregister.md index fb224f8f8..4b3732075 100644 --- a/hledger/Hledger/Cli/Commands/Aregister.md +++ b/hledger/Hledger/Cli/Commands/Aregister.md @@ -12,6 +12,7 @@ Flags: balance. --no-elide don't show only 2 commodities per amount --cumulative show running total from report start date + --invert display all amounts with reversed sign --no-header omit header row in table output -w --width=N set output width (default: terminal width or $COLUMNS). -wN,M sets description width as well.