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 Jan 24, 2019
1 parent b8330e1 commit 5336b55
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions swift/mt940.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package swift
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"go-hbci/domain"
"math"
"strconv"
"strings"
"time"
"unicode"

"github.com/mitch000001/go-hbci/domain"
"github.com/pkg/errors"
)

// MT940 represents a S.W.I.F.T. Transaction Report
Expand All @@ -34,9 +34,9 @@ func (m *MT940) AccountTransactions() []domain.AccountTransaction {
for _, transactionSequence := range m.Transactions {
tr := transactionSequence.Transaction
descr := transactionSequence.Description
var amount float64
var amount decimal.Decimal
if tr.DebitCreditIndicator == "D" {
amount = -tr.Amount
amount = tr.Amount.Neg()
} else {
amount = tr.Amount
}
Expand Down Expand Up @@ -147,14 +147,14 @@ type BalanceTag struct {
DebitCreditIndicator string
BookingDate domain.ShortDate
Currency string
Amount float64
Amount decimal.Decimal
}

// Balance returns the balance embodied in b
func (b *BalanceTag) Balance() domain.Balance {
amount := b.Amount
if b.DebitCreditIndicator == "D" {
amount = -amount
amount = amount.Neg()
}
return domain.Balance{
Amount: domain.Amount{Amount: amount, Currency: b.Currency},
Expand Down Expand Up @@ -182,7 +182,7 @@ func (b *BalanceTag) Unmarshal(value []byte) error {
b.BookingDate = domain.NewShortDate(date)
b.Currency = string(buf.Next(3))
amountString := strings.Replace(buf.String(), ",", ".", 1)
amount, err := strconv.ParseFloat(amountString, 64)
amount, err := decimal.NewFromString(amountString)
if err != nil {
return errors.Wrap(err, "MT940 Balance tag: error unmarshaling amount")
}
Expand All @@ -204,15 +204,15 @@ type TransactionTag struct {
BookingDate domain.ShortDate
DebitCreditIndicator string
CurrencyKind string
Amount float64
Amount decimal.Decimal
BookingKey string
Reference string
BankReference string
AdditionalInformation string
}

// 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,7 +235,7 @@ 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")
}
Expand Down
2 changes: 1 addition & 1 deletion swift/mt940_unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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 5336b55

Please sign in to comment.