handle 'caisse d'epargne' export encoding

This commit is contained in:
Simon Vieille 2025-02-02 20:09:40 +01:00
commit a1a19f810b

View file

@ -5,12 +5,15 @@ import (
"encoding/csv"
"encoding/hex"
"errors"
"io/ioutil"
"regexp"
"strconv"
"strings"
"time"
"gitnet.fr/deblan/budget/database/manager"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
)
type Transaction struct {
@ -168,14 +171,14 @@ func ImportCaisseEpargneTransactions(content string, bankAccountID int) ([]Trans
}
transaction := Transaction{
ShortLabel: fields[1],
Label: fields[2],
ShortLabel: toUTF8(fields[1]),
Label: toUTF8(fields[2]),
Reference: ref,
Information: fields[4],
OperationType: fields[5],
Information: toUTF8(fields[4]),
OperationType: toUTF8(fields[5]),
AccountedAt: toDate(fields[0], "02/01/2006"),
BankCategory: fields[6],
BankSubCategory: fields[7],
BankCategory: toUTF8(fields[6]),
BankSubCategory: toUTF8(fields[7]),
Debit: -toFloat(fields[8]),
Credit: toFloat(fields[9]),
BankAccountID: bankAccountID,
@ -321,3 +324,10 @@ func toDate(value, format string) time.Time {
return v
}
func toUTF8(input string) string {
reader := transform.NewReader(strings.NewReader(input), charmap.ISO8859_1.NewDecoder())
decoded, _ := ioutil.ReadAll(reader)
return string(decoded)
}