{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
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(..))
data LinkOpts = LinkOpts {
LinkOpts -> BalanceAccumulation
loAccum :: BalanceAccumulation,
LinkOpts -> DateSpan
loSpan :: DateSpan,
LinkOpts -> Bool
loDate2 :: Bool,
LinkOpts -> Bool
loIncludesSubs :: Bool,
LinkOpts -> Bool
loNegated :: Bool,
LinkOpts -> Maybe Text
loValue :: Maybe Text
}
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
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]]
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
<>)
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
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]
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 :: Maybe Text -> [Text] -> Text
composeAnchor :: Maybe Text -> [Text] -> Text
composeAnchor = [(Text, Text)] -> Maybe Text -> [Text] -> Text
composeAnchorWith []
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
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_)
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_)
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}
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
"")
headerDateSpanCell ::
Bool -> PeriodTitles -> Maybe Text -> [Text] -> DateSpan -> Spr.Cell () Text
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
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
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
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
renderPeriodHeading :: PeriodTitles -> DateSpan -> Text
renderPeriodHeading :: PeriodTitles -> DateSpan -> Text
renderPeriodHeading PeriodTitles
PTDates = DateSpan -> Text
showDateSpanFull
renderPeriodHeading PeriodTitles
PTCompact = DateSpan -> Text
showDateSpan
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