ui, lib: another historical balance fix for accountTransactionsReport

This commit is contained in:
Simon Michael 2016-09-06 08:43:25 -07:00
parent 2f4dde3699
commit 8c7e7c02c3

View File

@ -39,7 +39,7 @@ import Data.Time.Calendar
import Hledger.Data import Hledger.Data
import Hledger.Query import Hledger.Query
import Hledger.Reports.ReportOptions import Hledger.Reports.ReportOptions
--import Hledger.Utils.Debug import Hledger.Utils.Debug
-- | A transactions report includes a list of transactions -- | A transactions report includes a list of transactions
@ -111,11 +111,14 @@ journalTransactionsReport opts j q = (totallabel, items)
-- - the total increase/decrease to the current account -- - the total increase/decrease to the current account
-- --
-- - the report transactions' running total after this transaction; -- - the report transactions' running total after this transaction;
-- or if historical balance is requested, the historical running total, -- or if historical balance is requested (-H), the historical running total.
-- including transactions (filtered by the report query) from before -- The historical running total includes transactions from before the
-- the report start date -- report start date if one is specified, filtered by the report query.
-- The historical running total may or may not be the account's historical
-- running balance, depending on the report query.
-- --
-- Items are sorted by transaction context date, most recent first. -- Items are sorted by transaction register date (the earliest date the transaction
-- posts to the current account), most recent first.
-- Reporting intervals are currently ignored. -- Reporting intervals are currently ignored.
-- --
type AccountTransactionsReport = type AccountTransactionsReport =
@ -137,38 +140,39 @@ accountTransactionsReport :: ReportOpts -> Journal -> Query -> Query -> AccountT
accountTransactionsReport opts j reportq thisacctq = (label, items) accountTransactionsReport opts j reportq thisacctq = (label, items)
where where
-- a depth limit does not affect the account transactions report -- a depth limit does not affect the account transactions report
q = -- filterQuery (not . queryIsDepth) -- seems unnecessary for some reason XXX -- seems unnecessary for some reason XXX
reportq reportq' = -- filterQuery (not . queryIsDepth)
reportq
-- get all transactions, with amounts converted to cost basis if -B -- get all transactions, with amounts converted to cost basis if -B
ts1 = jtxns $ journalSelectingAmountFromOpts opts j ts1 = jtxns $ journalSelectingAmountFromOpts opts j
-- apply any cur:SYM filters in q -- apply any cur:SYM filters in reportq'
symq = filterQuery queryIsSym q symq = filterQuery queryIsSym reportq'
ts2 = (if queryIsNull symq then id else map (filterTransactionAmounts symq)) ts1 ts2 = (if queryIsNull symq then id else map (filterTransactionAmounts symq)) ts1
-- keep just the transactions affecting this account (via possibly realness or status-filtered postings) -- keep just the transactions affecting this account (via possibly realness or status-filtered postings)
realq = filterQuery queryIsReal q realq = filterQuery queryIsReal reportq'
statusq = filterQuery queryIsStatus q statusq = filterQuery queryIsStatus reportq'
ts3 = filter (matchesTransaction thisacctq . filterTransactionPostings (And [realq, statusq])) ts2 ts3 = filter (matchesTransaction thisacctq . filterTransactionPostings (And [realq, statusq])) ts2
-- better sort by the transaction's register date, for accurate starting balance -- sort by the transaction's register date, for accurate starting balance
ts = sortBy (comparing (transactionRegisterDate reportq thisacctq)) ts3 ts = sortBy (comparing (transactionRegisterDate reportq' thisacctq)) ts3
(startbal,label) (startbal,label)
-- queryIsNull q = (nullmixedamt, balancelabel)
| balancetype_ opts == HistoricalBalance = (sumPostings priorps, balancelabel) | balancetype_ opts == HistoricalBalance = (sumPostings priorps, balancelabel)
| otherwise = (nullmixedamt, totallabel) | otherwise = (nullmixedamt, totallabel)
where where
priorps = -- ltrace "priorps" $ priorps = dbg1 "priorps" $
filter (matchesPosting filter (matchesPosting
(-- ltrace "priormatcher" $ (dbg1 "priorq" $
And [thisacctq, realq, statusq, tostartdatequery])) And [thisacctq, tostartdateq, datelessreportq]))
$ transactionsPostings ts $ transactionsPostings ts
tostartdatequery = tostartdateq =
case mstartdate of case mstartdate of
Just _ -> Date (DateSpan Nothing mstartdate) Just _ -> Date (DateSpan Nothing mstartdate)
Nothing -> None -- no start date specified, don't add up any prior postings Nothing -> None -- no start date specified, there are no prior postings
mstartdate = queryStartDate (date2_ opts) q mstartdate = queryStartDate (date2_ opts) reportq'
datelessreportq = filterQuery (not . queryIsDateOrDate2) reportq'
items = reverse $ -- see also registerChartHtml items = reverse $ -- see also registerChartHtml
accountTransactionsReportItems q thisacctq startbal negate ts accountTransactionsReportItems reportq' thisacctq startbal negate ts
totallabel = "Period Total" totallabel = "Period Total"
balancelabel = "Historical Total" balancelabel = "Historical Total"