lib: multiBalanceReport: Break getPostings and makeReportQuery into separate functions.

This commit is contained in:
Stephen Morgan 2020-06-12 11:07:19 +10:00
parent f21bf53610
commit 44dcd613e8

View File

@ -95,12 +95,8 @@ multiBalanceReportWith ropts@ReportOpts{..} q j priceoracle =
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- 1. Queries, report/column dates. -- 1. Queries, report/column dates.
symq = dbg "symq" $ filterQuery queryIsSym $ dbg "requested q" q
depthq = dbg "depthq" $ filterQuery queryIsDepth q depthq = dbg "depthq" $ filterQuery queryIsDepth q
depth = queryDepth depthq depth = queryDepth depthq
depthless = dbg "depthless" . filterQuery (not . queryIsDepth)
datelessq = dbg "datelessq" $ filterQuery (not . queryIsDateOrDate2) q
dateqcons = if date2_ then Date2 else Date
-- The date span specified by -b/-e/-p options and query args if any. -- The date span specified by -b/-e/-p options and query args if any.
requestedspan = dbg "requestedspan" $ queryDateSpan date2_ q requestedspan = dbg "requestedspan" $ queryDateSpan date2_ q
-- If the requested span is open-ended, close it using the journal's end dates. -- If the requested span is open-ended, close it using the journal's end dates.
@ -118,12 +114,8 @@ multiBalanceReportWith ropts@ReportOpts{..} q j priceoracle =
-- The user's query with no depth limit, and expanded to the report span -- The user's query with no depth limit, and expanded to the report span
-- if there is one (otherwise any date queries are left as-is, which -- if there is one (otherwise any date queries are left as-is, which
-- handles the hledger-ui+future txns case above). -- handles the hledger-ui+future txns case above).
reportq = dbg "reportq" $ depthless $ reportq = dbg "reportq" $ makeReportQuery ropts reportspan q
if reportspan == nulldatespan
then q
else And [datelessq, reportspandatesq]
where
reportspandatesq = dbg "reportspandatesq" $ dateqcons reportspan
-- The date spans to be included as report columns. -- The date spans to be included as report columns.
colspans :: [DateSpan] = dbg "colspans" $ splitSpan interval_ displayspan colspans :: [DateSpan] = dbg "colspans" $ splitSpan interval_ displayspan
where where
@ -135,13 +127,9 @@ multiBalanceReportWith ropts@ReportOpts{..} q j priceoracle =
-- If doing cost valuation, convert amounts to cost. -- If doing cost valuation, convert amounts to cost.
j' = journalSelectingAmountFromOpts ropts j j' = journalSelectingAmountFromOpts ropts j
---------------------------------------------------------------------- -- The matched accounts with a starting balance. All of these shold appear
-- 2. Calculate starting balances, if needed for -H -- in the report, even if they have no postings during the report period.
startbals = dbg' "startbals" $ startingBalances ropts q j' reportspan
-- Balances at report start date, from all earlier postings which otherwise match the query.
-- These balances are unvalued except maybe converted to cost.
startbals :: [(AccountName, MixedAmount)] = dbg' "startbals" $
startingBalances ropts q j reportspan
-- The matched accounts with a starting balance. All of these should appear -- The matched accounts with a starting balance. All of these should appear
-- in the report even if they have no postings during the report period. -- in the report even if they have no postings during the report period.
startaccts = dbg'' "startaccts" $ map fst startbals startaccts = dbg'' "startaccts" $ map fst startbals
@ -152,17 +140,7 @@ multiBalanceReportWith ropts@ReportOpts{..} q j priceoracle =
-- 3. Gather postings for each column. -- 3. Gather postings for each column.
-- Postings matching the query within the report period. -- Postings matching the query within the report period.
ps :: [(Posting, Day)] = ps :: [(Posting, Day)] = dbg'' "ps" $ getPostings ropts reportq j'
dbg'' "ps" $
map postingWithDate $
journalPostings $
filterJournalAmounts symq $ -- remove amount parts excluded by cur:
filterJournalPostings reportq $ -- remove postings not matched by (adjusted) query
j'
where
postingWithDate p = case whichDateFromOpts ropts of
PrimaryDate -> (p, postingDate p)
SecondaryDate -> (p, postingDate2 p)
-- Group postings into their columns, with the column end dates. -- Group postings into their columns, with the column end dates.
colps :: [([Posting], Maybe Day)] = colps :: [([Posting], Maybe Day)] =
@ -369,6 +347,37 @@ startingBalances ropts q j reportspan = map (\(a,_,_,b) -> (a,b)) startbalanceit
DateSpan Nothing Nothing -> emptydatespan DateSpan Nothing Nothing -> emptydatespan
a -> a a -> a
-- | Gather postings matching the query within the report period.
getPostings :: ReportOpts -> Query -> Journal -> [(Posting, Day)]
getPostings ropts q =
map (\p -> (p, date p)) .
journalPostings .
filterJournalAmounts symq . -- remove amount parts excluded by cur:
filterJournalPostings reportq -- remove postings not matched by (adjusted) query
where
symq = dbg "symq" . filterQuery queryIsSym $ dbg1 "requested q" q
-- The user's query with no depth limit, and expanded to the report span
-- if there is one (otherwise any date queries are left as-is, which
-- handles the hledger-ui+future txns case above).
reportq = dbg "reportq" $ depthless q
depthless = dbg "depthless" . filterQuery (not . queryIsDepth)
date = case whichDateFromOpts ropts of
PrimaryDate -> postingDate
SecondaryDate -> postingDate2
-- | Remove any date queries and insert queries from the report span
makeReportQuery :: ReportOpts -> DateSpan -> Query -> Query
makeReportQuery ropts reportspan q
| reportspan == nulldatespan = depthlessq
| otherwise = And [dateless depthlessq, reportspandatesq]
where
depthlessq = dbg1 "depthless" $ filterQuery (not . queryIsDepth) q
reportspandatesq = dbg1 "reportspandatesq" $ dateqcons reportspan
dateless = dbg1 "dateless" . filterQuery (not . queryIsDateOrDate2)
dateqcons = if date2_ ropts then Date2 else Date
-- | Generates a simple non-columnar BalanceReport, but using multiBalanceReport, -- | Generates a simple non-columnar BalanceReport, but using multiBalanceReport,
-- in order to support --historical. Does not support tree-mode boring parent eliding. -- in order to support --historical. Does not support tree-mode boring parent eliding.
-- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts -- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts