Skip to content

Commit

Permalink
fix(swift): fix bookingdate bases correct bookingyear
Browse files Browse the repository at this point in the history
  • Loading branch information
jeppsson committed Feb 3, 2019
1 parent 9023d26 commit 3ed485f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
10 changes: 5 additions & 5 deletions swift/mt940.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package swift
import (
"bytes"
"fmt"
"math"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -212,7 +211,7 @@ type TransactionTag struct {
}

// Unmarshal unmarshals value into t
func (t *TransactionTag) Unmarshal(value []byte) error {
func (t *TransactionTag) Unmarshal(value []byte, bookingYear int) error {
elements, err := extractTagElements(value)
if err != nil {
return err
Expand All @@ -235,13 +234,14 @@ func (t *TransactionTag) Unmarshal(value []byte) error {
if unicode.IsDigit(r) {
buf.UnreadRune()
dateBytes = buf.Next(4)
date, err = parseDate(dateBytes, t.ValutaDate.Year())
date, err = parseDate(dateBytes, bookingYear)
if err != nil {
return errors.WithMessage(err, "unmarshal transaction tag: parsing booking date")
}
t.BookingDate = domain.NewShortDate(date)
monthDiff := int(math.Abs(float64(t.ValutaDate.Month() - t.BookingDate.Month())))
if monthDiff > 1 {
diff := t.ValutaDate.Sub(t.BookingDate.Time).Hours() / 24
fmt.Println(diff)
if diff > 31 {
t.BookingDate = domain.NewShortDate(t.BookingDate.AddDate(1, 0, 0))
}
}
Expand Down
12 changes: 11 additions & 1 deletion swift/mt940_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestTransactionTagUnmarshal(t *testing.T) {
for _, test := range tests {
tag := &TransactionTag{}

err := tag.Unmarshal([]byte(test.marshaledValue))
err := tag.Unmarshal([]byte(test.marshaledValue), 2015)

if err != nil {
t.Logf("Expected no error, got %T:%v\n", err, err)
Expand Down Expand Up @@ -219,20 +219,30 @@ func TestTransactionWithRedefineCustomDataTag(t *testing.T) {

func TestBookingDateBug(t *testing.T) {
testdata := "\r\n:20:HBCIKTOLST"
// Valuta Date 2019-01-01 Booking Date 2018-12-28
testdata += "\r\n:25:12345678/1234123456" +
"\r\n:28C:0" +
"\r\n:60F:C181228EUR1234,56" +
"\r\n:61:1901011228DR50,NMSCNONREF" +
"\r\n/OCMT/EUR50,//CHGS/ 0,/" +
"\r\n:86:177?00SB-SEPA-Ueberweisung?202018-12-28 ?30?31?32Max Maier ?33 ?34000" +
"\r\n:62F:C190125EUR1234,56"
// Valuta Date 2019-01-01 Booking Date 2019-01-01
testdata += "\r\n:25:12345678/1234123456" +
"\r\n:28C:0" +
"\r\n:60F:C181228EUR1234,56" +
"\r\n:61:1901010101DR50,NMSCNONREF" +
"\r\n/OCMT/EUR50,//CHGS/ 0,/" +
"\r\n:86:177?00SB-SEPA-Ueberweisung?202019-01-01 ?30?31?32Max Meier ?33 ?34000" +
"\r\n:62F:C190125EUR1234,56"
// Valuta Date 2018-12-28 Booking Date 2019-01-01 (not sure if that can happen ? )
testdata += "\r\n:25:12345678/1234123456" +
"\r\n:28C:0" +
"\r\n:60F:C181228EUR1234,56" +
"\r\n:61:1812280101DR50,NMSCNONREF" +
"\r\n/OCMT/EUR50,//CHGS/ 0,/" +
"\r\n:86:177?00SB-SEPA-Ueberweisung?202019-01-01 ?30?31?32Max Meier ?33 ?34000" +
"\r\n:62F:C190125EUR1234,56"
testdata += "\r\n-"
mt := &MT940{}
mt.Unmarshal([]byte(testdata))
Expand Down
3 changes: 1 addition & 2 deletions swift/mt940_unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ func (m *MT940) Unmarshal(value []byte) error {
case bytes.HasPrefix(tag, []byte(":61:")):

transaction := &TransactionTag{}

err = transaction.Unmarshal(tag)
err = transaction.Unmarshal(tag, m.StartingBalance.BookingDate.Year())
if err != nil {
return err
}
Expand Down

0 comments on commit 3ed485f

Please sign in to comment.