From 696469f6c2fdee5ff82189c3fe93acab0b3efd47 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 2 Dec 2021 20:49:07 +0000 Subject: [PATCH] Handle amount in import and display --- http/ynab-import.go | 11 ++++++++++- postgres/conn.go | 10 ++++++++++ web/budget.html | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/http/ynab-import.go b/http/ynab-import.go index 1b22bf2..f710922 100644 --- a/http/ynab-import.go +++ b/http/ynab-import.go @@ -123,7 +123,16 @@ func GetAmount(inflow string, outflow string) (pgtype.Numeric, error) { num := pgtype.Numeric{} err := num.Set(inflow) if err != nil { - return num, fmt.Errorf("Could not inflow %s: %w", inflow, err) + return num, fmt.Errorf("Could not parse inflow %s: %w", inflow, err) + } + + if num.Int.Int64() != 0 { + return num, nil + } + + err = num.Set("-" + outflow) + if err != nil { + return num, fmt.Errorf("Could not parse outflow %s: %w", inflow, err) } return num, nil } diff --git a/postgres/conn.go b/postgres/conn.go index 00d0c18..8f38c69 100644 --- a/postgres/conn.go +++ b/postgres/conn.go @@ -43,3 +43,13 @@ func (tx Transaction) GetAmount() float64 { } return amount } + +func (tx Transaction) GetAmountString() string { + amount := tx.GetAmount() + return fmt.Sprintf("%.2f", amount) +} + +func (tx Transaction) GetPositive() bool { + amount := tx.GetAmount() + return amount >= 0 +} diff --git a/web/budget.html b/web/budget.html index 696befc..fe9ea2d 100644 --- a/web/budget.html +++ b/web/budget.html @@ -19,7 +19,7 @@ {{.Memo}} - {{.GetAmount}} + {{.GetAmount}} {{end}}