lib: Filter starting balances by subreport query in compoundBalanceReportWith.

This commit is contained in:
Stephen Morgan 2020-08-31 14:13:53 +10:00 committed by Simon Michael
parent 40ca6c62e7
commit 5b647902c9
2 changed files with 49 additions and 14 deletions

View File

@ -115,8 +115,13 @@ multiBalanceReportWith ropts q j priceoracle = report
colps = dbg'' "colps" $ getPostingsByColumn ropts reportq j reportspan colps = dbg'' "colps" $ getPostingsByColumn ropts reportq j reportspan
colspans = dbg "colspans" $ M.keys colps colspans = dbg "colspans" $ M.keys colps
-- 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.
startbals = dbg' "startbals" $ startingBalances ropts reportq j reportspan
-- Generate and postprocess the report, negating balances and taking percentages if needed -- Generate and postprocess the report, negating balances and taking percentages if needed
report = dbg' "report" $ generateMultiBalanceReport ropts reportq j priceoracle reportspan colspans colps report = dbg' "report" $
generateMultiBalanceReport ropts reportq j priceoracle colspans colps startbals
-- | Generate a compound balance report from a list of CBCSubreportSpec. This -- | Generate a compound balance report from a list of CBCSubreportSpec. This
-- shares postings between the subreports. -- shares postings between the subreports.
@ -141,21 +146,24 @@ compoundBalanceReportWith ropts q j priceoracle subreportspecs = cbr
colps = dbg'' "colps" $ getPostingsByColumn ropts{empty_=True} reportq j reportspan colps = dbg'' "colps" $ getPostingsByColumn ropts{empty_=True} reportq j reportspan
colspans = dbg "colspans" $ M.keys colps colspans = dbg "colspans" $ M.keys colps
-- Filter the column postings according to each subreport -- The matched accounts with a starting balance. All of these should appear
subreportcolps = map filterSubreport subreportspecs -- in the report, even if they have no postings during the report period.
where filterSubreport sr = filter (matchesPosting $ cbcsubreportquery sr j) <$> colps startbals = dbg' "startbals" $ startingBalances ropts reportq j reportspan
subreports = zipWith generateSubreport subreportspecs subreportcolps subreports = map generateSubreport subreportspecs
where where
generateSubreport CBCSubreportSpec{..} colps' = generateSubreport CBCSubreportSpec{..} =
( cbcsubreporttitle ( cbcsubreporttitle
-- Postprocess the report, negating balances and taking percentages if needed -- Postprocess the report, negating balances and taking percentages if needed
, prNormaliseSign cbcsubreportnormalsign $ , prNormaliseSign cbcsubreportnormalsign $
generateMultiBalanceReport ropts' reportq j priceoracle reportspan colspans colps' generateMultiBalanceReport ropts' reportq j priceoracle colspans colps' startbals'
, cbcsubreportincreasestotal , cbcsubreportincreasestotal
) )
where where
ropts' = ropts{normalbalance_=Just cbcsubreportnormalsign} ropts' = ropts{normalbalance_=Just cbcsubreportnormalsign}
-- Filter the column postings according to each subreport
colps' = filter (matchesPosting $ cbcsubreportquery j) <$> colps
startbals' = HM.filterWithKey (\k _ -> matchesAccount (cbcsubreportquery j) k) startbals
-- Sum the subreport totals by column. Handle these cases: -- Sum the subreport totals by column. Handle these cases:
-- - no subreports -- - no subreports
@ -363,18 +371,15 @@ accumValueAmounts ropts j priceoracle colspans startbals acctchanges = -- PARTI
-- given by AccountName and columns by DateSpan, then generate a MultiBalanceReport -- given by AccountName and columns by DateSpan, then generate a MultiBalanceReport
-- from the columns. -- from the columns.
generateMultiBalanceReport :: ReportOpts -> Query -> Journal -> PriceOracle generateMultiBalanceReport :: ReportOpts -> Query -> Journal -> PriceOracle
-> DateSpan -> [DateSpan] -> [DateSpan]
-> Map DateSpan [Posting] -> Map DateSpan [Posting]
-> HashMap AccountName Account
-> MultiBalanceReport -> MultiBalanceReport
generateMultiBalanceReport ropts q j priceoracle reportspan colspans colps = report generateMultiBalanceReport ropts q j priceoracle colspans colps startbals = report
where where
-- Each account's balance changes across all columns. -- Each account's balance changes across all columns.
acctchanges = dbg'' "acctchanges" $ calculateAccountChanges ropts q colspans colps acctchanges = dbg'' "acctchanges" $ calculateAccountChanges ropts q colspans colps
-- 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.
startbals = dbg' "startbals" $ startingBalances ropts q j reportspan
-- Process changes into normal, cumulative, or historical amounts, plus value them -- Process changes into normal, cumulative, or historical amounts, plus value them
accumvalued = accumValueAmounts ropts j priceoracle colspans startbals acctchanges accumvalued = accumValueAmounts ropts j priceoracle colspans startbals acctchanges

View File

@ -278,3 +278,33 @@ Balance Sheet 2018-10-03
Net: || $120 Net: || $120
>>>2 >>>2
>>>= 0 >>>= 0
# 10. Check that starting balances are also filtered by subreport query. (See issue #1335)
hledger -f - balancesheet --begin 2020-03-02
<<<
2020-03-01 * Rent
assets:a -$1
expenses
2020-03-25 * Salary
assets:a $2
income
>>>
Balance Sheet 2020-03-25
|| 2020-03-25
=============++============
Assets ||
-------------++------------
assets:a || $1
-------------++------------
|| $1
=============++============
Liabilities ||
-------------++------------
-------------++------------
||
=============++============
Net: || $1
>>>2
>>>= 0