diff --git a/hledger/Hledger/Cli/CompoundBalanceCommand.hs b/hledger/Hledger/Cli/CompoundBalanceCommand.hs index 7b291837f..ce0732b54 100644 --- a/hledger/Hledger/Cli/CompoundBalanceCommand.hs +++ b/hledger/Hledger/Cli/CompoundBalanceCommand.hs @@ -14,9 +14,11 @@ module Hledger.Cli.CompoundBalanceCommand ( ) where import Data.List (foldl') -import Data.Maybe (fromMaybe,catMaybes) +import Data.Maybe import qualified Data.Text as TS import qualified Data.Text.Lazy as TL +import Data.Time.Calendar +import Data.Time.Format import System.Console.CmdArgs.Explicit as C import Hledger.Read.CsvReader (CSV, printCSV) import Lucid as L hiding (value_) @@ -128,28 +130,6 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r "change":_ -> Just PeriodChange _ -> Nothing balancetype = fromMaybe cbctype mBalanceTypeOverride - title = - cbctitle ++ " " ++ showDateSpan requestedspan - ++ maybe "" (' ':) mtitleclarification - ++ valuation - where - requestedspan = queryDateSpan date2_ userq `spanDefaultsFrom` journalDateSpan date2_ j - -- when user overrides, add an indication to the report title - mtitleclarification = flip fmap mBalanceTypeOverride $ \t -> - case t of - PeriodChange -> "(Balance Changes)" - CumulativeChange -> "(Cumulative Ending Balances)" - HistoricalBalance -> "(Historical Ending Balances)" - multiperiod = interval_ /= NoInterval - valuation = case value_ of - Just (AtCost _mc) -> ", valued at cost" - Just (AtEnd _mc) -> ", valued at period ends" - Just (AtNow _mc) -> ", current value" - Just (AtDefault _mc) | multiperiod -> ", valued at period ends" - Just (AtDefault _mc) -> ", current value" - Just (AtDate d _mc) -> ", valued at "++showDate d - Nothing -> "" - -- Set balance type in the report options. -- Also, use tree mode (by default, at least?) if --cumulative/--historical -- are used in single column mode, since in that situation we will be using @@ -174,10 +154,12 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r ,cbcsubreportincreasestotal )) cbcqueries + subtotalrows = [(coltotals, increasesoveralltotal) | (_, MultiBalanceReport (_,_,(coltotals,_,_)), increasesoveralltotal) <- subreports ] + -- Sum the subreport totals by column. Handle these cases: -- - no subreports -- - empty subreports, having no subtotals (#588) @@ -199,10 +181,49 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r | otherwise = fromIntegral (length coltotals) `divideMixedAmount` grandtotal in (coltotals, grandtotal, grandavg) + colspans = case subreports of (_, MultiBalanceReport (ds,_,_), _):_ -> ds [] -> [] + + title = + cbctitle + ++ " " + ++ titledatestr + ++ maybe "" (' ':) mtitleclarification + ++ valuationdesc + where + + -- XXX #1078 the title of ending balance reports + -- (HistoricalBalance) should mention the end date(s) shown as + -- column heading(s) (not the date span of the transactions). + -- Also the dates should not be simplified (it should show + -- "2008/01/01-2008/12/31", not "2008"). + titledatestr + | balancetype == HistoricalBalance = showEndDates enddates + | otherwise = showDateSpan requestedspan + where + enddates = map (addDays (-1)) $ catMaybes $ map spanEnd colspans -- these spans will always have a definite end date + requestedspan = queryDateSpan date2_ userq `spanDefaultsFrom` journalDateSpan date2_ j + + -- when user overrides, add an indication to the report title + mtitleclarification = flip fmap mBalanceTypeOverride $ \t -> + case t of + PeriodChange -> "(Balance Changes)" + CumulativeChange -> "(Cumulative Ending Balances)" + HistoricalBalance -> "(Historical Ending Balances)" + + valuationdesc = case value_ of + Just (AtCost _mc) -> ", valued at cost" + Just (AtEnd _mc) -> ", valued at period ends" + Just (AtNow _mc) -> ", current value" + Just (AtDefault _mc) | multiperiod -> ", valued at period ends" + Just (AtDefault _mc) -> ", current value" + Just (AtDate d _mc) -> ", valued at "++showDate d + Nothing -> "" + where + multiperiod = interval_ /= NoInterval cbr = (title ,colspans @@ -217,6 +238,18 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r "html" -> (++ "\n") $ TL.unpack $ L.renderText $ compoundBalanceReportAsHtml ropts cbr _ -> compoundBalanceReportAsText ropts' cbr +-- | Summarise one or more (inclusive) end dates, in a way that's +-- visually different from showDateSpan, suggesting discrete end dates +-- rather than a continuous span. +showEndDates :: [Day] -> String +showEndDates es = case es of + -- cf showPeriod + (e:_:_) -> showdate e ++ ",," ++ showdate (last es) + [e] -> showdate e + [] -> "" + where + showdate = formatTime defaultTimeLocale "%0C%y/%m/%d" + -- | Run one subreport for a compound balance command in multi-column mode. -- This returns a MultiBalanceReport. compoundBalanceSubreport :: ReportOpts -> Query -> Journal -> (Journal -> Query) -> NormalSign -> MultiBalanceReport diff --git a/tests/balancesheet.test b/tests/balancesheet.test index f4368b170..b6a45843a 100644 --- a/tests/balancesheet.test +++ b/tests/balancesheet.test @@ -48,7 +48,7 @@ Balance Sheet 2016/01/01 # hledger -f sample.journal balancesheet -p 'monthly in 2008' >>> -Balance Sheet 2008 +Balance Sheet 2008/01/31,,2008/12/31 || 2008/01/31 2008/02/29 2008/03/31 2008/04/30 2008/05/31 2008/06/30 2008/07/31 2008/08/31 2008/09/30 2008/10/31 2008/11/30 2008/12/31 ======================++================================================================================================================================================ @@ -94,7 +94,7 @@ Balance Sheet 2008 # hledger -f sample.journal balancesheet -p 'monthly in 2008' --tree >>> -Balance Sheet 2008 +Balance Sheet 2008/01/31,,2008/12/31 || 2008/01/31 2008/02/29 2008/03/31 2008/04/30 2008/05/31 2008/06/30 2008/07/31 2008/08/31 2008/09/30 2008/10/31 2008/11/30 2008/12/31 ==============++================================================================================================================================================ @@ -123,7 +123,7 @@ Balance Sheet 2008 # by default (shows ending balances). hledger -f sample.journal balancesheet -p 'monthly in 2008' -NAT >>> -Balance Sheet 2008 +Balance Sheet 2008/01/31,,2008/12/31 || 2008/01/31 2008/02/29 2008/03/31 2008/04/30 2008/05/31 2008/06/30 2008/07/31 2008/08/31 2008/09/30 2008/10/31 2008/11/30 2008/12/31 Average ======================++========================================================================================================================================================= @@ -199,7 +199,7 @@ hledger -f- balancesheet -YA 2017/1/1 (assets) $1 >>> -Balance Sheet 2017/01/01 +Balance Sheet 2017/12/31 || 2017/12/31 Average =============++===================== @@ -218,14 +218,14 @@ Balance Sheet 2017/01/01 >>>2 >>>=0 -# 9. --pretty-tables uses unicode chars for borders +# 8. --pretty-tables uses unicode chars for borders hledger -f - balancesheet -M --pretty-tables <<< 2016/1/1 assets 1 b >>> -Balance Sheet 2016/01/01 +Balance Sheet 2016/01/31 ║ 2016/01/31 ═════════════╬════════════ @@ -244,7 +244,7 @@ Balance Sheet 2016/01/01 >>>2 >>>= 0 -# 10. Check that accounts brought to zero by subaccount balances +# 9. Check that accounts brought to zero by subaccount balances # are not erased from balancesheet hledger -f - balancesheet <<< @@ -262,7 +262,7 @@ hledger -f - balancesheet assets:this account should not disappear:subaccount -$10 assets:this account should not disappear >>> -Balance Sheet 2018/10/01-2018/10/03 +Balance Sheet 2018/10/03 || 2018/10/03 =====================================++============ diff --git a/tests/cashflow.test b/tests/cashflow.test index a3384df33..e26038c43 100644 --- a/tests/cashflow.test +++ b/tests/cashflow.test @@ -175,7 +175,7 @@ Cashflow Statement 2008 # 7. Multicolumn test (historical) hledger -f sample.journal cashflow -p 'monthly in 2008' -A --historical >>> -Cashflow Statement 2008 (Historical Ending Balances) +Cashflow Statement 2008/01/31,,2008/12/31 (Historical Ending Balances) || 2008/01/31 2008/02/29 2008/03/31 2008/04/30 2008/05/31 2008/06/30 2008/07/31 2008/08/31 2008/09/30 2008/10/31 2008/11/30 2008/12/31 Average ======================++========================================================================================================================================================= diff --git a/tests/incomestatement.test b/tests/incomestatement.test index 4c6b69b1d..eb93b26e8 100644 --- a/tests/incomestatement.test +++ b/tests/incomestatement.test @@ -263,7 +263,7 @@ Income Statement 2008 # hledger -f sample.journal incomestatement -p 'monthly in 2008' --historical >>> -Income Statement 2008 (Historical Ending Balances) +Income Statement 2008/01/31,,2008/12/31 (Historical Ending Balances) || 2008/01/31 2008/02/29 2008/03/31 2008/04/30 2008/05/31 2008/06/30 2008/07/31 2008/08/31 2008/09/30 2008/10/31 2008/11/30 2008/12/31 ===================++================================================================================================================================================