From d3fde29b365dac2a25ff01797e26528bf58a5393 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 29 Jan 2018 14:52:03 -0800 Subject: [PATCH] bal: --invert flag to flip all signs --- hledger-lib/Hledger/Reports/BalanceReport.hs | 9 +++++++- .../Hledger/Reports/MultiBalanceReports.hs | 21 ++++++++++++++++--- hledger-lib/Hledger/Reports/ReportOptions.hs | 3 +++ hledger/Hledger/Cli/Commands/Balance.hs | 5 +++-- hledger/Hledger/Cli/CompoundBalanceCommand.hs | 13 ------------ hledger/hledger_balance.m4.md | 3 +++ 6 files changed, 35 insertions(+), 19 deletions(-) diff --git a/hledger-lib/Hledger/Reports/BalanceReport.hs b/hledger-lib/Hledger/Reports/BalanceReport.hs index 63d6b3e15..269aeb657 100644 --- a/hledger-lib/Hledger/Reports/BalanceReport.hs +++ b/hledger-lib/Hledger/Reports/BalanceReport.hs @@ -77,7 +77,9 @@ flatShowsExclusiveBalance = True -- This is like PeriodChangeReport with a single column (but more mature, -- eg this can do hierarchical display). balanceReport :: ReportOpts -> Query -> Journal -> BalanceReport -balanceReport opts q j = (items, total) +balanceReport opts q j = + (if invert_ opts then brNegate else id) $ + (items, total) where -- dbg1 = const id -- exclude from debug output dbg1 s = let p = "balanceReport" in Hledger.Utils.dbg1 (p++" "++s) -- add prefix in debug output @@ -148,6 +150,11 @@ balanceReportItem opts q a -- items = [(a,a',n, headDef 0 bs) | ((a,a',n), bs) <- mbrrows] -- total = headDef 0 mbrtotals +-- | Flip the sign of all amounts in a BalanceReport. +brNegate :: BalanceReport -> BalanceReport +brNegate (is, tot) = (map brItemNegate is, -tot) + where + brItemNegate (a, a', d, amt) = (a, a', d, -amt) tests_balanceReport = let diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs index 6444e5d88..caabe89f4 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs @@ -10,8 +10,8 @@ module Hledger.Reports.MultiBalanceReports ( MultiBalanceReportRow, multiBalanceReport, balanceReportFromMultiBalanceReport, --- mbrNegate, --- mbrNormaliseSign, + mbrNegate, + mbrNormaliseSign, -- -- * Tests tests_Hledger_Reports_MultiBalanceReport @@ -80,7 +80,9 @@ type ClippedAccountName = AccountName -- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts -- (see ReportOpts and CompoundBalanceCommand). multiBalanceReport :: ReportOpts -> Query -> Journal -> MultiBalanceReport -multiBalanceReport opts q j = MultiBalanceReport (displayspans, sorteditems, totalsrow) +multiBalanceReport opts q j = + (if invert_ opts then mbrNegate else id) $ + MultiBalanceReport (displayspans, sorteditems, totalsrow) where symq = dbg1 "symq" $ filterQuery queryIsSym $ dbg1 "requested q" q depthq = dbg1 "depthq" $ filterQuery queryIsDepth q @@ -244,6 +246,19 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, sorteditems, tot dbg1 s = let p = "multiBalanceReport" in Hledger.Utils.dbg1 (p++" "++s) -- add prefix in this function's debug output -- dbg1 = const id -- exclude this function from debug output +-- | Given a MultiBalanceReport and its normal balance sign, +-- if it is known to be normally negative, convert it to normally positive. +mbrNormaliseSign :: NormalSign -> MultiBalanceReport -> MultiBalanceReport +mbrNormaliseSign NormallyNegative = mbrNegate +mbrNormaliseSign _ = id + +-- | Flip the sign of all amounts in a MultiBalanceReport. +mbrNegate (MultiBalanceReport (colspans, rows, totalsrow)) = + MultiBalanceReport (colspans, map mbrRowNegate rows, mbrTotalsRowNegate totalsrow) + where + mbrRowNegate (acct,shortacct,indent,amts,tot,avg) = (acct,shortacct,indent,map negate amts,-tot,-avg) + mbrTotalsRowNegate (amts,tot,avg) = (map negate amts,-tot,-avg) + -- | Generates a simple non-columnar BalanceReport, but using multiBalanceReport, -- in order to support --historical. Does not support tree-mode boring parent eliding. -- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index 08167acbb..6ac8555eb 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -102,6 +102,7 @@ data ReportOpts = ReportOpts { ,value_ :: Bool ,pretty_tables_ :: Bool ,sort_amount_ :: Bool + ,invert_ :: Bool -- ^ if true, flip all amount signs in reports ,normalbalance_ :: Maybe NormalSign -- ^ This can be set when running balance reports on a set of accounts -- with the same normal balance type (eg all assets, or all incomes). @@ -145,6 +146,7 @@ defreportopts = ReportOpts def def def + def rawOptsToReportOpts :: RawOpts -> IO ReportOpts rawOptsToReportOpts rawopts = checkReportOpts <$> do @@ -173,6 +175,7 @@ rawOptsToReportOpts rawopts = checkReportOpts <$> do ,no_total_ = boolopt "no-total" rawopts' ,value_ = boolopt "value" rawopts' ,sort_amount_ = boolopt "sort-amount" rawopts' + ,invert_ = boolopt "invert" rawopts' ,pretty_tables_ = boolopt "pretty-tables" rawopts' ,color_ = color ,forecast_ = boolopt "forecast" rawopts' diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index b681d3489..8c4db5f70 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -292,6 +292,7 @@ balancemode = (defCommandMode $ ["balance"] ++ aliases) { -- also accept but don ,flagNone ["sort-amount","S"] (\opts -> setboolopt "sort-amount" opts) "sort by amount instead of account code/name (in flat mode). With multiple columns, sorts by the row total, or by row average if that is displayed." ,flagNone ["budget"] (setboolopt "budget") "show performance compared to budget goals defined by periodic transactions" ,flagNone ["show-unbudgeted"] (setboolopt "show-unbudgeted") "with --budget, show unbudgeted accounts also" + ,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign" ] ++ outputflags ,groupHidden = [] @@ -701,11 +702,11 @@ renderBalanceReportTable ropts = | otherwise = showMixedAmountOneLineWithoutPrice renderBalanceReportTable' :: ReportOpts -> (a -> String) -> Table String String a -> String -renderBalanceReportTable' (ReportOpts { pretty_tables_ = pretty}) showCell = +renderBalanceReportTable' (ReportOpts { pretty_tables_ = pretty}) showamt = unlines . trimborder . lines - . render pretty id id showCell + . render pretty id id showamt . align where trimborder = drop 1 . init . map (drop 1 . init) diff --git a/hledger/Hledger/Cli/CompoundBalanceCommand.hs b/hledger/Hledger/Cli/CompoundBalanceCommand.hs index 61cd3db62..18a0476d3 100644 --- a/hledger/Hledger/Cli/CompoundBalanceCommand.hs +++ b/hledger/Hledger/Cli/CompoundBalanceCommand.hs @@ -209,19 +209,6 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r "html" -> (++ "\n") $ TL.unpack $ L.renderText $ compoundBalanceReportAsHtml ropts cbr _ -> compoundBalanceReportAsText ropts' cbr --- | Given a MultiBalanceReport and its normal balance sign, --- if it is known to be normally negative, convert it to normally positive. -mbrNormaliseSign :: NormalSign -> MultiBalanceReport -> MultiBalanceReport -mbrNormaliseSign NormallyNegative = mbrNegate -mbrNormaliseSign _ = id - --- | Flip the sign of all amounts in a MultiBalanceReport. -mbrNegate (MultiBalanceReport (colspans, rows, totalsrow)) = - MultiBalanceReport (colspans, map mbrRowNegate rows, mbrTotalsRowNegate totalsrow) - where - mbrRowNegate (acct,shortacct,indent,amts,tot,avg) = (acct,shortacct,indent,map negate amts,-tot,-avg) - mbrTotalsRowNegate (amts,tot,avg) = (map negate amts,-tot,-avg) - -- | Run one subreport for a compound balance command in multi-column mode. -- This returns a MultiBalanceReport. compoundBalanceSubreport :: ReportOpts -> Query -> Journal -> (Journal -> Query) -> NormalSign -> MultiBalanceReport diff --git a/hledger/hledger_balance.m4.md b/hledger/hledger_balance.m4.md index dc82e4bb9..206b5fedf 100644 --- a/hledger/hledger_balance.m4.md +++ b/hledger/hledger_balance.m4.md @@ -47,6 +47,9 @@ txt, csv, html. `--sort-amount` : sort by amount instead of account code/name (in flat mode). With multiple columns, sorts by the row total, or by row average if that is displayed. +`--invert` +: display all amounts with reversed sign + `--budget` : show performance compared to budget goals defined by [periodic transactions](journal.html#periodic-transactions)