hledger-web-2.0: Hledger/Web/Handler/JournalR.hs
-- | /journal handlers.
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Hledger.Web.Handler.JournalR where
import Data.Text qualified as T
import Hledger.Utils.I18n (tr, trf)
import Hledger
import Hledger.Cli.CliOptions
import Hledger.Web.Import
import Hledger.Web.Paging
import Hledger.Web.WebOptions
import Hledger.Web.Widget.AddForm (addModal)
import Hledger.Web.Param (lookupParam, paramLinks, paramSettings)
import Hledger.Web.ReportPage (amountModeOf, calcParam, deepestDepth, depthParams, intervalLinks, listParam, optionsSummary, parseInterval, parseValue, removeAttributes, setAmountMode, sortParam, valueParams)
import Hledger.Web.Widget.Common
(accountParams, amountLinks, columnsLinks, commodityLinks,
controlsHint, controlsToggle, searchHeading,
depthLinks, inertControls, lotsLinks,
mixedAmountAsHtml, percentLinks, pricesLinks, realLinks,
statusLinks, typeLinks,
transactionFragment, journalDayQuery, zeroBalanceLinks)
-- | The formatted journal view, with sidebar.
getJournalR :: Handler Html
getJournalR = do
checkServerSideUiEnabled
VD{perms, j, jHasLots, lotsShown, q, opts, qparam, account, today, trs, hideEmpty, controlsOpen, commoditiesOpen} <- getViewData
require ViewPermission
mmode <- either (const Nothing) id . parseValue <$> lookupGetParam "value"
mlist <- lookupParam listParam
inferOn <- (== Just "1") <$> lookupGetParam "infer"
minterval <- lookupGetParam "interval"
mhinterval <- lookupGetParam "hinterval"
totalOn <- (== Just "1") <$> lookupGetParam "total"
avgOn <- (== Just "1") <$> lookupGetParam "avg"
sortAmt <- fromMaybe False <$> lookupParam sortParam
pctOn <- (== Just "1") <$> lookupGetParam "pct"
calcCur <- fromMaybe CalcChange <$> lookupParam calcParam
date2On <- (== Just "1") <$> lookupGetParam "date2"
pagereq <- pageRequest
let title = case account of
Nothing -> tr trs "General Journal"
Just (a, True) -> trf trs "Transactions in {account}" [("account", a)]
Just (a, False) -> trf trs "Transactions in {account} (excluding subaccounts)" [("account", a)]
title' = searchHeading trs (date2_ (_rsReportOpts (reportspec_ (cliopts_ opts))) || date2On) q title
-- An account's register, opened on the page holding this transaction.
acctlink a t = (RegisterR, [("q", qparam) | not (T.null qparam)] ++ accountParams True a ++ ("txn", T.pack $ show $ tindex t) : carried)
q' = filterQuery (not . queryIsDepth) q
rspec = (reportspec_ $ cliopts_ opts){_rsQuery = q'}
-- The matching transactions, newest first; this page shows one page of them.
alltxns = reverse $
styleAmounts (journalCommodityStylesWith HardRounding j) $
entriesReport rspec j
(page, items) = pageOf pagereq tindex alltxns
-- The years the search matches in, ignoring any date term in it.
years = map tdate $
maybe alltxns (\dq -> entriesReport rspec{_rsQuery = filterQuery (not . queryIsDepth) dq} j) $
datelessQuery today qparam
transactionFrag = transactionFragment j
-- Links staying on this page keep the states that shape the
-- sidebar: zero balances, amounts, the accounts mode, and
-- inferred prices. The journal's entries always show as written.
roptsStartup = _rsReportOpts $ reportspec_ $ cliopts_ opts
dfltHidden = empty_ roptsStartup
emptyParams = [("empty", if hideEmpty then "0" else "1") | hideEmpty /= dfltHidden]
valParams = valueParams roptsStartup mmode
elist = fromMaybe (accountlistmode_ roptsStartup) mlist
listParams = paramSettings listParam (accountlistmode_ roptsStartup) elist
inferParams = [("infer", "1") | inferOn]
-- The report-shaping state shapes nothing here, but it is passed
-- along and shown dimmed below, so the startup options' url
-- survives this landing page and the url's state is never
-- invisible.
stickyParams =
[("interval", w) | Just w <- [mfilter (not . T.null) minterval]] ++
[("hinterval", w) | Just w <- [mfilter (not . T.null) mhinterval]] ++
[("total", "1") | totalOn] ++
[("avg", "1") | avgOn] ++
paramSettings sortParam False sortAmt ++
[("pct", "1") | pctOn] ++
paramSettings calcParam CalcChange calcCur ++
[("date2", "1") | date2On]
lotsParams = [("lots", "1") | lotsShown]
carried = dbg1 "journal carried params" $ stickyParams ++ valParams ++ listParams ++ inferParams ++ emptyParams ++ lotsParams ++ depthParams q
qps = [("q", qparam) | not (T.null qparam)]
-- An entry's date link: the journal on that date, same modes.
dayParams t = ("q", journalDayQuery qparam (tdate t)) : carried
-- the rows of links above the entries: the filters (status,
-- realness, type, tag, commodity, and depth), and the sidebar's
-- accounts and amounts modes
statusRow = statusLinks trs JournalR carried qparam
realRow = realLinks trs JournalR carried qparam
typeRow = typeLinks trs JournalR carried qparam j
-- TRANSLATORS: the hint below the account type filter row.
typeHintRow = controlsHint $ tr trs "Shift-click selects several types."
commodityRow = commodityLinks trs JournalR carried qparam commoditiesOpen
-- TRANSLATORS: the hint below the commodity filter row.
(tr trs "Shift-click selects several commodities.") j
-- The depth and the accounts mode take effect on the report pages
-- (the sidebar has a depth of its own, and is always a tree), so
-- they are shown dimmed, carried there.
deepest = deepestDepth j
depthRow = inertControls trs $ depthLinks trs JournalR carried qparam deepest
listRow = inertControls trs $ paramLinks listParam trs JournalR (qps ++ carried) (accountlistmode_ roptsStartup) elist
-- The amounts mode converts the sidebar's amounts; the journal's
-- entries always show as written.
amountsRow = amountLinks trs JournalR (qps ++ removeAttributes ["value"] carried)
(amountModeOf roptsStartup) (amountModeOf $ maybe id setAmountMode mmode roptsStartup)
pricesRow = pricesLinks trs JournalR (qps ++ removeAttributes ["infer"] carried) inferOn
zeroRow = zeroBalanceLinks trs JournalR (qps ++ removeAttributes ["empty"] carried) dfltHidden hideEmpty
-- Lot detail shows in the entries and the sidebar; row hidden
-- when the journal has no lots.
lotsRow = if jHasLots
then lotsLinks trs JournalR (qps ++ removeAttributes ["lots"] carried) lotsShown
else const mempty
-- The report-shaping controls, operative on the report pages,
-- shown dimmed with the url's state marked.
rpInterval = maybe NoInterval (either (const NoInterval) id . parseInterval) minterval
-- The years row; its links rewrite the search's date narrowing themselves.
yearsNav = yearsRow trs JournalR qparam today carried years
intervalRow = inertControls trs $
intervalLinks trs JournalR (removeAttributes ["interval"] carried) qparam rpInterval
inertRows = inertControls trs $ mconcat
[ columnsLinks trs JournalR (qps ++ removeAttributes ["total", "avg"] carried) totalOn avgOn
, paramLinks sortParam trs JournalR (qps ++ carried) False sortAmt
, percentLinks trs JournalR (qps ++ removeAttributes ["pct"] carried) pctOn
, paramLinks calcParam trs JournalR (qps ++ carried) CalcChange calcCur
]
-- All of them behind the controls disclosure, closed by default,
-- in the same order as on the report pages.
controlsRows = controlsToggle trs controlsOpen (optionsSummary trs deepest carried) $ mconcat $
[intervalRow, yearsNav, statusRow, realRow, typeRow, typeHintRow, commodityRow] ++ [depthRow | deepest > 1] ++
[listRow, amountsRow, pricesRow, zeroRow, lotsRow, inertRows]
defaultLayout $ do
-- TRANSLATORS: the browser tab title of this page.
setTitleI (HMsg "journal - hledger-web")
$(widgetFile "journal")