web: account transactions report, register clarifications
Clarify the semantics and code of account transactions report a bit. In the web account register view, emphasise the "historical balance" vs "running total" distinction; show it as a label for the chart as well, to reduce confusion.
This commit is contained in:
parent
b3da48aaa0
commit
441cae645f
@ -24,13 +24,13 @@ module Hledger.Reports.TransactionsReports (
|
|||||||
where
|
where
|
||||||
|
|
||||||
import Data.List
|
import Data.List
|
||||||
import Data.Maybe
|
|
||||||
import Data.Ord
|
import Data.Ord
|
||||||
-- import Test.HUnit
|
-- import Test.HUnit
|
||||||
|
|
||||||
import Hledger.Data
|
import Hledger.Data
|
||||||
import Hledger.Query
|
import Hledger.Query
|
||||||
import Hledger.Reports.ReportOptions
|
import Hledger.Reports.ReportOptions
|
||||||
|
import Hledger.Utils
|
||||||
|
|
||||||
|
|
||||||
-- | A transactions report includes a list of transactions
|
-- | A transactions report includes a list of transactions
|
||||||
@ -43,8 +43,8 @@ import Hledger.Reports.ReportOptions
|
|||||||
type TransactionsReport = (String -- label for the balance column, eg "balance" or "total"
|
type TransactionsReport = (String -- label for the balance column, eg "balance" or "total"
|
||||||
,[TransactionsReportItem] -- line items, one per transaction
|
,[TransactionsReportItem] -- line items, one per transaction
|
||||||
)
|
)
|
||||||
type TransactionsReportItem = (Transaction -- the corresponding transaction
|
type TransactionsReportItem = (Transaction -- the original journal transaction, unmodified
|
||||||
,Transaction -- the transaction with postings to the current account(s) removed
|
,Transaction -- the transaction as seen from a particular account
|
||||||
,Bool -- is this a split, ie more than one other account posting
|
,Bool -- is this a split, ie more than one other account posting
|
||||||
,String -- a display string describing the other account(s), if any
|
,String -- a display string describing the other account(s), if any
|
||||||
,MixedAmount -- the amount posted to the current account(s) (or total amount posted)
|
,MixedAmount -- the amount posted to the current account(s) (or total amount posted)
|
||||||
@ -67,36 +67,56 @@ journalTransactionsReport :: ReportOpts -> Journal -> Query -> TransactionsRepor
|
|||||||
journalTransactionsReport opts j q = (totallabel, items)
|
journalTransactionsReport opts j q = (totallabel, items)
|
||||||
where
|
where
|
||||||
-- XXX items' first element should be the full transaction with all postings
|
-- XXX items' first element should be the full transaction with all postings
|
||||||
items = reverse $ accountTransactionsReportItems q Nothing nullmixedamt id ts
|
items = reverse $ accountTransactionsReportItems q None nullmixedamt id ts
|
||||||
ts = sortBy (comparing date) $ filter (q `matchesTransaction`) $ jtxns $ journalSelectingAmountFromOpts opts j
|
ts = sortBy (comparing date) $ filter (q `matchesTransaction`) $ jtxns $ journalSelectingAmountFromOpts opts j
|
||||||
date = transactionDateFn opts
|
date = transactionDateFn opts
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- | Select transactions within one or more current accounts, and make a
|
-- | An account transactions report represents transactions affecting
|
||||||
-- transactions report relative to those account(s). This means:
|
-- a particular account (or possibly several accounts, but we don't
|
||||||
|
-- use that). It is used by hledger-web's account register view, where
|
||||||
|
-- we want to show one row per journal transaction, with:
|
||||||
--
|
--
|
||||||
-- 1. it shows transactions from the point of view of the current account(s).
|
-- - the total increase/decrease to the current account
|
||||||
-- The transaction amount is the amount posted to the current account(s).
|
|
||||||
-- The other accounts' names are provided.
|
|
||||||
--
|
--
|
||||||
-- 2. With no transaction filtering in effect other than a start date, it
|
-- - the names of the other account(s) posted to/from
|
||||||
-- shows the accurate historical running balance for the current account(s).
|
|
||||||
-- Otherwise it shows a running total starting at 0.
|
|
||||||
--
|
--
|
||||||
-- This is used by eg hledger-web's account register view. Currently,
|
-- - transaction dates adjusted to the date of the earliest posting to
|
||||||
-- reporting intervals are not supported, and report items are most
|
-- the current account, if those postings have their own dates
|
||||||
-- recent first.
|
--
|
||||||
accountTransactionsReport :: ReportOpts -> Journal -> Query -> Query -> TransactionsReport
|
-- Currently, reporting intervals are not supported, and report items
|
||||||
|
-- are most recent first.
|
||||||
|
--
|
||||||
|
type AccountTransactionsReport =
|
||||||
|
(String -- label for the balance column, eg "balance" or "total"
|
||||||
|
,[AccountTransactionsReportItem] -- line items, one per transaction
|
||||||
|
)
|
||||||
|
|
||||||
|
type AccountTransactionsReportItem =
|
||||||
|
(
|
||||||
|
Transaction -- the original journal transaction
|
||||||
|
,Transaction -- the adjusted account transaction
|
||||||
|
,Bool -- is this a split, ie with more than one posting to other account(s)
|
||||||
|
,String -- a display string describing the other account(s), if any
|
||||||
|
,MixedAmount -- the amount posted to the current account(s) (or total amount posted)
|
||||||
|
,MixedAmount -- the running balance for the current account(s) after this transaction
|
||||||
|
)
|
||||||
|
|
||||||
|
accountTransactionsReport :: ReportOpts -> Journal -> Query -> Query -> AccountTransactionsReport
|
||||||
accountTransactionsReport opts j q thisacctquery = (label, items)
|
accountTransactionsReport opts j q thisacctquery = (label, items)
|
||||||
where
|
where
|
||||||
-- transactions affecting this account, in date order
|
-- transactions with excluded currencies removed
|
||||||
curq = filterQuery queryIsSym q
|
ts1 = jtxns $
|
||||||
ts = sortBy (comparing tdate) $
|
filterJournalAmounts (filterQuery queryIsSym q) $
|
||||||
filter (matchesTransaction thisacctquery) $
|
|
||||||
jtxns $
|
|
||||||
filterJournalAmounts curq $
|
|
||||||
journalSelectingAmountFromOpts opts j
|
journalSelectingAmountFromOpts opts j
|
||||||
|
-- affecting this account
|
||||||
|
ts2 = filter (matchesTransaction thisacctquery) ts1
|
||||||
|
-- with dates adjusted for account transactions report
|
||||||
|
ts3 = map (setTransactionDateToPostingDate q thisacctquery) ts2
|
||||||
|
-- and sorted
|
||||||
|
ts = sortBy (comparing tdate) ts3
|
||||||
|
|
||||||
-- starting balance: if we are filtering by a start date and nothing else,
|
-- starting balance: if we are filtering by a start date and nothing else,
|
||||||
-- the sum of postings to this account before that date; otherwise zero.
|
-- the sum of postings to this account before that date; otherwise zero.
|
||||||
(startbal,label) | queryIsNull q = (nullmixedamt, balancelabel)
|
(startbal,label) | queryIsNull q = (nullmixedamt, balancelabel)
|
||||||
@ -110,33 +130,54 @@ accountTransactionsReport opts j q thisacctquery = (label, items)
|
|||||||
$ transactionsPostings ts
|
$ transactionsPostings ts
|
||||||
tostartdatequery = Date (DateSpan Nothing startdate)
|
tostartdatequery = Date (DateSpan Nothing startdate)
|
||||||
startdate = queryStartDate (date2_ opts) q
|
startdate = queryStartDate (date2_ opts) q
|
||||||
items = reverse $ accountTransactionsReportItems q (Just thisacctquery) startbal negate ts
|
|
||||||
|
|
||||||
totallabel = "Total"
|
items = reverse $ -- see also registerChartHtml
|
||||||
balancelabel = "Balance"
|
accountTransactionsReportItems q thisacctquery startbal negate ts
|
||||||
|
|
||||||
|
-- | Adjust a transaction's date to the earliest date of postings to a
|
||||||
|
-- particular account, if any, after filtering with a certain query.
|
||||||
|
setTransactionDateToPostingDate :: Query -> Query -> Transaction -> Transaction
|
||||||
|
setTransactionDateToPostingDate query thisacctquery t = t'
|
||||||
|
where
|
||||||
|
queryps = tpostings $ filterTransactionPostings query t
|
||||||
|
thisacctps = filter (matchesPosting thisacctquery) queryps
|
||||||
|
t' = case thisacctps of
|
||||||
|
[] -> t
|
||||||
|
_ -> t{tdate=d}
|
||||||
|
where
|
||||||
|
d | null ds = tdate t
|
||||||
|
| otherwise = minimum ds
|
||||||
|
ds = map postingDate thisacctps
|
||||||
|
-- no opts here, don't even bother with that date/date2 rigmarole
|
||||||
|
|
||||||
|
totallabel = "Running Total"
|
||||||
|
balancelabel = "Historical Balance"
|
||||||
|
|
||||||
-- | Generate transactions report items from a list of transactions,
|
-- | Generate transactions report items from a list of transactions,
|
||||||
-- using the provided query and current account queries, starting balance,
|
-- using the provided query and current account queries, starting
|
||||||
-- sign-setting function and balance-summing function.
|
-- balance, sign-setting function and balance-summing function. With a
|
||||||
accountTransactionsReportItems :: Query -> Maybe Query -> MixedAmount -> (MixedAmount -> MixedAmount) -> [Transaction] -> [TransactionsReportItem]
|
-- "this account" query of None, this can be used the for the
|
||||||
|
-- journalTransactionsReport also.
|
||||||
|
accountTransactionsReportItems :: Query -> Query -> MixedAmount -> (MixedAmount -> MixedAmount) -> [Transaction] -> [TransactionsReportItem]
|
||||||
accountTransactionsReportItems _ _ _ _ [] = []
|
accountTransactionsReportItems _ _ _ _ [] = []
|
||||||
accountTransactionsReportItems query thisacctquery bal signfn (t:ts) =
|
accountTransactionsReportItems query thisacctquery bal signfn (torig:ts) =
|
||||||
-- This is used for both accountTransactionsReport and journalTransactionsReport,
|
-- This is used for both accountTransactionsReport and journalTransactionsReport,
|
||||||
-- which makes it a bit overcomplicated
|
-- which makes it a bit overcomplicated
|
||||||
case i of Just i' -> i':is
|
case i of Just i' -> i':is
|
||||||
Nothing -> is
|
Nothing -> is
|
||||||
where
|
where
|
||||||
tmatched@Transaction{tpostings=psmatched} = filterTransactionPostings query t
|
tacct@Transaction{tpostings=queryps} = dbg0 "tacct" $ filterTransactionPostings query torig
|
||||||
(psthisacct,psotheracct) = case thisacctquery of Just m -> partition (matchesPosting m) psmatched
|
(thisacctps, otheracctps) = -- partition (matchesPosting thisacctquery) queryps
|
||||||
Nothing -> ([],psmatched)
|
case thisacctquery of None -> ([],queryps)
|
||||||
numotheraccts = length $ nub $ map paccount psotheracct
|
q -> partition (matchesPosting q) queryps
|
||||||
amt = negate $ sum $ map pamount psthisacct
|
amt = negate $ sum $ map pamount thisacctps
|
||||||
acct | isNothing thisacctquery = summarisePostingAccounts psmatched
|
numotheraccts = length $ nub $ map paccount otheracctps
|
||||||
| numotheraccts == 0 = summarisePostingAccounts psthisacct
|
otheracctstr | thisacctquery == None = summarisePostingAccounts queryps
|
||||||
| otherwise = summarisePostingAccounts psotheracct
|
| numotheraccts == 0 = summarisePostingAccounts thisacctps
|
||||||
(i,bal') = case psmatched of
|
| otherwise = summarisePostingAccounts otheracctps
|
||||||
|
(i,bal') = case queryps of
|
||||||
[] -> (Nothing,bal)
|
[] -> (Nothing,bal)
|
||||||
_ -> (Just (t, tmatched, numotheraccts > 1, acct, a, b), b)
|
_ -> (Just (torig, tacct, numotheraccts > 1, otheracctstr, a, b), b)
|
||||||
where
|
where
|
||||||
a = signfn amt
|
a = signfn amt
|
||||||
b = bal + a
|
b = bal + a
|
||||||
|
|||||||
@ -5,6 +5,7 @@ module Handler.RegisterR where
|
|||||||
import Import
|
import Import
|
||||||
|
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
import Safe
|
||||||
|
|
||||||
import Handler.Common
|
import Handler.Common
|
||||||
import Handler.Post
|
import Handler.Post
|
||||||
@ -43,7 +44,7 @@ postRegisterR = handlePost
|
|||||||
-- Generate html for an account register, including a balance chart and transaction list.
|
-- Generate html for an account register, including a balance chart and transaction list.
|
||||||
registerReportHtml :: WebOpts -> ViewData -> TransactionsReport -> HtmlUrl AppRoute
|
registerReportHtml :: WebOpts -> ViewData -> TransactionsReport -> HtmlUrl AppRoute
|
||||||
registerReportHtml opts vd r = [hamlet|
|
registerReportHtml opts vd r = [hamlet|
|
||||||
^{registerChartHtml $ map snd $ transactionsReportByCommodity r}
|
^{registerChartHtml $ transactionsReportByCommodity r}
|
||||||
^{registerItemsHtml opts vd r}
|
^{registerItemsHtml opts vd r}
|
||||||
|]
|
|]
|
||||||
|
|
||||||
@ -56,7 +57,7 @@ registerItemsHtml _ vd (balancelabel,items) = [hamlet|
|
|||||||
Date
|
Date
|
||||||
<span .glyphicon .glyphicon-chevron-up>
|
<span .glyphicon .glyphicon-chevron-up>
|
||||||
<th.description style="text-align:left;">Description
|
<th.description style="text-align:left;">Description
|
||||||
<th.account style="text-align:left;">To/From Account
|
<th.account style="text-align:left;">To/From Account(s)
|
||||||
<th.amount style="text-align:right;">Amount Out/In
|
<th.amount style="text-align:right;">Amount Out/In
|
||||||
<th.balance style="text-align:right;">#{balancelabel'}
|
<th.balance style="text-align:right;">#{balancelabel'}
|
||||||
$forall i <- numberTransactionsReportItems items
|
$forall i <- numberTransactionsReportItems items
|
||||||
@ -68,11 +69,11 @@ registerItemsHtml _ vd (balancelabel,items) = [hamlet|
|
|||||||
|
|
||||||
-- filtering = m /= Any
|
-- filtering = m /= Any
|
||||||
itemAsHtml :: ViewData -> (Int, Bool, Bool, Bool, TransactionsReportItem) -> HtmlUrl AppRoute
|
itemAsHtml :: ViewData -> (Int, Bool, Bool, Bool, TransactionsReportItem) -> HtmlUrl AppRoute
|
||||||
itemAsHtml VD{..} (n, newd, newm, _, (t, _, split, acct, amt, bal)) = [hamlet|
|
itemAsHtml VD{..} (n, newd, newm, _, (torig, tacct, split, acct, amt, bal)) = [hamlet|
|
||||||
|
|
||||||
<tr ##{date} .item.#{evenodd}.#{firstposting}.#{datetransition} title="#{show t}" style="vertical-align:top;">
|
<tr ##{date} .item.#{evenodd}.#{firstposting}.#{datetransition} title="#{show torig}" style="vertical-align:top;">
|
||||||
<td.date><a href="/journal##{date}">#{date}
|
<td.date><a href="/journal##{date}">#{date}
|
||||||
<td.description title="#{show t}">#{elideRight 30 desc}
|
<td.description title="#{show torig}">#{elideRight 30 desc}
|
||||||
<td.account>#{elideRight 40 acct}
|
<td.account>#{elideRight 40 acct}
|
||||||
<td.amount style="text-align:right; white-space:nowrap;">
|
<td.amount style="text-align:right; white-space:nowrap;">
|
||||||
$if showamt
|
$if showamt
|
||||||
@ -85,7 +86,7 @@ registerItemsHtml _ vd (balancelabel,items) = [hamlet|
|
|||||||
datetransition | newm = "newmonth"
|
datetransition | newm = "newmonth"
|
||||||
| newd = "newday"
|
| newd = "newday"
|
||||||
| otherwise = "" :: String
|
| otherwise = "" :: String
|
||||||
(firstposting, date, desc) = (False, show $ tdate t, tdescription t)
|
(firstposting, date, desc) = (False, show $ tdate tacct, tdescription tacct)
|
||||||
-- acctquery = (here, [("q", pack $ accountQuery acct)])
|
-- acctquery = (here, [("q", pack $ accountQuery acct)])
|
||||||
showamt = not split || not (isZeroMixedAmount amt)
|
showamt = not split || not (isZeroMixedAmount amt)
|
||||||
|
|
||||||
@ -95,20 +96,22 @@ registerItemsHtml _ vd (balancelabel,items) = [hamlet|
|
|||||||
-- Data.Foldable.Foldable t1 =>
|
-- Data.Foldable.Foldable t1 =>
|
||||||
-- t1 (Transaction, t2, t3, t4, t5, MixedAmount)
|
-- t1 (Transaction, t2, t3, t4, t5, MixedAmount)
|
||||||
-- -> t -> Text.Blaze.Internal.HtmlM ()
|
-- -> t -> Text.Blaze.Internal.HtmlM ()
|
||||||
registerChartHtml :: [[TransactionsReportItem]] -> HtmlUrl AppRoute
|
registerChartHtml :: [(String, [TransactionsReportItem])] -> HtmlUrl AppRoute
|
||||||
registerChartHtml itemss =
|
registerChartHtml percommoditytxnreports =
|
||||||
-- have to make sure plot is not called when our container (maincontent)
|
-- have to make sure plot is not called when our container (maincontent)
|
||||||
-- is hidden, eg with add form toggled
|
-- is hidden, eg with add form toggled
|
||||||
[hamlet|
|
[hamlet|
|
||||||
<div#register-chart style="width:600px;height:100px; margin-bottom:1em;">
|
<label#register-chart-label style="float:left;margin-right:1em;">
|
||||||
|
<div#register-chart style="width:600px;height:100px; margin-bottom:1em;float:left;">
|
||||||
<script type=text/javascript>
|
<script type=text/javascript>
|
||||||
\$(document).ready(function() {
|
\$(document).ready(function() {
|
||||||
/* render chart with flot, if visible */
|
/* render chart with flot, if visible */
|
||||||
var chartdiv = $('#register-chart');
|
var chartdiv = $('#register-chart');
|
||||||
if (chartdiv.is(':visible'))
|
if (chartdiv.is(':visible'))
|
||||||
|
\$('#register-chart-label').text('#{label}');
|
||||||
\$.plot(chartdiv,
|
\$.plot(chartdiv,
|
||||||
[
|
[
|
||||||
$forall items <- itemss
|
$forall (_,items) <- percommoditytxnreports
|
||||||
[
|
[
|
||||||
$forall i <- reverse items
|
$forall i <- reverse items
|
||||||
[#{dayToJsTimestamp $ triDate i}, #{triSimpleBalance i}],
|
[#{dayToJsTimestamp $ triDate i}, #{triSimpleBalance i}],
|
||||||
@ -125,4 +128,8 @@ registerChartHtml itemss =
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|]
|
|]
|
||||||
|
where
|
||||||
|
label = case maybe "" fst $ headMay percommoditytxnreports
|
||||||
|
of "" -> ""
|
||||||
|
s -> s++":"
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user