balance: ledger compatibility fix: don't elide parent accounts with multiple displayed subaccounts

This commit is contained in:
Simon Michael 2011-09-22 20:40:06 +00:00
parent ea0cbaea40
commit f79b272ff3
4 changed files with 50 additions and 27 deletions

View File

@ -539,6 +539,7 @@ isInteresting :: ReportOpts -> Ledger -> AccountName -> Bool
isInteresting opts l a | flat_ opts = isInterestingFlat opts l a isInteresting opts l a | flat_ opts = isInterestingFlat opts l a
| otherwise = isInterestingIndented opts l a | otherwise = isInterestingIndented opts l a
-- | Determine whether an account should get its own line in the --flat balance report.
isInterestingFlat :: ReportOpts -> Ledger -> AccountName -> Bool isInterestingFlat :: ReportOpts -> Ledger -> AccountName -> Bool
isInterestingFlat opts l a = notempty || emptyflag isInterestingFlat opts l a = notempty || emptyflag
where where
@ -546,16 +547,19 @@ isInterestingFlat opts l a = notempty || emptyflag
notempty = not $ isZeroMixedAmount $ exclusiveBalance acct notempty = not $ isZeroMixedAmount $ exclusiveBalance acct
emptyflag = empty_ opts emptyflag = empty_ opts
-- | Determine whether an account should get its own line in the indented
-- balance report. Cf Balance module doc.
isInterestingIndented :: ReportOpts -> Ledger -> AccountName -> Bool isInterestingIndented :: ReportOpts -> Ledger -> AccountName -> Bool
isInterestingIndented opts l a isInterestingIndented opts l a
| numinterestingsubs==1 && not atmaxdepth = notlikesub | numinterestingsubs == 1 && samebalanceassub && not atmaxdepth = False
| otherwise = notzero || emptyflag | numinterestingsubs < 2 && zerobalance && not emptyflag = False
| otherwise = True
where where
atmaxdepth = isJust d && Just (accountNameLevel a) == d where d = depth_ opts atmaxdepth = isJust d && Just (accountNameLevel a) == d where d = depth_ opts
emptyflag = empty_ opts emptyflag = empty_ opts
acct = ledgerAccount l a acct = ledgerAccount l a
notzero = not $ isZeroMixedAmount inclbalance where inclbalance = abalance acct zerobalance = isZeroMixedAmount inclbalance where inclbalance = abalance acct
notlikesub = not $ isZeroMixedAmount exclbalance where exclbalance = sumPostings $ apostings acct samebalanceassub = isZeroMixedAmount exclbalance where exclbalance = sumPostings $ apostings acct
numinterestingsubs = length $ filter isInterestingTree subtrees numinterestingsubs = length $ filter isInterestingTree subtrees
where where
isInterestingTree = treeany (isInteresting opts l . aname) isInterestingTree = treeany (isInteresting opts l . aname)

View File

@ -249,20 +249,6 @@ tests_Hledger_Cli = TestList
," 0" ," 0"
] ]
,"balance report elides zero-balance root account(s)" ~: do
j <- readJournal'
(unlines
["2008/1/1 one"
," test:a 1"
," test:b"
])
accountsReportAsText defreportopts (accountsReport defreportopts nullfilterspec j) `is`
[" 1 test:a"
," -1 test:b"
,"--------------------"
," 0"
]
] ]
,"journalCanonicaliseAmounts" ~: ,"journalCanonicaliseAmounts" ~:

View File

@ -82,15 +82,14 @@ Also, the balance report shows the total of all displayed accounts, when
that is non-zero. Here, it is displayed because the accounts shown add up that is non-zero. Here, it is displayed because the accounts shown add up
to $-1. to $-1.
Here is a more precise definition of \"interesting\" accounts in ledger's Also, non-interesting accounts may be elided. Here's an imperfect
balance report: description of the ledger balance command's eliding behaviour:
\"Interesting\" accounts are displayed on their own line. An account less
- an account which has just one interesting subaccount branch, and which deep than the report's max depth, with just one interesting subaccount,
is not at the report's maximum depth, is interesting if the balance is and the same balance as the subaccount, is non-interesting, and prefixed
different from the subaccount's, and otherwise boring. to the subaccount's line, unless (hledger's) --no-elide is in effect.
An account with a zero inclusive balance and less than two interesting
- any other account is interesting if it has a non-zero balance, or the -E subaccounts is not displayed at all, unless --empty is in effect.
flag is used.
-} -}

View File

@ -1,3 +1,4 @@
#!/usr/bin/env shelltest
# 1. One commodity. Zero accounts should be elided but the final total should not. # 1. One commodity. Zero accounts should be elided but the final total should not.
bin/hledger -f - balance bin/hledger -f - balance
<<< <<<
@ -12,3 +13,36 @@ bin/hledger -f - balance
-------------------- --------------------
0 0
>>>=0 >>>=0
# 2. An uninteresting parent account (with same balance as its single subaccount) is elided by default, like ledger
bin/hledger -f - balance --no-total
<<<
1/1
(a:b) 1
>>>
1 a:b
>>>=0
# 3. But not with --no-elide
bin/hledger -f - balance --no-total --no-elide
<<<
1/1
(a:b) 1
>>>
1 a
1 b
>>>=0
# 4. Nor when it has more than one subaccount
bin/hledger -f - balance --no-total
<<<
1/1
(a:b) 1
(a:c) -1
>>>
0 a
1 b
-1 c
>>>2
>>>=0