From 50f73b74342966619d3eca6ba63723b4a6b9cfc6 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Thu, 9 Sep 2021 11:14:48 +1000 Subject: [PATCH] fix: balance: Show all empty accounts in flat balance reports. (#1688) Previously we only showed empty leaves. --- .../Hledger/Reports/MultiBalanceReport.hs | 15 ++++++++++----- hledger/test/balance/no-total-no-elide.test | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index 176669774..f2ba236df 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -409,13 +409,18 @@ displayedAccounts ReportSpec{_rsQuery=query,_rsReportOpts=ropts} valuedaccts -- Accounts interesting for their own sake isInteresting name amts = - d <= depth -- Throw out anything too deep - && ((empty_ ropts && all (null . asubs) amts) -- Keep all leaves when using empty_ - || not (isZeroRow balance amts)) -- Throw out anything with zero balance + d <= depth -- Throw out anything too deep + && ( (empty_ ropts && keepWhenEmpty amts) -- Keep empty accounts when called with --empty + || not (isZeroRow balance amts) -- Keep everything with a non-zero balance in the row + ) where d = accountNameLevel name - balance | ALTree <- accountlistmode_ ropts, d == depth = maybeStripPrices . aibalance - | otherwise = maybeStripPrices . aebalance + keepWhenEmpty = case accountlistmode_ ropts of + ALFlat -> const True -- Keep all empty accounts in flat mode + ALTree -> all (null . asubs) -- Keep only empty leaves in tree mode + balance = maybeStripPrices . case accountlistmode_ ropts of + ALTree | d == depth -> aibalance + _ -> aebalance where maybeStripPrices = if show_costs_ ropts then id else mixedAmountStripPrices -- Accounts interesting because they are a fork for interesting subaccounts diff --git a/hledger/test/balance/no-total-no-elide.test b/hledger/test/balance/no-total-no-elide.test index 4feb8d421..d22105918 100644 --- a/hledger/test/balance/no-total-no-elide.test +++ b/hledger/test/balance/no-total-no-elide.test @@ -21,7 +21,7 @@ $ hledger -f - balance (a:b) 1 # 2. An uninteresting parent account (with same balance as its single subaccount) is elided by default, like ledger -$ hledger -f - balance --no-total +$ hledger -f - balance --no-total --tree > 1 a:b >=0 @@ -98,3 +98,19 @@ Balance changes in 2020: a || 1 aa || 0 aaa || 0 + +# 11. In flat mode, display all zero-balance accounts, including non-leaves +$ hledger -f - balance --flat --no-total --empty + 1 a + 0 a:aa + 0 a:aa:aaa + +# 12. Same as 11 for multiperiod +$ hledger -f - balance --flat --no-total -Y --empty +Balance changes in 2020: + + || 2020 +==========++====== + a || 1 + a:aa || 0 + a:aa:aaa || 0