;lib: refactor journalAddForecast

This commit is contained in:
Simon Michael 2020-10-19 08:52:00 -07:00
parent fe5a97be81
commit 831ec0dcf0

View File

@ -120,37 +120,39 @@ journalAddForecast CliOpts{inputopts_=iopts, reportopts_=ropts} j = do
today <- getCurrentDay today <- getCurrentDay
-- "They can start no earlier than: the day following the latest normal transaction in the journal (or today if there are none)." -- "They can start no earlier than: the day following the latest normal transaction in the journal (or today if there are none)."
let mjournalend = dbg2 "journalEndDate" $ journalEndDate False j -- ignore secondary dates let
forecastbeginDefault = dbg2 "forecastbeginDefault" $ fromMaybe today mjournalend mjournalend = dbg2 "journalEndDate" $ journalEndDate False j -- ignore secondary dates
forecastbeginDefault = dbg2 "forecastbeginDefault" $ fromMaybe today mjournalend
-- "They end on or before the specified report end date, or 180 days from today if unspecified." -- "They end on or before the specified report end date, or 180 days from today if unspecified."
mspecifiedend <- snd . dbg2 "specifieddates" <$> specifiedStartEndDates ropts mspecifiedend <- snd . dbg2 "specifieddates" <$> specifiedStartEndDates ropts
let forecastendDefault = dbg2 "forecastendDefault" $ fromMaybe (addDays 180 today) mspecifiedend let
forecastendDefault = dbg2 "forecastendDefault" $ fromMaybe (addDays 180 today) mspecifiedend
let forecastspan = dbg2 "forecastspan" $ forecastspan = dbg2 "forecastspan" $
spanDefaultsFrom spanDefaultsFrom
(fromMaybe nulldatespan $ dbg2 "forecastspan flag" $ forecast_ ropts) (fromMaybe nulldatespan $ dbg2 "forecastspan flag" $ forecast_ ropts)
(DateSpan (Just forecastbeginDefault) (Just forecastendDefault)) (DateSpan (Just forecastbeginDefault) (Just forecastendDefault))
forecasttxns = forecasttxns =
[ txnTieKnot t | pt <- jperiodictxns j [ txnTieKnot t | pt <- jperiodictxns j
, t <- runPeriodicTransaction pt forecastspan , t <- runPeriodicTransaction pt forecastspan
, spanContainsDate forecastspan (tdate t) , spanContainsDate forecastspan (tdate t)
] ]
-- With --auto enabled, transaction modifiers are also applied to forecast txns -- With --auto enabled, transaction modifiers are also applied to forecast txns
forecasttxns' = forecasttxns' =
(if auto_ iopts then either error' id . modifyTransactions today (jtxnmodifiers j) else id) -- PARTIAL: (if auto_ iopts then either error' id . modifyTransactions today (jtxnmodifiers j) else id) -- PARTIAL:
forecasttxns forecasttxns
return $ journalBalanceTransactions' iopts j =
case forecast_ ropts of either error' id $ journalBalanceTransactions assrt j -- PARTIAL:
Just _ -> journalBalanceTransactions' iopts j{ jtxns = concat [jtxns j, forecasttxns'] } where assrt = not . ignore_assertions_ $ iopts
Nothing -> j
where let j' = case forecast_ ropts of
journalBalanceTransactions' iopts j = Just _ -> journalBalanceTransactions' iopts j{ jtxns = concat [jtxns j, forecasttxns'] }
let assrt = not . ignore_assertions_ $ iopts Nothing -> j
in
either error' id $ journalBalanceTransactions assrt j -- PARTIAL: return j'
-- | Write some output to stdout or to a file selected by --output-file. -- | Write some output to stdout or to a file selected by --output-file.
-- If the file exists it will be overwritten. -- If the file exists it will be overwritten.