{-# LANGUAGE OverloadedStrings    #-}
{-# LANGUAGE QuasiQuotes          #-}
{-|
Links from report cells to hledger-web's register: for hledger-web's own
report pages, and for HTML and FODS output with --base-url. A figure
links to the register that derives it, an account name to the account's
register, a period heading to the register for that period.
-}
module Hledger.Cli.Anchor (
    LinkOpts(..),
    defaultLinkOpts,
    linkParams,
    valueParam,
    composeAnchor,
    composeAnchorWith,
    accountParams,
    dateTerm,
    removeDates,
    removeInacct,
    withLink,
    setAccountAnchor,
    setAccountAnchorWith,
    dateCell,
    dateSpanCell,
    dateSpanCellWith,
    totalDateSpanCell,
    headerDateSpanCell,
    renderPeriodHeading,
    amountPhrase,
    ) where

import Data.Text qualified as Text
import Data.Text (Text)
import Data.Time (Day)
import Data.Maybe (fromMaybe)

import Text.URI qualified as Uri
import Text.URI.QQ qualified as UriQQ

import Hledger.Write.Spreadsheet qualified as Spr
import Hledger.Write.Spreadsheet (headerCell)
import Hledger.Utils.IO (error')
import Hledger.Utils.Text (quoteForQuery)
import Hledger.Data.Dates (showDateSpan, showDateSpanFull, showDateSpanForQuery, showDate, nulldatespan)
import Hledger.Data.Types (DateSpan(..))
import Hledger.Data.Valuation (ConversionOp(..), ValuationType(..))
import Hledger.Reports.ReportOptions (PeriodTitles(..), BalanceAccumulation(..))


-- | What a report's register links need to know about the report,
-- besides its query and the base url.
data LinkOpts = LinkOpts {
    LinkOpts -> BalanceAccumulation
loAccum        :: BalanceAccumulation,
      -- ^ With historical balances, the register starts from the balance
      --   before the period, so that it ends on the figure in the cell.
    LinkOpts -> DateSpan
loSpan         :: DateSpan,
      -- ^ The whole report span. A cumulative figure's register runs from its start.
    LinkOpts -> Bool
loDate2        :: Bool,
      -- ^ Does the report use secondary dates ? Then so must the register.
    LinkOpts -> Bool
loIncludesSubs :: Bool,
      -- ^ Does the row's figure include subaccounts, or not (subs=0) ?
    LinkOpts -> Bool
loNegated      :: Bool,
      -- ^ Are the figures shown with the opposite sign to the register's ?
    LinkOpts -> Maybe Text
loValue        :: Maybe Text
      -- ^ The register's value parameter, for figures converted to cost
      --   or to market value (see 'valueParam').
}

defaultLinkOpts :: LinkOpts
defaultLinkOpts :: LinkOpts
defaultLinkOpts = BalanceAccumulation
-> DateSpan -> Bool -> Bool -> Bool -> Maybe Text -> LinkOpts
LinkOpts BalanceAccumulation
PerPeriod DateSpan
nulldatespan Bool
False Bool
True Bool
False Maybe Text
forall a. Maybe a
Nothing

-- | The query parameters a link carries besides q: the register's
-- accumulation mode, when it is not the register's default, and how it
-- converts amounts, when the report converts them.
linkParams :: LinkOpts -> [(Text, Text)]
linkParams :: LinkOpts -> [(Text, Text)]
linkParams LinkOpts
lo =
    [(Text
"accum", Text
"historical") | LinkOpts -> BalanceAccumulation
loAccum LinkOpts
lo BalanceAccumulation -> BalanceAccumulation -> Bool
forall a. Eq a => a -> a -> Bool
== BalanceAccumulation
Historical] [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall a. [a] -> [a] -> [a]
++
    [(Text
"value", Text
v) | Just Text
v <- [LinkOpts -> Maybe Text
loValue LinkOpts
lo]]

-- | The value parameter that asks hledger-web's register to convert
-- amounts as a report does, mirroring --value's WHEN[,COMM] syntax:
-- "cost" for -B, "end" for -V, "end,COMM" for -X COMM, "then", "now",
-- or a date for the other valuations. A register converting them the
-- same way ends on the figure a link was clicked on. Cost combined
-- with a valuation has none, and such links open the register as it is.
valueParam :: Maybe ConversionOp -> Maybe ValuationType -> Maybe Text
valueParam :: Maybe ConversionOp -> Maybe ValuationType -> Maybe Text
valueParam Maybe ConversionOp
conv Maybe ValuationType
val =
    case (Maybe ConversionOp
conv Maybe ConversionOp -> Maybe ConversionOp -> Bool
forall a. Eq a => a -> a -> Bool
== ConversionOp -> Maybe ConversionOp
forall a. a -> Maybe a
Just ConversionOp
ToCost, Maybe ValuationType
val) of
        (Bool
True,  Maybe ValuationType
Nothing) -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"cost"
        (Bool
True,  Just ValuationType
_)  -> Maybe Text
forall a. Maybe a
Nothing
        (Bool
False, Maybe ValuationType
Nothing) -> Maybe Text
forall a. Maybe a
Nothing
        (Bool
False, Just ValuationType
vt) -> Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ case ValuationType
vt of
            AtEnd  Maybe Text
mc   -> Text
"end"  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe Text -> Text
comm Maybe Text
mc
            AtThen Maybe Text
mc   -> Text
"then" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe Text -> Text
comm Maybe Text
mc
            AtNow  Maybe Text
mc   -> Text
"now"  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe Text -> Text
comm Maybe Text
mc
            AtDate Day
d Maybe Text
mc -> Day -> Text
showDate Day
d Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe Text -> Text
comm Maybe Text
mc
  where comm :: Maybe Text -> Text
comm = Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (Text
"," Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>)

-- | The span a figure's register covers: the period, or, for a
-- cumulative figure, the report's start to the period's end.
linkSpan :: LinkOpts -> DateSpan -> DateSpan
linkSpan :: LinkOpts -> DateSpan -> DateSpan
linkSpan LinkOpts
lo spn :: DateSpan
spn@(DateSpan Maybe EFDay
_ Maybe EFDay
e) =
    case LinkOpts -> BalanceAccumulation
loAccum LinkOpts
lo of
        BalanceAccumulation
Cumulative -> let DateSpan Maybe EFDay
s Maybe EFDay
_ = LinkOpts -> DateSpan
loSpan LinkOpts
lo in Maybe EFDay -> Maybe EFDay -> DateSpan
DateSpan Maybe EFDay
s Maybe EFDay
e
        BalanceAccumulation
_          -> DateSpan
spn

-- | The query parameters naming the account a link's register is for:
-- with its subaccounts, or with subs=0, without them. A depth 0
-- report's "..." row, all accounts in one, names none: its register
-- is the register of everything.
accountParams :: Bool -> Text -> [(Text, Text)]
accountParams :: Bool -> Text -> [(Text, Text)]
accountParams Bool
_ Text
"..." = []
accountParams Bool
includesSubs Text
acct = (Text
"account", Text
acct) (Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
: [(Text
"subs", Text
"0") | Bool -> Bool
not Bool
includesSubs]

-- | A date: term for the span (date2: for a report using secondary
-- dates), or none for the unbounded span.
dateTerm :: Bool -> DateSpan -> [Text]
dateTerm :: Bool -> DateSpan -> [Text]
dateTerm Bool
date2 DateSpan
spn =
    [(if Bool
date2 then Text
"date2:" else Text
"date:") Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t | let t :: Text
t = DateSpan -> Text
showDateSpanForQuery DateSpan
spn, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Bool
Text.null Text
t]

registerQueryUrl :: [(Text, Text)] -> [Text] -> Text
registerQueryUrl :: [(Text, Text)] -> [Text] -> Text
registerQueryUrl [(Text, Text)]
params [Text]
query =
    URI -> Text
Uri.render (URI -> Text) -> URI -> Text
forall a b. (a -> b) -> a -> b
$
    [UriQQ.uri|register|] {
        Uri.uriQuery =
            [Uri.QueryParam [UriQQ.queryKey|q|] (queryValue q) | not $ Text.null q]
            ++ [Uri.QueryParam (queryKey k) (queryValue v) | (k, v) <- params]
    }
  where
    q :: Text
q = [Text] -> Text
Text.unwords ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
quoteForQuery ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
Text.null) [Text]
query
    queryKey :: Text -> RText 'QueryKey
queryKey   = RText 'QueryKey -> Maybe (RText 'QueryKey) -> RText 'QueryKey
forall a. a -> Maybe a -> a
fromMaybe (String -> RText 'QueryKey
forall a. String -> a
error' String
"register URI query construction failed") (Maybe (RText 'QueryKey) -> RText 'QueryKey)
-> (Text -> Maybe (RText 'QueryKey)) -> Text -> RText 'QueryKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe (RText 'QueryKey)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'QueryKey)
Uri.mkQueryKey
    queryValue :: Text -> RText 'QueryValue
queryValue = RText 'QueryValue -> Maybe (RText 'QueryValue) -> RText 'QueryValue
forall a. a -> Maybe a -> a
fromMaybe (String -> RText 'QueryValue
forall a. String -> a
error' String
"register URI query construction failed") (Maybe (RText 'QueryValue) -> RText 'QueryValue)
-> (Text -> Maybe (RText 'QueryValue)) -> Text -> RText 'QueryValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe (RText 'QueryValue)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'QueryValue)
Uri.mkQueryValue

{- |
>>> composeAnchor Nothing ["date:2024"]
""
>>> composeAnchor (Just "") ["date:2024"]
"register?q=date:2024"
>>> composeAnchor (Just "/") ["date:2024"]
"/register?q=date:2024"
>>> composeAnchor (Just "foo") ["date:2024"]
"foo/register?q=date:2024"
>>> composeAnchor (Just "foo/") ["date:2024"]
"foo/register?q=date:2024"
-}
composeAnchor :: Maybe Text -> [Text] -> Text
composeAnchor :: Maybe Text -> [Text] -> Text
composeAnchor = [(Text, Text)] -> Maybe Text -> [Text] -> Text
composeAnchorWith []

{- | Like 'composeAnchor', with query parameters after q.

>>> composeAnchorWith [("accum","historical")] (Just "") ["date:2024"]
"register?q=date:2024&accum=historical"
-}
composeAnchorWith :: [(Text, Text)] -> Maybe Text -> [Text] -> Text
composeAnchorWith :: [(Text, Text)] -> Maybe Text -> [Text] -> Text
composeAnchorWith [(Text, Text)]
_ Maybe Text
Nothing [Text]
_ = Text
forall a. Monoid a => a
mempty
composeAnchorWith [(Text, Text)]
params (Just Text
baseUrl) [Text]
query =
    Text
baseUrl Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
    (if ((Text, Char) -> Bool) -> Maybe (Text, Char) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Char
'/'Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==) (Char -> Bool) -> ((Text, Char) -> Char) -> (Text, Char) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Char) -> Char
forall a b. (a, b) -> b
snd) (Maybe (Text, Char) -> Bool) -> Maybe (Text, Char) -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Maybe (Text, Char)
Text.unsnoc Text
baseUrl then Text
"" else Text
"/") Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
    [(Text, Text)] -> [Text] -> Text
registerQueryUrl [(Text, Text)]
params [Text]
query

-- cf. Web.Widget.Common
removeDates :: [Text] -> [Text]
removeDates :: [Text] -> [Text]
removeDates =
    (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Text
term_ ->
        Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Bool
Text.isPrefixOf Text
"date:" Text
term_ Bool -> Bool -> Bool
|| Text -> Text -> Bool
Text.isPrefixOf Text
"date2:" Text
term_)

-- | Drop the terms naming an account for the register. A link names
-- the account it is for, and the register reads only the first such term.
removeInacct :: [Text] -> [Text]
removeInacct :: [Text] -> [Text]
removeInacct =
    (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Text
term_ ->
        Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Bool
Text.isPrefixOf Text
"inacct:" Text
term_ Bool -> Bool -> Bool
|| Text -> Text -> Bool
Text.isPrefixOf Text
"inacctonly:" Text
term_)

-- | Give a cell a link and its title, unless the link is empty (no base url).
withLink :: Text -> Text -> Spr.Cell border text -> Spr.Cell border text
withLink :: forall border text.
Text -> Text -> Cell border text -> Cell border text
withLink Text
anchor Text
title Cell border text
cell
    | Text -> Bool
Text.null Text
anchor = Cell border text
cell
    | Bool
otherwise        = Cell border text
cell {Spr.cellAnchor = anchor, Spr.cellTitle = title}

-- | The title of a link from a figure to the register that derives it,
-- with a note when the report shows figures with the opposite sign.
amountPhrase :: LinkOpts -> Bool -> Text
amountPhrase :: LinkOpts -> Bool -> Text
amountPhrase LinkOpts
lo Bool
isTotal =
    (case (LinkOpts -> BalanceAccumulation
loAccum LinkOpts
lo, Bool
isTotal) of
        (BalanceAccumulation
Historical, Bool
False) -> Text
"Show the transactions behind this balance"
        (BalanceAccumulation
Historical, Bool
True)  -> Text
"Show the transactions behind this total"
        (BalanceAccumulation
_,          Bool
False) -> Text
"Show the transactions that make up this amount"
        (BalanceAccumulation
_,          Bool
True)  -> Text
"Show the transactions that make up this total")
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (if LinkOpts -> Bool
loNegated LinkOpts
lo then Text
", which the register shows with the opposite sign" else Text
"")

-- | A column heading for the period, linking to the register for it.
-- The link's date term is the span as a period expression, which can
-- differ from the heading text: a query reads the end date of
-- 2025-01-15..2025-02-14 as exclusive, and reads no week names.
headerDateSpanCell ::
    Bool -> PeriodTitles -> Maybe Text -> [Text] -> DateSpan -> Spr.Cell () Text
headerDateSpanCell :: Bool
-> PeriodTitles -> Maybe Text -> [Text] -> DateSpan -> Cell () Text
headerDateSpanCell Bool
date2 PeriodTitles
ph Maybe Text
base [Text]
query DateSpan
spn =
    Text -> Text -> Cell () Text -> Cell () Text
forall border text.
Text -> Text -> Cell border text -> Cell border text
withLink
        (Maybe Text -> [Text] -> Text
composeAnchor Maybe Text
base ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ Bool -> DateSpan -> [Text]
dateTerm Bool
date2 DateSpan
spn [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text] -> [Text]
removeDates ([Text] -> [Text]
removeInacct [Text]
query))
        Text
"Show the transactions in this period" (Cell () Text -> Cell () Text) -> Cell () Text -> Cell () Text
forall a b. (a -> b) -> a -> b
$
    Text -> Cell () Text
forall borders. Lines borders => Text -> Cell borders Text
headerCell (Text -> Cell () Text) -> Text -> Cell () Text
forall a b. (a -> b) -> a -> b
$ PeriodTitles -> DateSpan -> Text
renderPeriodHeading PeriodTitles
ph DateSpan
spn

-- | A cell showing a period in an account's row, linking to the
-- account's register for that period.
dateSpanCellWith ::
    (Spr.Lines border) =>
    LinkOpts -> PeriodTitles -> Maybe Text -> [Text] -> Text -> DateSpan -> Spr.Cell border Text
dateSpanCellWith :: forall border.
Lines border =>
LinkOpts
-> PeriodTitles
-> Maybe Text
-> [Text]
-> Text
-> DateSpan
-> Cell border Text
dateSpanCellWith LinkOpts
lo PeriodTitles
ph Maybe Text
base [Text]
query Text
acct DateSpan
spn =
    Text -> Text -> Cell border Text -> Cell border Text
forall border text.
Text -> Text -> Cell border text -> Cell border text
withLink
        ([(Text, Text)] -> Maybe Text -> [Text] -> Text
composeAnchorWith (Bool -> Text -> [(Text, Text)]
accountParams (LinkOpts -> Bool
loIncludesSubs LinkOpts
lo) Text
acct [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall a. [a] -> [a] -> [a]
++ LinkOpts -> [(Text, Text)]
linkParams LinkOpts
lo) Maybe Text
base ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
            Bool -> DateSpan -> [Text]
dateTerm (LinkOpts -> Bool
loDate2 LinkOpts
lo) (LinkOpts -> DateSpan -> DateSpan
linkSpan LinkOpts
lo DateSpan
spn) [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text] -> [Text]
removeDates ([Text] -> [Text]
removeInacct [Text]
query))
        Text
"Show transactions affecting this account in this period" (Cell border Text -> Cell border Text)
-> Cell border Text -> Cell border Text
forall a b. (a -> b) -> a -> b
$
    Text -> Cell border Text
forall border text. Lines border => text -> Cell border text
Spr.defaultCell (Text -> Cell border Text) -> Text -> Cell border Text
forall a b. (a -> b) -> a -> b
$ PeriodTitles -> DateSpan -> Text
renderPeriodHeading PeriodTitles
ph DateSpan
spn

dateSpanCell ::
    (Spr.Lines border) =>
    PeriodTitles -> Maybe Text -> [Text] -> Text -> DateSpan -> Spr.Cell border Text
dateSpanCell :: forall border.
Lines border =>
PeriodTitles
-> Maybe Text -> [Text] -> Text -> DateSpan -> Cell border Text
dateSpanCell = LinkOpts
-> PeriodTitles
-> Maybe Text
-> [Text]
-> Text
-> DateSpan
-> Cell border Text
forall border.
Lines border =>
LinkOpts
-> PeriodTitles
-> Maybe Text
-> [Text]
-> Text
-> DateSpan
-> Cell border Text
dateSpanCellWith LinkOpts
defaultLinkOpts

-- | A cell showing a date in an account's row, linking to the account's
-- register for that day.
dateCell ::
    (Spr.Lines border) =>
    Maybe Text -> [Text] -> Text -> Day -> Spr.Cell border Text
dateCell :: forall border.
Lines border =>
Maybe Text -> [Text] -> Text -> Day -> Cell border Text
dateCell Maybe Text
base [Text]
query Text
acct Day
d =
    Text -> Text -> Cell border Text -> Cell border Text
forall border text.
Text -> Text -> Cell border text -> Cell border text
withLink
        ([(Text, Text)] -> Maybe Text -> [Text] -> Text
composeAnchorWith (Bool -> Text -> [(Text, Text)]
accountParams Bool
True Text
acct) Maybe Text
base ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
            (Text
"date:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Day -> Text
showDate Day
d) Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text] -> [Text]
removeDates ([Text] -> [Text]
removeInacct [Text]
query))
        Text
"Show transactions affecting this account on this date" (Cell border Text -> Cell border Text)
-> Cell border Text -> Cell border Text
forall a b. (a -> b) -> a -> b
$
    Text -> Cell border Text
forall border text. Lines border => text -> Cell border text
Spr.defaultCell (Text -> Cell border Text) -> Text -> Cell border Text
forall a b. (a -> b) -> a -> b
$ Day -> Text
showDate Day
d

-- | A cell for a period in a totals row, linking to the register of
-- everything in the report's query, restricted by the given terms (a
-- section's account types, say), for that period.
totalDateSpanCell ::
    (Spr.Lines border) =>
    LinkOpts -> PeriodTitles -> Maybe Text -> [Text] -> [Text] -> DateSpan -> Spr.Cell border Text
totalDateSpanCell :: forall border.
Lines border =>
LinkOpts
-> PeriodTitles
-> Maybe Text
-> [Text]
-> [Text]
-> DateSpan
-> Cell border Text
totalDateSpanCell LinkOpts
lo PeriodTitles
ph Maybe Text
base [Text]
terms [Text]
query DateSpan
spn =
    Text -> Text -> Cell border Text -> Cell border Text
forall border text.
Text -> Text -> Cell border text -> Cell border text
withLink
        ([(Text, Text)] -> Maybe Text -> [Text] -> Text
composeAnchorWith (LinkOpts -> [(Text, Text)]
linkParams LinkOpts
lo) Maybe Text
base ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
            [Text]
terms [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ Bool -> DateSpan -> [Text]
dateTerm (LinkOpts -> Bool
loDate2 LinkOpts
lo) (LinkOpts -> DateSpan -> DateSpan
linkSpan LinkOpts
lo DateSpan
spn) [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text] -> [Text]
removeDates ([Text] -> [Text]
removeInacct [Text]
query))
        (LinkOpts -> Bool -> Text
amountPhrase LinkOpts
lo Bool
True) (Cell border Text -> Cell border Text)
-> Cell border Text -> Cell border Text
forall a b. (a -> b) -> a -> b
$
    Text -> Cell border Text
forall border text. Lines border => text -> Cell border text
Spr.defaultCell (Text -> Cell border Text) -> Text -> Cell border Text
forall a b. (a -> b) -> a -> b
$ PeriodTitles -> DateSpan -> Text
renderPeriodHeading PeriodTitles
ph DateSpan
spn

-- | Render a DateSpan as a period heading according to the requested style.
renderPeriodHeading :: PeriodTitles -> DateSpan -> Text
renderPeriodHeading :: PeriodTitles -> DateSpan -> Text
renderPeriodHeading PeriodTitles
PTDates   = DateSpan -> Text
showDateSpanFull
renderPeriodHeading PeriodTitles
PTCompact = DateSpan -> Text
showDateSpan

-- | Link an account's cell to the account's register, restricted to the
-- given query. The query says which dates; the link options say whether
-- subaccounts are included and how the register accumulates.
setAccountAnchorWith ::
    LinkOpts -> Maybe Text -> [Text] -> Text -> Spr.Cell border text -> Spr.Cell border text
setAccountAnchorWith :: forall border text.
LinkOpts
-> Maybe Text
-> [Text]
-> Text
-> Cell border text
-> Cell border text
setAccountAnchorWith LinkOpts
lo Maybe Text
base [Text]
query Text
acct =
    Text -> Text -> Cell border text -> Cell border text
forall border text.
Text -> Text -> Cell border text -> Cell border text
withLink
        ([(Text, Text)] -> Maybe Text -> [Text] -> Text
composeAnchorWith (Bool -> Text -> [(Text, Text)]
accountParams (LinkOpts -> Bool
loIncludesSubs LinkOpts
lo) Text
acct [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall a. [a] -> [a] -> [a]
++ LinkOpts -> [(Text, Text)]
linkParams LinkOpts
lo) Maybe Text
base ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
            [Text] -> [Text]
removeInacct [Text]
query)
        (if LinkOpts -> Bool
loIncludesSubs LinkOpts
lo
            then Text
"Show transactions affecting this account and subaccounts"
            else Text
"Show transactions affecting this account but not subaccounts")

setAccountAnchor ::
    Maybe Text -> [Text] -> Text -> Spr.Cell border text -> Spr.Cell border text
setAccountAnchor :: forall border text.
Maybe Text
-> [Text] -> Text -> Cell border text -> Cell border text
setAccountAnchor = LinkOpts
-> Maybe Text
-> [Text]
-> Text
-> Cell border text
-> Cell border text
forall border text.
LinkOpts
-> Maybe Text
-> [Text]
-> Text
-> Cell border text
-> Cell border text
setAccountAnchorWith LinkOpts
defaultLinkOpts