balance: make -H work with single-column reports #392

-H/--historical now makes a single-column balance report with a start
date show historical balances reflecting earlier postings.  This is
equivalent to specifying no start date, but it's more consistent.
This commit is contained in:
Simon Michael 2016-08-08 17:40:41 -07:00
parent 156922e419
commit 39c5eb9801
3 changed files with 31 additions and 6 deletions

View File

@ -9,7 +9,8 @@ module Hledger.Reports.MultiBalanceReports (
MultiBalanceReport(..),
MultiBalanceReportRow,
multiBalanceReport,
multiBalanceReportValue
multiBalanceReportValue,
singleBalanceReport
-- -- * Tests
-- tests_Hledger_Reports_MultiBalanceReport
@ -71,6 +72,20 @@ instance Show MultiBalanceReport where
-- type alias just to remind us which AccountNames might be depth-clipped, below.
type ClippedAccountName = AccountName
-- | Generates a single column BalanceReport like balanceReport, but uses
-- multiBalanceReport, so supports --historical. Does not support
-- boring parent eliding yet.
singleBalanceReport :: ReportOpts -> Query -> Journal -> BalanceReport
singleBalanceReport opts q j = (rows', total)
where
MultiBalanceReport (_, rows, (totals, _, _)) = multiBalanceReport opts q j
rows' = [(a
,if tree_ opts then a' else a -- BalanceReport expects full account name here with --flat
,if tree_ opts then d-1 else 0 -- BalanceReport uses 0-based account depths
, headDef nullmixedamt amts -- 0 columns is illegal, should not happen, return zeroes if it does
) | (a,a',d, amts, _, _) <- rows]
total = headDef nullmixedamt totals
-- | Generate a multicolumn balance report for the matched accounts,
-- showing the change of balance, accumulated balance, or historical balance
-- in each of the specified periods.

View File

@ -272,8 +272,8 @@ balancemode = (defCommandMode $ ["balance"] ++ aliases) { -- also accept but don
,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts"
,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "singlecolumn mode: use this custom line format"
,flagNone ["no-elide"] (\opts -> setboolopt "no-elide" opts) "tree mode: don't squash boring parent accounts"
,flagNone ["historical","H"] (\opts -> setboolopt "historical" opts) "multicolumn mode: show historical ending balances"
,flagNone ["cumulative"] (\opts -> setboolopt "cumulative" opts) "multicolumn mode: show accumulated ending balances"
,flagNone ["historical","H"] (\opts -> setboolopt "historical" opts) "show historical ending balances, reflecting postings before report start"
,flagNone ["cumulative"] (\opts -> setboolopt "cumulative" opts) "in multicolumn mode: show ending balances accumulated from 0 at report start"
,flagNone ["average","A"] (\opts -> setboolopt "average" opts) "multicolumn mode: show a row average column"
,flagNone ["row-total","T"] (\opts -> setboolopt "row-total" opts) "multicolumn mode: show a row total column"
,flagNone ["no-total","N"] (\opts -> setboolopt "no-total" opts) "don't show the final total row"
@ -297,9 +297,19 @@ balance opts@CliOpts{reportopts_=ropts} j = do
interval = interval_ ropts
baltype = balancetype_ ropts
valuedate = fromMaybe d $ queryEndDate False $ queryFromOpts d ropts
-- shenanigans: use single/multiBalanceReport when we must,
-- ie when there's a report interval, or --historical or -- cumulative.
-- Otherwise prefer the older balanceReport since it can elide boring parents.
case interval of
NoInterval -> do
let report = balanceReport ropts (queryFromOpts d ropts) j
let report
-- For --historical/--cumulative, we must use multiBalanceReport.
-- (This forces --no-elide.)
| balancetype_ ropts `elem` [HistoricalBalance, CumulativeBalance]
= let ropts' | flat_ ropts = ropts
| otherwise = ropts{accountlistmode_=ALTree}
in singleBalanceReport ropts' (queryFromOpts d ropts) j
| otherwise = balanceReport ropts (queryFromOpts d ropts) j
convert | value_ ropts = balanceReportValue j valuedate
| otherwise = id
render = case format of

View File

@ -17,10 +17,10 @@ Show accounts and their balances. Alias: bal.
: in tree mode: don't squash boring parent accounts
`-H --historical`
: in multicolumn mode: show historical ending balances
: show historical ending balances, reflecting postings before report start
`--cumulative`
: in multicolumn mode: show accumulated ending balances
: in multicolumn mode: show ending balances accumulated from 0 at report start
`-A --average`
: in multicolumn mode: show a row average column