Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution for booking date bug #18 #23

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions element/swift.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package element
import (
"github.com/mitch000001/go-hbci/domain"
"github.com/mitch000001/go-hbci/swift"
"time"
)

// SwiftMT940DataElement represents a DataElement containing SWIFT MT940
Expand All @@ -26,6 +27,7 @@ func (s *SwiftMT940DataElement) UnmarshalHBCI(value []byte) error {
}
for _, message := range messages {
tr := &swift.MT940{}
tr.ReferenceDate = time.Now()
err = tr.Unmarshal(message)
if err != nil {
return err
Expand Down
29 changes: 21 additions & 8 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 All @@ -15,6 +14,7 @@ import (

// MT940 represents a S.W.I.F.T. Transaction Report
type MT940 struct {
ReferenceDate time.Time
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I read it correctly you're using ReferenceDate as a way for dependency injection. As the value is always hardcoded to time.Now at element.swift.go:30. What do you think about not exposing it yet and guarding against zero value within the Unmarshal method at swift/mt940_unmarshaler.go:12?

JobReference *AlphaNumericTag
Reference *AlphaNumericTag
Account *AccountTag
Expand Down Expand Up @@ -163,7 +163,7 @@ func (b *BalanceTag) Balance() domain.Balance {
}

// Unmarshal unmarshals value into b
func (b *BalanceTag) Unmarshal(value []byte) error {
func (b *BalanceTag) Unmarshal(value []byte, today time.Time) error {
elements, err := extractTagElements(value)
if err != nil {
return err
Expand All @@ -175,7 +175,7 @@ func (b *BalanceTag) Unmarshal(value []byte) error {
buf := bytes.NewBuffer(elements[1])
b.DebitCreditIndicator = string(buf.Next(1))
dateBytes := buf.Next(6)
date, err := parseDate(dateBytes, time.Now().Year())
date, err := parseDate(dateBytes, today.Year())
if err != nil {
return errors.WithMessage(err, "unmarshal balance tag: parsing booking date")
}
Expand Down Expand Up @@ -212,7 +212,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 @@ -223,7 +223,7 @@ func (t *TransactionTag) Unmarshal(value []byte) error {
t.Tag = string(elements[0])
buf := bytes.NewBuffer(elements[1])
dateBytes := buf.Next(6)
date, err := parseDate(dateBytes, time.Now().Year())
date, err := parseDate(dateBytes, bookingYear)
if err != nil {
return errors.WithMessage(err, "unmarshal transaction tag: parsing valuta date")
}
Expand All @@ -235,13 +235,13 @@ 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
if diff > 31 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I haven't yet understood what we're calculating here and why we're checking for 31.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how may days can be between a booking and a valuta date? I am sure it us less then 31 days.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding this thread https://homebanking-hilfe.de/forum/topic.php?t=8970 which may or may not be representative the number of days can be way higher

t.BookingDate = domain.NewShortDate(t.BookingDate.AddDate(1, 0, 0))
}
}
Expand Down Expand Up @@ -320,11 +320,24 @@ func parseDate(value []byte, referenceYear int) (time.Time, error) {
yearBegin := fmt.Sprintf("%d", referenceYear)[:offset]
dateString := yearBegin + string(value)
date, err := time.Parse("20060102", dateString)

if err != nil {
if strings.HasSuffix(dateString, "0229") {
return time.Date(referenceYear, 2, 29, 0, 0, 0, 0, time.UTC), nil
}
return time.Time{}, err
}

diff := date.Year() - referenceYear
if diff == 100 {
return time.Date(referenceYear, date.Month(), date.Day(), 0, 0, 0, 0, time.UTC), nil
}
if diff == 99 {
return time.Date(referenceYear-1, date.Month(), date.Day(), 0, 0, 0, 0, time.UTC), nil
}
if diff == -99 {
return time.Date(referenceYear+1, date.Month(), date.Day(), 0, 0, 0, 0, time.UTC), nil

}
return date.Truncate(24 * time.Hour), nil
}
136 changes: 82 additions & 54 deletions swift/mt940_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package swift

import (
"reflect"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -139,7 +137,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 All @@ -154,65 +152,95 @@ func TestTransactionTagUnmarshal(t *testing.T) {
}
}

func TestTransactionTagOrder(t *testing.T) {
testdata := "\r\n:20:HBCIKTOLST"
for i := 0; i < 10; i++ {
testdata += "\r\n:25:12345678/1234123456" +
"\r\n:28C:0" +
"\r\n:60F:C181105EUR1234,56" +
"\r\n:61:1811051105DR50,NMSCNONREF" +
"\r\n/OCMT/EUR50,//CHGS/ 0,/" +
"\r\n:86:177?00SB-SEPA-Ueberweisung?20" + strconv.Itoa(i+10) + " ?30?31?32Max Meier ?33 ?34000" +
"\r\n:62F:C190125EUR1234,56"
func TestBookingDateBug(t *testing.T) {

tests := []struct {
today string
balanceStartBookingDateString string
Copy link
Owner

@mitch000001 mitch000001 Feb 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are already using the dates as time.Time in line 202ff we should change the type from string to time.Time. Also, the setup gets easier as we don't have to parse the date strings

balanceClosingBookingDateString string
bookingDateString string
valutaDateString string
}{
{
today: "2019-02-10",
bookingDateString: "2018-12-28",
valutaDateString: "2019-01-01",
balanceStartBookingDateString: "2018-12-28",
balanceClosingBookingDateString: "2019-01-25",
},
{
today: "2019-02-10",
bookingDateString: "2019-01-01",
valutaDateString: "2019-01-01",
balanceStartBookingDateString: "2018-12-28",
balanceClosingBookingDateString: "2019-01-25",
}, {
today: "2019-02-10",
bookingDateString: "2019-01-01",
valutaDateString: "2018-12-28",
balanceStartBookingDateString: "2018-12-28",
balanceClosingBookingDateString: "2019-01-25",
}, {
today: "2100-02-10",
bookingDateString: "2100-01-01",
valutaDateString: "2099-12-28",
balanceStartBookingDateString: "2099-12-28",
balanceClosingBookingDateString: "2100-01-25",
}, {
today: "2100-02-10",
bookingDateString: "2100-01-01",
valutaDateString: "2100-12-28",
balanceStartBookingDateString: "2099-12-28",
balanceClosingBookingDateString: "2100-01-25",
},
}
testdata += "\r\n-"
mt := &MT940{}
mt.Unmarshal([]byte(testdata))
for i, tr := range mt.Transactions {
if strings.TrimSpace(tr.Description.Purpose[0]) != strconv.Itoa(i+10) {
t.Logf("Purpose at index %d should be %d but is %s", i, i+10, tr.Description.Purpose[0])

for _, test := range tests {
mt := &MT940{}
today, _ := time.Parse("2006-01-02", test.today)
mt.ReferenceDate = today
expectedBookingDate, _ := time.Parse("2006-01-02", test.bookingDateString)
expectedValutaDate, _ := time.Parse("2006-01-02", test.valutaDateString)
expectedBalanceStartBookingDate, _ := time.Parse("2006-01-02", test.balanceStartBookingDateString)
expectedBalanceClosingBookingDate, _ := time.Parse("2006-01-02", test.balanceClosingBookingDateString)
testdata := "\r\n:20:HBCIKTOLST" + "\r\n:25:12345678/1234123456" +
"\r\n:28C:0" +
"\r\n:60F:C" + expectedBalanceStartBookingDate.Format("060102") + "EUR1234,56" +
"\r\n:61:" + expectedValutaDate.Format("060102") + expectedBookingDate.Format("0102") + "DR50,NMSCNONREF" +
"\r\n/OCMT/EUR50,//CHGS/ 0,/" +
"\r\n:86:177?00SB-SEPA-Ueberweisung?20 ?30?31?32Max Maier ?33 ?34000" +
"\r\n:62F:C" + expectedBalanceClosingBookingDate.Format("060102") + "EUR1234,56" +
"\r\n-"
err := mt.Unmarshal([]byte(testdata))
if err != nil {
t.Log(err)
t.Fail()
}

}
if len(mt.Transactions) != 1 {
t.Log("There should be exactly one transaction")
t.Fail()
}

}
if test.bookingDateString != mt.Transactions[0].Transaction.BookingDate.String() {
t.Logf("Booking date should be %s but is %s", test.bookingDateString, mt.Transactions[0].Transaction.BookingDate.String())
t.Fail()
}

func TestTransactionListWithUnvalidData(t *testing.T) {
testdata := "\r\n:20:HBCIKTOLST"
testdata += "\r\n:25:12345678/1234123456" +
"\r\n:28C:0" +
"\r\n:60F:C181105EUR1234,56" +
"\r\n:86:177?00SB-SEPA-Ueberweisung?20 ?30?31?32Max Meier ?33 ?34000" +
"\r\n:62F:C190125EUR1234,56"

testdata += "\r\n-"
mt := &MT940{}
error := mt.Unmarshal([]byte(testdata))
if error == nil {
t.Log("Error expected because of CustomTag without TransactionTag")
t.Fail()
}
if test.valutaDateString != mt.Transactions[0].Transaction.ValutaDate.String() {
t.Logf("Valudate date should be %s but is %s", test.valutaDateString, mt.Transactions[0].Transaction.ValutaDate.String())
t.Fail()
}

}
if test.balanceStartBookingDateString != mt.StartingBalance.BookingDate.String() {
t.Logf("balance start booking date should be %s but is %s", test.balanceStartBookingDateString, mt.StartingBalance.BookingDate.String())
t.Fail()
}

func TestTransactionWithRedefineCustomDataTag(t *testing.T) {
testdata := "\r\n:20:HBCIKTOLST"
testdata += "\r\n:25:12345678/1234123456" +
"\r\n:28C:0" +
"\r\n:60F:C181105EUR1234,56" +

"\r\n:61:1811051105DR50,NMSCNONREF" +
"\r\n:86:177?00SB-SEPA-Ueberweisung?20 ?30?31?32Max Meier ?33 ?34000" +
"\r\n:86:177?00SB-SEPA-Ueberweisung?20 ?30?31?32Max Meier ?33 ?34000" +
"\r\n:62F:C190125EUR1234,56"

testdata += "\r\n-"
mt := &MT940{}
error := mt.Unmarshal([]byte(testdata))
if error == nil {
t.Log("Error expected because of more than CustomTag after TransactionTag")
t.Fail()
if test.balanceClosingBookingDateString != mt.ClosingBalance.BookingDate.String() {
t.Logf("balance closing booking date should be %s but is %s", test.balanceClosingBookingDateString, mt.ClosingBalance.BookingDate.String())
t.Fail()
}
}

}
11 changes: 5 additions & 6 deletions swift/mt940_unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,36 @@ func (m *MT940) Unmarshal(value []byte) error {
}
case bytes.HasPrefix(tag, []byte(":60")):
m.StartingBalance = &BalanceTag{}
err = m.StartingBalance.Unmarshal(tag)
err = m.StartingBalance.Unmarshal(tag, m.ReferenceDate)
if err != nil {
return errors.WithMessage(err, "unmarshal starting balance tag")
}
balanceTagOpen = true
case bytes.HasPrefix(tag, []byte(":62")):

m.ClosingBalance = &BalanceTag{}
err = m.ClosingBalance.Unmarshal(tag)
err = m.ClosingBalance.Unmarshal(tag, m.ReferenceDate)
if err != nil {
return errors.WithMessage(err, "unmarshal closing balance tag")
}

balanceTagOpen = false
case bytes.HasPrefix(tag, []byte(":64:")):
m.CurrentValutaBalance = &BalanceTag{}
err = m.CurrentValutaBalance.Unmarshal(tag)
err = m.CurrentValutaBalance.Unmarshal(tag, m.ReferenceDate)
if err != nil {
return errors.WithMessage(err, "unmarshal current valuta balance tag")
}
case bytes.HasPrefix(tag, []byte(":65:")):
m.FutureValutaBalance = &BalanceTag{}
err = m.FutureValutaBalance.Unmarshal(tag)
err = m.FutureValutaBalance.Unmarshal(tag, m.ReferenceDate)
if err != nil {
return errors.WithMessage(err, "unmarshal future valuta balance tag")
}
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