Small fixes for ynab import

This commit is contained in:
2021-12-02 16:22:27 +00:00
parent e9173a3d37
commit 61c55dda3a
5 changed files with 48 additions and 11 deletions

View File

@ -5,7 +5,9 @@ import (
"encoding/csv"
"fmt"
"io"
"strings"
"time"
"unicode/utf8"
"git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/google/uuid"
@ -51,6 +53,7 @@ func (ynab *YNABImport) Import(r io.Reader) error {
return fmt.Errorf("could not read from tsv: %w", err)
}
count := 0
for _, record := range csvData[1:] {
accountName := record[0]
account, err := ynab.GetAccount(accountName)
@ -77,7 +80,10 @@ func (ynab *YNABImport) Import(r io.Reader) error {
outflow := record[8]
inflow := record[9]
amount := GetAmount(inflow, outflow)
amount, err := GetAmount(inflow, outflow)
if err != nil {
return fmt.Errorf("could not parse amount from (%s/%s): %w", inflow, outflow, err)
}
//cleared := record[10]
@ -88,20 +94,38 @@ func (ynab *YNABImport) Import(r io.Reader) error {
PayeeID: payeeID,
Amount: amount,
}
ynab.queries.CreateTransaction(ynab.Context, transaction)
_, err = ynab.queries.CreateTransaction(ynab.Context, transaction)
if err != nil {
return fmt.Errorf("could not save transaction %v: %w", transaction, err)
}
count++
}
fmt.Printf("Imported %d transactions\n", count)
return nil
}
func GetAmount(inflow string, outflow string) pgtype.Numeric {
func trimLastChar(s string) string {
r, size := utf8.DecodeLastRuneInString(s)
if r == utf8.RuneError && (size == 0 || size == 1) {
size = 0
}
return s[:len(s)-size]
}
func GetAmount(inflow string, outflow string) (pgtype.Numeric, error) {
// Remove currency
inflow = inflow[:len(inflow)-1]
outflow = outflow[:len(outflow)-1]
inflow = strings.Replace(trimLastChar(inflow), ",", ".", 1)
outflow = strings.Replace(trimLastChar(outflow), ",", ".", 1)
num := pgtype.Numeric{}
num.Set(inflow)
return num
err := num.Set(inflow)
if err != nil {
return num, fmt.Errorf("Could not inflow %s: %w", inflow, err)
}
return num, nil
}
func (ynab *YNABImport) GetAccount(name string) (*postgres.Account, error) {