lib,cli: Move reportPeriodName to Hledger.Reports.ReportOptions, use it

for HTML and CSV output for compound balance reports.
This commit is contained in:
Stephen Morgan 2021-04-04 09:03:49 +10:00 committed by Simon Michael
parent d0940bf8ce
commit 5fbb67b893
3 changed files with 23 additions and 24 deletions

View File

@ -20,7 +20,6 @@ module Hledger.Reports.BudgetReport (
budgetReportAsText,
budgetReportAsCsv,
-- * Helpers
reportPeriodName,
combineBudgetAndActual,
-- * Tests
tests_BudgetReport
@ -318,27 +317,6 @@ budgetReportAsTable
++ [grandavg | average_ ropts && not (null coltots)]
))
-- | Make a name for the given period in a multiperiod report, given
-- the type of balance being reported and the full set of report
-- periods. This will be used as a column heading (or row heading, in
-- a register summary report). We try to pick a useful name as follows:
--
-- - ending-balance reports: the period's end date
--
-- - balance change reports where the periods are months and all in the same year:
-- the short month name in the current locale
--
-- - all other balance change reports: a description of the datespan,
-- abbreviated to compact form if possible (see showDateSpan).
--
reportPeriodName :: BalanceType -> [DateSpan] -> DateSpan -> T.Text
reportPeriodName balancetype spans =
case balancetype of
PeriodChange -> if multiyear then showDateSpan else showDateSpanMonthAbbrev
where
multiyear = (>1) $ length $ nubSort $ map spanStartYear spans
_ -> maybe "" (showDate . prevday) . spanEnd
-- XXX generalise this with multiBalanceReportAsCsv ?
-- | Render a budget report as CSV. Like multiBalanceReportAsCsv,
-- but includes alternating actual and budget amount columns.

View File

@ -42,6 +42,7 @@ module Hledger.Reports.ReportOptions (
reportPeriodOrJournalStart,
reportPeriodLastDay,
reportPeriodOrJournalLastDay,
reportPeriodName
)
where
@ -592,3 +593,23 @@ reportPeriodOrJournalLastDay rspec j = reportPeriodLastDay rspec <|> journalOrPr
Just (AtEnd _) -> max (journalEndDate False j) lastPriceDirective
_ -> journalEndDate False j
lastPriceDirective = fmap (addDays 1) . maximumMay . map pddate $ jpricedirectives j
-- | Make a name for the given period in a multiperiod report, given
-- the type of balance being reported and the full set of report
-- periods. This will be used as a column heading (or row heading, in
-- a register summary report). We try to pick a useful name as follows:
--
-- - ending-balance reports: the period's end date
--
-- - balance change reports where the periods are months and all in the same year:
-- the short month name in the current locale
--
-- - all other balance change reports: a description of the datespan,
-- abbreviated to compact form if possible (see showDateSpan).
reportPeriodName :: BalanceType -> [DateSpan] -> DateSpan -> T.Text
reportPeriodName balancetype spans =
case balancetype of
PeriodChange -> if multiyear then showDateSpan else showDateSpanMonthAbbrev
where
multiyear = (>1) $ length $ nubSort $ map spanStartYear spans
_ -> maybe "" (showDate . prevday) . spanEnd

View File

@ -243,7 +243,7 @@ compoundBalanceReportAsCsv ropts (CompoundPeriodicReport title colspans subrepor
addtotals $
padRow title
: ( "Account"
: map showDateSpanMonthAbbrev colspans
: map (reportPeriodName (balancetype_ ropts) colspans) colspans
++ (if row_total_ ropts then ["Total"] else [])
++ (if average_ ropts then ["Average"] else [])
)
@ -288,7 +288,7 @@ compoundBalanceReportAsHtml ropts cbr =
[tr_ $ th_ [colspanattr, leftattr] $ h2_ $ toHtml title]
++ [thRow $
"" :
map showDateSpanMonthAbbrev colspans
map (reportPeriodName (balancetype_ ropts) colspans) colspans
++ (if row_total_ ropts then ["Total"] else [])
++ (if average_ ropts then ["Average"] else [])
]