diff --git a/hledger/Hledger/Cli/CompoundBalanceCommand.hs b/hledger/Hledger/Cli/CompoundBalanceCommand.hs index 3130884f9..c0e7f3e98 100644 --- a/hledger/Hledger/Cli/CompoundBalanceCommand.hs +++ b/hledger/Hledger/Cli/CompoundBalanceCommand.hs @@ -14,7 +14,7 @@ module Hledger.Cli.CompoundBalanceCommand ( ) where import Data.List (foldl') -import Data.Maybe (fromMaybe) +import Data.Maybe (fromMaybe,catMaybes) import qualified Data.Text as TS import qualified Data.Text.Lazy as TL import System.Console.CmdArgs.Explicit as C @@ -218,13 +218,19 @@ compoundBalanceSubreport ropts userq j subreportqfn subreportnormalsign = r' -- run the report q = And [subreportqfn j, userq] r@(MultiBalanceReport (dates, rows, totals)) = multiBalanceReport ropts' q j - -- if user didn't specify --empty, now remove the all-zero rows + -- if user didn't specify --empty, now remove the all-zero rows, unless they have non-zero subaccounts + -- in this report r' | empty_ ropts = r | otherwise = MultiBalanceReport (dates, rows', totals) where + nonzeroaccounts = + dbg1 "nonzeroaccounts" $ + catMaybes $ map (\(act,_,_,amts,_,_) -> + if not (all isZeroMixedAmount amts) then Just act else Nothing) rows rows' = filter (not . emptyRow) rows where - emptyRow (_,_,_,amts,_,_) = all isZeroMixedAmount amts + emptyRow (act,_,_,amts,_,_) = + all isZeroMixedAmount amts && all (not . (act `isAccountNamePrefixOf`)) nonzeroaccounts -- | Render a compound balance report as plain text suitable for console output. {- Eg: diff --git a/tests/balancesheet/balancesheet.test b/tests/balancesheet/balancesheet.test index ec3d6332b..1bf7536bd 100644 --- a/tests/balancesheet/balancesheet.test +++ b/tests/balancesheet/balancesheet.test @@ -241,3 +241,44 @@ Balance Sheet 2016/01/01 Net: ║ 1 >>>2 >>>= 0 + +# 10. Check that accounts brought to zero by subaccount balances +# are not erased from balancesheet +hledger -f - balancesheet +<<< +2018-10-01 + income:whatever + assets:bank $100 + assets:bank:subaccount $10 + assets:bank:this subaccount should disappear $10 + +2018-10-02 + assets:bank:this subaccount should disappear -$10 + assets:bank + +2018-10-03 + assets:this account should not disappear:subaccount -$10 + assets:this account should not disappear +>>> +Balance Sheet 2018/10/01-2018/10/03 + + || 2018/10/03 +=====================================++============ + Assets || +-------------------------------------++------------ + assets || $120 + bank || $120 + subaccount || $10 + this account should not disappear || 0 + subaccount || $-10 +-------------------------------------++------------ + || $120 +=====================================++============ + Liabilities || +-------------------------------------++------------ +-------------------------------------++------------ + || +=====================================++============ + Net: || $120 +>>>2 +>>>= 0