| 1 |
package swift |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"math" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
"unicode" |
| 11 |
|
| 12 |
"github.com/mitch000001/go-hbci/domain" |
| 13 |
"github.com/pkg/errors" |
| 14 |
) |
| 15 |
|
| 16 |
// MT940 represents a S.W.I.F.T. Transaction Report |
| 17 |
type MT940 struct { |
| 18 |
JobReference *AlphaNumericTag |
| 19 |
Reference *AlphaNumericTag |
| 20 |
Account *AccountTag |
| 21 |
StatementNumber *StatementNumberTag |
| 22 |
StartingBalance *BalanceTag |
| 23 |
Transactions []*TransactionSequence |
| 24 |
ClosingBalance *BalanceTag |
| 25 |
CurrentValutaBalance *BalanceTag |
| 26 |
FutureValutaBalance *BalanceTag |
| 27 |
CustomField *CustomFieldTag |
| 28 |
} |
| 29 |
|
| 30 |
// AccountTransactions returns a slice of account transactions created from m |
| 31 |
func (m *MT940) AccountTransactions() []domain.AccountTransaction { |
| 32 |
accountConnection := domain.AccountConnection{BankID: m.Account.BankID, AccountID: m.Account.AccountID, CountryCode: 280} |
| 33 |
var transactions []domain.AccountTransaction |
| 34 |
for _, transactionSequence := range m.Transactions { |
| 35 |
tr := transactionSequence.Transaction |
| 36 |
descr := transactionSequence.Description |
| 37 |
var amount float64 |
| 38 |
if tr.DebitCreditIndicator == "D" { |
| 39 |
amount = -tr.Amount |
| 40 |
} else { |
| 41 |
amount = tr.Amount |
| 42 |
} |
| 43 |
transaction := domain.AccountTransaction{ |
| 44 |
Account: accountConnection, |
| 45 |
Amount: domain.Amount{Amount: amount, Currency: m.StartingBalance.Currency}, |
| 46 |
ValutaDate: tr.ValutaDate.Time, |
| 47 |
BookingDate: tr.BookingDate.Time, |
| 48 |
AccountBalanceBefore: domain.Balance{ |
| 49 |
Amount: domain.Amount{ |
| 50 |
Amount: m.StartingBalance.Amount, |
| 51 |
Currency: m.StartingBalance.Currency, |
| 52 |
}, |
| 53 |
TransmissionDate: m.StartingBalance.BookingDate.Time, |
| 54 |
}, |
| 55 |
AccountBalanceAfter: domain.Balance{ |
| 56 |
Amount: domain.Amount{ |
| 57 |
Amount: m.ClosingBalance.Amount, |
| 58 |
Currency: m.ClosingBalance.Currency, |
| 59 |
}, |
| 60 |
TransmissionDate: m.ClosingBalance.BookingDate.Time, |
| 61 |
}, |
| 62 |
} |
| 63 |
if descr != nil { |
| 64 |
transaction.BookingText = descr.BookingText |
| 65 |
transaction.BankID = descr.BankID |
| 66 |
transaction.AccountID = descr.AccountID |
| 67 |
transaction.Name = descr.Name |
| 68 |
transaction.Purpose = strings.Join(descr.Purpose, " ") |
| 69 |
transaction.Purpose2 = strings.Join(descr.Purpose2, " ") |
| 70 |
transaction.TransactionID = descr.TransactionID |
| 71 |
} |
| 72 |
transactions = append(transactions, transaction) |
| 73 |
} |
| 74 |
return transactions |
| 75 |
} |
| 76 |
|
| 77 |
// AccountTag represents an account in S.W.I.F.T. |
| 78 |
type AccountTag struct { |
| 79 |
Tag string |
| 80 |
BankID string |
| 81 |
AccountID string |
| 82 |
} |
| 83 |
|
| 84 |
// Unmarshal unmarshals value into a |
| 85 |
func (a *AccountTag) Unmarshal(value []byte) error { |
| 86 |
elements, err := extractTagElements(value) |
| 87 |
if err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
if len(elements) != 2 { |
| 91 |
return fmt.Errorf("%T: Malformed marshaled value", a) |
| 92 |
} |
| 93 |
a.Tag = string(elements[0]) |
| 94 |
fields := bytes.Split(elements[1], []byte("/")) |
| 95 |
if len(fields) != 2 { |
| 96 |
return fmt.Errorf("%T: Malformed marshaled value", a) |
| 97 |
} |
| 98 |
a.BankID = string(fields[0]) |
| 99 |
a.AccountID = string(fields[1]) |
| 100 |
return nil |
| 101 |
} |
| 102 |
|
| 103 |
// StatementNumberTag represents a S.W.I.F.T. statement number |
| 104 |
type StatementNumberTag struct { |
| 105 |
Tag string |
| 106 |
Number int |
| 107 |
SheetNumber int |
| 108 |
} |
| 109 |
|
| 110 |
// Unmarshal unmarshals value into s |
| 111 |
func (s *StatementNumberTag) Unmarshal(value []byte) error { |
| 112 |
elements, err := extractTagElements(value) |
| 113 |
if err != nil { |
| 114 |
return err |
| 115 |
} |
| 116 |
if len(elements) != 2 { |
| 117 |
return fmt.Errorf("%T: Malformed marshaled value", s) |
| 118 |
} |
| 119 |
s.Tag = string(elements[0]) |
| 120 |
var numBytes []byte |
| 121 |
if bytes.IndexByte(elements[1], '/') != -1 { |
| 122 |
buf := bytes.NewBuffer(elements[1]) |
| 123 |
numBytes, err = buf.ReadBytes('/') |
| 124 |
if err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
numBytes = numBytes[:len(numBytes)-1] |
| 128 |
sheetNum, err := strconv.Atoi(buf.String()) |
| 129 |
if err != nil { |
| 130 |
return err |
| 131 |
} |
| 132 |
s.SheetNumber = sheetNum |
| 133 |
} else { |
| 134 |
numBytes = elements[1] |
| 135 |
} |
| 136 |
num, err := strconv.Atoi(string(numBytes)) |
| 137 |
if err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
s.Number = num |
| 141 |
return nil |
| 142 |
} |
| 143 |
|
| 144 |
// A BalanceTag represents a balance in S.W.I.F.T. |
| 145 |
type BalanceTag struct { |
| 146 |
Tag string |
| 147 |
DebitCreditIndicator string |
| 148 |
BookingDate domain.ShortDate |
| 149 |
Currency string |
| 150 |
Amount float64 |
| 151 |
} |
| 152 |
|
| 153 |
// Balance returns the balance embodied in b |
| 154 |
func (b *BalanceTag) Balance() domain.Balance { |
| 155 |
amount := b.Amount |
| 156 |
if b.DebitCreditIndicator == "D" { |
| 157 |
amount = -amount |
| 158 |
} |
| 159 |
return domain.Balance{ |
| 160 |
Amount: domain.Amount{Amount: amount, Currency: b.Currency}, |
| 161 |
TransmissionDate: b.BookingDate.Time, |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// Unmarshal unmarshals value into b |
| 166 |
func (b *BalanceTag) Unmarshal(value []byte) error { |
| 167 |
elements, err := extractTagElements(value) |
| 168 |
if err != nil { |
| 169 |
return err |
| 170 |
} |
| 171 |
if len(elements) != 2 { |
| 172 |
return fmt.Errorf("%T: Malformed marshaled value", b) |
| 173 |
} |
| 174 |
b.Tag = string(elements[0]) |
| 175 |
buf := bytes.NewBuffer(elements[1]) |
| 176 |
b.DebitCreditIndicator = string(buf.Next(1)) |
| 177 |
dateBytes := buf.Next(6) |
| 178 |
date, err := parseDate(dateBytes, time.Now().Year()) |
| 179 |
if err != nil { |
| 180 |
return errors.WithMessage(err, "unmarshal balance tag: parsing booking date") |
| 181 |
} |
| 182 |
b.BookingDate = domain.NewShortDate(date) |
| 183 |
b.Currency = string(buf.Next(3)) |
| 184 |
amountString := strings.Replace(buf.String(), ",", ".", 1) |
| 185 |
amount, err := strconv.ParseFloat(amountString, 64) |
| 186 |
if err != nil { |
| 187 |
return errors.Wrap(err, "MT940 Balance tag: error unmarshaling amount") |
| 188 |
} |
| 189 |
b.Amount = amount |
| 190 |
return nil |
| 191 |
} |
| 192 |
|
| 193 |
// A TransactionSequence represents a transaction with an additional |
| 194 |
// description in S.W.I.F.T. |
| 195 |
type TransactionSequence struct { |
| 196 |
Transaction *TransactionTag |
| 197 |
Description *CustomFieldTag |
| 198 |
} |
| 199 |
|
| 200 |
// A TransactionTag represents a transaction in S.W.I.F.T. |
| 201 |
type TransactionTag struct { |
| 202 |
Tag string |
| 203 |
ValutaDate domain.ShortDate |
| 204 |
BookingDate domain.ShortDate |
| 205 |
DebitCreditIndicator string |
| 206 |
CurrencyKind string |
| 207 |
Amount float64 |
| 208 |
BookingKey string |
| 209 |
Reference string |
| 210 |
BankReference string |
| 211 |
AdditionalInformation string |
| 212 |
} |
| 213 |
|
| 214 |
// Unmarshal unmarshals value into t |
| 215 |
func (t *TransactionTag) Unmarshal(value []byte) error { |
| 216 |
elements, err := extractTagElements(value) |
| 217 |
if err != nil { |
| 218 |
return err |
| 219 |
} |
| 220 |
if len(elements) != 2 { |
| 221 |
return fmt.Errorf("%T: Malformed marshaled value", t) |
| 222 |
} |
| 223 |
t.Tag = string(elements[0]) |
| 224 |
buf := bytes.NewBuffer(elements[1]) |
| 225 |
dateBytes := buf.Next(6) |
| 226 |
date, err := parseDate(dateBytes, time.Now().Year()) |
| 227 |
if err != nil { |
| 228 |
return errors.WithMessage(err, "unmarshal transaction tag: parsing valuta date") |
| 229 |
} |
| 230 |
t.ValutaDate = domain.NewShortDate(date) |
| 231 |
r, _, err := buf.ReadRune() |
| 232 |
if err != nil { |
| 233 |
return err |
| 234 |
} |
| 235 |
if unicode.IsDigit(r) { |
| 236 |
buf.UnreadRune() |
| 237 |
dateBytes = buf.Next(4) |
| 238 |
date, err = parseDate(dateBytes, t.ValutaDate.Year()) |
| 239 |
if err != nil { |
| 240 |
return errors.WithMessage(err, "unmarshal transaction tag: parsing booking date") |
| 241 |
} |
| 242 |
t.BookingDate = domain.NewShortDate(date) |
| 243 |
monthDiff := int(math.Abs(float64(t.ValutaDate.Month() - t.BookingDate.Month()))) |
| 244 |
if monthDiff > 1 { |
| 245 |
t.BookingDate = domain.NewShortDate(t.BookingDate.AddDate(1, 0, 0)) |
| 246 |
} |
| 247 |
} |
| 248 |
var runes []rune |
| 249 |
for { |
| 250 |
r, _, err := buf.ReadRune() |
| 251 |
if err != nil { |
| 252 |
return err |
| 253 |
} |
| 254 |
runes = append(runes, r) |
| 255 |
if unicode.IsDigit(r) { |
| 256 |
buf.UnreadRune() |
| 257 |
runes = runes[:len(runes)-1] |
| 258 |
if len(runes) == 3 { |
| 259 |
t.DebitCreditIndicator = string(runes[:2]) |
| 260 |
t.CurrencyKind = string(runes[2:]) |
| 261 |
} else if len(runes) == 2 { |
| 262 |
t.DebitCreditIndicator = string(runes[:1]) |
| 263 |
t.CurrencyKind = string(runes[1:]) |
| 264 |
} else if len(runes) == 1 { |
| 265 |
t.DebitCreditIndicator = string(runes) |
| 266 |
} else { |
| 267 |
return fmt.Errorf("%T: Malformed marshaled value", t) |
| 268 |
} |
| 269 |
break |
| 270 |
} |
| 271 |
} |
| 272 |
amountBytes, err := buf.ReadBytes('N') |
| 273 |
if err != nil { |
| 274 |
return err |
| 275 |
} |
| 276 |
amountBytes = bytes.Replace(amountBytes[:len(amountBytes)-1], []byte(","), []byte("."), 1) |
| 277 |
amount, err := strconv.ParseFloat(string(amountBytes), 64) |
| 278 |
if err != nil { |
| 279 |
return errors.Wrap(err, "MT940 Transaction tag: error unmarshaling amount") |
| 280 |
} |
| 281 |
t.Amount = amount |
| 282 |
t.BookingKey = string(buf.Next(3)) |
| 283 |
remaining := buf.String() |
| 284 |
addInfSepIdx := strings.Index(remaining, "\r\n/") |
| 285 |
doubleSlashIdx := strings.Index(remaining, "//") |
| 286 |
|
| 287 |
if doubleSlashIdx != -1 && addInfSepIdx != -1 { |
| 288 |
t.Reference = remaining[:doubleSlashIdx] |
| 289 |
if doubleSlashIdx < addInfSepIdx { |
| 290 |
t.BankReference = remaining[doubleSlashIdx+2 : addInfSepIdx] |
| 291 |
t.AdditionalInformation = remaining[addInfSepIdx+3:] |
| 292 |
} else { |
| 293 |
// The only valid case in the FINTS30 documentation in the other |
| 294 |
// one, but the data we receive are sometimes formatted like that |
| 295 |
// :( |
| 296 |
t.BankReference = remaining[addInfSepIdx+3 : doubleSlashIdx] |
| 297 |
t.AdditionalInformation = remaining[doubleSlashIdx+2:] |
| 298 |
} |
| 299 |
} else { |
| 300 |
t.Reference = remaining |
| 301 |
if doubleSlashIdx != -1 { |
| 302 |
t.Reference = remaining[:doubleSlashIdx] |
| 303 |
t.BankReference = remaining[doubleSlashIdx+2:] |
| 304 |
} |
| 305 |
if addInfSepIdx != -1 { |
| 306 |
t.Reference = remaining[:addInfSepIdx] |
| 307 |
t.AdditionalInformation = remaining[addInfSepIdx+3:] |
| 308 |
} |
| 309 |
} |
| 310 |
return nil |
| 311 |
} |
| 312 |
|
| 313 |
func parseDate(value []byte, referenceYear int) (time.Time, error) { |
| 314 |
var offset int |
| 315 |
if len(value) == 6 { |
| 316 |
offset = 2 |
| 317 |
} else { |
| 318 |
offset = 4 |
| 319 |
} |
| 320 |
yearBegin := fmt.Sprintf("%d", referenceYear)[:offset] |
| 321 |
dateString := yearBegin + string(value) |
| 322 |
date, err := time.Parse("20060102", dateString) |
| 323 |
if err != nil { |
| 324 |
if strings.HasSuffix(dateString, "0229") { |
| 325 |
return time.Date(referenceYear, 2, 29, 0, 0, 0, 0, time.UTC), nil |
| 326 |
} |
| 327 |
if strings.HasSuffix(dateString, "0230") { |
| 328 |
return time.Date(referenceYear, 2, 30, 0, 0, 0, 0, time.UTC), nil |
| 329 |
} |
| 330 |
return time.Time{}, err |
| 331 |
} |
| 332 |
return date.Truncate(24 * time.Hour), nil |
| 333 |
} |