Siisti nimistöä
Tämä muutos mm. poistaa tito-osia nimistä, koska ne käyttäjä voi helposti lisätä tuomalla koko moduulin omaan nimiavaruuteen.
This commit is contained in:
parent
6b399c80eb
commit
1a9d7e37ae
@ -1,6 +1,11 @@
|
|||||||
# TITO
|
# TITO
|
||||||
|
|
||||||
Tito on Haskell-kirjasto TITO-muotoisten tiliotteiden lukemiseen.
|
Tito on Haskell-kirjasto TITO-muotoisten tiliotteiden lukemiseen. Moduuli
|
||||||
|
`Data.TITO` sisältää ylätason funktiot tiliotteen tulkitsemiseen,
|
||||||
|
`Data.TITO.Types` määrittää tiliotteen tietorakenteen ja `Data.TITO.Parser`
|
||||||
|
ohjeet sen tekstimuodon tulkitsemisen. Kaikki moduulien nimet on suunniteltu
|
||||||
|
tuotavaksi omaan nimiavaruuteensa, mutta vain `Data.TITO.readFile` menee
|
||||||
|
päällekkäin `Prelude`:n nimen kanssa.
|
||||||
|
|
||||||
## Asentaminen
|
## Asentaminen
|
||||||
|
|
||||||
|
@ -8,10 +8,11 @@ import qualified Text.Megaparsec as P
|
|||||||
import Data.TITO.Parser
|
import Data.TITO.Parser
|
||||||
import Data.TITO.Types
|
import Data.TITO.Types
|
||||||
|
|
||||||
readTITO :: BS.ByteString -> Either ParseErrors TITO
|
decode :: BS.ByteString -> Either ParseErrors AccountStatement
|
||||||
readTITO = P.parse tito ""
|
decode = P.parse accountStatement ""
|
||||||
|
|
||||||
readTITOFile :: FilePath -> IO TITO
|
readFile :: FilePath -> IO AccountStatement
|
||||||
readTITOFile fp =
|
readFile fp =
|
||||||
BS.readFile fp >>=
|
BS.readFile fp
|
||||||
either (throwIO . ErrorCall . P.errorBundlePretty) pure . P.parse tito fp
|
>>= either (throwIO . ErrorCall . P.errorBundlePretty) pure
|
||||||
|
. P.parse accountStatement fp
|
||||||
|
@ -94,8 +94,8 @@ transactionType = do
|
|||||||
case n of
|
case n of
|
||||||
1 -> pure Deposit
|
1 -> pure Deposit
|
||||||
2 -> pure Withdrawal
|
2 -> pure Withdrawal
|
||||||
3 -> pure DepositFix
|
3 -> pure DepositCorrection
|
||||||
4 -> pure WithdrawalFix
|
4 -> pure WithdrawalCorrection
|
||||||
9 -> pure Declined
|
9 -> pure Declined
|
||||||
_ -> P.failure Nothing mempty -- TODO: proper error message
|
_ -> P.failure Nothing mempty -- TODO: proper error message
|
||||||
|
|
||||||
@ -118,46 +118,47 @@ nameSource = do
|
|||||||
"A" -> pure Customer
|
"A" -> pure Customer
|
||||||
_ -> P.failure Nothing mempty -- TODO: proper error message
|
_ -> P.failure Nothing mempty -- TODO: proper error message
|
||||||
|
|
||||||
tito :: Parser TITO
|
accountStatement :: Parser AccountStatement
|
||||||
tito = TITO <$> titoRoot <*> P.many titoRecord <* P.eof
|
accountStatement = do
|
||||||
|
tito <- record "00" $ do
|
||||||
|
P.string "100" -- version number
|
||||||
|
account <- alphaNumeric 14
|
||||||
|
statementNumber <- alphaNumeric 3
|
||||||
|
startDate <- date
|
||||||
|
endDate <- date
|
||||||
|
created <- timestamp
|
||||||
|
customer <- alphaNumeric 17
|
||||||
|
startBalanceDate <- date
|
||||||
|
startBalance <- money
|
||||||
|
_records <- optional' numeric 6
|
||||||
|
currency <- optional' alphaNumeric 3
|
||||||
|
accountName <- optional' alphaNumeric 30
|
||||||
|
accountLimit <- fmap Money <$> optional' numeric 18
|
||||||
|
accountOwnerName <- alphaNumeric 35
|
||||||
|
bankName <- alphaNumeric 40
|
||||||
|
contactInformation <- optional' alphaNumeric 40
|
||||||
|
bankSpecific <- optional' alphaNumeric 30
|
||||||
|
ibanAndBic <- fmap (fmap $ T.break isSpace) $ optional' alphaNumeric 30 -- FI1234567 XXXXX
|
||||||
|
return AccountStatement {events = [], ..}
|
||||||
|
events <- P.many titoRecord <* P.eof
|
||||||
|
pure $ tito {events}
|
||||||
|
|
||||||
titoRoot :: Parser TITORoot
|
titoRecord :: Parser Event
|
||||||
titoRoot = record "00" $ do
|
titoRecord = P.try transaction
|
||||||
P.string "100" -- version number
|
|
||||||
account <- alphaNumeric 14
|
|
||||||
statementNumber <- alphaNumeric 3
|
|
||||||
startDate <- date
|
|
||||||
endDate <- date
|
|
||||||
created <- timestamp
|
|
||||||
customer <- alphaNumeric 17
|
|
||||||
startBalanceDate <- date
|
|
||||||
startBalance <- money
|
|
||||||
_records <- optional' numeric 6
|
|
||||||
currency <- optional' alphaNumeric 3
|
|
||||||
accountName <- optional' alphaNumeric 30
|
|
||||||
accountLimit <- fmap Money <$> optional' numeric 18
|
|
||||||
accountOwnerName <- alphaNumeric 35
|
|
||||||
bankName <- alphaNumeric 40
|
|
||||||
contactInformation <- optional' alphaNumeric 40
|
|
||||||
bankSpecific <- optional' alphaNumeric 30
|
|
||||||
ibanAndBic <- fmap (fmap $ T.break isSpace) $ optional' alphaNumeric 30 -- FI1234567 XXXXX
|
|
||||||
return TITORoot {..}
|
|
||||||
|
|
||||||
titoRecord :: Parser TITORecord
|
|
||||||
titoRecord = P.try (transaction False)
|
|
||||||
<|> P.try balance
|
<|> P.try balance
|
||||||
<|> P.try summary
|
<|> P.try summary
|
||||||
<|> P.try fixSummary
|
<|> P.try correctionSummary
|
||||||
<|> P.try special
|
<|> P.try bankSpecific
|
||||||
<|> P.try info
|
<|> P.try message
|
||||||
<|> P.try (transaction True)
|
<|> P.try transactionNotification
|
||||||
|
|
||||||
transaction :: Bool -> Parser TITORecord
|
transaction, transactionNotification :: Parser Event
|
||||||
transaction = fmap . TransactionRecord <*> transaction' 0
|
transaction = BasicTransaction <$> transaction' 0 False
|
||||||
|
transactionNotification = TransactionNotification <$> transaction' 0 True
|
||||||
|
|
||||||
transaction' :: Int -> Bool -> Parser Transaction
|
transaction' :: Int -> Bool -> Parser Transaction
|
||||||
transaction' minimumDepth isInformational = do
|
transaction' minimumDepth isNotification = do
|
||||||
(depth, transaction) <- record (if isInformational then "80" else "10") $ do
|
(depth, transaction) <- record (if isNotification then "80" else "10") $ do
|
||||||
transactionNumber <- numeric 6
|
transactionNumber <- numeric 6
|
||||||
archiveId <- optional' alphaNumeric 18
|
archiveId <- optional' alphaNumeric 18
|
||||||
parsedDate <- date
|
parsedDate <- date
|
||||||
@ -178,13 +179,13 @@ transaction' minimumDepth isInformational = do
|
|||||||
depth <- fromIntegral <$> numeric 1 <|> const 0 <$> P.char 32 -- space
|
depth <- fromIntegral <$> numeric 1 <|> const 0 <$> P.char 32 -- space
|
||||||
guard $ depth >= minimumDepth
|
guard $ depth >= minimumDepth
|
||||||
return (depth, Transaction {details = [], itemisation = [], date = parsedDate, ..})
|
return (depth, Transaction {details = [], itemisation = [], date = parsedDate, ..})
|
||||||
details <- P.many $ P.try $ transactionDetail' isInformational
|
details <- P.many $ P.try $ transactionDetail' isNotification
|
||||||
itemisation <- P.many $ P.try $ transaction' (depth + 1) isInformational
|
itemisation <- P.many $ P.try $ transaction' (depth + 1) isNotification
|
||||||
pure $ transaction {details, itemisation}
|
pure $ transaction {details, itemisation}
|
||||||
|
|
||||||
transactionDetail' :: Bool -> Parser TransactionDetail
|
transactionDetail' :: Bool -> Parser TransactionDetail
|
||||||
transactionDetail' isInformational =
|
transactionDetail' isNotification =
|
||||||
record' (if isInformational then "81" else "11") $ \recordLength -> do
|
record' (if isNotification then "81" else "11") $ \recordLength -> do
|
||||||
let detailLength = recordLength - 8
|
let detailLength = recordLength - 8
|
||||||
detailType <- optional' alphaNumeric 2
|
detailType <- optional' alphaNumeric 2
|
||||||
case detailType of
|
case detailType of
|
||||||
@ -201,7 +202,7 @@ transactionDetail' isInformational =
|
|||||||
date <- date
|
date <- date
|
||||||
pure Invoice {..}
|
pure Invoice {..}
|
||||||
Just "03" -> Card <$> alphaNumeric 19 <* alphaNumeric 1 <*> optional' alphaNumeric 14
|
Just "03" -> Card <$> alphaNumeric 19 <* alphaNumeric 1 <*> optional' alphaNumeric 14
|
||||||
Just "04" -> Fix <$> alphaNumeric 18
|
Just "04" -> Correction <$> alphaNumeric 18
|
||||||
Just "05" -> do
|
Just "05" -> do
|
||||||
foreignAmount <- money
|
foreignAmount <- money
|
||||||
alphaNumeric 1
|
alphaNumeric 1
|
||||||
@ -228,14 +229,14 @@ transactionDetail' isInformational =
|
|||||||
pure SEPA {..}
|
pure SEPA {..}
|
||||||
_ -> Unknown <$> alphaNumeric detailLength
|
_ -> Unknown <$> alphaNumeric detailLength
|
||||||
|
|
||||||
balance :: Parser TITORecord
|
balance :: Parser Event
|
||||||
balance = record "40" $ do
|
balance = record "40" $ do
|
||||||
date <- date
|
date <- date
|
||||||
endBalance <- money
|
endBalance <- money
|
||||||
usableBalance <- optional 19 money
|
usableBalance <- optional 19 money
|
||||||
pure Balance {..}
|
pure Balance {..}
|
||||||
|
|
||||||
summary :: Parser TITORecord
|
summary :: Parser Event
|
||||||
summary = record "50" $ do
|
summary = record "50" $ do
|
||||||
period <- period
|
period <- period
|
||||||
date <- date
|
date <- date
|
||||||
@ -245,22 +246,22 @@ summary = record "50" $ do
|
|||||||
withdrawalsTotal <- money
|
withdrawalsTotal <- money
|
||||||
pure Summary {..}
|
pure Summary {..}
|
||||||
|
|
||||||
fixSummary :: Parser TITORecord
|
correctionSummary :: Parser Event
|
||||||
fixSummary = record "51" $ do
|
correctionSummary = record "51" $ do
|
||||||
period <- period
|
period <- period
|
||||||
date <- date
|
date <- date
|
||||||
depositFixes <- numeric 8
|
depositCorrections <- numeric 8
|
||||||
depositFixesTotal <- money
|
depositCorrectionsTotal <- money
|
||||||
withdrawalFixes <- numeric 8
|
withdrawalCorrections <- numeric 8
|
||||||
withdrawalFixesTotal <- money
|
withdrawalCorrectionsTotal <- money
|
||||||
pure FixSummary {..}
|
pure CorrectionSummary {..}
|
||||||
|
|
||||||
special :: Parser TITORecord
|
bankSpecific :: Parser Event
|
||||||
special = record' "60" $ \recordLength -> do
|
bankSpecific = record' "60" $ \recordLength -> do
|
||||||
BankSpecific <$> alphaNumeric 3 <*> P.takeP Nothing (recordLength - 9)
|
BankSpecific <$> alphaNumeric 3 <*> P.takeP Nothing (recordLength - 9)
|
||||||
|
|
||||||
info :: Parser TITORecord
|
message :: Parser Event
|
||||||
info = record' "70" $ \recordLength -> do
|
message = record' "70" $ \recordLength -> do
|
||||||
bankId <- alphaNumeric 3
|
bankId <- alphaNumeric 3
|
||||||
info <- T.unlines . T.chunksOf 80 <$> alphaNumeric (recordLength - 9)
|
message <- T.unlines . T.chunksOf 80 <$> alphaNumeric (recordLength - 9)
|
||||||
pure Info {..}
|
pure Message {..}
|
||||||
|
@ -8,14 +8,7 @@ import Data.Text (Text)
|
|||||||
import Data.Time (Day, LocalTime)
|
import Data.Time (Day, LocalTime)
|
||||||
import Data.List.NonEmpty (NonEmpty)
|
import Data.List.NonEmpty (NonEmpty)
|
||||||
|
|
||||||
data TITO = TITO
|
data AccountStatement = AccountStatement
|
||||||
{ root :: TITORoot
|
|
||||||
, records :: [TITORecord]
|
|
||||||
} deriving (Show, Eq)
|
|
||||||
|
|
||||||
newtype Money = Money Integer deriving (Eq, Show)
|
|
||||||
|
|
||||||
data TITORoot = TITORoot -- T00
|
|
||||||
{ account :: Text
|
{ account :: Text
|
||||||
, statementNumber :: Text
|
, statementNumber :: Text
|
||||||
, startDate :: Day
|
, startDate :: Day
|
||||||
@ -32,42 +25,43 @@ data TITORoot = TITORoot -- T00
|
|||||||
, contactInformation :: Maybe Text
|
, contactInformation :: Maybe Text
|
||||||
, bankSpecific :: Maybe Text
|
, bankSpecific :: Maybe Text
|
||||||
, ibanAndBic :: Maybe (Text, Text)
|
, ibanAndBic :: Maybe (Text, Text)
|
||||||
|
, events :: [Event]
|
||||||
} deriving (Show, Eq)
|
} deriving (Show, Eq)
|
||||||
|
|
||||||
data TITORecord = TransactionRecord -- T80 if isInformational, else T10
|
newtype Money = Money Integer deriving (Eq, Show)
|
||||||
{ isInformational :: Bool
|
|
||||||
, transaction :: Transaction
|
data Event = BasicTransaction Transaction -- T10
|
||||||
}
|
| TransactionNotification Transaction -- T80
|
||||||
| Balance -- T40
|
| Balance -- T40
|
||||||
{ date :: Day
|
{ date :: Day
|
||||||
, endBalance :: Money
|
, endBalance :: Money
|
||||||
, usableBalance :: Maybe Money
|
, usableBalance :: Maybe Money
|
||||||
}
|
}
|
||||||
| Summary -- T50
|
| Summary -- T50
|
||||||
{ period :: Period
|
{ period :: Period
|
||||||
, date :: Day
|
, date :: Day
|
||||||
, deposits :: Integer
|
, deposits :: Integer
|
||||||
, depositsTotal :: Money
|
, depositsTotal :: Money
|
||||||
, withdrawals :: Integer
|
, withdrawals :: Integer
|
||||||
, withdrawalsTotal :: Money
|
, withdrawalsTotal :: Money
|
||||||
}
|
}
|
||||||
| FixSummary -- T51
|
| CorrectionSummary -- T51
|
||||||
{ period :: Period
|
{ period :: Period
|
||||||
, date :: Day
|
, date :: Day
|
||||||
, depositFixes :: Integer
|
, depositCorrections :: Integer
|
||||||
, depositFixesTotal :: Money
|
, depositCorrectionsTotal :: Money
|
||||||
, withdrawalFixes :: Integer
|
, withdrawalCorrections :: Integer
|
||||||
, withdrawalFixesTotal :: Money
|
, withdrawalCorrectionsTotal :: Money
|
||||||
}
|
}
|
||||||
| BankSpecific -- T60
|
| BankSpecific -- T60
|
||||||
{ bankId :: Text
|
{ bankId :: Text
|
||||||
, rawData :: ByteString
|
, rawData :: ByteString
|
||||||
}
|
}
|
||||||
| Info -- T70
|
| Message -- T70
|
||||||
{ bankId :: Text
|
{ bankId :: Text
|
||||||
, info :: Text
|
, message :: Text
|
||||||
}
|
}
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
data Transaction = Transaction
|
data Transaction = Transaction
|
||||||
{ transactionNumber :: Integer
|
{ transactionNumber :: Integer
|
||||||
@ -102,8 +96,8 @@ data TransactionDetail = Freeform (NonEmpty Text) -- 00
|
|||||||
{ cardNumber :: Text
|
{ cardNumber :: Text
|
||||||
, merchantsReference :: Maybe Text
|
, merchantsReference :: Maybe Text
|
||||||
}
|
}
|
||||||
| Fix -- 04
|
| Correction -- 04
|
||||||
{ fixedTransaction :: Text}
|
{ correctedTransaction :: Text}
|
||||||
| ForeignCurrency -- 05
|
| ForeignCurrency -- 05
|
||||||
{ foreignAmount :: Money
|
{ foreignAmount :: Money
|
||||||
, currency :: Text
|
, currency :: Text
|
||||||
@ -129,7 +123,11 @@ data TransactionDetail = Freeform (NonEmpty Text) -- 00
|
|||||||
| Unknown Text -- all others
|
| Unknown Text -- all others
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
data TransactionType = Deposit | Withdrawal | DepositFix | WithdrawalFix | Declined deriving (Show, Eq)
|
data TransactionType = Deposit
|
||||||
|
| Withdrawal
|
||||||
|
| DepositCorrection
|
||||||
|
| WithdrawalCorrection
|
||||||
|
| Declined deriving (Show, Eq)
|
||||||
|
|
||||||
data Period = Day | Statement | Month | Year deriving (Show, Eq)
|
data Period = Day | Statement | Month | Year deriving (Show, Eq)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user