Extract package

This commit is contained in:
2022-02-23 21:30:28 +00:00
parent d89a8f4e2e
commit 28c20aacd3
11 changed files with 81 additions and 69 deletions

View File

@ -7,8 +7,8 @@ import (
"io"
"strings"
"time"
"unicode/utf8"
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
"github.com/google/uuid"
)
@ -242,7 +242,7 @@ func (ynab *YNABImport) ImportRegularTransaction(context context.Context, payeeN
func (ynab *YNABImport) ImportTransferTransaction(context context.Context, payeeName string,
transaction CreateTransactionParams, openTransfers *[]Transfer,
account *Account, amount Numeric) error {
account *Account, amount numeric.Numeric) error {
transferToAccountName := payeeName[11:]
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
if err != nil {
@ -295,34 +295,10 @@ func (ynab *YNABImport) ImportTransferTransaction(context context.Context, payee
return nil
}
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 ParseNumeric(text string) (Numeric, error) {
// Remove trailing currency
text = trimLastChar(text)
// Unify decimal separator
text = strings.Replace(text, ",", ".", 1)
num := Numeric{}
err := num.Set(text)
func GetAmount(inflow string, outflow string) (numeric.Numeric, error) {
in, err := numeric.Parse(inflow)
if err != nil {
return num, fmt.Errorf("parse numeric %s: %w", text, err)
}
return num, nil
}
func GetAmount(inflow string, outflow string) (Numeric, error) {
in, err := ParseNumeric(inflow)
if err != nil {
return in, err
return in, fmt.Errorf("parse inflow: %w", err)
}
if !in.IsZero() {
@ -330,9 +306,9 @@ func GetAmount(inflow string, outflow string) (Numeric, error) {
}
// if inflow is zero, use outflow
out, err := ParseNumeric("-" + outflow)
out, err := numeric.Parse("-" + outflow)
if err != nil {
return out, err
return out, fmt.Errorf("parse outflow: %w", err)
}
return out, nil
}