lib,cli: Remove old impure ReportOpts date functions.

This commit is contained in:
Stephen Morgan 2020-09-11 17:16:07 +10:00 committed by Simon Michael
parent 2aaab4b1b7
commit 19ab222599
5 changed files with 30 additions and 42 deletions

View File

@ -61,8 +61,8 @@ main = do
}
withJournalDo copts' $ \j -> do
today <- getCurrentDay
menddate <- specifiedEndDate ropts
let
menddate = reportPeriodLastDay ropts
args = words' $ query_ ropts
q = queryFromOpts today ropts
acct = T.pack $ headDef (error' "Please provide an account name argument") args

View File

@ -30,9 +30,6 @@ module Hledger.Reports.ReportOptions (
reportSpan,
reportStartDate,
reportEndDate,
specifiedStartEndDates,
specifiedStartDate,
specifiedEndDate,
reportPeriodStart,
reportPeriodOrJournalStart,
reportPeriodLastDay,
@ -457,39 +454,19 @@ queryFromFlags ReportOpts{..} = simplifyQuery $ And flagsq
-- options or queries, or otherwise the earliest and latest transaction or
-- posting dates in the journal. If no dates are specified by options/queries
-- and the journal is empty, returns the null date span.
-- Needs IO to parse smart dates in options/queries.
reportSpan :: Journal -> ReportOpts -> IO DateSpan
reportSpan j ropts = do
(mspecifiedstartdate, mspecifiedenddate) <-
dbg3 "specifieddates" <$> specifiedStartEndDates ropts
let
reportSpan :: Journal -> ReportOpts -> DateSpan
reportSpan j ropts = dbg3 "reportspan" $ DateSpan mstartdate menddate
where
DateSpan mjournalstartdate mjournalenddate =
dbg3 "journalspan" $ journalDateSpan False j -- ignore secondary dates
mstartdate = mspecifiedstartdate <|> mjournalstartdate
menddate = mspecifiedenddate <|> mjournalenddate
return $ dbg3 "reportspan" $ DateSpan mstartdate menddate
mstartdate = queryStartDate False (query_ ropts) <|> mjournalstartdate
menddate = queryEndDate False (query_ ropts) <|> mjournalenddate
reportStartDate :: Journal -> ReportOpts -> IO (Maybe Day)
reportStartDate j ropts = spanStart <$> reportSpan j ropts
reportStartDate :: Journal -> ReportOpts -> Maybe Day
reportStartDate j ropts = spanStart $ reportSpan j ropts
reportEndDate :: Journal -> ReportOpts -> IO (Maybe Day)
reportEndDate j ropts = spanEnd <$> reportSpan j ropts
-- | The specified report start/end dates are the dates specified by options or queries, if any.
-- Needs IO to parse smart dates in options/queries.
specifiedStartEndDates :: ReportOpts -> IO (Maybe Day, Maybe Day)
specifiedStartEndDates ropts = do
let
q = query_ ropts
mspecifiedstartdate = queryStartDate False q
mspecifiedenddate = queryEndDate False q
return (mspecifiedstartdate, mspecifiedenddate)
specifiedStartDate :: ReportOpts -> IO (Maybe Day)
specifiedStartDate ropts = fst <$> specifiedStartEndDates ropts
specifiedEndDate :: ReportOpts -> IO (Maybe Day)
specifiedEndDate ropts = snd <$> specifiedStartEndDates ropts
reportEndDate :: Journal -> ReportOpts -> Maybe Day
reportEndDate j ropts = spanEnd $ reportSpan j ropts
-- Some pure alternatives to the above. XXX review/clean up

View File

@ -119,7 +119,19 @@ data WebOpts = WebOpts
} deriving (Show)
defwebopts :: WebOpts
defwebopts = WebOpts def def Nothing def def def def [CapView, CapAdd] Nothing def Nothing
defwebopts = WebOpts
{ serve_ = False
, serve_api_ = False
, cors_ = Nothing
, host_ = ""
, port_ = def
, base_url_ = ""
, file_url_ = Nothing
, capabilities_ = [CapView, CapAdd]
, capabilitiesHeader_ = Nothing
, cliopts_ = def
, socket_ = Nothing
}
instance Default WebOpts where def = defwebopts

View File

@ -310,8 +310,8 @@ balance opts@CliOpts{rawopts_=rawopts,reportopts_=ropts@ReportOpts{..}} j = do
fmt = outputFormatFromOpts opts
if budget then do -- single or multi period budget report
reportspan <- reportSpan j ropts
let budgetreport = dbg4 "budgetreport" $ budgetReport ropts assrt reportspan j
let reportspan = reportSpan j ropts
budgetreport = dbg4 "budgetreport" $ budgetReport ropts assrt reportspan j
where
assrt = not $ ignore_assertions_ $ inputopts_ opts
render = case fmt of

View File

@ -123,20 +123,19 @@ journalAddForecast CliOpts{inputopts_=iopts, reportopts_=ropts} j =
today <- getCurrentDay
-- "They can start no earlier than: the day following the latest normal transaction in the journal (or today if there are none)."
let
let
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."
mspecifiedend <- snd . dbg2 "specifieddates" <$> specifiedStartEndDates ropts
let
-- "They end on or before the specified report end date, or 180 days from today if unspecified."
mspecifiedend = dbg2 "specifieddates" $ reportPeriodLastDay ropts
forecastendDefault = dbg2 "forecastendDefault" $ fromMaybe (addDays 180 today) mspecifiedend
forecastspan = dbg2 "forecastspan" $
spanDefaultsFrom
(fromMaybe nulldatespan $ dbg2 "forecastspan flag" $ forecast_ ropts)
(DateSpan (Just forecastbeginDefault) (Just forecastendDefault))
forecasttxns =
[ txnTieKnot t | pt <- jperiodictxns j
, t <- runPeriodicTransaction pt forecastspan