module Handler.JournalR where
import Import
import Handler.Common
import Handler.Post
import Hledger.Data
import Hledger.Query
import Hledger.Reports
import Hledger.Utils
import Hledger.Cli.Options
import Hledger.Web.Options
getJournalR :: Handler Html
getJournalR = do
vd@VD{..} <- getViewData
let
inacct = inAccount qopts
filtering = m /= Any
title = case inacct of
Nothing -> "General Journal"++s2
Just (a,inclsubs) -> "Transactions in "++a++s1++s2
where s1 = if inclsubs then "" else " (excluding subaccounts)"
where
s2 = if filtering then ", filtered" else ""
maincontent = journalTransactionsReportAsHtml opts vd $ journalTransactionsReport (reportopts_ $ cliopts_ opts) j m
hledgerLayout vd "journal" [hamlet|
<h2#contenttitle>#{title}
<!-- p>Journal entries record movements of commodities between accounts. -->
<a#addformlink role="button" style="cursor:pointer;" datatoggle="modal" datatarget="#addmodal" title="Add a new transaction to the journal" style="margin-top:1em;">Add a transaction
^{maincontent}
|]
postJournalR :: Handler Html
postJournalR = handlePost
journalTransactionsReportAsHtml :: WebOpts -> ViewData -> TransactionsReport -> HtmlUrl AppRoute
journalTransactionsReportAsHtml _ vd (_,items) = [hamlet|
<table.transactionsreport>
<tr.headings>
<th.date style="text-align:left;">
Date
<span .glyphicon .glyphiconchevronup>
<th.description style="text-align:left;">Description
<th.account style="text-align:left;">Account
<th.amount style="text-align:right;">Amount
$forall i <- numberTransactionsReportItems items
^{itemAsHtml vd i}
|]
where
itemAsHtml :: ViewData -> (Int, Bool, Bool, Bool, TransactionsReportItem) -> HtmlUrl AppRoute
itemAsHtml VD{..} (n, _, _, _, (t, _, split, _, amt, _)) = [hamlet|
<tr ##{date} .item.#{evenodd}.#{firstposting} style="vertical-align:top;" title="#{show t}">
<td.date>#{date}
<td.description colspan=2>#{elideRight 60 desc}
<td.amount style="text-align:right;">
$if showamt
\#{mixedAmountAsHtml amt}
$forall p' <- tpostings t
<tr .item.#{evenodd}.posting title="#{show t}">
<td.date>
<td.description>
<td.account>
<a href="/register?q=inacct:'#{paccount p'}'##{date}" title="#{paccount p'}">#{elideAccountName 40 $ paccount p'}
<td.amount style="text-align:right;">#{mixedAmountAsHtml $ pamount p'}
<tr.#{evenodd}>
<td>
<td>
<td>
<td>
|]
where
evenodd = if even n then "even" else "odd" :: String
(firstposting, date, desc) = (False, show $ tdate t, tdescription t)
showamt = not split || not (isZeroMixedAmount amt)