Skip to content

Commit

Permalink
cli: Commands.Balance.multiBalanceReportAsSpreadsheet: add date query…
Browse files Browse the repository at this point in the history
… anchors to period headers
  • Loading branch information
thielema committed Sep 14, 2024
1 parent 9b1283e commit 7ece4d0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
69 changes: 52 additions & 17 deletions hledger/Hledger/Cli/Commands/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ module Hledger.Cli.Commands.Balance (
,multiBalanceReportTableAsText
,multiBalanceReportAsSpreadsheet
,addTotalBorders
,simpleDateSpanCell
,RowClass(..)
-- ** HTML output helpers
,stylesheet_
Expand Down Expand Up @@ -599,6 +600,30 @@ headerCell text =
(Ods.cellBorder deflt) {Ods.borderBottom = Ods.DoubleLine}
}

headerDateSpanCell :: Maybe Text -> DateSpan -> Ods.Cell Ods.NumLines Text
headerDateSpanCell base spn =
let prd = showDateSpan spn in
(headerCell prd) {
Ods.cellAnchor =
maybe mempty (\url -> url <> "register?q=date:" <> prd) base
}

simpleDateSpanCell :: DateSpan -> Ods.Cell Ods.NumLines Text
simpleDateSpanCell = Ods.defaultCell . showDateSpan

dateSpanCell ::
(Ods.Lines border) => Maybe Text -> Text -> DateSpan -> Ods.Cell border Text
dateSpanCell base acct spn =
let prd = showDateSpan spn in
(Ods.defaultCell prd) {
Ods.cellAnchor =
maybe mempty
(\url -> url <>
"register?q=inacct:" <> quoteIfSpaced acct <>
" date:" <> prd)
base
}

addTotalBorders :: [[Ods.Cell border text]] -> [[Ods.Cell Ods.NumLines text]]
addTotalBorders =
zipWith
Expand Down Expand Up @@ -734,21 +759,26 @@ multiBalanceReportAsSpreadsheetHelper ishtml opts@ReportOpts{..} (PeriodicReport
LayoutBare -> headerCell "commodity" : dateHeaders
_ -> dateHeaders
dateHeaders =
map (headerCell . showDateSpan) colspans ++
map (headerDateSpanCell balance_base_url_) colspans ++
[hCell "rowtotal" "total" | row_total_] ++
[hCell "rowaverage" "average" | average_]
fullRowAsTexts row = map (anchorCell:) $ rowAsText Value row
where anchorCell =
let name = prrFullName row in
setAccountAnchor balance_base_url_ name $
accountCell $ accountNameDrop drop_ name
fullRowAsTexts row =
map (anchorCell:) $
rowAsText Value (dateSpanCell balance_base_url_ acctName) row
where acctName = prrFullName row
anchorCell =
setAccountAnchor balance_base_url_ acctName $
accountCell $ accountNameDrop drop_ acctName
totalrows
| no_total_ = []
| ishtml = zipWith (:) (accountCell totalRowHeadingHtml : repeat Ods.emptyCell) $ rowAsText Total tr
| otherwise = map (accountCell totalRowHeadingCsv :) $ rowAsText Total tr
rowAsText rc =
| ishtml = zipWith (:) (accountCell totalRowHeadingHtml : repeat Ods.emptyCell) $
rowAsText Total simpleDateSpanCell tr
| otherwise = map (accountCell totalRowHeadingCsv :) $
rowAsText Total simpleDateSpanCell tr
rowAsText rc dsCell =
let fmt = if ishtml then oneLineNoCostFmt else machineFmt
in map (map (fmap wbToText)) . multiBalanceRowAsCellBuilders fmt opts colspans rc
in map (map (fmap wbToText)) .
multiBalanceRowAsCellBuilders fmt opts colspans rc dsCell

-- Helpers and CSS styles for HTML output.

Expand Down Expand Up @@ -901,10 +931,11 @@ multiBalanceReportAsTable opts@ReportOpts{summary_only_, average_, row_total_, b

multiBalanceRowAsCellBuilders ::
AmountFormat -> ReportOpts -> [DateSpan] ->
RowClass -> PeriodicReportRow a MixedAmount ->
RowClass -> (DateSpan -> Ods.Cell Ods.NumLines Text) ->
PeriodicReportRow a MixedAmount ->
[[Ods.Cell Ods.NumLines WideBuilder]]
multiBalanceRowAsCellBuilders bopts ReportOpts{..} colspans
rc (PeriodicReportRow _ as rowtot rowavg) =
rc renderDateSpanCell (PeriodicReportRow _acct as rowtot rowavg) =
case layout_ of
LayoutWide width -> [fmap (cellFromMixedAmount bopts{displayMaxWidth=width}) clsamts]
LayoutTall -> paddedTranspose Ods.emptyCell
Expand All @@ -931,9 +962,11 @@ multiBalanceRowAsCellBuilders bopts ReportOpts{..} colspans
clsamts = (if not summary_only_ then classified else []) ++
[(rowTotalClass rc, rowtot) | totalscolumn && not (null as)] ++
[(rowAverageClass rc, rowavg) | average_ && not (null as)]
addDateColumns spn@(DateSpan s e) = (wbCell (showDateSpan spn) :)
. (wbDate (maybe "" showEFDate s) :)
. (wbDate (maybe "" (showEFDate . modifyEFDay (addDays (-1))) e) :)
addDateColumns spn@(DateSpan s e) remCols =
(wbFromText <$> renderDateSpanCell spn) :
wbDate (maybe "" showEFDate s) :
wbDate (maybe "" (showEFDate . modifyEFDay (addDays (-1))) e) :
remCols

paddedTranspose :: a -> [[a]] -> [[a]]
paddedTranspose _ [] = [[]]
Expand All @@ -953,12 +986,14 @@ multiBalanceRowAsCellBuilders bopts ReportOpts{..} colspans
multiBalanceRowAsText :: ReportOpts -> PeriodicReportRow a MixedAmount -> [[WideBuilder]]
multiBalanceRowAsText opts =
rawTableContent .
multiBalanceRowAsCellBuilders oneLineNoCostFmt{displayColour=color_ opts} opts [] Value
multiBalanceRowAsCellBuilders oneLineNoCostFmt{displayColour=color_ opts} opts []
Value simpleDateSpanCell

multiBalanceRowAsCsvText :: ReportOpts -> [DateSpan] -> PeriodicReportRow a MixedAmount -> [[T.Text]]
multiBalanceRowAsCsvText opts colspans =
map (map (wbToText . Ods.cellContent)) .
multiBalanceRowAsCellBuilders machineFmt opts colspans Value
multiBalanceRowAsCellBuilders machineFmt opts colspans
Value simpleDateSpanCell


-- Budget reports
Expand Down
3 changes: 2 additions & 1 deletion hledger/Hledger/Cli/CompoundBalanceCommand.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ compoundBalanceReportAsHtml ropts cbr =
totalrows =
if no_total_ ropts || length subreports == 1 then []
else
multiBalanceRowAsCellBuilders machineFmt ropts colspans Total totalrow
multiBalanceRowAsCellBuilders machineFmt ropts colspans
Total simpleDateSpanCell totalrow
-- make a table of rendered lines of the report totals row
& map (map (fmap wbToText))
& zipWith (:) (Spr.defaultCell "Net:" : repeat Spr.emptyCell)
Expand Down

0 comments on commit 7ece4d0

Please sign in to comment.