Small fixes for ynab import
This commit is contained in:
parent
e9173a3d37
commit
61c55dda3a
@ -5,7 +5,9 @@ import (
|
|||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||||
"github.com/google/uuid"
|
"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)
|
return fmt.Errorf("could not read from tsv: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count := 0
|
||||||
for _, record := range csvData[1:] {
|
for _, record := range csvData[1:] {
|
||||||
accountName := record[0]
|
accountName := record[0]
|
||||||
account, err := ynab.GetAccount(accountName)
|
account, err := ynab.GetAccount(accountName)
|
||||||
@ -77,7 +80,10 @@ func (ynab *YNABImport) Import(r io.Reader) error {
|
|||||||
|
|
||||||
outflow := record[8]
|
outflow := record[8]
|
||||||
inflow := record[9]
|
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]
|
//cleared := record[10]
|
||||||
|
|
||||||
@ -88,20 +94,38 @@ func (ynab *YNABImport) Import(r io.Reader) error {
|
|||||||
PayeeID: payeeID,
|
PayeeID: payeeID,
|
||||||
Amount: amount,
|
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
|
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
|
// Remove currency
|
||||||
inflow = inflow[:len(inflow)-1]
|
inflow = strings.Replace(trimLastChar(inflow), ",", ".", 1)
|
||||||
outflow = outflow[:len(outflow)-1]
|
outflow = strings.Replace(trimLastChar(outflow), ",", ".", 1)
|
||||||
|
|
||||||
num := pgtype.Numeric{}
|
num := pgtype.Numeric{}
|
||||||
num.Set(inflow)
|
err := num.Set(inflow)
|
||||||
return num
|
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) {
|
func (ynab *YNABImport) GetAccount(name string) (*postgres.Account, error) {
|
||||||
|
@ -39,3 +39,12 @@ func Connect(server string, user string, password string, database string) (*Que
|
|||||||
|
|
||||||
return New(connPG), nil
|
return New(connPG), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx Transaction) GetAmount() float64 {
|
||||||
|
var amount float64
|
||||||
|
err := tx.Amount.AssignTo(&amount)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return amount
|
||||||
|
}
|
||||||
|
@ -7,8 +7,10 @@ RETURNING *;
|
|||||||
-- name: GetTransactionsForBudget :many
|
-- name: GetTransactionsForBudget :many
|
||||||
SELECT transactions.* FROM transactions
|
SELECT transactions.* FROM transactions
|
||||||
LEFT JOIN accounts ON accounts.id = transactions.account_id
|
LEFT JOIN accounts ON accounts.id = transactions.account_id
|
||||||
WHERE accounts.budget_id = $1;
|
WHERE accounts.budget_id = $1
|
||||||
|
ORDER BY transactions.date DESC;
|
||||||
|
|
||||||
-- name: GetTransactionsForAccount :many
|
-- name: GetTransactionsForAccount :many
|
||||||
SELECT transactions.* FROM transactions
|
SELECT transactions.* FROM transactions
|
||||||
WHERE transactions.account_id = $1;
|
WHERE transactions.account_id = $1
|
||||||
|
ORDER BY transactions.date DESC;
|
@ -49,6 +49,7 @@ func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionPa
|
|||||||
const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many
|
const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many
|
||||||
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions
|
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions
|
||||||
WHERE transactions.account_id = $1
|
WHERE transactions.account_id = $1
|
||||||
|
ORDER BY transactions.date DESC
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]Transaction, error) {
|
func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]Transaction, error) {
|
||||||
@ -82,6 +83,7 @@ const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many
|
|||||||
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions
|
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions
|
||||||
LEFT JOIN accounts ON accounts.id = transactions.account_id
|
LEFT JOIN accounts ON accounts.id = transactions.account_id
|
||||||
WHERE accounts.budget_id = $1
|
WHERE accounts.budget_id = $1
|
||||||
|
ORDER BY transactions.date DESC
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]Transaction, error) {
|
func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]Transaction, error) {
|
||||||
|
@ -17,9 +17,9 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td>{{.Date}}</td>
|
<td>{{.Date}}</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="transaction/{{.ID}}">{{.Memo.String}}</a>
|
<a href="transaction/{{.ID}}">{{.Memo}}</a>
|
||||||
</td>
|
</td>
|
||||||
<td>{{.Amount}}</td>
|
<td>{{.GetAmount}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
</table>
|
</table>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user