75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
package model
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"errors"
|
|
"strings"
|
|
|
|
"gitnet.fr/deblan/budget/database/manager"
|
|
)
|
|
|
|
func ImportCaisseEpargneTransactions(content string, bankAccountID int) ([]Transaction, error) {
|
|
lines := strings.Split(content, "\n")
|
|
db := manager.Get().Db
|
|
datas := []Transaction{}
|
|
|
|
for key, line := range lines {
|
|
if key == 0 {
|
|
continue
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if line == "" {
|
|
continue
|
|
}
|
|
|
|
fields := strings.Split(line, ";")
|
|
|
|
if len(fields) != 13 {
|
|
return datas, errors.New("Invalid format")
|
|
}
|
|
|
|
ref := fields[3]
|
|
|
|
if ref == "" {
|
|
hash := md5.New()
|
|
hash.Write([]byte(line))
|
|
hashInBytes := hash.Sum(nil)
|
|
ref = hex.EncodeToString(hashInBytes)
|
|
}
|
|
|
|
var count int64
|
|
|
|
db.Model(Transaction{}).Where(Transaction{
|
|
Reference: ref,
|
|
BankAccountID: bankAccountID,
|
|
}).Count(&count)
|
|
|
|
if count > 0 {
|
|
continue
|
|
}
|
|
|
|
transaction := Transaction{
|
|
ShortLabel: ToUTF8(fields[1]),
|
|
Label: ToUTF8(fields[2]),
|
|
Reference: ref,
|
|
Information: fields[4],
|
|
OperationType: fields[5],
|
|
AccountedAt: ToDate(fields[0], "02/01/2006"),
|
|
BankCategory: fields[6],
|
|
BankSubCategory: fields[7],
|
|
Debit: -ToFloat(fields[8]),
|
|
Credit: ToFloat(fields[9]),
|
|
BankAccountID: bankAccountID,
|
|
Date: ToDate(fields[10], "02/01/2006"),
|
|
}
|
|
|
|
db.Model(Transaction{}).Save(&transaction)
|
|
|
|
datas = append(datas, transaction)
|
|
}
|
|
|
|
return datas, nil
|
|
}
|