2023-01-05 22:16:06 -05:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
module Internal.Types where
|
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
import Data.Fix (Fix (..), foldFix)
|
|
|
|
import Data.Functor.Foldable (embed)
|
|
|
|
import qualified Data.Functor.Foldable.TH as TH
|
2023-02-01 23:02:07 -05:00
|
|
|
import Database.Persist.Sql hiding (Desc, In, Statement)
|
2023-01-05 22:16:06 -05:00
|
|
|
import Dhall hiding (embed, maybe)
|
|
|
|
import Dhall.TH
|
|
|
|
import Language.Haskell.TH.Syntax (Lift)
|
|
|
|
import RIO
|
|
|
|
import qualified RIO.Map as M
|
2023-02-25 22:56:23 -05:00
|
|
|
import qualified RIO.NonEmpty as NE
|
2023-01-05 22:16:06 -05:00
|
|
|
import qualified RIO.Text as T
|
|
|
|
import RIO.Time
|
2023-02-01 23:02:07 -05:00
|
|
|
import Text.Regex.TDFA
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
2023-01-30 20:13:25 -05:00
|
|
|
-- DHALL CONFIG
|
2022-12-11 17:51:11 -05:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
makeHaskellTypesWith
|
2023-01-06 23:10:44 -05:00
|
|
|
(defaultGenerateOptions {generateToDhallInstance = False})
|
|
|
|
[ MultipleConstructors "SqlConfig" "(./dhall/Types.dhall).SqlConfig"
|
|
|
|
, MultipleConstructors "TimeUnit" "(./dhall/Types.dhall).TimeUnit"
|
|
|
|
, MultipleConstructors "Weekday" "(./dhall/Types.dhall).Weekday"
|
|
|
|
, MultipleConstructors "WeekdayPat" "(./dhall/Types.dhall).WeekdayPat"
|
|
|
|
, MultipleConstructors "MDYPat" "(./dhall/Types.dhall).MDYPat"
|
|
|
|
, MultipleConstructors "DatePat" "(./dhall/Types.dhall).DatePat"
|
|
|
|
, MultipleConstructors "MatchYMD" "(./dhall/Types.dhall).MatchYMD"
|
|
|
|
, MultipleConstructors "MatchDate" "(./dhall/Types.dhall).MatchDate"
|
|
|
|
, MultipleConstructors "SplitNum" "(./dhall/Types.dhall).SplitNum"
|
2023-01-30 20:13:25 -05:00
|
|
|
, MultipleConstructors "ExpenseBucket" "(./dhall/Types.dhall).ExpenseBucket"
|
|
|
|
, MultipleConstructors "IncomeBucket" "(./dhall/Types.dhall).IncomeBucket"
|
2023-02-12 16:23:32 -05:00
|
|
|
, MultipleConstructors "AmountType" "(./dhall/Types.dhall).AmountType"
|
2023-02-12 21:52:41 -05:00
|
|
|
, MultipleConstructors "TransferTarget" "(./dhall/Types.dhall).TransferTarget"
|
2023-01-06 23:10:44 -05:00
|
|
|
, SingleConstructor "Currency" "Currency" "(./dhall/Types.dhall).Currency"
|
|
|
|
, SingleConstructor "Gregorian" "Gregorian" "(./dhall/Types.dhall).Gregorian"
|
|
|
|
, SingleConstructor "GregorianM" "GregorianM" "(./dhall/Types.dhall).GregorianM"
|
|
|
|
, SingleConstructor "Interval" "Interval" "(./dhall/Types.dhall).Interval"
|
|
|
|
, SingleConstructor "Global" "Global" "(./dhall/Types.dhall).Global"
|
|
|
|
, SingleConstructor "RepeatPat" "RepeatPat" "(./dhall/Types.dhall).RepeatPat"
|
|
|
|
, SingleConstructor "ModPat" "ModPat" "(./dhall/Types.dhall).ModPat.Type"
|
|
|
|
, SingleConstructor "CronPat" "CronPat" "(./dhall/Types.dhall).CronPat.Type"
|
|
|
|
, SingleConstructor "Decimal" "D" "(./dhall/Types.dhall).Decimal"
|
|
|
|
, SingleConstructor "MatchVal" "MatchVal" "(./dhall/Types.dhall).MatchVal.Type"
|
|
|
|
, SingleConstructor "Manual" "Manual" "(./dhall/Types.dhall).Manual"
|
|
|
|
, SingleConstructor "Tax" "Tax" "(./dhall/Types.dhall).Tax"
|
2023-01-30 20:13:25 -05:00
|
|
|
, SingleConstructor "Amount" "Amount" "(./dhall/Types.dhall).Amount"
|
|
|
|
, SingleConstructor "TimeAmount" "TimeAmount" "(./dhall/Types.dhall).TimeAmount"
|
|
|
|
, SingleConstructor "Allocation" "Allocation" "(./dhall/Types.dhall).Allocation"
|
|
|
|
, SingleConstructor "Income" "Income" "(./dhall/Types.dhall).Income"
|
|
|
|
, SingleConstructor "Budget" "Budget" "(./dhall/Types.dhall).Budget"
|
2023-02-12 21:52:41 -05:00
|
|
|
, SingleConstructor "Transfer" "Transfer" "(./dhall/Types.dhall).Transfer"
|
2023-02-26 11:27:11 -05:00
|
|
|
, SingleConstructor "AcntSet" "AcntSet" "(./dhall/Types.dhall).AcntSet.Type"
|
2023-02-25 22:56:23 -05:00
|
|
|
, SingleConstructor "ShadowMatch" "ShadowMatch" "(./dhall/Types.dhall).ShadowMatch.Type"
|
2023-02-13 19:57:39 -05:00
|
|
|
, SingleConstructor "ShadowTransfer" "ShadowTransfer" "(./dhall/Types.dhall).ShadowTransfer"
|
2023-01-06 23:10:44 -05:00
|
|
|
]
|
2022-12-11 18:53:54 -05:00
|
|
|
|
2022-12-11 17:51:11 -05:00
|
|
|
-------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- account tree
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
data AccountTree
|
2023-01-06 23:10:44 -05:00
|
|
|
= Placeholder T.Text T.Text [AccountTree]
|
|
|
|
| Account T.Text T.Text
|
2023-02-05 18:45:56 -05:00
|
|
|
deriving (Eq, Generic, Hashable)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
TH.makeBaseFunctor ''AccountTree
|
|
|
|
|
|
|
|
deriving instance Generic (AccountTreeF a)
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-11 17:51:11 -05:00
|
|
|
deriving instance FromDhall a => FromDhall (AccountTreeF a)
|
|
|
|
|
|
|
|
data AccountRoot_ a = AccountRoot_
|
2023-01-06 23:10:44 -05:00
|
|
|
{ arAssets :: ![a]
|
|
|
|
, arEquity :: ![a]
|
|
|
|
, arExpenses :: ![a]
|
|
|
|
, arIncome :: ![a]
|
|
|
|
, arLiabilities :: ![a]
|
|
|
|
}
|
|
|
|
deriving (Generic)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
type AccountRootF = AccountRoot_ (Fix AccountTreeF)
|
|
|
|
|
|
|
|
deriving instance FromDhall AccountRootF
|
|
|
|
|
|
|
|
type AccountRoot = AccountRoot_ AccountTree
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- curencies
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq Currency
|
2023-01-05 22:16:06 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Lift Currency
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable Currency
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
type CurID = T.Text
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- DHALL CONFIG
|
2022-12-11 17:51:11 -05:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
data Config_ a = Config_
|
2023-01-06 23:10:44 -05:00
|
|
|
{ global :: !Global
|
2023-02-05 18:45:56 -05:00
|
|
|
, budget :: ![Budget]
|
2023-01-06 23:10:44 -05:00
|
|
|
, currencies :: ![Currency]
|
|
|
|
, statements :: ![Statement]
|
|
|
|
, accounts :: !a
|
|
|
|
, sqlConfig :: !SqlConfig
|
|
|
|
}
|
|
|
|
deriving (Generic)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
type ConfigF = Config_ AccountRootF
|
|
|
|
|
|
|
|
type Config = Config_ AccountRoot
|
|
|
|
|
|
|
|
unfix :: ConfigF -> Config
|
2023-01-06 23:10:44 -05:00
|
|
|
unfix c@Config_ {accounts = a} = c {accounts = a'}
|
2022-12-11 17:51:11 -05:00
|
|
|
where
|
2023-01-05 22:16:06 -05:00
|
|
|
a' =
|
2023-01-06 23:10:44 -05:00
|
|
|
AccountRoot_
|
|
|
|
{ arAssets = unfixTree arAssets
|
|
|
|
, arEquity = unfixTree arEquity
|
|
|
|
, arExpenses = unfixTree arExpenses
|
|
|
|
, arIncome = unfixTree arIncome
|
|
|
|
, arLiabilities = unfixTree arLiabilities
|
|
|
|
}
|
2022-12-11 17:51:11 -05:00
|
|
|
unfixTree f = foldFix embed <$> f a
|
|
|
|
|
|
|
|
instance FromDhall a => FromDhall (Config_ a)
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- accounts
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
type AcntID = T.Text
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- Time Patterns (for assigning when budget events will happen)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq TimeUnit
|
2023-01-05 22:16:06 -05:00
|
|
|
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving instance Ord TimeUnit
|
|
|
|
|
2023-01-27 21:05:25 -05:00
|
|
|
deriving instance Show TimeUnit
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable TimeUnit
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq Weekday
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving instance Ord Weekday
|
|
|
|
|
2023-01-27 21:05:25 -05:00
|
|
|
deriving instance Show Weekday
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable Weekday
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-02-02 23:18:36 -05:00
|
|
|
deriving instance Enum Weekday
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq WeekdayPat
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving instance Ord WeekdayPat
|
|
|
|
|
2023-01-27 21:05:25 -05:00
|
|
|
deriving instance Show WeekdayPat
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable WeekdayPat
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-19 23:13:05 -05:00
|
|
|
deriving instance Show RepeatPat
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq RepeatPat
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving instance Ord RepeatPat
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable RepeatPat
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-19 23:13:05 -05:00
|
|
|
deriving instance Show MDYPat
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq MDYPat
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving instance Ord MDYPat
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable MDYPat
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq Gregorian
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Show Gregorian
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable Gregorian
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq GregorianM
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Show GregorianM
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable GregorianM
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-19 23:13:05 -05:00
|
|
|
-- Dhall.TH rearranges my fields :(
|
|
|
|
instance Ord Gregorian where
|
2023-01-06 23:10:44 -05:00
|
|
|
compare
|
|
|
|
Gregorian {gYear = y, gMonth = m, gDay = d}
|
|
|
|
Gregorian {gYear = y', gMonth = m', gDay = d'} =
|
|
|
|
compare y y'
|
|
|
|
<> compare m m'
|
|
|
|
<> compare d d'
|
2022-12-19 23:13:05 -05:00
|
|
|
|
|
|
|
instance Ord GregorianM where
|
2023-01-06 23:10:44 -05:00
|
|
|
compare
|
|
|
|
GregorianM {gmYear = y, gmMonth = m}
|
|
|
|
GregorianM {gmYear = y', gmMonth = m'} = compare y y' <> compare m m'
|
2022-12-19 23:13:05 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq ModPat
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving instance Ord ModPat
|
|
|
|
|
2023-01-27 21:05:25 -05:00
|
|
|
deriving instance Show ModPat
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable ModPat
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq CronPat
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving instance Ord CronPat
|
|
|
|
|
2023-01-27 21:05:25 -05:00
|
|
|
deriving instance Show CronPat
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable CronPat
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq DatePat
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving instance Ord DatePat
|
|
|
|
|
2023-01-27 21:05:25 -05:00
|
|
|
deriving instance Show DatePat
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable DatePat
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- Budget (projecting into the future)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-02-05 18:45:56 -05:00
|
|
|
deriving instance Eq Budget
|
|
|
|
|
|
|
|
deriving instance Hashable Budget
|
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Eq Income
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Hashable Income
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq Tax
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable Tax
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Eq Amount
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Hashable Amount
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Eq Allocation
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Hashable Allocation
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Eq IncomeBucket
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Hashable IncomeBucket
|
|
|
|
|
|
|
|
deriving instance Show IncomeBucket
|
|
|
|
|
2023-01-30 21:47:17 -05:00
|
|
|
deriving instance Read IncomeBucket
|
|
|
|
|
|
|
|
toPersistText :: Show a => a -> PersistValue
|
|
|
|
toPersistText = PersistText . T.pack . show
|
|
|
|
|
|
|
|
fromPersistText :: Read a => T.Text -> PersistValue -> Either T.Text a
|
|
|
|
fromPersistText what (PersistText t) = case readMaybe $ T.unpack t of
|
|
|
|
Just v -> Right v
|
|
|
|
Nothing -> Left $ T.unwords ["error when reading", what, "from text:", t]
|
|
|
|
fromPersistText what x =
|
|
|
|
Left $ T.unwords ["error when deserializing", what, "; got", T.pack (show x)]
|
|
|
|
|
|
|
|
instance PersistField IncomeBucket where
|
|
|
|
toPersistValue = toPersistText
|
|
|
|
|
|
|
|
fromPersistValue = fromPersistText "IncomeBucket"
|
|
|
|
|
|
|
|
instance PersistFieldSql IncomeBucket where
|
|
|
|
sqlType _ = SqlString
|
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Eq ExpenseBucket
|
|
|
|
|
|
|
|
deriving instance Hashable ExpenseBucket
|
|
|
|
|
|
|
|
deriving instance Show ExpenseBucket
|
|
|
|
|
2023-01-30 21:47:17 -05:00
|
|
|
deriving instance Read ExpenseBucket
|
|
|
|
|
|
|
|
instance PersistField ExpenseBucket where
|
|
|
|
toPersistValue = toPersistText
|
|
|
|
|
|
|
|
fromPersistValue = fromPersistText "ExpenseBucket"
|
|
|
|
|
|
|
|
instance PersistFieldSql ExpenseBucket where
|
|
|
|
sqlType _ = SqlString
|
|
|
|
|
2023-02-12 16:23:32 -05:00
|
|
|
deriving instance Eq AmountType
|
|
|
|
|
|
|
|
deriving instance Hashable AmountType
|
|
|
|
|
2023-01-30 20:13:25 -05:00
|
|
|
deriving instance Eq TimeAmount
|
|
|
|
|
|
|
|
deriving instance Hashable TimeAmount
|
|
|
|
|
2023-02-12 21:52:41 -05:00
|
|
|
deriving instance Eq TransferTarget
|
|
|
|
|
|
|
|
deriving instance Hashable TransferTarget
|
|
|
|
|
2023-01-30 21:12:08 -05:00
|
|
|
deriving instance Eq Transfer
|
2023-01-30 20:13:25 -05:00
|
|
|
|
2023-01-30 21:12:08 -05:00
|
|
|
deriving instance Hashable Transfer
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-02-13 19:57:39 -05:00
|
|
|
deriving instance Eq ShadowTransfer
|
|
|
|
|
|
|
|
deriving instance Hashable ShadowTransfer
|
|
|
|
|
2023-02-26 11:27:11 -05:00
|
|
|
deriving instance Eq AcntSet
|
|
|
|
|
|
|
|
deriving instance Hashable AcntSet
|
|
|
|
|
2023-02-13 19:57:39 -05:00
|
|
|
deriving instance Eq ShadowMatch
|
|
|
|
|
|
|
|
deriving instance Hashable ShadowMatch
|
|
|
|
|
2022-12-11 17:51:11 -05:00
|
|
|
--------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- Statements (data from the past)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
data Statement
|
2023-01-30 22:57:42 -05:00
|
|
|
= StmtManual !Manual
|
|
|
|
| StmtImport !Import
|
2023-02-05 18:45:56 -05:00
|
|
|
deriving (Eq, Hashable, Generic, FromDhall)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving instance Eq Manual
|
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable Manual
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
data Split a v c = Split
|
2023-01-06 23:10:44 -05:00
|
|
|
{ sAcnt :: !a
|
|
|
|
, sValue :: !v
|
|
|
|
, sCurrency :: !c
|
|
|
|
, sComment :: !T.Text
|
|
|
|
}
|
|
|
|
deriving (Eq, Generic, Hashable, Show, FromDhall)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
type ExpSplit = Split SplitAcnt (Maybe SplitNum) SplitCur
|
|
|
|
|
|
|
|
data Tx s = Tx
|
2023-01-06 23:10:44 -05:00
|
|
|
{ txDescr :: !T.Text
|
|
|
|
, txDate :: !Day
|
|
|
|
, txTags :: ![T.Text]
|
|
|
|
, txSplits :: ![s]
|
|
|
|
}
|
|
|
|
deriving (Generic)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
type ExpTx = Tx ExpSplit
|
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
instance FromDhall ExpTx
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-02-05 11:34:37 -05:00
|
|
|
data TxOpts re = TxOpts
|
|
|
|
{ toDate :: !T.Text
|
|
|
|
, toAmount :: !T.Text
|
|
|
|
, toDesc :: !T.Text
|
|
|
|
, toOther :: ![T.Text]
|
|
|
|
, toDateFmt :: !T.Text
|
|
|
|
, toAmountFmt :: !re
|
|
|
|
}
|
|
|
|
deriving (Eq, Generic, Hashable, Show, FromDhall)
|
|
|
|
|
2022-12-11 17:51:11 -05:00
|
|
|
data Import = Import
|
2023-01-06 23:10:44 -05:00
|
|
|
{ impPaths :: ![FilePath]
|
2023-02-01 23:02:07 -05:00
|
|
|
, impMatches :: ![Match T.Text]
|
2023-01-06 23:10:44 -05:00
|
|
|
, impDelim :: !Word
|
2023-02-05 11:34:37 -05:00
|
|
|
, impTxOpts :: !(TxOpts T.Text)
|
2023-01-06 23:10:44 -05:00
|
|
|
, impSkipLines :: !Natural
|
|
|
|
}
|
2023-02-01 20:56:29 -05:00
|
|
|
deriving (Eq, Hashable, Generic, FromDhall)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq MatchVal
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable MatchVal
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Show MatchVal
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq MatchYMD
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable MatchYMD
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Show MatchYMD
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq MatchDate
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable MatchDate
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Show MatchDate
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
-- TODO this just looks silly...but not sure how to simplify it
|
|
|
|
instance Ord MatchYMD where
|
2023-01-06 23:10:44 -05:00
|
|
|
compare (Y y) (Y y') = compare y y'
|
|
|
|
compare (YM g) (YM g') = compare g g'
|
|
|
|
compare (YMD g) (YMD g') = compare g g'
|
|
|
|
compare (Y y) (YM g) = compare y (gmYear g) <> LT
|
|
|
|
compare (Y y) (YMD g) = compare y (gYear g) <> LT
|
|
|
|
compare (YM g) (Y y') = compare (gmYear g) y' <> GT
|
|
|
|
compare (YMD g) (Y y') = compare (gYear g) y' <> GT
|
|
|
|
compare (YM g) (YMD g') = compare g (gregM g') <> LT
|
|
|
|
compare (YMD g) (YM g') = compare (gregM g) g' <> GT
|
2022-12-19 23:13:05 -05:00
|
|
|
|
|
|
|
gregM :: Gregorian -> GregorianM
|
2023-01-06 23:10:44 -05:00
|
|
|
gregM Gregorian {gYear = y, gMonth = m} =
|
|
|
|
GregorianM {gmYear = y, gmMonth = m}
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
instance Ord MatchDate where
|
2023-01-06 23:10:44 -05:00
|
|
|
compare (On d) (On d') = compare d d'
|
|
|
|
compare (In d r) (In d' r') = compare d d' <> compare r r'
|
|
|
|
compare (On d) (In d' _) = compare d d' <> LT
|
|
|
|
compare (In d _) (On d') = compare d d' <> GT
|
2022-12-14 23:59:23 -05:00
|
|
|
|
|
|
|
deriving instance Eq SplitNum
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable SplitNum
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Show SplitNum
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-06 23:10:44 -05:00
|
|
|
-- | the value of a field in split (text version)
|
|
|
|
-- can either be a raw (constant) value, a lookup from the record, or a map
|
|
|
|
-- between the lookup and some other value
|
2023-01-05 22:16:06 -05:00
|
|
|
data SplitText t
|
2023-01-06 23:10:44 -05:00
|
|
|
= ConstT !t
|
|
|
|
| LookupT !T.Text
|
2023-01-30 22:57:42 -05:00
|
|
|
| MapT !(FieldMap T.Text t)
|
|
|
|
| Map2T !(FieldMap (T.Text, T.Text) t)
|
2023-01-06 23:10:44 -05:00
|
|
|
deriving (Eq, Generic, Hashable, Show, FromDhall)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
type SplitCur = SplitText CurID
|
|
|
|
|
|
|
|
type SplitAcnt = SplitText AcntID
|
|
|
|
|
|
|
|
data Field k v = Field
|
2023-01-06 23:10:44 -05:00
|
|
|
{ fKey :: !k
|
|
|
|
, fVal :: !v
|
|
|
|
}
|
2023-02-01 23:02:07 -05:00
|
|
|
deriving (Show, Eq, Hashable, Generic, FromDhall, Foldable, Traversable)
|
|
|
|
|
|
|
|
instance Functor (Field f) where
|
|
|
|
fmap f (Field k v) = Field k $ f v
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
type FieldMap k v = Field k (M.Map k v)
|
|
|
|
|
2023-02-01 23:02:07 -05:00
|
|
|
data MatchOther re
|
|
|
|
= Desc !(Field T.Text re)
|
2023-01-30 22:57:42 -05:00
|
|
|
| Val !(Field T.Text MatchVal)
|
2023-02-01 23:02:07 -05:00
|
|
|
deriving (Eq, Hashable, Generic, FromDhall, Functor, Foldable, Traversable)
|
|
|
|
|
|
|
|
deriving instance Show (MatchOther T.Text)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
data ToTx = ToTx
|
2023-01-06 23:10:44 -05:00
|
|
|
{ ttCurrency :: !SplitCur
|
|
|
|
, ttPath :: !SplitAcnt
|
|
|
|
, ttSplit :: ![ExpSplit]
|
|
|
|
}
|
|
|
|
deriving (Eq, Generic, Hashable, Show, FromDhall)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-02-01 23:02:07 -05:00
|
|
|
data Match re = Match
|
2023-01-30 22:57:42 -05:00
|
|
|
{ mDate :: !(Maybe MatchDate)
|
|
|
|
, mVal :: !MatchVal
|
2023-02-01 23:02:07 -05:00
|
|
|
, mDesc :: !(Maybe re)
|
|
|
|
, mOther :: ![MatchOther re]
|
2023-01-30 22:57:42 -05:00
|
|
|
, mTx :: !(Maybe ToTx)
|
|
|
|
, mTimes :: !(Maybe Natural)
|
2023-01-06 23:10:44 -05:00
|
|
|
, mPriority :: !Integer
|
|
|
|
}
|
2023-02-01 23:02:07 -05:00
|
|
|
deriving (Eq, Generic, Hashable, FromDhall, Functor)
|
|
|
|
|
|
|
|
deriving instance Show (Match T.Text)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- Specialized dhall types
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Eq Decimal
|
2023-01-05 22:16:06 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Hashable Decimal
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
deriving instance Show Decimal
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- database cache types
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
data ConfigHashes = ConfigHashes
|
2023-01-06 23:10:44 -05:00
|
|
|
{ chIncome :: ![Int]
|
|
|
|
, chExpense :: ![Int]
|
|
|
|
, chManual :: ![Int]
|
|
|
|
, chImport :: ![Int]
|
|
|
|
}
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-02-13 19:57:39 -05:00
|
|
|
data ConfigType = CTIncome | CTExpense | CTShadow | CTManual | CTImport
|
2023-01-06 23:10:44 -05:00
|
|
|
deriving (Eq, Show, Read, Enum)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
instance PersistFieldSql ConfigType where
|
2023-01-06 23:10:44 -05:00
|
|
|
sqlType _ = SqlString
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
instance PersistField ConfigType where
|
2023-01-06 23:10:44 -05:00
|
|
|
toPersistValue = PersistText . T.pack . show
|
2023-01-05 22:16:06 -05:00
|
|
|
|
2023-01-06 23:10:44 -05:00
|
|
|
-- TODO these error messages *might* be good enough?
|
|
|
|
fromPersistValue (PersistText v) =
|
|
|
|
maybe (Left $ "could not parse: " <> v) Right $ readMaybe $ T.unpack v
|
|
|
|
fromPersistValue _ = Left "wrong type"
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
2023-01-05 22:16:06 -05:00
|
|
|
-- misc
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
data AcntType
|
2023-01-06 23:10:44 -05:00
|
|
|
= AssetT
|
|
|
|
| EquityT
|
|
|
|
| ExpenseT
|
|
|
|
| IncomeT
|
|
|
|
| LiabilityT
|
|
|
|
deriving (Show, Eq, Ord, Lift, Hashable, Generic, Read, FromDhall)
|
2022-12-14 23:59:23 -05:00
|
|
|
|
|
|
|
atName :: AcntType -> T.Text
|
2023-01-05 22:16:06 -05:00
|
|
|
atName AssetT = "asset"
|
|
|
|
atName EquityT = "equity"
|
|
|
|
atName ExpenseT = "expense"
|
|
|
|
atName IncomeT = "income"
|
2022-12-14 23:59:23 -05:00
|
|
|
atName LiabilityT = "liability"
|
|
|
|
|
|
|
|
data AcntPath = AcntPath
|
2023-01-06 23:10:44 -05:00
|
|
|
{ apType :: !AcntType
|
|
|
|
, apChildren :: ![T.Text]
|
|
|
|
}
|
|
|
|
deriving (Eq, Ord, Show, Lift, Hashable, Generic, Read, FromDhall)
|
2022-12-14 23:59:23 -05:00
|
|
|
|
|
|
|
data TxRecord = TxRecord
|
2023-01-06 23:10:44 -05:00
|
|
|
{ trDate :: !Day
|
|
|
|
, trAmount :: !Rational
|
2023-02-05 12:29:43 -05:00
|
|
|
, trDesc :: !T.Text
|
|
|
|
, trOther :: !(M.Map T.Text T.Text)
|
2023-01-30 22:57:42 -05:00
|
|
|
, trFile :: !FilePath
|
2023-01-06 23:10:44 -05:00
|
|
|
}
|
|
|
|
deriving (Show, Eq, Ord)
|
2022-12-14 23:59:23 -05:00
|
|
|
|
2023-02-05 10:34:26 -05:00
|
|
|
type Bounds = (Day, Natural)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-02-05 10:34:26 -05:00
|
|
|
-- type MaybeBounds = (Maybe Day, Maybe Day)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
data Keyed a = Keyed
|
2023-01-06 23:10:44 -05:00
|
|
|
{ kKey :: !Int64
|
|
|
|
, kVal :: !a
|
|
|
|
}
|
|
|
|
deriving (Eq, Show, Functor)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
data Tree a = Branch !a ![Tree a] | Leaf !a deriving (Show)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
data AcntSign = Credit | Debit
|
2023-01-06 23:10:44 -05:00
|
|
|
deriving (Show)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
sign2Int :: AcntSign -> Int
|
2023-01-05 22:16:06 -05:00
|
|
|
sign2Int Debit = 1
|
2022-12-11 17:51:11 -05:00
|
|
|
sign2Int Credit = 1
|
|
|
|
|
|
|
|
accountSign :: AcntType -> AcntSign
|
2023-01-05 22:16:06 -05:00
|
|
|
accountSign AssetT = Debit
|
|
|
|
accountSign ExpenseT = Debit
|
|
|
|
accountSign IncomeT = Credit
|
2022-12-11 17:51:11 -05:00
|
|
|
accountSign LiabilityT = Credit
|
2023-01-05 22:16:06 -05:00
|
|
|
accountSign EquityT = Credit
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
type RawSplit = Split AcntID (Maybe Rational) CurID
|
|
|
|
|
|
|
|
type BalSplit = Split AcntID Rational CurID
|
|
|
|
|
|
|
|
type RawTx = Tx RawSplit
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2022-12-11 17:51:11 -05:00
|
|
|
type BalTx = Tx BalSplit
|
2023-01-06 23:10:44 -05:00
|
|
|
|
2023-01-30 22:57:42 -05:00
|
|
|
data MatchRes a = MatchPass !a | MatchFail | MatchSkip
|
2023-01-07 23:42:04 -05:00
|
|
|
|
2023-01-24 23:24:41 -05:00
|
|
|
data BalanceType = TooFewSplits | NotOneBlank deriving (Show)
|
2023-01-07 23:42:04 -05:00
|
|
|
|
2023-01-27 23:33:34 -05:00
|
|
|
data MatchType = MatchNumeric | MatchText deriving (Show)
|
|
|
|
|
|
|
|
data SplitIDType = AcntField | CurField deriving (Show)
|
|
|
|
|
|
|
|
data LookupSuberr
|
2023-01-30 22:57:42 -05:00
|
|
|
= SplitIDField !SplitIDType
|
2023-01-27 23:33:34 -05:00
|
|
|
| SplitValField
|
2023-01-30 22:57:42 -05:00
|
|
|
| MatchField !MatchType
|
|
|
|
| DBKey !SplitIDType
|
2023-01-27 23:33:34 -05:00
|
|
|
deriving (Show)
|
2023-01-25 23:04:54 -05:00
|
|
|
|
2023-01-27 20:31:13 -05:00
|
|
|
data AllocationSuberr
|
|
|
|
= NoAllocations
|
|
|
|
| ExceededTotal
|
|
|
|
| MissingBlank
|
|
|
|
| TooManyBlanks
|
|
|
|
deriving (Show)
|
|
|
|
|
2023-02-05 10:34:26 -05:00
|
|
|
data PatternSuberr = ZeroLength | ZeroRepeats deriving (Show)
|
|
|
|
|
2023-01-24 23:24:41 -05:00
|
|
|
data InsertError
|
2023-01-30 22:57:42 -05:00
|
|
|
= RegexError !T.Text
|
|
|
|
| MatchValPrecisionError !Natural !Natural
|
2023-02-25 22:56:23 -05:00
|
|
|
| AccountError !AcntID !(NE.NonEmpty AcntType)
|
2023-01-30 22:57:42 -05:00
|
|
|
| InsertIOError !T.Text
|
|
|
|
| ParseError !T.Text
|
2023-02-05 12:29:43 -05:00
|
|
|
| ConversionError !T.Text
|
2023-01-30 22:57:42 -05:00
|
|
|
| LookupError !LookupSuberr !T.Text
|
|
|
|
| BalanceError !BalanceType !CurID ![RawSplit]
|
|
|
|
| IncomeError !DatePat
|
2023-02-05 10:34:26 -05:00
|
|
|
| PatternError !Natural !Natural !(Maybe Natural) !PatternSuberr
|
|
|
|
| BoundsError !Gregorian !(Maybe Gregorian)
|
2023-02-01 23:02:07 -05:00
|
|
|
| StatementError ![TxRecord] ![MatchRe]
|
2023-01-24 23:24:41 -05:00
|
|
|
deriving (Show)
|
2023-01-07 23:42:04 -05:00
|
|
|
|
2023-01-24 23:24:41 -05:00
|
|
|
newtype InsertException = InsertException [InsertError] deriving (Show)
|
2023-01-07 23:42:04 -05:00
|
|
|
|
|
|
|
instance Exception InsertException
|
2023-01-24 23:24:41 -05:00
|
|
|
|
|
|
|
type EitherErr = Either InsertError
|
2023-01-26 23:41:45 -05:00
|
|
|
|
|
|
|
type EitherErrs = Either [InsertError]
|
2023-02-01 20:56:29 -05:00
|
|
|
|
|
|
|
data XGregorian = XGregorian
|
|
|
|
{ xgYear :: !Int
|
|
|
|
, xgMonth :: !Int
|
|
|
|
, xgDay :: !Int
|
|
|
|
, xgDayOfWeek :: !Int
|
|
|
|
}
|
2023-02-01 23:02:07 -05:00
|
|
|
|
|
|
|
type MatchRe = Match (T.Text, Regex)
|
|
|
|
|
2023-02-05 11:34:37 -05:00
|
|
|
type TxOptsRe = TxOpts (T.Text, Regex)
|
|
|
|
|
2023-02-01 23:02:07 -05:00
|
|
|
type MatchOtherRe = MatchOther (T.Text, Regex)
|
|
|
|
|
|
|
|
instance Show (Match (T.Text, Regex)) where
|
|
|
|
show = show . fmap fst
|