From 4cd3ed15a061c16ad709b3034930697290ac7d72 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Thu, 29 Oct 2020 14:33:45 +1100 Subject: [PATCH] lib: Use simpler MultiBalanceReport valuation calculations in more cases. --- .../Hledger/Reports/MultiBalanceReport.hs | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index 7c49cdf22..d5c69a643 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -349,26 +349,29 @@ accumValueAmounts ropts valuation colspans startbals acctchanges = -- PARTIAL: CumulativeChange -> cumulative PeriodChange -> changeamts where - -- Calculate the valued historical balance in each column, ensuring every - -- columns has an entry. + -- Calculate the valued balances in each column, ensuring every column has an entry. historical = cumulativeSum startingBalance - -- If no valuation can sum the changes directly, otherwise need to - -- subtract the valued starting amount from the historical sum - cumulative = case value_ ropts of - Nothing -> cumulativeSum nullacct - Just _ -> fmap (`subtractAcct` valuedStart) historical - -- If no valuation can use the change list directly, otherwise need to - -- calculate the incremental differences in the historical sum - changeamts = case value_ ropts of - Nothing -> changes - Just _ -> let (dates, histamts) = unzip $ M.toAscList historical - in M.fromDistinctAscList . zip dates $ - zipWith subtractAcct histamts (valuedStart:histamts) + cumulative | fixedValuationDate = cumulativeSum nullacct + | otherwise = fmap (`subtractAcct` valuedStart) historical + changeamts | fixedValuationDate = M.mapWithKey valueAcct changes + | otherwise = M.fromDistinctAscList . zip dates $ + zipWith subtractAcct histamts (valuedStart:histamts) + where (dates, histamts) = unzip $ M.toAscList historical cumulativeSum start = snd $ M.mapAccumWithKey accumValued start changes where accumValued startAmt date newAmt = (s, valueAcct date s) where s = sumAcct startAmt newAmt + -- Whether the market price is measured at the same date for all report + -- periods, and we can therefore use the simpler calculations for + -- cumulative and change reports. + fixedValuationDate = case value_ ropts of + Just (AtCost (Just _)) -> singleperiod + Just (AtEnd _) -> singleperiod + Just (AtDefault _) -> singleperiod + _ -> True + where singleperiod = interval_ ropts == NoInterval + changes = changes' <> zeros startingBalance = HM.lookupDefault nullacct name startbals valuedStart = valueAcct (DateSpan Nothing historicalDate) startingBalance