From 2b545caae530219762c4b59be714ae7f0ffcf132 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Mon, 11 Jan 2021 16:29:36 +1100 Subject: [PATCH] lib: Calculate value at posting date for register --value=then -M. --- hledger-lib/Hledger/Reports/MultiBalanceReport.hs | 8 -------- hledger-lib/Hledger/Reports/PostingsReport.hs | 10 +++++----- hledger-lib/Hledger/Reports/ReportOptions.hs | 9 +++++++++ hledger/test/journal/valuation.test | 8 ++++---- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index be9f4345a..1ab60700d 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -560,14 +560,6 @@ subtractAcct :: Account -> Account -> Account subtractAcct a@Account{aibalance=i1,aebalance=e1} Account{aibalance=i2,aebalance=e2} = a{aibalance = i1 - i2, aebalance = e1 - e2} --- | Whether the market price for postings might change when reported in --- different report periods. -changingValuation :: ReportOpts -> Bool -changingValuation ropts = case value_ ropts of - Just (AtCost (Just _)) -> True - Just (AtEnd _) -> True - _ -> False - -- | Extract period changes from a cumulative list periodChanges :: Account -> Map k Account -> Map k Account periodChanges start amtmap = diff --git a/hledger-lib/Hledger/Reports/PostingsReport.hs b/hledger-lib/Hledger/Reports/PostingsReport.hs index 0abd31fc7..073757e46 100644 --- a/hledger-lib/Hledger/Reports/PostingsReport.hs +++ b/hledger-lib/Hledger/Reports/PostingsReport.hs @@ -80,12 +80,12 @@ postingsReport rspec@ReportSpec{rsOpts=ropts@ReportOpts{..}} j = items -- Postings, or summary postings with their subperiod's end date, to be displayed. displayps :: [(Posting, Maybe Day)] - | multiperiod = - let summaryps = summarisePostingsByInterval interval_ whichdate mdepth showempty reportspan reportps - in [(pvalue lastday p, Just periodend) | (p, periodend) <- summaryps, let lastday = addDays (-1) periodend] - | otherwise = - [(pvalue reportorjournallast p, Nothing) | p <- reportps] + | multiperiod && changingValuation ropts = [(pvalue lastday p, Just periodend) | (p, periodend) <- summariseps reportps, let lastday = addDays (-1) periodend] + | multiperiod = [(p, Just periodend) | (p, periodend) <- summariseps valuedps] + | otherwise = [(p, Nothing) | p <- valuedps] where + summariseps = summarisePostingsByInterval interval_ whichdate mdepth showempty reportspan + valuedps = map (pvalue reportorjournallast) reportps showempty = empty_ || average_ reportorjournallast = fromMaybe (error' "postingsReport: expected a non-empty journal") $ -- PARTIAL: shouldn't happen diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index 375239377..e127d26db 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -23,6 +23,7 @@ module Hledger.Reports.ReportOptions ( rawOptsToReportSpec, flat_, tree_, + changingValuation, reportOptsToggleStatus, simplifyStatuses, whichDateFromOpts, @@ -471,6 +472,14 @@ queryFromFlags ReportOpts{..} = simplifyQuery $ And flagsq consIf f b = if b then (f True:) else id consJust f = maybe id ((:) . f) +-- | Whether the market price for postings might change when reported in +-- different report periods. +changingValuation :: ReportOpts -> Bool +changingValuation ropts = case value_ ropts of + Just (AtCost (Just _)) -> True + Just (AtEnd _) -> True + _ -> False + -- Report dates. -- | The effective report span is the start and end dates specified by diff --git a/hledger/test/journal/valuation.test b/hledger/test/journal/valuation.test index 5128926fe..b065c0c60 100644 --- a/hledger/test/journal/valuation.test +++ b/hledger/test/journal/valuation.test @@ -603,8 +603,8 @@ $ hledger -f- reg --value=then -b 2020-03 -H 2020-04-01 (a) 4 B 10 B >=0 -# 52. --value=then with a report interval. How are the summary amounts valued ? -# Currently each interval's unvalued sum is valued on its first day. +# 52. --value=then with a report interval. Summary amounts are the sums of the +# values of each posting at their posting date. < P 2020-01-01 A 1 B P 2020-02-01 A 2 B @@ -624,8 +624,8 @@ P 2020-04-01 A 4 B (a) 1 A $ hledger -f- reg --value=then -Q -2020Q1 a 3 B 3 B -2020Q2 a 4 B 7 B +2020Q1 a 6 B 6 B +2020Q2 a 4 B 10 B >=0 # 53. print --value should affect all postings, including when there's an implicit transaction price