{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Hledger.Data.PeriodicTransaction (
runPeriodicTransaction
, checkPeriodicTransactionStartDate
)
where
import Data.Function ((&))
import Data.Maybe (isNothing)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Text.Printf
import Hledger.Data.Types
import Hledger.Data.Dates
import Hledger.Data.Amount
import Hledger.Data.Posting (post, commentAddTagNextLine)
import Hledger.Data.Transaction
_ptgen :: [Char] -> IO ()
_ptgen [Char]
str = do
let
t :: Text
t = [Char] -> Text
T.pack [Char]
str
(Interval
i,DateSpan
s) = Day -> Text -> (Interval, DateSpan)
parsePeriodExpr' Day
nulldate Text
t
(Transaction -> IO ()) -> [Transaction] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text -> IO ()
T.putStr (Text -> IO ()) -> (Transaction -> Text) -> Transaction -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Text
showTransaction) ([Transaction] -> IO ()) -> [Transaction] -> IO ()
forall a b. (a -> b) -> a -> b
$
Bool -> PeriodicTransaction -> DateSpan -> [Transaction]
runPeriodicTransaction Bool
True
PeriodicTransaction
nullperiodictransaction{ ptperiodexpr :: Text
ptperiodexpr=Text
t , ptspan :: DateSpan
ptspan=DateSpan
s, ptinterval :: Interval
ptinterval=Interval
i, ptpostings :: [Posting]
ptpostings=[Text
"a" Text -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1] }
DateSpan
nulldatespan
_ptgenspan :: [Char] -> DateSpan -> IO ()
_ptgenspan [Char]
str DateSpan
spn = do
let
t :: Text
t = [Char] -> Text
T.pack [Char]
str
(Interval
i,DateSpan
s) = Day -> Text -> (Interval, DateSpan)
parsePeriodExpr' Day
nulldate Text
t
(Transaction -> IO ()) -> [Transaction] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text -> IO ()
T.putStr (Text -> IO ()) -> (Transaction -> Text) -> Transaction -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Text
showTransaction) ([Transaction] -> IO ()) -> [Transaction] -> IO ()
forall a b. (a -> b) -> a -> b
$
Bool -> PeriodicTransaction -> DateSpan -> [Transaction]
runPeriodicTransaction Bool
True
PeriodicTransaction
nullperiodictransaction{ ptperiodexpr :: Text
ptperiodexpr=Text
t , ptspan :: DateSpan
ptspan=DateSpan
s, ptinterval :: Interval
ptinterval=Interval
i, ptpostings :: [Posting]
ptpostings=[Text
"a" Text -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1] }
DateSpan
spn
instance Show PeriodicTransaction where
show :: PeriodicTransaction -> [Char]
show PeriodicTransaction{[Tag]
[Posting]
(SourcePos, SourcePos)
Text
Status
Interval
DateSpan
pttags :: PeriodicTransaction -> [Tag]
ptcomment :: PeriodicTransaction -> Text
ptdescription :: PeriodicTransaction -> Text
ptcode :: PeriodicTransaction -> Text
ptstatus :: PeriodicTransaction -> Status
ptsourcepos :: PeriodicTransaction -> (SourcePos, SourcePos)
ptpostings :: [Posting]
pttags :: [Tag]
ptcomment :: Text
ptdescription :: Text
ptcode :: Text
ptstatus :: Status
ptsourcepos :: (SourcePos, SourcePos)
ptspan :: DateSpan
ptinterval :: Interval
ptperiodexpr :: Text
ptpostings :: PeriodicTransaction -> [Posting]
ptinterval :: PeriodicTransaction -> Interval
ptspan :: PeriodicTransaction -> DateSpan
ptperiodexpr :: PeriodicTransaction -> Text
..} =
[Char]
-> [Char]
-> [Char]
-> [Char]
-> [Char]
-> [Char]
-> [Char]
-> [Char]
-> [Char]
-> [Char]
-> ShowS
forall r. PrintfType r => [Char] -> r
printf [Char]
"PeriodicTransactionPP {%s, %s, %s, %s, %s, %s, %s, %s, %s, %s}"
([Char]
"ptperiodexpr=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
forall a. Show a => a -> [Char]
show Text
ptperiodexpr)
([Char]
"ptinterval=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Interval -> [Char]
forall a. Show a => a -> [Char]
show Interval
ptinterval)
([Char]
"ptspan=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> [Char]
show (DateSpan -> [Char]
forall a. Show a => a -> [Char]
show DateSpan
ptspan))
([Char]
"ptsourcepos=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (SourcePos, SourcePos) -> [Char]
forall a. Show a => a -> [Char]
show (SourcePos, SourcePos)
ptsourcepos)
([Char]
"ptstatus=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> [Char]
show (Status -> [Char]
forall a. Show a => a -> [Char]
show Status
ptstatus))
([Char]
"ptcode=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
forall a. Show a => a -> [Char]
show Text
ptcode)
([Char]
"ptdescription=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
forall a. Show a => a -> [Char]
show Text
ptdescription)
([Char]
"ptcomment=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
forall a. Show a => a -> [Char]
show Text
ptcomment)
([Char]
"pttags=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Tag] -> [Char]
forall a. Show a => a -> [Char]
show [Tag]
pttags)
([Char]
"ptpostings=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Posting] -> [Char]
forall a. Show a => a -> [Char]
show [Posting]
ptpostings)
runPeriodicTransaction :: Bool -> PeriodicTransaction -> DateSpan -> [Transaction]
runPeriodicTransaction :: Bool -> PeriodicTransaction -> DateSpan -> [Transaction]
runPeriodicTransaction Bool
verbosetags PeriodicTransaction{[Tag]
[Posting]
(SourcePos, SourcePos)
Text
Status
Interval
DateSpan
ptpostings :: [Posting]
pttags :: [Tag]
ptcomment :: Text
ptdescription :: Text
ptcode :: Text
ptstatus :: Status
ptsourcepos :: (SourcePos, SourcePos)
ptspan :: DateSpan
ptinterval :: Interval
ptperiodexpr :: Text
pttags :: PeriodicTransaction -> [Tag]
ptcomment :: PeriodicTransaction -> Text
ptdescription :: PeriodicTransaction -> Text
ptcode :: PeriodicTransaction -> Text
ptstatus :: PeriodicTransaction -> Status
ptsourcepos :: PeriodicTransaction -> (SourcePos, SourcePos)
ptpostings :: PeriodicTransaction -> [Posting]
ptinterval :: PeriodicTransaction -> Interval
ptspan :: PeriodicTransaction -> DateSpan
ptperiodexpr :: PeriodicTransaction -> Text
..} DateSpan
requestedspan =
[ Transaction
t{tdate :: Day
tdate=Day
d} | (DateSpan (Just EFDay
efd) Maybe EFDay
_) <- [DateSpan]
alltxnspans, let d :: Day
d = EFDay -> Day
fromEFDay EFDay
efd, DateSpan -> Day -> Bool
spanContainsDate DateSpan
requestedspan Day
d ]
where
t :: Transaction
t = Transaction
nulltransaction{
tsourcepos :: (SourcePos, SourcePos)
tsourcepos = (SourcePos, SourcePos)
ptsourcepos
,tstatus :: Status
tstatus = Status
ptstatus
,tcode :: Text
tcode = Text
ptcode
,tdescription :: Text
tdescription = Text
ptdescription
,tcomment :: Text
tcomment = Text
ptcomment Text -> (Text -> Text) -> Text
forall a b. a -> (a -> b) -> b
&
(if Bool
verbosetags then (Text -> Tag -> Text
`commentAddTagNextLine` (Text
"generated-transaction",Text
period)) else Text -> Text
forall a. a -> a
id)
,ttags :: [Tag]
ttags = [Tag]
pttags [Tag] -> ([Tag] -> [Tag]) -> [Tag]
forall a b. a -> (a -> b) -> b
&
((Text
"_generated-transaction",Text
period) Tag -> [Tag] -> [Tag]
forall a. a -> [a] -> [a]
:) [Tag] -> ([Tag] -> [Tag]) -> [Tag]
forall a b. a -> (a -> b) -> b
&
(if Bool
verbosetags then ((Text
"generated-transaction" ,Text
period) Tag -> [Tag] -> [Tag]
forall a. a -> [a] -> [a]
:) else [Tag] -> [Tag]
forall a. a -> a
id)
,tpostings :: [Posting]
tpostings = [Posting]
ptpostings
}
period :: Text
period = Text
"~ " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ptperiodexpr
alltxnspans :: [DateSpan]
alltxnspans = Bool -> Interval -> DateSpan -> [DateSpan]
splitSpan Bool
adjust Interval
ptinterval DateSpan
span'
where
span' :: DateSpan
span' = DateSpan
ptspan DateSpan -> DateSpan -> DateSpan
`spanDefaultsFrom` DateSpan
requestedspan
adjust :: Bool
adjust = Maybe Day -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe Day -> Bool) -> Maybe Day -> Bool
forall a b. (a -> b) -> a -> b
$ DateSpan -> Maybe Day
spanStart DateSpan
span'
checkPeriodicTransactionStartDate :: Interval -> DateSpan -> T.Text -> Maybe String
checkPeriodicTransactionStartDate :: Interval -> DateSpan -> Text -> Maybe [Char]
checkPeriodicTransactionStartDate Interval
i DateSpan
s Text
periodexpr =
case (Interval
i, DateSpan -> Maybe Day
spanStart DateSpan
s) of
(Weeks Int
_, Just Day
d) -> Day -> SmartInterval -> Maybe [Char]
checkStart Day
d SmartInterval
Week
(Months Int
_, Just Day
d) -> Day -> SmartInterval -> Maybe [Char]
checkStart Day
d SmartInterval
Month
(Quarters Int
_, Just Day
d) -> Day -> SmartInterval -> Maybe [Char]
checkStart Day
d SmartInterval
Quarter
(Years Int
_, Just Day
d) -> Day -> SmartInterval -> Maybe [Char]
checkStart Day
d SmartInterval
Year
(Interval, Maybe Day)
_ -> Maybe [Char]
forall a. Maybe a
Nothing
where
checkStart :: Day -> SmartInterval -> Maybe [Char]
checkStart Day
d SmartInterval
x =
let firstDate :: Day
firstDate = EFDay -> Day
fromEFDay (EFDay -> Day) -> EFDay -> Day
forall a b. (a -> b) -> a -> b
$ Day -> SmartDate -> EFDay
fixSmartDate Day
d (SmartDate -> EFDay) -> SmartDate -> EFDay
forall a b. (a -> b) -> a -> b
$ Integer -> SmartInterval -> SmartDate
SmartRelative Integer
0 SmartInterval
x
in
if Day
d Day -> Day -> Bool
forall a. Eq a => a -> a -> Bool
== Day
firstDate
then Maybe [Char]
forall a. Maybe a
Nothing
else [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just ([Char] -> Maybe [Char]) -> [Char] -> Maybe [Char]
forall a b. (a -> b) -> a -> b
$
[Char]
"Unable to generate transactions according to "[Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ShowS
forall a. Show a => a -> [Char]
show (Text -> [Char]
T.unpack Text
periodexpr)
[Char] -> ShowS
forall a. [a] -> [a] -> [a]
++[Char]
" because "[Char] -> ShowS
forall a. [a] -> [a] -> [a]
++Day -> [Char]
forall a. Show a => a -> [Char]
show Day
d[Char] -> ShowS
forall a. [a] -> [a] -> [a]
++[Char]
" is not a first day of the "[Char] -> ShowS
forall a. [a] -> [a] -> [a]
++SmartInterval -> [Char]
forall a. Show a => a -> [Char]
show SmartInterval
x