diff --git a/hledger-lib/Hledger/Query.hs b/hledger-lib/Hledger/Query.hs index 9c0750380..b35a65cb1 100644 --- a/hledger-lib/Hledger/Query.hs +++ b/hledger-lib/Hledger/Query.hs @@ -26,6 +26,7 @@ module Hledger.Query ( queryIsSym, queryIsReal, queryIsStatus, + queryIsEmpty, queryStartDate, queryEndDate, queryDateSpan, @@ -480,6 +481,10 @@ queryIsStatus :: Query -> Bool queryIsStatus (Status _) = True queryIsStatus _ = False +queryIsEmpty :: Query -> Bool +queryIsEmpty (Empty _) = True +queryIsEmpty _ = 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 4a52ec5ae..aa8b8a5aa 100644 --- a/hledger-lib/Hledger/Reports/TransactionsReports.hs +++ b/hledger-lib/Hledger/Reports/TransactionsReports.hs @@ -149,9 +149,9 @@ accountTransactionsReport opts j reportq thisacctq = (label, items) -- starting balance: if we are filtering by a start date and nothing else, -- this is the sum of the (possibly realness or status-filtered) postings - -- to this account before that date; otherwise zero. + -- to this account before that date; otherwise start from zero. (startbal,label) | queryIsNull q = (nullmixedamt, balancelabel) - | queryIsStartDateOnly (date2_ opts) q = (sumPostings priorps, balancelabel) + | queryIsStartBalancePreserving opts q = (sumPostings priorps, balancelabel) | otherwise = (nullmixedamt, totallabel) where priorps = -- ltrace "priorps" $ @@ -165,6 +165,20 @@ accountTransactionsReport opts j reportq thisacctq = (label, items) items = reverse $ -- see also registerChartHtml accountTransactionsReportItems q thisacctq startbal negate ts +-- Assuming this query specifies a start date, if we switch that to +-- an end date, will the resulting query calculate an accurate +-- starting balance ? +-- XXX WIP, probably misses many balance-preserving cases, +-- not sure how this should work. +queryIsStartBalancePreserving :: ReportOpts -> Query -> Bool +queryIsStartBalancePreserving opts q = + queryIsStartDateOnly (date2_ opts) $ + filterQuery (\q -> not ( + queryIsEmpty q + || queryIsSym q + -- || queryIsReal q)) + )) q + totallabel = "Running Total" balancelabel = "Historical Balance"