refactor & use reportSpans

This commit is contained in:
Simon Michael 2014-04-14 08:31:57 -07:00
parent 020975555e
commit 5885914547
2 changed files with 36 additions and 32 deletions

View File

@ -81,19 +81,7 @@ multiBalanceReport opts q j = MultiBalanceReport (spans, items, totals)
filterJournalPostings nodepthq $ -- exclude unmatched postings, but include all depths filterJournalPostings nodepthq $ -- exclude unmatched postings, but include all depths
journalSelectingAmountFromOpts opts j journalSelectingAmountFromOpts opts j
-- the report's span will be the requested span intersected with (reportspan, spans) = dbg "report spans" $ reportSpans opts q j ps
-- the selected data's span; or with -E, the requested span
-- limited by the journal's overall span.
reportspan | empty_ opts = requestedspan `orDatesFrom` journalspan
| otherwise = requestedspan `spanIntersect` matchedspan
where
requestedspan = queryDateSpan (date2_ opts) q -- based on -b/-e/-p opts and query args IIRC
journalspan = journalDateSpan j
matchedspan = postingsDateSpan' (whichDateFromOpts opts) ps
spans :: [DateSpan] =
dbg "spans" $
splitSpan (intervalFromOpts opts) reportspan
-- (reportspan, spans) = dbg "report spans" $ reportSpans opts q j
psPerSpan :: [[Posting]] = psPerSpan :: [[Posting]] =
dbg "psPerSpan" $ dbg "psPerSpan" $

View File

@ -290,29 +290,45 @@ tests_queryOptsFromOpts = [
}) })
] ]
-- | Calculate the overall span and per-period date spans for a report -- | Calculate the overall and (if a reporting interval is specified)
-- based on command-line options, the parsed search query, and the -- per-interval date spans for a report, based on command-line
-- journal data. If a reporting interval is specified, the report span -- options, the search query, and the journal data.
-- will be enlarged to include a whole number of report periods. --
-- Reports will sometimes trim these spans further when appropriate. -- The basic overall report span is:
reportSpans :: ReportOpts -> Query -> Journal -> (DateSpan, [DateSpan]) -- without -E: the intersection of the requested span and the matched data's span.
reportSpans opts q j = (reportspan, spans) -- with -E: the full requested span, including leading/trailing intervals that are empty.
-- If the begin or end date is not specified, those of the journal are used.
--
-- The report span will be enlarged if necessary to include a whole
-- number of report periods, when a reporting interval is specified.
--
reportSpans :: ReportOpts -> Query -> Journal -> [Posting] -> (DateSpan, [DateSpan])
reportSpans opts q j matchedps = (reportspan, spans)
where where
-- get the requested span from the query, which is based on (reportspan, spans)
-- -b/-e/-p opts and query args. | empty_ opts = (dbg "reportspan1" $ enlargedrequestedspan, sps)
requestedspan = queryDateSpan (date2_ opts) q | otherwise = (dbg "reportspan2" $ requestedspan `spanIntersect` matchedspan, sps)
where
sps = dbg "spans" $ splitSpan (intervalFromOpts opts) reportspan
-- set the start and end date to the journal's if not specified -- the requested span specified by the query (-b/-e/-p options and query args);
requestedspan' = requestedspan `orDatesFrom` journalDateSpan j -- might be open-ended
requestedspan = dbg "requestedspan" $ queryDateSpan (date2_ opts) q
-- if there's a reporting interval, calculate the report periods -- the requested span with unspecified dates filled from the journal data
-- which enclose the requested span finiterequestedspan = dbg "finiterequestedspan" $ requestedspan `orDatesFrom` journalDateSpan j
spans = splitSpan (intervalFromOpts opts) requestedspan'
-- enlarge the requested span to a whole number of periods
enlargedrequestedspan = dbg "enlargedrequestedspan" $
DateSpan (maybe Nothing spanStart $ headMay requestedspans)
(maybe Nothing spanEnd $ lastMay requestedspans)
where
-- spans of the requested report interval which enclose the requested span
requestedspans = dbg "requestedspans" $ splitSpan (intervalFromOpts opts) finiterequestedspan
-- earliest and latest dates of matched postings
matchedspan = dbg "matchedspan" $ postingsDateSpan' (whichDateFromOpts opts) matchedps
-- the overall report span encloses the periods
reportspan = DateSpan
(maybe Nothing spanStart $ headMay spans)
(maybe Nothing spanEnd $ lastMay spans)
tests_Hledger_Reports_ReportOptions :: Test tests_Hledger_Reports_ReportOptions :: Test
tests_Hledger_Reports_ReportOptions = TestList $ tests_Hledger_Reports_ReportOptions = TestList $