From 39c5eb9801cd6ba09c432da18482fdb6e3ff574e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 8 Aug 2016 17:40:41 -0700 Subject: [PATCH] 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. --- .../Hledger/Reports/MultiBalanceReports.hs | 17 ++++++++++++++++- hledger/Hledger/Cli/Balance.hs | 16 +++++++++++++--- hledger/doc/commands-balance.m4.md | 4 ++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs index 49ec93489..5064403be 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs @@ -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. diff --git a/hledger/Hledger/Cli/Balance.hs b/hledger/Hledger/Cli/Balance.hs index 9d26559b6..c04587a61 100644 --- a/hledger/Hledger/Cli/Balance.hs +++ b/hledger/Hledger/Cli/Balance.hs @@ -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 diff --git a/hledger/doc/commands-balance.m4.md b/hledger/doc/commands-balance.m4.md index c6e6785eb..9d8a57310 100644 --- a/hledger/doc/commands-balance.m4.md +++ b/hledger/doc/commands-balance.m4.md @@ -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