Skip to content

Commit

Permalink
bal/bs/bse: -H or --cumulative now disables -T (#329)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simonmichael committed May 12, 2019
1 parent 76342a3 commit e21afc4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 55 deletions.
30 changes: 18 additions & 12 deletions hledger-lib/Hledger/Reports/MultiBalanceReports.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
] ]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down
11 changes: 6 additions & 5 deletions hledger/Hledger/Cli/Commands/Balance.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -591,31 +591,32 @@ 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
(T.Group NoLine $ map Header accts) (T.Group NoLine $ map Header accts)
(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)
Expand Down
3 changes: 3 additions & 0 deletions hledger/Hledger/Cli/Commands/Balance.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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`.


Expand Down
2 changes: 1 addition & 1 deletion hledger/Hledger/Cli/Commands/Balancesheet.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 33 additions & 31 deletions tests/balancesheet/balancesheet.test
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
13 changes: 7 additions & 6 deletions tests/journal/market-prices.test
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e21afc4

Please sign in to comment.