You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.8 KiB
76 lines
1.8 KiB
package main
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
|
|
"src.wolkict.net/sepa/pain"
|
|
)
|
|
|
|
// FieldsPerBatchLine are the expected number of fields in each CSV line.
|
|
const FieldsPerBatchLine = 11
|
|
|
|
// ImportIndex keeps track of what column corresponds to what data.
|
|
type ImportIndex int
|
|
|
|
// These are our numbered input fields
|
|
const (
|
|
ImportCustomerId ImportIndex = iota
|
|
ImportName
|
|
ImportAddr1
|
|
ImportAddr2
|
|
ImportCountry
|
|
ImportIBAN
|
|
ImportMandateId
|
|
ImportSignatureDate
|
|
ImportAmount
|
|
ImportInfo
|
|
ImportInfoLength
|
|
)
|
|
|
|
func ImportCSV(fn string) ([]pain.DrctDbtTxInf, error) {
|
|
f, err := os.Open(fn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("opening file %q failed: %v", fn, err)
|
|
}
|
|
defer f.Close()
|
|
|
|
csvReader := csv.NewReader(f)
|
|
csvReader.FieldsPerRecord = FieldsPerBatchLine
|
|
|
|
all, err := csvReader.ReadAll()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("reading csv %q failed: %v", fn, err)
|
|
}
|
|
|
|
ret := make([]pain.DrctDbtTxInf, len(all))
|
|
for i, line := range all {
|
|
if line[ImportCustomerId] == "customer_id" {
|
|
// Header line
|
|
continue
|
|
}
|
|
|
|
dbtr := pain.NewDebtor(line[ImportName], line[ImportAddr1], line[ImportAddr2], line[ImportCountry])
|
|
dbtrAcct := pain.NewDbtrAcct(line[ImportIBAN])
|
|
mandateInfo := pain.NewMandateInfo(line[ImportMandateId], line[ImportSignatureDate])
|
|
amount, err := strconv.ParseFloat(line[ImportAmount], 64)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error importing line %d, cannot parse amount: %v", i, err)
|
|
}
|
|
if amount == 0.0 {
|
|
continue
|
|
}
|
|
info := line[ImportInfo]
|
|
if info == "" {
|
|
info = EMPTY_INFO
|
|
}
|
|
if len(info) > MAXLEN_INFO {
|
|
return nil, fmt.Errorf("error: info field larger (%d) than allowed (%d)", len(info), MAXLEN_INFO)
|
|
}
|
|
|
|
ret[i] = pain.NewDirectDebitTransaction(dbtr, dbtrAcct, mandateInfo, amount, info, i)
|
|
}
|
|
return ret, nil
|
|
}
|
|
|