Skip to content

Commit

Permalink
statement parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
volyanyk committed Aug 8, 2023
1 parent af30e3f commit cb25219
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 29 deletions.
40 changes: 29 additions & 11 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mt940_converter

import (
"fmt"
"regexp"
"strings"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -68,8 +69,16 @@ type Balance struct {
BalanceType BalanceType
}
type TransactionStatement struct {
LongDate LongDate
ShortDate ShortDate
TransactionType TransactionType
ThirdCurrencyCharacter string
Amount MyDecimal
Description string
DescriptionPrefix string
}
type TransactionInformation struct {
Info string
}
type Transaction struct {
Index int
Expand Down Expand Up @@ -197,29 +206,38 @@ func GetTransactions(input string) (*[]Transaction, error) {
Information: info,
})
}
index := strings.Index(input, transactionDescription)
result := input[len(transactionDescription):index]
log.Printf(result)
return &transactions, nil
}

func GetTransactionInfo(transactionString string) TransactionInformation {
var info = transactionString[strings.LastIndex(transactionString, transactionDescription)+len(transactionDescription):]
log.Printf(info)
return TransactionInformation{}
return TransactionInformation{Info: info}
}

func GetStatement(transactionString string) TransactionStatement {
var stmt = transactionString[:strings.Index(transactionString, transactionDescription)]
var valueLongDate = GetLongDate(stmt[:6])
var valueShortDate = GetShortDate(stmt[6:10])
var transactionType = GetTransactionType(stmt[10:11])
//var transactionAmout = GetAmount(stmt[11:])
//_ := regexp.MustCompile("^([A-Za-z])?(\\d{1,12},\\d{2}|\\d{1,3},\\d{3},\\d{2}|\\d{1,15})[A-Za-z]$")

log.Print(valueLongDate)
log.Print(valueShortDate)
log.Print(transactionType)
//log.Print(transactionAmout)
regex := regexp.MustCompile("^([A-Za-z])?(\\d{1,12},\\d{2}|\\d{1,3},\\d{3},\\d{2}|\\d{1,15})([A-Za-z])(.*?)$")
matches := regex.FindStringSubmatch(regexp.MustCompile(`\r?\n`).ReplaceAllString(stmt[11:], " "))
if matches != nil {
thirdCurrencyCharacter := matches[1]
amount, _ := GetDecimal(matches[2])
descriptionPrefix := matches[3]
descr := matches[4]

return TransactionStatement{
LongDate: valueLongDate,
ShortDate: valueShortDate,
TransactionType: transactionType,
ThirdCurrencyCharacter: thirdCurrencyCharacter,
Amount: amount,
DescriptionPrefix: descriptionPrefix,
Description: descr,
}
}
return TransactionStatement{}

}
136 changes: 118 additions & 18 deletions converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mt940_converter
import (
"testing"

"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -278,21 +277,122 @@ func TestAvailableBalanceCase(t *testing.T) {
}

func TestGetTransactionsCase(t *testing.T) {
data := ":61:0710091009DN2,50NCHGNONREF//BR07282102000059\n824-OPŁ. ZA PRZEL. ELIXIR MT\n:86:824 OPŁATA ZA PRZELEW ELIXIR; TNR: 145271016138274.040001\n" +
":61:0501120112DN449,77NTRFSP300//BR05012139000001\n944-PRZEL.KRAJ.WYCH.MT.ELX\n:86:944 CompanyNet Przelew krajowy; na rach.: 35109010560000000006093440; dla: PHU Test ul.Dolna\n1 00-950 Warszawa; tyt.: fv 100/2007; TNR: 145271016138277.020002" +
":61:2306040604D1,89S07397301056237\n:86:073\n:86:073~00VE02\n~20PàatnoòÜ kart• 02.06.2023 \n~21Nr karty 4246xx4970~22\n~23~24\n~25\n~3010500031~311915031/19730\n~32BOLT.EU/R/2306021457 ~33Tallinn \n~34073"
actual, err := GetTransactions(data)
log.Print(actual)
log.Print(err)
//log.Print(actual.Value[0])
//log.Print(actual.Value[1])
//log.Print(actual.Value[2])
//if err != nil {
// assert.Nil(t, actual)
//} else {
// assert.NotNil(t, actual)
// assert.NotNil(t, actual.Value[0])
// assert.NotNil(t, actual.Value[1])
// assert.NotNil(t, actual.Value[2])
//}2
//data := "" +
// ":61:0501120112DN449,77NTRFSP300//BR05012139000001\n944-PRZEL.KRAJ.WYCH.MT.ELX\n:86:944 CompanyNet Przelew krajowy; na rach.: 35109010560000000006093440; dla: PHU Test ul.Dolna\n1 00-950 Warszawa; tyt.: fv 100/2007; TNR: 145271016138277.020002" +
// ":61:2306040604D1,89S07397301056237\n:86:073\n:86:073~00VE02\n~20PàatnoòÜ kart• 02.06.2023 \n~21Nr karty 4246xx4970~22\n~23~24\n~25\n~3010500031~311915031/19730\n~32BOLT.EU/R/2306021457 ~33Tallinn \n~34073"
type testCase struct {
name string
input string
expectedResult *[]Transaction
hasError bool
}
decim1, _ := GetDecimal("2,50")
decim2, _ := GetDecimal("449,77")
decim3, _ := GetDecimal("1,89")
testTable := []testCase{
{
name: "Successful parsing the transaction",
input: ":61:0710091009DN2,50NCHGNONREF//BR07282102000059\n824-OPŁ. ZA PRZEL. ELIXIR MT\n:86:824 OPŁATA ZA PRZELEW ELIXIR; TNR: 145271016138274.040001\n",
expectedResult: &[]Transaction{
{
Index: 1,
Statement: TransactionStatement{
LongDate: LongDate{
Year: 7,
Month: 10,
Day: 9,
},
ShortDate: ShortDate{
Month: 10,
Day: 9,
},
TransactionType: DEBIT,
ThirdCurrencyCharacter: "N",
Amount: decim1,
DescriptionPrefix: "N",
Description: "CHGNONREF//BR07282102000059 824-OPŁ. ZA PRZEL. ELIXIR MT ",
},
Information: TransactionInformation{Info: "824 OPŁATA ZA PRZELEW ELIXIR; TNR: 145271016138274.040001\n"},
}},
hasError: false,
}, {
name: "Successful parsing the transaction, but as list",
input: ":61:0710091009DN2,50NCHGNONREF//BR07282102000059\n824-OPŁ. ZA PRZEL. ELIXIR MT\n:86:824 OPŁATA ZA PRZELEW ELIXIR; TNR: 145271016138274.040001\n" +
":61:0501120112DN449,77NTRFSP300//BR05012139000001\n944-PRZEL.KRAJ.WYCH.MT.ELX\n:86:944 CompanyNet Przelew krajowy; na rach.: 35109010560000000006093440; dla: PHU Test ul.Dolna\n1 00-950 Warszawa; tyt.: fv 100/2007; TNR: 145271016138277.020002" +
":61:2306040604D1,89S07397301056237\n:86:073\n:86:073~00VE02\n~20PàatnoòÜ kart• 02.06.2023 \n~21Nr karty 4246xx4970~22\n~23~24\n~25\n~3010500031~311915031/19730\n~32BOLT.EU/R/2306021457 ~33Tallinn \n~34073",
expectedResult: &[]Transaction{
{
Index: 1,
Statement: TransactionStatement{
LongDate: LongDate{
Year: 7,
Month: 10,
Day: 9,
},
ShortDate: ShortDate{
Month: 10,
Day: 9,
},
TransactionType: DEBIT,
ThirdCurrencyCharacter: "N",
Amount: decim1,
DescriptionPrefix: "N",
Description: "CHGNONREF//BR07282102000059 824-OPŁ. ZA PRZEL. ELIXIR MT ",
},
Information: TransactionInformation{Info: "824 OPŁATA ZA PRZELEW ELIXIR; TNR: 145271016138274.040001\n"},
},
{
Index: 2,
Statement: TransactionStatement{
LongDate: LongDate{
Year: 5,
Month: 1,
Day: 12,
},
ShortDate: ShortDate{
Month: 1,
Day: 12,
},
TransactionType: DEBIT,
ThirdCurrencyCharacter: "N",
Amount: decim2,
DescriptionPrefix: "N",
Description: "TRFSP300//BR05012139000001 944-PRZEL.KRAJ.WYCH.MT.ELX ",
},
Information: TransactionInformation{Info: "944 CompanyNet Przelew krajowy; na rach.: 35109010560000000006093440; dla: PHU Test ul.Dolna\n1 00-950 Warszawa; tyt.: fv 100/2007; TNR: 145271016138277.020002"},
},
{
Index: 3,
Statement: TransactionStatement{
LongDate: LongDate{
Year: 23,
Month: 6,
Day: 4,
},
ShortDate: ShortDate{
Month: 6,
Day: 4,
},
TransactionType: DEBIT,
ThirdCurrencyCharacter: "",
Amount: decim3,
DescriptionPrefix: "S",
Description: "07397301056237 ",
},
Information: TransactionInformation{Info: "073~00VE02\n~20PàatnoòÜ kart• 02.06.2023 \n~21Nr karty 4246xx4970~22\n~23~24\n~25\n~3010500031~311915031/19730\n~32BOLT.EU/R/2306021457 ~33Tallinn \n~34073"},
}},
hasError: false,
},
}

for _, test := range testTable {
actual, err := GetTransactions(test.input)
assert.Equal(t, test.expectedResult, actual, test.name)

if test.hasError {
assert.NotNil(t, err, test.name)
} else {
assert.Nil(t, err, test.name)
}
}
}

0 comments on commit cb25219

Please sign in to comment.