From 98cbcced57998c2dd445b5bf171001157c93dcbe Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 3 Jun 2016 17:51:10 -0700 Subject: [PATCH] lib: account transactions report filters by realness & status Two fixes for this report when --real/--cleared/real:/status: are in effect, affecting hledger-ui and possibly hledger-web: 1. exclude transactions which affect the current account via an excluded posting type. Eg when --real is in effect, a transaction posting to the current account with only virtual postings will not appear in the report. 2. when showing historical balances, don't count excluded posting types in the starting balance. Eg with --real, the starting balance will be the sum of only the non-virtual prior postings. This is complicated and there might be some ways to confuse it still, causing wrongly included/excluded transactions or wrong historical balances/running totals (transactions with both real and virtual postings to the current account, perhaps ?) --- hledger-lib/Hledger/Query.hs | 5 +++++ hledger-lib/Hledger/Reports/TransactionsReports.hs | 14 ++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/hledger-lib/Hledger/Query.hs b/hledger-lib/Hledger/Query.hs index f90a10ac4..6b4f56e28 100644 --- a/hledger-lib/Hledger/Query.hs +++ b/hledger-lib/Hledger/Query.hs @@ -25,6 +25,7 @@ module Hledger.Query ( queryIsStartDateOnly, queryIsSym, queryIsReal, + queryIsStatus, queryStartDate, queryEndDate, queryDateSpan, @@ -460,6 +461,10 @@ queryIsReal :: Query -> Bool queryIsReal (Real _) = True queryIsReal _ = False +queryIsStatus :: Query -> Bool +queryIsStatus (Status _) = True +queryIsStatus _ = False + -- | Does this query specify a start date and nothing else (that would -- filter postings prior to the date) ? -- When the flag is true, look for a starting secondary date instead. diff --git a/hledger-lib/Hledger/Reports/TransactionsReports.hs b/hledger-lib/Hledger/Reports/TransactionsReports.hs index 90439a717..f31757afc 100644 --- a/hledger-lib/Hledger/Reports/TransactionsReports.hs +++ b/hledger-lib/Hledger/Reports/TransactionsReports.hs @@ -109,7 +109,7 @@ type AccountTransactionsReportItem = ,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 + ,MixedAmount -- the historical balance or running total for the current account(s) after this transaction ) accountTransactionsReport :: ReportOpts -> Journal -> Query -> Query -> AccountTransactionsReport @@ -120,16 +120,18 @@ accountTransactionsReport opts j q thisacctquery = (label, items) -- apply any cur:SYM filters in q symq = filterQuery queryIsSym q ts2 = (if queryIsNull symq then id else map (filterTransactionAmounts symq)) ts1 - -- keep just the transactions affecting this account - ts3 = filter (matchesTransaction thisacctquery) ts2 + -- keep just the transactions affecting this account (via possibly realness or status-filtered postings) + realq = filterQuery queryIsReal q + statusq = filterQuery queryIsStatus q + ts3 = filter (matchesTransaction thisacctquery . filterTransactionPostings (And [realq, statusq])) ts2 -- adjust the transaction dates to the dates of postings to this account - -- XXX can be wrong since we filter real postings later ? ts4 = map (setTransactionDateToPostingDate q thisacctquery) ts3 -- sort by the new dates ts = sortBy (comparing tdate) ts4 -- 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. + -- this is the sum of the (possibly realness or status-filtered) postings + -- to this account before that date; otherwise zero. (startbal,label) | queryIsNull q = (nullmixedamt, balancelabel) | queryIsStartDateOnly (date2_ opts) q = (sumPostings priorps, balancelabel) | otherwise = (nullmixedamt, totallabel) @@ -137,7 +139,7 @@ accountTransactionsReport opts j q thisacctquery = (label, items) priorps = -- ltrace "priorps" $ filter (matchesPosting (-- ltrace "priormatcher" $ - And [thisacctquery, tostartdatequery])) + And [thisacctquery, realq, statusq, tostartdatequery])) $ transactionsPostings ts tostartdatequery = Date (DateSpan Nothing startdate) startdate = queryStartDate (date2_ opts) q