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.
This commit is contained in:
Dmitry Astapov 2021-10-11 22:05:34 +01:00 committed by Simon Michael
parent 1f2c11d1bc
commit 4b1919de02

View File

@ -70,6 +70,7 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsReportOpts=ReportO
let let
ropts = _rsReportOpts rspec ropts = _rsReportOpts rspec
wd = whichDate ropts
showCashFlow = boolopt "cashflow" rawopts showCashFlow = boolopt "cashflow" rawopts
prettyTables = pretty_ prettyTables = pretty_
makeQuery flag = do makeQuery flag = do
@ -83,7 +84,7 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsReportOpts=ReportO
trans = dbg3 "investments" $ jtxns $ filterJournalTransactions investmentsQuery j trans = dbg3 "investments" $ jtxns $ filterJournalTransactions investmentsQuery j
journalSpan = journalSpan =
let dates = map transactionDate2 trans in let dates = map (transactionDateOrDate2 wd) trans in
DateSpan (Just $ minimum dates) (Just $ addDays 1 $ maximum dates) DateSpan (Just $ minimum dates) (Just $ addDays 1 $ maximum dates)
requestedSpan = periodAsDateSpan period_ requestedSpan = periodAsDateSpan period_
@ -121,14 +122,14 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsReportOpts=ReportO
cashFlow = cashFlow =
((map (,nullmixedamt) priceDates)++) $ ((map (,nullmixedamt) priceDates)++) $
cashFlowApplyCostValue $ cashFlowApplyCostValue $
calculateCashFlow trans (And [ Not investmentsQuery calculateCashFlow wd trans (And [ Not investmentsQuery
, Not pnlQuery , Not pnlQuery
, Date span ] ) , Date span ] )
pnl = pnl =
cashFlowApplyCostValue $ cashFlowApplyCostValue $
calculateCashFlow trans (And [ Not investmentsQuery calculateCashFlow wd trans (And [ Not investmentsQuery
, pnlQuery , pnlQuery
, Date span ] ) , Date span ] )
@ -290,9 +291,9 @@ interestSum referenceDay cf rate = sum $ map go cf
where go (t,m) = realToFrac (unMix m) * rate ** (fromIntegral (referenceDay `diffDays` t) / 365) where go (t,m) = realToFrac (unMix m) * rate ** (fromIntegral (referenceDay `diffDays` t) / 365)
calculateCashFlow :: [Transaction] -> Query -> CashFlow calculateCashFlow :: WhichDate -> [Transaction] -> Query -> CashFlow
calculateCashFlow trans query = filter (maIsNonZero . snd) $ map go trans calculateCashFlow wd trans query =
where go t = (transactionDate2 t, total [t] query) [ (postingDateOrDate2 wd p, pamount p) | p <- filter (matchesPosting query) (concatMap realPostings trans), maIsNonZero (pamount p) ]
total :: [Transaction] -> Query -> MixedAmount total :: [Transaction] -> Query -> MixedAmount
total trans query = sumPostings . filter (matchesPosting query) $ concatMap realPostings trans total trans query = sumPostings . filter (matchesPosting query) $ concatMap realPostings trans