{-# LANGUAGE OverloadedStrings, QuasiQuotes, RecordWildCards #-} -- | /register handlers. module Handler.RegisterR where import Import import Data.Time import Data.List (intersperse) import Data.Maybe (fromMaybe, isJust) import qualified Data.Text as T import Safe (headMay) import Handler.AddForm (postAddForm) import Handler.Common import Hledger.Data import Hledger.Query import Hledger.Reports import Hledger.Utils import Hledger.Cli.CliOptions import Hledger.Web.WebOptions -- | The main journal/account register view, with accounts sidebar. getRegisterR :: Handler Html getRegisterR = do vd@VD{..} <- getViewData let title = a <> s1 <> s2 where (a,inclsubs) = fromMaybe ("all accounts",True) $ inAccount qopts s1 = if inclsubs then "" else " (excluding subaccounts)" s2 = if m /= Any then ", filtered" else "" hledgerLayout vd "register" $ do _ <- [hamlet|

#{title}|] registerReportHtml opts vd $ accountTransactionsReport (reportopts_ $ cliopts_ opts) j m $ fromMaybe Any $ inAccountQuery qopts postRegisterR :: Handler Html postRegisterR = postAddForm -- | Generate html for an account register, including a balance chart and transaction list. registerReportHtml :: WebOpts -> ViewData -> TransactionsReport -> HtmlUrl AppRoute registerReportHtml opts vd r = [hamlet|
^{registerChartHtml $ transactionsReportByCommodity r} ^{registerItemsHtml opts vd r} |] -- | Generate html for a transaction list from an "TransactionsReport". registerItemsHtml :: WebOpts -> ViewData -> TransactionsReport -> HtmlUrl AppRoute registerItemsHtml _ vd (balancelabel,items) = [hamlet|
Date Description To/From Account(s) Amount Out/In #{balancelabel'} $forall i <- numberTransactionsReportItems items ^{itemAsHtml vd i} |] where insomeacct = isJust $ inAccount $ qopts vd balancelabel' = if insomeacct then balancelabel else "Total" itemAsHtml :: ViewData -> (Int, Bool, Bool, Bool, TransactionsReportItem) -> HtmlUrl AppRoute itemAsHtml VD{..} (n, newd, newm, _, (torig, tacct, split, acct, amt, bal)) = [hamlet| #{date} #{textElideRight 30 desc} #{elideRight 40 acct} $if showamt \#{mixedAmountAsHtml amt} #{mixedAmountAsHtml bal} |] where evenodd = if even n then "even" else "odd" :: Text datetransition | newm = "newmonth" | newd = "newday" | otherwise = "" :: Text (firstposting, date, desc) = (False, show $ tdate tacct, tdescription tacct) showamt = not split || not (isZeroMixedAmount amt) -- | Generate javascript/html for a register balance line chart based on -- the provided "TransactionsReportItem"s. registerChartHtml :: [(CommoditySymbol, (String, [TransactionsReportItem]))] -> HtmlUrl AppRoute registerChartHtml percommoditytxnreports = -- have to make sure plot is not called when our container (maincontent) -- is hidden, eg with add form toggled [hamlet|