bs: show end date(s) in title, not transactions date span (#1078)

Compound balance reports showing ending balances (eg balancesheet),
now show the ending date (single column) or range of ending
dates (multi column) in their title. ,, (double comma) is used
rather than - (hyphen) to suggest a sequence of discrete dates
rather than a continuous span.
This commit is contained in:
Simon Michael 2019-08-14 19:27:57 +01:00
parent 7177f533b2
commit 7c2511aa29
4 changed files with 66 additions and 33 deletions

View File

@ -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

View File

@ -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
=====================================++============

View File

@ -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
======================++=========================================================================================================================================================

View File

@ -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
===================++================================================================================================================================================