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.
This commit is contained in:
Simon Michael 2019-05-11 11:55:04 -07:00
parent 76342a3fd0
commit e21afc466a
6 changed files with 68 additions and 55 deletions

View File

@ -48,17 +48,20 @@ import Hledger.Reports.BalanceReport
-- --
-- * the account's depth -- * 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 -- * 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 = newtype MultiBalanceReport =
MultiBalanceReport ([DateSpan] MultiBalanceReport ([DateSpan]
,[MultiBalanceReportRow] ,[MultiBalanceReportRow]
@ -304,7 +307,7 @@ multiBalanceReport ropts@ReportOpts{..} q j =
CumulativeChange -> drop 1 $ scanl (+) 0 changes CumulativeChange -> drop 1 $ scanl (+) 0 changes
_ -> changes _ -> changes
-- The total and average for the row. -- 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 , let rowavg = averageMixedAmounts unvaluedbals
, empty_ || depth == 0 || any (not . isZeroMixedAmount) 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] Just (AtDate d) -> [mixedAmountValue prices d amt | amt <- valuedbals1]
_ -> unvaluedbals --value-at=transaction was handled earlier _ -> unvaluedbals --value-at=transaction was handled earlier
-- The total and average for the row, and their values. -- 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 rowavg = averageMixedAmounts unvaluedbals
, let valuedrowtot = case mvalueat of , let valuedrowtot = case mvalueat of
Just AtPeriod -> mixedAmountValue prices reportlastday rowtot Just AtPeriod -> mixedAmountValue prices reportlastday rowtot
@ -404,7 +407,10 @@ multiBalanceReport ropts@ReportOpts{..} q j =
Just (AtDate d) -> map (maybevalue d . sum) colamts Just (AtDate d) -> map (maybevalue d . sum) colamts
-- Calculate and maybe value the grand total and average. -- Calculate and maybe value the grand total and average.
[grandtotal,grandaverage] = [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 in case mvalueat of
Nothing -> amts Nothing -> amts
Just AtTransaction -> 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` (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}]) ("assets:bank:checking", "checking", 3, [mamountp' "$1.00"] , Mixed [nullamt], Mixed [amount0 {aquantity=1}])
,("income:salary","salary",2, [mamountp' "$-1.00"], mamountp' "$-1.00",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" $ ,_test "a valid history on an empty period" $
(defreportopts{period_= PeriodBetween (fromGregorian 2008 1 2) (fromGregorian 2008 1 3), balancetype_=HistoricalBalance}, samplejournal) `gives` (defreportopts{period_= PeriodBetween (fromGregorian 2008 1 2) (fromGregorian 2008 1 3), balancetype_=HistoricalBalance}, samplejournal) `gives`

View File

@ -591,7 +591,7 @@ multiBalanceReportAsText ropts@ReportOpts{..} r =
-- | Build a 'Table' from a multi-column balance report. -- | Build a 'Table' from a multi-column balance report.
balanceReportAsTable :: ReportOpts -> MultiBalanceReport -> Table String String MixedAmount 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 $ maybetranspose $
addtotalrow $ addtotalrow $
Table Table
@ -599,23 +599,24 @@ balanceReportAsTable opts@ReportOpts{average_, row_total_} (MultiBalanceReport (
(T.Group NoLine $ map Header colheadings) (T.Group NoLine $ map Header colheadings)
(map rowvals items) (map rowvals items)
where where
mkDate = case balancetype_ opts of totalscolumn = row_total_ && not (balancetype_ `elem` [CumulativeChange, HistoricalBalance])
mkDate = case balancetype_ of
PeriodChange -> showDateSpanMonthAbbrev PeriodChange -> showDateSpanMonthAbbrev
_ -> maybe "" (showDate . prevday) . spanEnd _ -> maybe "" (showDate . prevday) . spanEnd
colheadings = map mkDate colspans colheadings = map mkDate colspans
++ [" Total" | row_total_] ++ [" Total" | totalscolumn]
++ ["Average" | average_] ++ ["Average" | average_]
accts = map renderacct items accts = map renderacct items
renderacct (a,a',i,_,_,_) renderacct (a,a',i,_,_,_)
| tree_ opts = replicate ((i-1)*2) ' ' ++ T.unpack a' | tree_ opts = replicate ((i-1)*2) ' ' ++ T.unpack a'
| otherwise = T.unpack $ maybeAccountNameDrop opts a | otherwise = T.unpack $ maybeAccountNameDrop opts a
rowvals (_,_,_,as,rowtot,rowavg) = as rowvals (_,_,_,as,rowtot,rowavg) = as
++ [rowtot | row_total_] ++ [rowtot | totalscolumn]
++ [rowavg | average_] ++ [rowavg | average_]
addtotalrow | no_total_ opts = id addtotalrow | no_total_ opts = id
| otherwise = (+----+ (row "" $ | otherwise = (+----+ (row "" $
coltotals coltotals
++ [tot | row_total_ && not (null coltotals)] ++ [tot | totalscolumn && not (null coltotals)]
++ [avg | average_ && not (null coltotals)] ++ [avg | average_ && not (null coltotals)]
)) ))
maybetranspose | transpose_ opts = \(Table rh ch vals) -> Table ch rh (transpose vals) maybetranspose | transpose_ opts = \(Table rh ch vals) -> Table ch rh (transpose vals)

View File

@ -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; Multicolumn balance reports display accounts in flat mode by default;
to see the hierarchy, use `--tree`. to see the hierarchy, use `--tree`.

View File

@ -39,7 +39,7 @@ As with [multicolumn balance reports](#multicolumn-balance-reports),
you can alter the report mode with `--change`/`--cumulative`/`--historical`. you can alter the report mode with `--change`/`--cumulative`/`--historical`.
Normally balancesheet shows historical ending balances, which is what Normally balancesheet shows historical ending balances, which is what
you need for a balance sheet; note this means it ignores report begin 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 This command also supports
[output destination](/manual.html#output-destination) and [output destination](/manual.html#output-destination) and

View File

@ -118,26 +118,28 @@ Balance Sheet 2008
Net: || $1 $1 $1 $1 $1 0 0 0 0 0 0 0 Net: || $1 $1 $1 $1 $1 0 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 hledger -f sample.journal balancesheet -p 'monthly in 2008' -NAT
>>> >>>
Balance Sheet 2008 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 || 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 ||
----------------------++------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------------------++---------------------------------------------------------------------------------------------------------------------------------------------------------
assets:bank:checking || $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 0 $11 $1 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 $7 $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 $-14 $-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 $4 0 || $1 $1 $1 $1 $1 0 0 0 0 0 0 $-1 0
======================++================================================================================================================================================================== ======================++=========================================================================================================================================================
Liabilities || Liabilities ||
----------------------++------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------------------++---------------------------------------------------------------------------------------------------------------------------------------------------------
liabilities:debts || 0 0 0 0 0 0 0 0 0 0 0 $-1 $-1 0 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 $-1 0 || 0 0 0 0 0 0 0 0 0 0 0 $-1 0
>>>= 0 >>>= 0
# 5. Tree output still works, #565 # 5. Tree output still works, #565
@ -192,27 +194,27 @@ Balance Sheet 2017/01/01
>>>=0 >>>=0
# 7. An empty section does not disrupt the overall totals, #588 # 7. An empty section does not disrupt the overall totals, #588
hledger -f- balancesheet -YTA hledger -f- balancesheet -YA
<<< <<<
2017/1/1 2017/1/1
(assets) $1 (assets) $1
>>> >>>
Balance Sheet 2017/01/01 Balance Sheet 2017/01/01
|| 2017/12/31 Total Average || 2017/12/31 Average
=============++============================== =============++=====================
Assets || Assets ||
-------------++------------------------------ -------------++---------------------
assets || $1 $1 $1 assets || $1 $1
-------------++------------------------------ -------------++---------------------
|| $1 $1 $1 || $1 $1
=============++============================== =============++=====================
Liabilities || Liabilities ||
-------------++------------------------------ -------------++---------------------
-------------++------------------------------ -------------++---------------------
|| ||
=============++============================== =============++=====================
Net: || $1 $1 $1 Net: || $1 $1
>>>2 >>>2
>>>=0 >>>=0

View File

@ -474,14 +474,15 @@ Ending balances (historical) in 2000q1, valued at transaction dates:
|| 1 B 1 B 1 B || 1 B 1 B 1 B
# 42. multicolumn balance report with -H, valuing each period's carried-over balances at period end. # 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: Ending balances (historical) in 2000q1, valued at period ends:
|| 2000/01/31 2000/02/29 2000/03/31 || 2000/01/31 2000/02/29 2000/03/31 Average
===++==================================== ===++=============================================
a || 5 B 2 B 3 B a || 5 B 2 B 3 B 3 B
---++------------------------------------ ---++---------------------------------------------
|| 5 B 2 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. # 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 $ hledger -f- bal -ME -H -p200001-200004 --value-at=2000-01-15