|
|
@ -23,12 +23,18 @@ import qualified RIO.Vector as V
|
|
|
|
|
|
|
|
|
|
|
|
-- TODO this probably won't scale well (pipes?)
|
|
|
|
-- TODO this probably won't scale well (pipes?)
|
|
|
|
|
|
|
|
|
|
|
|
readImport :: MonadUnliftIO m => Import -> MappingT m StatementRes
|
|
|
|
readImport :: MonadUnliftIO m => Import -> MappingT m [BalTx]
|
|
|
|
readImport Import {..} = do
|
|
|
|
readImport Import {..} = do
|
|
|
|
rs <- L.sort . concat <$> mapM (readImport_ impSkipLines impDelim impTxOpts) impPaths
|
|
|
|
rs <- L.sort . concat <$> mapM (readImport_ impSkipLines impDelim impTxOpts) impPaths
|
|
|
|
-- TODO show more useful information here (eg which file emitted this
|
|
|
|
case matchRecords impMatches rs of
|
|
|
|
-- error and possibly where)
|
|
|
|
Right (ts, es, notfound) -> do
|
|
|
|
return $ matchRecords impMatches rs
|
|
|
|
liftIO $ mapM_ putStrLn $ reverse es
|
|
|
|
|
|
|
|
liftIO $ mapM_ print notfound
|
|
|
|
|
|
|
|
return ts
|
|
|
|
|
|
|
|
Left e -> do
|
|
|
|
|
|
|
|
liftIO $ print e
|
|
|
|
|
|
|
|
-- TODO make sure that a blank list results in a cache reset in the db
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
readImport_
|
|
|
|
readImport_
|
|
|
|
:: MonadUnliftIO m
|
|
|
|
:: MonadUnliftIO m
|
|
|
@ -61,15 +67,19 @@ parseTxRecord TxOpts {..} r = do
|
|
|
|
d' <- parseTimeM True defaultTimeLocale (T.unpack toDateFmt) d
|
|
|
|
d' <- parseTimeM True defaultTimeLocale (T.unpack toDateFmt) d
|
|
|
|
return $ Just $ TxRecord d' a e os
|
|
|
|
return $ Just $ TxRecord d' a e os
|
|
|
|
|
|
|
|
|
|
|
|
matchRecords :: [Match] -> [TxRecord] -> StatementRes
|
|
|
|
matchRecords :: [Match] -> [TxRecord] -> PureErr ([BalTx], [String], [Match])
|
|
|
|
matchRecords ms rs = case matchAll (matchPriorities ms) rs of
|
|
|
|
matchRecords ms rs = do
|
|
|
|
Left e -> StatementFail $ StatementErrors [] [] [e]
|
|
|
|
(matched, unmatched, notfound) <- matchAll (matchPriorities ms) rs
|
|
|
|
Right (matched, unmatched, notfound) ->
|
|
|
|
let (es, ts) =
|
|
|
|
-- TODO record number of times each match hits for debugging
|
|
|
|
partitionEithers $
|
|
|
|
let (errors, matched_) = partitionEithers $ balanceTx <$> matched
|
|
|
|
fmap Just . balanceTx <$> catMaybes matched
|
|
|
|
in case (matched_, unmatched, notfound, errors) of
|
|
|
|
let bu = fmap (\x -> T.pack $ "unmatched: " ++ show x) unmatched
|
|
|
|
(xs, [], [], []) -> StatementPass xs
|
|
|
|
return
|
|
|
|
(_, us, ns, es) -> StatementFail $ StatementErrors us ns es
|
|
|
|
( catMaybes ts
|
|
|
|
|
|
|
|
, T.unpack <$> (es ++ bu)
|
|
|
|
|
|
|
|
, -- TODO record number of times each match hits for debugging
|
|
|
|
|
|
|
|
notfound
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
matchPriorities :: [Match] -> [MatchGroup]
|
|
|
|
matchPriorities :: [Match] -> [MatchGroup]
|
|
|
|
matchPriorities =
|
|
|
|
matchPriorities =
|
|
|
@ -146,7 +156,7 @@ matchDec m@Match {mTimes = t} =
|
|
|
|
where
|
|
|
|
where
|
|
|
|
t' = fmap pred t
|
|
|
|
t' = fmap pred t
|
|
|
|
|
|
|
|
|
|
|
|
matchAll :: [MatchGroup] -> [TxRecord] -> PureErr ([RawTx], [TxRecord], [Match])
|
|
|
|
matchAll :: [MatchGroup] -> [TxRecord] -> PureErr ([Maybe RawTx], [TxRecord], [Match])
|
|
|
|
matchAll = go ([], [])
|
|
|
|
matchAll = go ([], [])
|
|
|
|
where
|
|
|
|
where
|
|
|
|
go (matched, unused) gs rs = case (gs, rs) of
|
|
|
|
go (matched, unused) gs rs = case (gs, rs) of
|
|
|
@ -156,21 +166,16 @@ matchAll = go ([], [])
|
|
|
|
(ts, unmatched, us) <- matchGroup g rs
|
|
|
|
(ts, unmatched, us) <- matchGroup g rs
|
|
|
|
go (ts ++ matched, us ++ unused) gs' unmatched
|
|
|
|
go (ts ++ matched, us ++ unused) gs' unmatched
|
|
|
|
|
|
|
|
|
|
|
|
matchGroup :: MatchGroup -> [TxRecord] -> PureErr ([RawTx], [TxRecord], [Match])
|
|
|
|
matchGroup :: MatchGroup -> [TxRecord] -> PureErr ([Maybe RawTx], [TxRecord], [Match])
|
|
|
|
matchGroup MatchGroup {mgDate = ds, mgNoDate = ns} rs = do
|
|
|
|
matchGroup MatchGroup {mgDate = ds, mgNoDate = ns} rs = do
|
|
|
|
(md, rest, ud) <- matchDates ds rs
|
|
|
|
(md, rest, ud) <- matchDates ds rs
|
|
|
|
(mn, unmatched, un) <- matchNonDates ns rest
|
|
|
|
(mn, unmatched, un) <- matchNonDates ns rest
|
|
|
|
return (md ++ mn, unmatched, filter ((/= Nothing) . mTimes) $ ud ++ un)
|
|
|
|
return (md ++ mn, unmatched, filter ((/= Nothing) . mTimes) $ ud ++ un)
|
|
|
|
|
|
|
|
|
|
|
|
matchDates :: [Match] -> [TxRecord] -> PureErr ([RawTx], [TxRecord], [Match])
|
|
|
|
matchDates :: [Match] -> [TxRecord] -> PureErr ([Maybe RawTx], [TxRecord], [Match])
|
|
|
|
matchDates ms = go ([], [], initZipper ms)
|
|
|
|
matchDates ms = go ([], [], initZipper ms)
|
|
|
|
where
|
|
|
|
where
|
|
|
|
go (matched, unmatched, z) [] =
|
|
|
|
go (matched, unmatched, z) [] = Right (matched, reverse unmatched, recoverZipper z)
|
|
|
|
Right
|
|
|
|
|
|
|
|
( catMaybes matched
|
|
|
|
|
|
|
|
, reverse unmatched
|
|
|
|
|
|
|
|
, recoverZipper z
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
go (matched, unmatched, z) (r : rs) = case zipperSlice findDate r z of
|
|
|
|
go (matched, unmatched, z) (r : rs) = case zipperSlice findDate r z of
|
|
|
|
Left zipped -> go (matched, r : unmatched, zipped) rs
|
|
|
|
Left zipped -> go (matched, r : unmatched, zipped) rs
|
|
|
|
Right unzipped -> do
|
|
|
|
Right unzipped -> do
|
|
|
@ -182,15 +187,10 @@ matchDates ms = go ([], [], initZipper ms)
|
|
|
|
go (m, u, z') rs
|
|
|
|
go (m, u, z') rs
|
|
|
|
findDate m r = maybe EQ (`compareDate` trDate r) $ mDate m
|
|
|
|
findDate m r = maybe EQ (`compareDate` trDate r) $ mDate m
|
|
|
|
|
|
|
|
|
|
|
|
matchNonDates :: [Match] -> [TxRecord] -> PureErr ([RawTx], [TxRecord], [Match])
|
|
|
|
matchNonDates :: [Match] -> [TxRecord] -> PureErr ([Maybe RawTx], [TxRecord], [Match])
|
|
|
|
matchNonDates ms = go ([], [], initZipper ms)
|
|
|
|
matchNonDates ms = go ([], [], initZipper ms)
|
|
|
|
where
|
|
|
|
where
|
|
|
|
go (matched, unmatched, z) [] =
|
|
|
|
go (matched, unmatched, z) [] = Right (matched, reverse unmatched, recoverZipper z)
|
|
|
|
Right
|
|
|
|
|
|
|
|
( catMaybes matched
|
|
|
|
|
|
|
|
, reverse unmatched
|
|
|
|
|
|
|
|
, recoverZipper z
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
go (matched, unmatched, z) (r : rs) = do
|
|
|
|
go (matched, unmatched, z) (r : rs) = do
|
|
|
|
(z', res) <- zipperMatch' z r
|
|
|
|
(z', res) <- zipperMatch' z r
|
|
|
|
let (m, u) = case res of
|
|
|
|
let (m, u) = case res of
|
|
|
@ -199,12 +199,12 @@ matchNonDates ms = go ([], [], initZipper ms)
|
|
|
|
MatchFail -> (matched, r : unmatched)
|
|
|
|
MatchFail -> (matched, r : unmatched)
|
|
|
|
in go (m, u, resetZipper z') rs
|
|
|
|
in go (m, u, resetZipper z') rs
|
|
|
|
|
|
|
|
|
|
|
|
balanceTx :: RawTx -> PureErr BalTx
|
|
|
|
balanceTx :: RawTx -> Either T.Text BalTx
|
|
|
|
balanceTx t@Tx {txSplits = ss} = do
|
|
|
|
balanceTx t@Tx {txSplits = ss} = do
|
|
|
|
bs <- balanceSplits ss
|
|
|
|
bs <- balanceSplits ss
|
|
|
|
return $ t {txSplits = bs}
|
|
|
|
return $ t {txSplits = bs}
|
|
|
|
|
|
|
|
|
|
|
|
balanceSplits :: [RawSplit] -> PureErr [BalSplit]
|
|
|
|
balanceSplits :: [RawSplit] -> Either T.Text [BalSplit]
|
|
|
|
balanceSplits ss =
|
|
|
|
balanceSplits ss =
|
|
|
|
fmap concat
|
|
|
|
fmap concat
|
|
|
|
<$> mapM (uncurry bal)
|
|
|
|
<$> mapM (uncurry bal)
|
|
|
|