From a1a19f810b6cd64587d049d2b95ff4c9fe7e3ccf Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 2 Feb 2025 20:09:40 +0100 Subject: [PATCH] handle 'caisse d'epargne' export encoding --- database/model/transaction.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/database/model/transaction.go b/database/model/transaction.go index 6e08556..b2d4462 100644 --- a/database/model/transaction.go +++ b/database/model/transaction.go @@ -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) +}