From 5b647902c9f89b84ce814dec5bdc50778c82a153 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Mon, 31 Aug 2020 14:13:53 +1000 Subject: [PATCH] lib: Filter starting balances by subreport query in compoundBalanceReportWith. --- .../Hledger/Reports/MultiBalanceReport.hs | 33 +++++++++++-------- tests/balancesheet.test | 30 +++++++++++++++++ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index 6797b6829..dc3e0b254 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -115,8 +115,13 @@ multiBalanceReportWith ropts q j priceoracle = report colps = dbg'' "colps" $ getPostingsByColumn ropts reportq j reportspan 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 - 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 -- 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 colspans = dbg "colspans" $ M.keys colps - -- Filter the column postings according to each subreport - subreportcolps = map filterSubreport subreportspecs - where filterSubreport sr = filter (matchesPosting $ cbcsubreportquery sr j) <$> 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 - subreports = zipWith generateSubreport subreportspecs subreportcolps + subreports = map generateSubreport subreportspecs where - generateSubreport CBCSubreportSpec{..} colps' = + generateSubreport CBCSubreportSpec{..} = ( cbcsubreporttitle -- Postprocess the report, negating balances and taking percentages if needed , prNormaliseSign cbcsubreportnormalsign $ - generateMultiBalanceReport ropts' reportq j priceoracle reportspan colspans colps' + generateMultiBalanceReport ropts' reportq j priceoracle colspans colps' startbals' , cbcsubreportincreasestotal ) 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: -- - 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 -- from the columns. generateMultiBalanceReport :: ReportOpts -> Query -> Journal -> PriceOracle - -> DateSpan -> [DateSpan] + -> [DateSpan] -> Map DateSpan [Posting] + -> HashMap AccountName Account -> MultiBalanceReport -generateMultiBalanceReport ropts q j priceoracle reportspan colspans colps = report +generateMultiBalanceReport ropts q j priceoracle colspans colps startbals = report where -- Each account's balance changes across all columns. 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 accumvalued = accumValueAmounts ropts j priceoracle colspans startbals acctchanges diff --git a/tests/balancesheet.test b/tests/balancesheet.test index 8cc05da7c..ae3fa3993 100644 --- a/tests/balancesheet.test +++ b/tests/balancesheet.test @@ -278,3 +278,33 @@ Balance Sheet 2018-10-03 Net: || $120 >>>2 >>>= 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