2023-01-05 22:16:06 -05:00
|
|
|
{-# LANGUAGE GADTs #-}
|
2022-12-11 17:51:11 -05:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2023-01-05 22:16:06 -05:00
|
|
|
{-# LANGUAGE TupleSections #-}
|
2023-01-28 22:55:07 -05:00
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
module Internal.Insert
|
|
|
|
( insertStatements
|
|
|
|
, insertBudget
|
2023-01-05 22:16:06 -05:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Hashable
|
|
|
|
import Database.Persist.Class
|
|
|
|
import Database.Persist.Sql hiding (Single, Statement)
|
|
|
|
import Internal.Database.Model
|
|
|
|
import Internal.Statement
|
|
|
|
import Internal.Types hiding (sign)
|
|
|
|
import Internal.Utils
|
|
|
|
import RIO hiding (to)
|
|
|
|
import qualified RIO.Text as T
|
|
|
|
import RIO.Time
|
|
|
|
|
2022-12-11 17:51:11 -05:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- intervals
|
|
|
|
|
2023-01-28 19:32:56 -05:00
|
|
|
expandDatePat :: Bounds -> DatePat -> [Day]
|
|
|
|
expandDatePat (a, b) (Cron cp) = filter (cronPatternMatches cp) [a .. b]
|
2023-01-05 22:16:06 -05:00
|
|
|
expandDatePat i (Mod mp) = expandModPat mp i
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-28 19:32:56 -05:00
|
|
|
expandModPat :: ModPat -> Bounds -> [Day]
|
2023-01-05 22:16:06 -05:00
|
|
|
expandModPat
|
2023-01-25 23:04:54 -05:00
|
|
|
ModPat {mpStart = s, mpBy = b, mpUnit = u, mpRepeats = r}
|
2023-01-28 19:32:56 -05:00
|
|
|
(lower, upper) =
|
|
|
|
takeWhile (<= upper) $
|
|
|
|
(`addFun` start) . (* b')
|
|
|
|
<$> maybe id (take . fromIntegral) r [0 ..]
|
2023-01-05 22:16:06 -05:00
|
|
|
where
|
2023-01-28 19:32:56 -05:00
|
|
|
start = maybe lower fromGregorian' s
|
2023-01-05 22:16:06 -05:00
|
|
|
b' = fromIntegral b
|
|
|
|
addFun = case u of
|
|
|
|
Day -> addDays
|
|
|
|
Week -> addDays . (* 7)
|
|
|
|
Month -> addGregorianMonthsClip
|
|
|
|
Year -> addGregorianYearsClip
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-19 23:13:05 -05:00
|
|
|
-- TODO this can be optimized to prevent filtering a bunch of dates for
|
|
|
|
-- one/a few cron patterns
|
2022-12-11 17:51:11 -05:00
|
|
|
cronPatternMatches :: CronPat -> Day -> Bool
|
2023-01-05 22:16:06 -05:00
|
|
|
cronPatternMatches
|
|
|
|
CronPat
|
|
|
|
{ cronWeekly = w
|
|
|
|
, cronYear = y
|
|
|
|
, cronMonth = m
|
|
|
|
, cronDay = d
|
|
|
|
}
|
|
|
|
x =
|
2023-01-29 11:35:30 -05:00
|
|
|
yMaybe y' y && mdMaybe m' m && mdMaybe d' d && wdMaybe (dayOfWeek_ x) w
|
2023-01-05 22:16:06 -05:00
|
|
|
where
|
|
|
|
testMaybe = maybe True
|
2023-01-29 10:51:03 -05:00
|
|
|
yMaybe z = testMaybe (mdyPatternMatches (fromIntegral z))
|
|
|
|
mdMaybe z = testMaybe (mdyPatternMatches (fromIntegral z))
|
2023-01-05 22:16:06 -05:00
|
|
|
wdMaybe z = testMaybe (`weekdayPatternMatches` z)
|
|
|
|
(y', m', d') = toGregorian x
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-14 23:59:23 -05:00
|
|
|
dayOfWeek_ :: Day -> Weekday
|
|
|
|
dayOfWeek_ d = case dayOfWeek d of
|
2023-01-05 22:16:06 -05:00
|
|
|
Monday -> Mon
|
|
|
|
Tuesday -> Tue
|
2022-12-14 23:59:23 -05:00
|
|
|
Wednesday -> Wed
|
2023-01-05 22:16:06 -05:00
|
|
|
Thursday -> Thu
|
|
|
|
Friday -> Fri
|
|
|
|
Saturday -> Sat
|
|
|
|
Sunday -> Sun
|
2022-12-14 23:59:23 -05:00
|
|
|
|
|
|
|
weekdayPatternMatches :: WeekdayPat -> Weekday -> Bool
|
2023-01-05 22:16:06 -05:00
|
|
|
weekdayPatternMatches (OnDay x) = (== x)
|
2022-12-11 17:51:11 -05:00
|
|
|
weekdayPatternMatches (OnDays xs) = (`elem` xs)
|
|
|
|
|
2023-01-29 10:51:03 -05:00
|
|
|
mdyPatternMatches :: Natural -> MDYPat -> Bool
|
|
|
|
mdyPatternMatches x p = case p of
|
|
|
|
Single y -> x == y
|
|
|
|
Multi xs -> x `elem` xs
|
2023-01-05 22:16:06 -05:00
|
|
|
Repeat (RepeatPat {rpStart = s, rpBy = b, rpRepeats = r}) ->
|
2023-01-29 10:51:03 -05:00
|
|
|
s >= x && mod x b == 0 && maybe True (\y -> x <= s + b * y) r
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- budget
|
|
|
|
|
2023-01-25 23:04:54 -05:00
|
|
|
insertBudget :: MonadUnliftIO m => Budget -> MappingT m [InsertError]
|
2023-01-05 22:16:06 -05:00
|
|
|
insertBudget Budget {income = is, expenses = es} = do
|
2023-01-28 22:55:07 -05:00
|
|
|
es1 <- mapM insertIncome is
|
|
|
|
es2 <- mapM insertExpense es
|
|
|
|
return $ concat $ es1 ++ es2
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
-- TODO this hashes twice (not that it really matters)
|
2023-01-05 22:16:06 -05:00
|
|
|
whenHash
|
2023-01-24 23:24:41 -05:00
|
|
|
:: (Hashable a, MonadUnliftIO m)
|
2023-01-05 22:16:06 -05:00
|
|
|
=> ConfigType
|
|
|
|
-> a
|
2023-01-24 23:24:41 -05:00
|
|
|
-> b
|
|
|
|
-> (Key CommitR -> MappingT m b)
|
|
|
|
-> MappingT m b
|
|
|
|
whenHash t o def f = do
|
2022-12-11 17:51:11 -05:00
|
|
|
let h = hash o
|
|
|
|
hs <- asks kmNewCommits
|
2023-01-24 23:24:41 -05:00
|
|
|
if h `elem` hs then f =<< lift (insert $ CommitR h t) else return def
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-27 20:31:13 -05:00
|
|
|
insertIncome :: MonadUnliftIO m => Income -> MappingT m [InsertError]
|
2023-01-05 22:16:06 -05:00
|
|
|
insertIncome
|
|
|
|
i@Income
|
|
|
|
{ incCurrency = cur
|
|
|
|
, incWhen = dp
|
|
|
|
, incAccount = from
|
2023-01-28 22:55:07 -05:00
|
|
|
, incTaxes = taxes
|
2023-01-05 22:16:06 -05:00
|
|
|
} =
|
2023-01-28 22:55:07 -05:00
|
|
|
whenHash CTIncome i [] $ \c ->
|
2023-01-28 19:32:56 -05:00
|
|
|
unlessLeft (balanceIncome i) $ \balanced -> do
|
2023-01-28 22:55:07 -05:00
|
|
|
bounds <- (liftIO . resolveBounds) =<< asks kmBudgetInterval
|
|
|
|
fmap concat $ forM (expandDatePat bounds dp) $ \day -> do
|
|
|
|
-- TODO why are these separate?
|
|
|
|
nontaxRes <- alloTxs concat (allocationToTx from day) balanced
|
|
|
|
taxRes <- alloTxs (fmap (,Fixed)) (taxToTx from day cur) taxes
|
|
|
|
unlessLefts_ (concatEithers2 nontaxRes taxRes (++)) $ \txs ->
|
|
|
|
lift $ mapM_ (\(t, b) -> insertTxBucket (Just b) c t) txs
|
|
|
|
where
|
|
|
|
alloTxs squish toTx = fmap (fmap squish . concatEithersL) . mapM toTx
|
2023-01-27 20:31:13 -05:00
|
|
|
|
|
|
|
balanceIncome :: Income -> EitherErr [BalAllocation]
|
2023-01-05 22:16:06 -05:00
|
|
|
balanceIncome
|
|
|
|
Income
|
|
|
|
{ incGross = g
|
2023-01-28 19:32:56 -05:00
|
|
|
, incWhen = dp
|
2023-01-05 22:16:06 -05:00
|
|
|
, incPretax = pre
|
|
|
|
, incTaxes = tax
|
|
|
|
, incPosttax = post
|
2023-01-28 19:32:56 -05:00
|
|
|
} = (preRat ++) <$> balancePostTax dp bal postRat
|
2023-01-05 22:16:06 -05:00
|
|
|
where
|
|
|
|
preRat = mapAlloAmts dec2Rat <$> pre
|
|
|
|
postRat = mapAlloAmts (fmap dec2Rat) <$> post
|
|
|
|
bal = dec2Rat g - (sumAllocations preRat + sumTaxes tax)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
mapAlloAmts :: (a -> b) -> Allocation a -> Allocation b
|
2023-01-05 22:16:06 -05:00
|
|
|
mapAlloAmts f a@Allocation {alloAmts = as} = a {alloAmts = fmap f <$> as}
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
sumAllocations :: [BalAllocation] -> Rational
|
|
|
|
sumAllocations = sum . concatMap (fmap amtValue . alloAmts)
|
|
|
|
|
|
|
|
sumTaxes :: [Tax] -> Rational
|
|
|
|
sumTaxes = sum . fmap (dec2Rat . taxValue)
|
|
|
|
|
2023-01-27 21:05:25 -05:00
|
|
|
balancePostTax :: DatePat -> Rational -> [RawAllocation] -> EitherErr [BalAllocation]
|
|
|
|
balancePostTax dp bal as
|
|
|
|
| null as = err NoAllocations
|
2022-12-11 17:51:11 -05:00
|
|
|
| otherwise = case partitionEithers $ fmap hasVal as of
|
|
|
|
([([empty], nonmissing)], bs) ->
|
2023-01-05 22:16:06 -05:00
|
|
|
let s = bal - sumAllocations (nonmissing : bs)
|
|
|
|
in if s < 0
|
2023-01-27 21:05:25 -05:00
|
|
|
then err ExceededTotal
|
2023-01-05 22:16:06 -05:00
|
|
|
else Right $ mapAmts (empty {amtValue = s} :) nonmissing : bs
|
2023-01-27 21:05:25 -05:00
|
|
|
([], _) -> err MissingBlank
|
|
|
|
_ -> err TooManyBlanks
|
2022-12-11 17:51:11 -05:00
|
|
|
where
|
2023-01-05 22:16:06 -05:00
|
|
|
hasVal a@Allocation {alloAmts = xs} =
|
2022-12-11 17:51:11 -05:00
|
|
|
case partitionEithers $ fmap maybeAmt xs of
|
2023-01-05 22:16:06 -05:00
|
|
|
([], bs) -> Right a {alloAmts = bs}
|
|
|
|
(unbal, bs) -> Left (unbal, a {alloAmts = bs})
|
|
|
|
maybeAmt a@Amount {amtValue = Just v} = Right a {amtValue = v}
|
|
|
|
maybeAmt a = Left a
|
2023-01-27 21:05:25 -05:00
|
|
|
err t = Left $ AllocationError t dp
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
-- TODO lens reinvention
|
|
|
|
mapAmts :: ([Amount a] -> [Amount b]) -> Allocation a -> Allocation b
|
2023-01-05 22:16:06 -05:00
|
|
|
mapAmts f a@Allocation {alloAmts = xs} = a {alloAmts = f xs}
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
allocationToTx
|
2023-01-05 22:23:22 -05:00
|
|
|
:: MonadUnliftIO m
|
2023-01-05 22:16:06 -05:00
|
|
|
=> AcntID
|
|
|
|
-> Day
|
|
|
|
-> BalAllocation
|
2023-01-28 22:55:07 -05:00
|
|
|
-> MappingT m (EitherErrs [(KeyTx, Bucket)])
|
2023-01-05 22:16:06 -05:00
|
|
|
allocationToTx
|
|
|
|
from
|
|
|
|
day
|
|
|
|
Allocation
|
|
|
|
{ alloPath = to
|
|
|
|
, alloBucket = b
|
|
|
|
, alloCurrency = cur
|
|
|
|
, alloAmts = as
|
|
|
|
} =
|
2023-01-28 22:55:07 -05:00
|
|
|
second (fmap (,b)) . concatEithersL <$> mapM (transferToTx day from to cur) as
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-28 22:55:07 -05:00
|
|
|
taxToTx
|
|
|
|
:: MonadUnliftIO m
|
|
|
|
=> AcntID
|
|
|
|
-> Day
|
|
|
|
-> T.Text
|
|
|
|
-> Tax
|
|
|
|
-> MappingT m (EitherErrs KeyTx)
|
2023-01-05 22:16:06 -05:00
|
|
|
taxToTx from day cur Tax {taxAcnt = to, taxValue = v} =
|
2022-12-11 17:51:11 -05:00
|
|
|
txPair day from to cur (dec2Rat v) ""
|
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
transferToTx
|
2023-01-05 22:23:22 -05:00
|
|
|
:: MonadUnliftIO m
|
2023-01-05 22:16:06 -05:00
|
|
|
=> Day
|
|
|
|
-> AcntID
|
|
|
|
-> AcntID
|
|
|
|
-> T.Text
|
|
|
|
-> BalAmount
|
2023-01-28 22:55:07 -05:00
|
|
|
-> MappingT m (EitherErrs KeyTx)
|
2023-01-05 22:16:06 -05:00
|
|
|
transferToTx day from to cur Amount {amtValue = v, amtDesc = d} =
|
2022-12-11 17:51:11 -05:00
|
|
|
txPair day from to cur v d
|
|
|
|
|
2023-01-28 22:55:07 -05:00
|
|
|
insertExpense :: MonadUnliftIO m => Expense -> MappingT m [InsertError]
|
2023-01-05 22:16:06 -05:00
|
|
|
insertExpense
|
|
|
|
e@Expense
|
|
|
|
{ expFrom = from
|
|
|
|
, expTo = to
|
|
|
|
, expCurrency = cur
|
|
|
|
, expBucket = buc
|
|
|
|
, expAmounts = as
|
|
|
|
} = do
|
2023-01-28 22:55:07 -05:00
|
|
|
whenHash CTExpense e [] $ \key -> concat <$> mapM (go key) as
|
2023-01-25 23:04:54 -05:00
|
|
|
where
|
|
|
|
go key amt = do
|
2023-01-28 22:55:07 -05:00
|
|
|
res <- timeAmountToTx from to cur amt
|
|
|
|
unlessLefts_ res $
|
|
|
|
lift . mapM_ (insertTxBucket (Just buc) key)
|
2023-01-05 22:16:06 -05:00
|
|
|
|
|
|
|
timeAmountToTx
|
2023-01-05 22:23:22 -05:00
|
|
|
:: MonadUnliftIO m
|
2023-01-05 22:16:06 -05:00
|
|
|
=> AcntID
|
|
|
|
-> AcntID
|
2023-01-25 23:04:54 -05:00
|
|
|
-> CurID
|
2023-01-05 22:16:06 -05:00
|
|
|
-> TimeAmount
|
2023-01-28 22:55:07 -05:00
|
|
|
-> MappingT m (EitherErrs [KeyTx])
|
2023-01-05 22:16:06 -05:00
|
|
|
timeAmountToTx
|
|
|
|
from
|
|
|
|
to
|
|
|
|
cur
|
|
|
|
TimeAmount
|
|
|
|
{ taWhen = dp
|
|
|
|
, taAmt =
|
|
|
|
Amount
|
|
|
|
{ amtValue = v
|
|
|
|
, amtDesc = d
|
|
|
|
}
|
|
|
|
} = do
|
|
|
|
bounds <- (liftIO . resolveBounds) =<< asks kmBudgetInterval
|
2023-01-28 22:55:07 -05:00
|
|
|
concatEithersL <$> mapM tx (expandDatePat bounds dp)
|
2023-01-05 22:16:06 -05:00
|
|
|
where
|
|
|
|
tx day = txPair day from to cur (dec2Rat v) d
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- statements
|
|
|
|
|
2023-01-25 23:04:54 -05:00
|
|
|
insertStatements :: MonadUnliftIO m => Config -> MappingT m [InsertError]
|
2023-01-27 20:31:13 -05:00
|
|
|
insertStatements conf = concat <$> mapM insertStatement (statements conf)
|
2023-01-25 23:04:54 -05:00
|
|
|
|
2023-01-27 20:31:13 -05:00
|
|
|
insertStatement :: MonadUnliftIO m => Statement -> MappingT m [InsertError]
|
2023-01-28 22:55:07 -05:00
|
|
|
insertStatement (StmtManual m) = insertManual m
|
2022-12-11 17:51:11 -05:00
|
|
|
insertStatement (StmtImport i) = insertImport i
|
|
|
|
|
2023-01-28 22:55:07 -05:00
|
|
|
insertManual :: MonadUnliftIO m => Manual -> MappingT m [InsertError]
|
2023-01-05 22:16:06 -05:00
|
|
|
insertManual
|
|
|
|
m@Manual
|
|
|
|
{ manualDate = dp
|
|
|
|
, manualFrom = from
|
|
|
|
, manualTo = to
|
|
|
|
, manualValue = v
|
|
|
|
, manualCurrency = u
|
|
|
|
, manualDesc = e
|
|
|
|
} = do
|
2023-01-28 22:55:07 -05:00
|
|
|
whenHash CTManual m [] $ \c -> do
|
2023-01-05 22:16:06 -05:00
|
|
|
bounds <- (liftIO . resolveBounds) =<< asks kmStatementInterval
|
2023-01-28 22:55:07 -05:00
|
|
|
res <- mapM tx $ expandDatePat bounds dp
|
|
|
|
unlessLefts_ (concatEithersL res) $ lift . mapM_ (insertTx c)
|
2023-01-05 22:16:06 -05:00
|
|
|
where
|
|
|
|
tx day = txPair day from to u (dec2Rat v) e
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-27 20:31:13 -05:00
|
|
|
insertImport :: MonadUnliftIO m => Import -> MappingT m [InsertError]
|
|
|
|
insertImport i = whenHash CTImport i [] $ \c -> do
|
2022-12-11 17:51:11 -05:00
|
|
|
-- TODO this isn't efficient, the whole file will be read and maybe no
|
|
|
|
-- transactions will be desired
|
2023-01-28 22:55:07 -05:00
|
|
|
recoverIO (readImport i) $ \r -> unlessLefts r $ \bs -> do
|
|
|
|
bounds <- asks kmStatementInterval
|
|
|
|
res <- mapM resolveTx $ filter (inMaybeBounds bounds . txDate) bs
|
|
|
|
unlessLefts_ (concatEithersL res) $ lift . mapM_ (insertTx c)
|
|
|
|
where
|
|
|
|
recoverIO x rest = do
|
|
|
|
res <- tryIO x
|
|
|
|
case res of
|
|
|
|
Right r -> rest r
|
|
|
|
-- If file is not found (or something else happens) then collect the
|
|
|
|
-- error try the remaining imports
|
|
|
|
Left e -> return [InsertIOError $ showT e]
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- low-level transaction stuff
|
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
txPair
|
2023-01-05 22:23:22 -05:00
|
|
|
:: MonadUnliftIO m
|
2023-01-05 22:16:06 -05:00
|
|
|
=> Day
|
|
|
|
-> AcntID
|
|
|
|
-> AcntID
|
|
|
|
-> T.Text
|
|
|
|
-> Rational
|
|
|
|
-> T.Text
|
2023-01-28 22:55:07 -05:00
|
|
|
-> MappingT m (EitherErrs KeyTx)
|
2022-12-11 17:51:11 -05:00
|
|
|
txPair day from to cur val desc = resolveTx tx
|
|
|
|
where
|
2023-01-05 22:16:06 -05:00
|
|
|
split a v = Split {sAcnt = a, sValue = v, sComment = "", sCurrency = cur}
|
|
|
|
tx =
|
|
|
|
Tx
|
|
|
|
{ txDescr = desc
|
|
|
|
, txDate = day
|
|
|
|
, txTags = []
|
|
|
|
, txSplits = [split from (-val), split to val]
|
|
|
|
}
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-28 22:55:07 -05:00
|
|
|
resolveTx :: MonadUnliftIO m => BalTx -> MappingT m (EitherErrs KeyTx)
|
2023-01-05 22:16:06 -05:00
|
|
|
resolveTx t@Tx {txSplits = ss} = do
|
2023-01-28 22:55:07 -05:00
|
|
|
res <- concatEithersL <$> mapM resolveSplit ss
|
|
|
|
return $ fmap (\kss -> t {txSplits = kss}) res
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-28 22:55:07 -05:00
|
|
|
resolveSplit :: MonadUnliftIO m => BalSplit -> MappingT m (EitherErrs KeySplit)
|
2023-01-05 22:16:06 -05:00
|
|
|
resolveSplit s@Split {sAcnt = p, sCurrency = c, sValue = v} = do
|
2022-12-11 17:51:11 -05:00
|
|
|
aid <- lookupAccountKey p
|
|
|
|
cid <- lookupCurrency c
|
|
|
|
sign <- lookupAccountSign p
|
|
|
|
-- TODO correct sign here?
|
|
|
|
-- TODO lenses would be nice here
|
2023-01-28 22:55:07 -05:00
|
|
|
return $ concatEither3 aid cid sign $ \aid_ cid_ sign_ ->
|
|
|
|
s
|
|
|
|
{ sAcnt = aid_
|
|
|
|
, sCurrency = cid_
|
|
|
|
, sValue = v * fromIntegral (sign2Int sign_)
|
|
|
|
}
|
|
|
|
|
|
|
|
-- return $ case (aid, cid, sign) of
|
|
|
|
-- _ -> Nothing
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-05 22:23:22 -05:00
|
|
|
insertTxBucket :: MonadUnliftIO m => Maybe Bucket -> Key CommitR -> KeyTx -> SqlPersistT m ()
|
2023-01-05 22:16:06 -05:00
|
|
|
insertTxBucket b c Tx {txDate = d, txDescr = e, txSplits = ss} = do
|
2022-12-11 17:51:11 -05:00
|
|
|
k <- insert $ TransactionR c d e (fmap (T.pack . show) b)
|
|
|
|
mapM_ (insertSplit k) ss
|
|
|
|
|
2023-01-05 22:23:22 -05:00
|
|
|
insertTx :: MonadUnliftIO m => Key CommitR -> KeyTx -> SqlPersistT m ()
|
2022-12-11 17:51:11 -05:00
|
|
|
insertTx = insertTxBucket Nothing
|
|
|
|
|
2023-01-05 22:23:22 -05:00
|
|
|
insertSplit :: MonadUnliftIO m => Key TransactionR -> KeySplit -> SqlPersistT m ()
|
2023-01-05 22:16:06 -05:00
|
|
|
insertSplit t Split {sAcnt = aid, sCurrency = cid, sValue = v, sComment = c} = do
|
2022-12-11 17:51:11 -05:00
|
|
|
insert_ $ SplitR t cid aid c v
|
2023-01-28 22:55:07 -05:00
|
|
|
|
|
|
|
lookupAccount :: MonadUnliftIO m => AcntID -> MappingT m (EitherErr (Key AccountR, AcntSign))
|
|
|
|
lookupAccount p = lookupErr (DBKey AcntField) p <$> asks kmAccount
|
|
|
|
|
|
|
|
lookupAccountKey :: MonadUnliftIO m => AcntID -> MappingT m (EitherErr (Key AccountR))
|
|
|
|
lookupAccountKey = fmap (fmap fst) . lookupAccount
|
|
|
|
|
|
|
|
lookupAccountSign :: MonadUnliftIO m => AcntID -> MappingT m (EitherErr AcntSign)
|
|
|
|
lookupAccountSign = fmap (fmap snd) . lookupAccount
|
|
|
|
|
|
|
|
lookupCurrency :: MonadUnliftIO m => T.Text -> MappingT m (EitherErr (Key CurrencyR))
|
|
|
|
lookupCurrency c = lookupErr (DBKey CurField) c <$> asks kmCurrency
|