Skip to content

Commit 4b1919d

Browse files
adeptsimonmichael
authored andcommitted
fix: roi: use posting dates when available, honor --date2
This will not change the results computed for the typical use-case, just makes "roi" more thorough/consistent by supporting posting dates when they are provided.
1 parent 1f2c11d commit 4b1919d

File tree

1 file changed

+11
-10
lines changed
  • hledger/Hledger/Cli/Commands

1 file changed

+11
-10
lines changed

hledger/Hledger/Cli/Commands/Roi.hs

+11-10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsReportOpts=ReportO
7070

7171
let
7272
ropts = _rsReportOpts rspec
73+
wd = whichDate ropts
7374
showCashFlow = boolopt "cashflow" rawopts
7475
prettyTables = pretty_
7576
makeQuery flag = do
@@ -83,7 +84,7 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsReportOpts=ReportO
8384
trans = dbg3 "investments" $ jtxns $ filterJournalTransactions investmentsQuery j
8485

8586
journalSpan =
86-
let dates = map transactionDate2 trans in
87+
let dates = map (transactionDateOrDate2 wd) trans in
8788
DateSpan (Just $ minimum dates) (Just $ addDays 1 $ maximum dates)
8889

8990
requestedSpan = periodAsDateSpan period_
@@ -121,16 +122,16 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsReportOpts=ReportO
121122
cashFlow =
122123
((map (,nullmixedamt) priceDates)++) $
123124
cashFlowApplyCostValue $
124-
calculateCashFlow trans (And [ Not investmentsQuery
125-
, Not pnlQuery
126-
, Date span ] )
125+
calculateCashFlow wd trans (And [ Not investmentsQuery
126+
, Not pnlQuery
127+
, Date span ] )
127128

128129

129130
pnl =
130131
cashFlowApplyCostValue $
131-
calculateCashFlow trans (And [ Not investmentsQuery
132-
, pnlQuery
133-
, Date span ] )
132+
calculateCashFlow wd trans (And [ Not investmentsQuery
133+
, pnlQuery
134+
, Date span ] )
134135

135136
thisSpan = dbg3 "processing span" $
136137
OneSpan spanBegin spanEnd valueBefore valueAfter cashFlow pnl
@@ -290,9 +291,9 @@ interestSum referenceDay cf rate = sum $ map go cf
290291
where go (t,m) = realToFrac (unMix m) * rate ** (fromIntegral (referenceDay `diffDays` t) / 365)
291292

292293

293-
calculateCashFlow :: [Transaction] -> Query -> CashFlow
294-
calculateCashFlow trans query = filter (maIsNonZero . snd) $ map go trans
295-
where go t = (transactionDate2 t, total [t] query)
294+
calculateCashFlow :: WhichDate -> [Transaction] -> Query -> CashFlow
295+
calculateCashFlow wd trans query =
296+
[ (postingDateOrDate2 wd p, pamount p) | p <- filter (matchesPosting query) (concatMap realPostings trans), maIsNonZero (pamount p) ]
296297

297298
total :: [Transaction] -> Query -> MixedAmount
298299
total trans query = sumPostings . filter (matchesPosting query) $ concatMap realPostings trans

0 commit comments

Comments
 (0)