{-# LANGUAGE TemplateHaskell #-}
module Hledger.Read.InputOptions (
InputOpts(..)
, HasInputOpts(..)
, definputopts
, forecastPeriod
) where
import Control.Applicative ((<|>))
import Data.Time (Day, addDays)
import Hledger.Data.Types
import Hledger.Data.Journal (journalEndDate)
import Hledger.Data.Dates (nulldate, nulldatespan)
import Hledger.Data.Balancing (BalancingOpts(..), HasBalancingOpts(..), defbalancingopts)
import Hledger.Utils (dbg2, makeHledgerClassyLenses)
data InputOpts = InputOpts {
InputOpts -> Maybe StorageFormat
mformat_ :: Maybe StorageFormat
,InputOpts -> Maybe StorageFormat
mrules_file_ :: Maybe FilePath
,InputOpts -> [StorageFormat]
aliases_ :: [String]
,InputOpts -> Bool
anon_ :: Bool
,InputOpts -> Bool
new_ :: Bool
,InputOpts -> Bool
new_save_ :: Bool
,InputOpts -> StorageFormat
pivot_ :: String
,InputOpts -> Maybe DateSpan
forecast_ :: Maybe DateSpan
,InputOpts -> Bool
verbose_tags_ :: Bool
,InputOpts -> DateSpan
reportspan_ :: DateSpan
,InputOpts -> Bool
auto_ :: Bool
,InputOpts -> Bool
infer_equity_ :: Bool
,InputOpts -> Bool
infer_costs_ :: Bool
,InputOpts -> BalancingOpts
balancingopts_ :: BalancingOpts
,InputOpts -> Bool
strict_ :: Bool
,InputOpts -> Day
_ioDay :: Day
} deriving (Int -> InputOpts -> ShowS
[InputOpts] -> ShowS
InputOpts -> StorageFormat
forall a.
(Int -> a -> ShowS)
-> (a -> StorageFormat) -> ([a] -> ShowS) -> Show a
showList :: [InputOpts] -> ShowS
$cshowList :: [InputOpts] -> ShowS
show :: InputOpts -> StorageFormat
$cshow :: InputOpts -> StorageFormat
showsPrec :: Int -> InputOpts -> ShowS
$cshowsPrec :: Int -> InputOpts -> ShowS
Show)
definputopts :: InputOpts
definputopts :: InputOpts
definputopts = InputOpts
{ mformat_ :: Maybe StorageFormat
mformat_ = forall a. Maybe a
Nothing
, mrules_file_ :: Maybe StorageFormat
mrules_file_ = forall a. Maybe a
Nothing
, aliases_ :: [StorageFormat]
aliases_ = []
, anon_ :: Bool
anon_ = Bool
False
, new_ :: Bool
new_ = Bool
False
, new_save_ :: Bool
new_save_ = Bool
True
, pivot_ :: StorageFormat
pivot_ = StorageFormat
""
, forecast_ :: Maybe DateSpan
forecast_ = forall a. Maybe a
Nothing
, verbose_tags_ :: Bool
verbose_tags_ = Bool
False
, reportspan_ :: DateSpan
reportspan_ = DateSpan
nulldatespan
, auto_ :: Bool
auto_ = Bool
False
, infer_equity_ :: Bool
infer_equity_ = Bool
False
, infer_costs_ :: Bool
infer_costs_ = Bool
False
, balancingopts_ :: BalancingOpts
balancingopts_ = BalancingOpts
defbalancingopts
, strict_ :: Bool
strict_ = Bool
False
, _ioDay :: Day
_ioDay = Day
nulldate
}
forecastPeriod :: InputOpts -> Journal -> Maybe DateSpan
forecastPeriod :: InputOpts -> Journal -> Maybe DateSpan
forecastPeriod InputOpts
iopts Journal
j = do
DateSpan Maybe EFDay
requestedStart Maybe EFDay
requestedEnd <- InputOpts -> Maybe DateSpan
forecast_ InputOpts
iopts
let forecastStart :: Maybe Day
forecastStart = EFDay -> Day
fromEFDay forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe EFDay
requestedStart forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. Ord a => a -> a -> a
max Maybe Day
mjournalend (EFDay -> Day
fromEFDay forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe EFDay
reportStart) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. a -> Maybe a
Just (InputOpts -> Day
_ioDay InputOpts
iopts)
forecastEnd :: Maybe Day
forecastEnd = EFDay -> Day
fromEFDay forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe EFDay
requestedEnd forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> EFDay -> Day
fromEFDay forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe EFDay
reportEnd forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Integer -> Day -> Day
addDays Integer
180 forall a b. (a -> b) -> a -> b
$ InputOpts -> Day
_ioDay InputOpts
iopts)
mjournalend :: Maybe Day
mjournalend = forall a. Show a => StorageFormat -> a -> a
dbg2 StorageFormat
"journalEndDate" forall a b. (a -> b) -> a -> b
$ Bool -> Journal -> Maybe Day
journalEndDate Bool
False Journal
j
DateSpan Maybe EFDay
reportStart Maybe EFDay
reportEnd = InputOpts -> DateSpan
reportspan_ InputOpts
iopts
forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => StorageFormat -> a -> a
dbg2 StorageFormat
"forecastspan" forall a b. (a -> b) -> a -> b
$ Maybe EFDay -> Maybe EFDay -> DateSpan
DateSpan (Day -> EFDay
Exact forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Day
forecastStart) (Day -> EFDay
Exact forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Day
forecastEnd)
makeHledgerClassyLenses ''InputOpts
instance HasBalancingOpts InputOpts where
balancingOpts :: Lens' InputOpts BalancingOpts
balancingOpts = forall c. HasInputOpts c => Lens' c BalancingOpts
balancingopts