2021-07-04 12:44:35 +03:00
{- |
Various options to use when reading journal files .
Similar to CliOptions . inputflags , simplifies the journal - reading functions .
- }
2021-08-08 11:03:46 +03:00
{- # LANGUAGE TemplateHaskell # -}
2021-07-04 12:44:35 +03:00
module Hledger.Read.InputOptions (
2021-08-23 08:30:54 +03:00
-- * Types and helpers for input options
InputOpts ( .. )
2021-08-08 11:03:46 +03:00
, HasInputOpts ( .. )
2021-08-23 08:30:54 +03:00
, definputopts
, forecastPeriod
) where
import Control.Applicative ( ( <|> ) )
import Data.Time ( Day , addDays )
2021-07-04 12:44:35 +03:00
import Hledger.Data.Types
2021-08-23 08:30:54 +03:00
import Hledger.Data.Journal ( journalEndDate )
2021-09-10 03:00:44 +03:00
import Hledger.Data.Dates ( nulldate , nulldatespan )
2021-08-24 05:34:32 +03:00
import Hledger.Data.Balancing ( BalancingOpts ( .. ) , HasBalancingOpts ( .. ) , defbalancingopts )
2021-08-08 11:03:46 +03:00
import Hledger.Utils ( dbg2 , makeHledgerClassyLenses )
2021-07-04 12:44:35 +03:00
data InputOpts = InputOpts {
-- files_ :: [FilePath]
2021-08-23 08:30:54 +03:00
mformat_ :: Maybe StorageFormat -- ^ a file/storage format to try, unless overridden
-- by a filename prefix. Nothing means try all.
, mrules_file_ :: Maybe FilePath -- ^ a conversion rules file to use (when reading CSV)
, aliases_ :: [ String ] -- ^ account name aliases to apply
, anon_ :: Bool -- ^ do light anonymisation/obfuscation of the data
, new_ :: Bool -- ^ read only new transactions since this file was last read
, new_save_ :: Bool -- ^ save latest new transactions state for next time
, pivot_ :: String -- ^ use the given field's value as the account name
, forecast_ :: Maybe DateSpan -- ^ span in which to generate forecast transactions
, reportspan_ :: DateSpan -- ^ a dirty hack keeping the query dates in InputOpts. This rightfully lives in ReportSpec, but is duplicated here.
, auto_ :: Bool -- ^ generate automatic postings when journal is parsed
, balancingopts_ :: BalancingOpts -- ^ options for balancing transactions
, strict_ :: Bool -- ^ do extra error checking (eg, all posted accounts are declared, no prices are inferred)
2021-09-10 03:00:44 +03:00
, _ioDay :: Day -- ^ today's date, for use with forecast transactions XXX this duplicates _rsDay, and should eventually be removed when it's not needed anymore.
2021-07-04 12:44:35 +03:00
} deriving ( Show )
definputopts :: InputOpts
definputopts = InputOpts
{ mformat_ = Nothing
, mrules_file_ = Nothing
, aliases_ = []
, anon_ = False
, new_ = False
, new_save_ = True
, pivot_ = " "
, forecast_ = Nothing
2021-08-23 08:30:54 +03:00
, reportspan_ = nulldatespan
2021-07-04 12:44:35 +03:00
, auto_ = False
2021-08-23 16:15:04 +03:00
, balancingopts_ = defbalancingopts
2021-07-04 12:44:35 +03:00
, strict_ = False
2021-09-10 03:00:44 +03:00
, _ioDay = nulldate
2021-07-04 12:44:35 +03:00
}
2021-08-23 08:30:54 +03:00
-- | Get the Maybe the DateSpan to generate forecast options from.
-- This begins on:
-- - the start date supplied to the `--forecast` argument, if present
-- - otherwise, the later of
-- - the report start date if specified with -b/-p/date:
-- - the day after the latest normal (non-periodic) transaction in the journal, if any
-- - otherwise today.
-- It ends on:
-- - the end date supplied to the `--forecast` argument, if present
-- - otherwise the report end date if specified with -e/-p/date:
-- - otherwise 180 days (6 months) from today.
2021-09-10 03:00:44 +03:00
forecastPeriod :: InputOpts -> Journal -> Maybe DateSpan
forecastPeriod iopts j = do
2021-08-23 08:30:54 +03:00
DateSpan requestedStart requestedEnd <- forecast_ iopts
2021-09-10 03:00:44 +03:00
let forecastStart = requestedStart <|> max mjournalend reportStart <|> Just ( _ioDay iopts )
forecastEnd = requestedEnd <|> reportEnd <|> Just ( addDays 180 $ _ioDay iopts )
2021-08-23 08:30:54 +03:00
mjournalend = dbg2 " journalEndDate " $ journalEndDate False j -- ignore secondary dates
DateSpan reportStart reportEnd = reportspan_ iopts
return . dbg2 " forecastspan " $ DateSpan forecastStart forecastEnd
2021-08-08 11:03:46 +03:00
-- ** Lenses
makeHledgerClassyLenses ''InputOpts
instance HasBalancingOpts InputOpts where
balancingOpts = balancingopts