-- | /journal handlers.

{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}

module Hledger.Web.Handler.JournalR where

import Hledger
import Hledger.Cli.CliOptions
import Hledger.Web.Import
import Hledger.Web.WebOptions
import Hledger.Web.Widget.AddForm (addModal)
import Hledger.Web.Widget.Common
            (accountQuery, mixedAmountAsHtml,
             transactionFragment, replaceInacct)

-- | The formatted journal view, with sidebar.
getJournalR :: Handler Html
getJournalR :: Handler Html
getJournalR = do
  Handler ()
checkServerSideUiEnabled
  VD{perms, j, q, opts, qparam, qopts, today} <- Handler ViewData
getViewData
  require ViewPermission
  let title = case [QueryOpt] -> Maybe (AccountName, Bool)
inAccount [QueryOpt]
qopts of
        Maybe (AccountName, Bool)
Nothing -> AccountName
"General Journal"
        Just (AccountName
a, Bool
inclsubs) -> AccountName
"Transactions in " AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
a AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> if Bool
inclsubs then AccountName
"" else AccountName
" (excluding subaccounts)"
      title' = AccountName
title AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> if Query
q Query -> Query -> Bool
forall a. Eq a => a -> a -> Bool
/= Query
Any then AccountName
", filtered" else AccountName
""
      acctlink AccountName
a = (Route App
RegisterR, [(a
"q", AccountName -> AccountName -> AccountName
replaceInacct AccountName
qparam (AccountName -> AccountName) -> AccountName -> AccountName
forall a b. (a -> b) -> a -> b
$ AccountName -> AccountName
accountQuery AccountName
a)])
      rspec = (CliOpts -> ReportSpec
reportspec_ (CliOpts -> ReportSpec) -> CliOpts -> ReportSpec
forall a b. (a -> b) -> a -> b
$ WebOpts -> CliOpts
cliopts_ WebOpts
opts){_rsQuery = filterQuery (not . queryIsDepth) q}
      items = [EntriesReportItem] -> [EntriesReportItem]
forall a. [a] -> [a]
reverse ([EntriesReportItem] -> [EntriesReportItem])
-> [EntriesReportItem] -> [EntriesReportItem]
forall a b. (a -> b) -> a -> b
$
        Map AccountName AmountStyle
-> [EntriesReportItem] -> [EntriesReportItem]
forall a. HasAmounts a => Map AccountName AmountStyle -> a -> a
styleAmounts (Rounding -> Journal -> Map AccountName AmountStyle
journalCommodityStylesWith Rounding
HardRounding Journal
j) ([EntriesReportItem] -> [EntriesReportItem])
-> [EntriesReportItem] -> [EntriesReportItem]
forall a b. (a -> b) -> a -> b
$
        ReportSpec -> Journal -> [EntriesReportItem]
entriesReport ReportSpec
rspec Journal
j
      transactionFrag = Journal -> EntriesReportItem -> String
transactionFragment Journal
j

  defaultLayout $ do
    setTitle "journal - hledger-web"
    $(widgetFile "journal")