From e21afc466a9c7db6b43c6ae27022f20ee15ebf67 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 11 May 2019 11:55:04 -0700 Subject: [PATCH] bal/bs/bse: -H or --cumulative now disables -T (#329) To reduce confusion, multiperiod balance reports using -H/--historical or --cumulative, which show end balances, no longer show a Totals column since summing end balances generally doesn't make sense. Also the underlying MultiBalanceReport now returns zero for those totals when in cumulative or historical mode. --- .../Hledger/Reports/MultiBalanceReports.hs | 30 +++++---- hledger/Hledger/Cli/Commands/Balance.hs | 11 ++-- hledger/Hledger/Cli/Commands/Balance.md | 3 + hledger/Hledger/Cli/Commands/Balancesheet.md | 2 +- tests/balancesheet/balancesheet.test | 64 ++++++++++--------- tests/journal/market-prices.test | 13 ++-- 6 files changed, 68 insertions(+), 55 deletions(-) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs index befa06eb1..ecb1027db 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs @@ -48,17 +48,20 @@ import Hledger.Reports.BalanceReport -- -- * the account's depth -- --- * a list of amounts, one for each column +-- * A list of amounts, one for each column. The meaning of the +-- amounts depends on the type of multi balance report, of which +-- there are three: periodic, cumulative and historical (see +-- 'BalanceType' and "Hledger.Cli.Commands.Balance"). -- --- * the total of the row's amounts +-- * the total of the row's amounts for a periodic report, +-- or zero for cumulative/historical reports (since summing +-- end balances generally doesn't make sense). -- -- * the average of the row's amounts -- --- 3. the column totals and the overall total and average +-- 3. the column totals, and the overall grand total (or zero for +-- cumulative/historical reports) and grand average. -- --- The meaning of the amounts depends on the type of multi balance --- report, of which there are three: periodic, cumulative and historical --- (see 'BalanceType' and "Hledger.Cli.Commands.Balance"). newtype MultiBalanceReport = MultiBalanceReport ([DateSpan] ,[MultiBalanceReportRow] @@ -304,7 +307,7 @@ multiBalanceReport ropts@ReportOpts{..} q j = CumulativeChange -> drop 1 $ scanl (+) 0 changes _ -> changes -- The total and average for the row. - , let rowtot = sum unvaluedbals + , let rowtot = if balancetype_==PeriodChange then sum unvaluedbals else 0 , let rowavg = averageMixedAmounts unvaluedbals , empty_ || depth == 0 || any (not . isZeroMixedAmount) unvaluedbals ] @@ -329,7 +332,7 @@ multiBalanceReport ropts@ReportOpts{..} q j = Just (AtDate d) -> [mixedAmountValue prices d amt | amt <- valuedbals1] _ -> unvaluedbals --value-at=transaction was handled earlier -- The total and average for the row, and their values. - , let rowtot = sum unvaluedbals + , let rowtot = if balancetype_==PeriodChange then sum unvaluedbals else 0 , let rowavg = averageMixedAmounts unvaluedbals , let valuedrowtot = case mvalueat of Just AtPeriod -> mixedAmountValue prices reportlastday rowtot @@ -404,7 +407,10 @@ multiBalanceReport ropts@ReportOpts{..} q j = Just (AtDate d) -> map (maybevalue d . sum) colamts -- Calculate and maybe value the grand total and average. [grandtotal,grandaverage] = - let amts = map ($ map sum colamts) [sum, averageMixedAmounts] + let amts = map ($ map sum colamts) + [if balancetype_==PeriodChange then sum else const 0 + ,averageMixedAmounts + ] in case mvalueat of Nothing -> amts Just AtTransaction -> amts @@ -486,10 +492,10 @@ tests_MultiBalanceReports = tests "MultiBalanceReports" [ (defreportopts{period_= PeriodBetween (fromGregorian 2008 1 1) (fromGregorian 2008 1 2), balancetype_=HistoricalBalance}, samplejournal) `gives` ( [ - ("assets:bank:checking","checking",3, [mamountp' "$1.00"], mamountp' "$1.00",Mixed [amount0 {aquantity=1}]) - ,("income:salary","salary",2, [mamountp' "$-1.00"], mamountp' "$-1.00",Mixed [amount0 {aquantity=(-1)}]) + ("assets:bank:checking", "checking", 3, [mamountp' "$1.00"] , Mixed [nullamt], Mixed [amount0 {aquantity=1}]) + ,("income:salary" ,"salary" , 2, [mamountp' "$-1.00"], Mixed [nullamt], Mixed [amount0 {aquantity=(-1)}]) ], - Mixed [usd0]) + Mixed [nullamt]) ,_test "a valid history on an empty period" $ (defreportopts{period_= PeriodBetween (fromGregorian 2008 1 2) (fromGregorian 2008 1 3), balancetype_=HistoricalBalance}, samplejournal) `gives` diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index c43c83a95..9bb278b3f 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -591,7 +591,7 @@ multiBalanceReportAsText ropts@ReportOpts{..} r = -- | Build a 'Table' from a multi-column balance report. balanceReportAsTable :: ReportOpts -> MultiBalanceReport -> Table String String MixedAmount -balanceReportAsTable opts@ReportOpts{average_, row_total_} (MultiBalanceReport (colspans, items, (coltotals,tot,avg))) = +balanceReportAsTable opts@ReportOpts{average_, row_total_, balancetype_} (MultiBalanceReport (colspans, items, (coltotals,tot,avg))) = maybetranspose $ addtotalrow $ Table @@ -599,23 +599,24 @@ balanceReportAsTable opts@ReportOpts{average_, row_total_} (MultiBalanceReport ( (T.Group NoLine $ map Header colheadings) (map rowvals items) where - mkDate = case balancetype_ opts of + totalscolumn = row_total_ && not (balancetype_ `elem` [CumulativeChange, HistoricalBalance]) + mkDate = case balancetype_ of PeriodChange -> showDateSpanMonthAbbrev _ -> maybe "" (showDate . prevday) . spanEnd colheadings = map mkDate colspans - ++ [" Total" | row_total_] + ++ [" Total" | totalscolumn] ++ ["Average" | average_] accts = map renderacct items renderacct (a,a',i,_,_,_) | tree_ opts = replicate ((i-1)*2) ' ' ++ T.unpack a' | otherwise = T.unpack $ maybeAccountNameDrop opts a rowvals (_,_,_,as,rowtot,rowavg) = as - ++ [rowtot | row_total_] + ++ [rowtot | totalscolumn] ++ [rowavg | average_] addtotalrow | no_total_ opts = id | otherwise = (+----+ (row "" $ coltotals - ++ [tot | row_total_ && not (null coltotals)] + ++ [tot | totalscolumn && not (null coltotals)] ++ [avg | average_ && not (null coltotals)] )) maybetranspose | transpose_ opts = \(Table rh ch vals) -> Table ch rh (transpose vals) diff --git a/hledger/Hledger/Cli/Commands/Balance.md b/hledger/Hledger/Cli/Commands/Balance.md index 64ed3dcd1..ab84e5d32 100644 --- a/hledger/Hledger/Cli/Commands/Balance.md +++ b/hledger/Hledger/Cli/Commands/Balance.md @@ -233,6 +233,9 @@ you are showing only the data after a certain start date: ``` +Note that `--cumulative` or `--historical/-H` disable `--row-total/-T`, +since summing end balances generally does not make sense. + Multicolumn balance reports display accounts in flat mode by default; to see the hierarchy, use `--tree`. diff --git a/hledger/Hledger/Cli/Commands/Balancesheet.md b/hledger/Hledger/Cli/Commands/Balancesheet.md index 0174f0d7c..a8ac8f627 100644 --- a/hledger/Hledger/Cli/Commands/Balancesheet.md +++ b/hledger/Hledger/Cli/Commands/Balancesheet.md @@ -39,7 +39,7 @@ As with [multicolumn balance reports](#multicolumn-balance-reports), you can alter the report mode with `--change`/`--cumulative`/`--historical`. Normally balancesheet shows historical ending balances, which is what you need for a balance sheet; note this means it ignores report begin -dates. +dates (and `-T/--row-total`, since summing end balances generally does not make sense). This command also supports [output destination](/manual.html#output-destination) and diff --git a/tests/balancesheet/balancesheet.test b/tests/balancesheet/balancesheet.test index 1bf7536bd..f4368b170 100644 --- a/tests/balancesheet/balancesheet.test +++ b/tests/balancesheet/balancesheet.test @@ -118,26 +118,28 @@ Balance Sheet 2008 Net: || $1 $1 $1 $1 $1 0 0 0 0 0 0 0 >>>= 0 -# 4. monthly balancesheet with average/total columns and without overall totals row +# 4. monthly balancesheet with average column and without overall totals row. +# Total column is requested but not shown because balancesheet is in historical mode +# by default (shows ending balances). hledger -f sample.journal balancesheet -p 'monthly in 2008' -NAT >>> Balance Sheet 2008 - || 2008/01/31 2008/02/29 2008/03/31 2008/04/30 2008/05/31 2008/06/30 2008/07/31 2008/08/31 2008/09/30 2008/10/31 2008/11/30 2008/12/31 Total Average -======================++================================================================================================================================================================== - Assets || -----------------------++------------------------------------------------------------------------------------------------------------------------------------------------------------------ - assets:bank:checking || $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 0 $11 $1 - assets:bank:saving || 0 0 0 0 0 $1 $1 $1 $1 $1 $1 $1 $7 $1 - assets:cash || 0 0 0 0 0 $-2 $-2 $-2 $-2 $-2 $-2 $-2 $-14 $-1 -----------------------++------------------------------------------------------------------------------------------------------------------------------------------------------------------ - || $1 $1 $1 $1 $1 0 0 0 0 0 0 $-1 $4 0 -======================++================================================================================================================================================================== - Liabilities || -----------------------++------------------------------------------------------------------------------------------------------------------------------------------------------------------ - liabilities:debts || 0 0 0 0 0 0 0 0 0 0 0 $-1 $-1 0 -----------------------++------------------------------------------------------------------------------------------------------------------------------------------------------------------ - || 0 0 0 0 0 0 0 0 0 0 0 $-1 $-1 0 + || 2008/01/31 2008/02/29 2008/03/31 2008/04/30 2008/05/31 2008/06/30 2008/07/31 2008/08/31 2008/09/30 2008/10/31 2008/11/30 2008/12/31 Average +======================++========================================================================================================================================================= + Assets || +----------------------++--------------------------------------------------------------------------------------------------------------------------------------------------------- + assets:bank:checking || $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 0 $1 + assets:bank:saving || 0 0 0 0 0 $1 $1 $1 $1 $1 $1 $1 $1 + assets:cash || 0 0 0 0 0 $-2 $-2 $-2 $-2 $-2 $-2 $-2 $-1 +----------------------++--------------------------------------------------------------------------------------------------------------------------------------------------------- + || $1 $1 $1 $1 $1 0 0 0 0 0 0 $-1 0 +======================++========================================================================================================================================================= + Liabilities || +----------------------++--------------------------------------------------------------------------------------------------------------------------------------------------------- + liabilities:debts || 0 0 0 0 0 0 0 0 0 0 0 $-1 0 +----------------------++--------------------------------------------------------------------------------------------------------------------------------------------------------- + || 0 0 0 0 0 0 0 0 0 0 0 $-1 0 >>>= 0 # 5. Tree output still works, #565 @@ -192,27 +194,27 @@ Balance Sheet 2017/01/01 >>>=0 # 7. An empty section does not disrupt the overall totals, #588 -hledger -f- balancesheet -YTA +hledger -f- balancesheet -YA <<< 2017/1/1 (assets) $1 >>> Balance Sheet 2017/01/01 - || 2017/12/31 Total Average -=============++============================== - Assets || --------------++------------------------------ - assets || $1 $1 $1 --------------++------------------------------ - || $1 $1 $1 -=============++============================== - Liabilities || --------------++------------------------------ --------------++------------------------------ - || -=============++============================== - Net: || $1 $1 $1 + || 2017/12/31 Average +=============++===================== + Assets || +-------------++--------------------- + assets || $1 $1 +-------------++--------------------- + || $1 $1 +=============++===================== + Liabilities || +-------------++--------------------- +-------------++--------------------- + || +=============++===================== + Net: || $1 $1 >>>2 >>>=0 diff --git a/tests/journal/market-prices.test b/tests/journal/market-prices.test index d9260b755..627072f8f 100644 --- a/tests/journal/market-prices.test +++ b/tests/journal/market-prices.test @@ -474,14 +474,15 @@ Ending balances (historical) in 2000q1, valued at transaction dates: || 1 B 1 B 1 B # 42. multicolumn balance report with -H, valuing each period's carried-over balances at period end. -$ hledger -f- bal -ME -H -p200001-200004 --value-at=p +# Unrelated, also -H always disables -T. +$ hledger -f- bal -META -H -p200001-200004 --value-at=p Ending balances (historical) in 2000q1, valued at period ends: - || 2000/01/31 2000/02/29 2000/03/31 -===++==================================== - a || 5 B 2 B 3 B ----++------------------------------------ - || 5 B 2 B 3 B + || 2000/01/31 2000/02/29 2000/03/31 Average +===++============================================= + a || 5 B 2 B 3 B 3 B +---++--------------------------------------------- + || 5 B 2 B 3 B 3 B # 43. multicolumn balance report with -H, valuing each period's carried-over balances at other date. $ hledger -f- bal -ME -H -p200001-200004 --value-at=2000-01-15