cli: compoundbalancecommand keeps zero-balance accounts with children

This commit is contained in:
Dmitry Astapov 2018-10-17 22:45:23 +01:00 committed by Simon Michael
parent a6a73e36e3
commit 15c666702b
2 changed files with 50 additions and 3 deletions

View File

@ -14,7 +14,7 @@ module Hledger.Cli.CompoundBalanceCommand (
) where ) where
import Data.List (foldl') import Data.List (foldl')
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe,catMaybes)
import qualified Data.Text as TS import qualified Data.Text as TS
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
import System.Console.CmdArgs.Explicit as C import System.Console.CmdArgs.Explicit as C
@ -218,13 +218,19 @@ compoundBalanceSubreport ropts userq j subreportqfn subreportnormalsign = r'
-- run the report -- run the report
q = And [subreportqfn j, userq] q = And [subreportqfn j, userq]
r@(MultiBalanceReport (dates, rows, totals)) = multiBalanceReport ropts' q j 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 r' | empty_ ropts = r
| otherwise = MultiBalanceReport (dates, rows', totals) | otherwise = MultiBalanceReport (dates, rows', totals)
where where
nonzeroaccounts =
dbg1 "nonzeroaccounts" $
catMaybes $ map (\(act,_,_,amts,_,_) ->
if not (all isZeroMixedAmount amts) then Just act else Nothing) rows
rows' = filter (not . emptyRow) rows rows' = filter (not . emptyRow) rows
where 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. -- | Render a compound balance report as plain text suitable for console output.
{- Eg: {- Eg:

View File

@ -241,3 +241,44 @@ Balance Sheet 2016/01/01
Net: ║ 1 Net: ║ 1
>>>2 >>>2
>>>= 0 >>>= 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