From c89bec0e17bde93310d58af2c806a3851233ae3d Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 25 Jul 2017 09:17:54 -0700 Subject: [PATCH] bs/is: don't let an empty subreport disable the grand totals (fixes #588) --- hledger/Hledger/Cli/CompoundBalanceCommand.hs | 10 +++++-- tests/balancesheet/balancesheet.test | 26 +++++++++++++++++++ tests/balancesheet/pretty.test | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/hledger/Hledger/Cli/CompoundBalanceCommand.hs b/hledger/Hledger/Cli/CompoundBalanceCommand.hs index fd0966260..b9c5862b2 100644 --- a/hledger/Hledger/Cli/CompoundBalanceCommand.hs +++ b/hledger/Hledger/Cli/CompoundBalanceCommand.hs @@ -140,8 +140,14 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} CliOpts{command_=cmd, repo row "Total" overalltotals' where overalltotals = case subreporttotals of - a1:as -> foldl' (zipWith (+)) a1 as - [] -> [] + [] -> [] + ts -> foldl' (zipWith (+)) zeros ts' + where + -- A subreport might be empty and have no subtotals, count those as zeroes (#588). + -- Varying-length subtotals rows are handled similarly, though that is not expected to happen. + len = maximum $ map length ts + ts' = [take len $ as ++ repeat nullmixedamt | as <- ts] + zeros = replicate len nullmixedamt overalltotals' | null overalltotals = [] | otherwise = overalltotals diff --git a/tests/balancesheet/balancesheet.test b/tests/balancesheet/balancesheet.test index ef4a32aec..12a901b32 100644 --- a/tests/balancesheet/balancesheet.test +++ b/tests/balancesheet/balancesheet.test @@ -174,3 +174,29 @@ Total: >>>2 >>>=0 +# 8. An empty section is does not disrupt the overall totals, #588 +hledger -f- balancesheet -YTA +<<< +2017/1/1 + (assets) $1 +>>> +Balance Sheet + + || 2017/12/31 Total Average +=============++=============================== + Assets || +-------------++------------------------------- + assets || $1 $1 $1 +-------------++------------------------------- + || $1 $1 $1 +=============++=============================== + Liabilities || +-------------++------------------------------- +-------------++------------------------------- + || +=============++=============================== + Total || $1 $1 $1 + + +>>>2 +>>>=0 diff --git a/tests/balancesheet/pretty.test b/tests/balancesheet/pretty.test index d95eddf38..787955a78 100644 --- a/tests/balancesheet/pretty.test +++ b/tests/balancesheet/pretty.test @@ -20,7 +20,7 @@ Balance Sheet ─────────────╫───────────── ║ ═════════════╬═════════════ - Total ║ + Total ║ 1 >>>2