diff --git a/postgres/ynab-export.go b/postgres/ynab-export.go index f003dc4..2fa1a4c 100644 --- a/postgres/ynab-export.go +++ b/postgres/ynab-export.go @@ -43,8 +43,8 @@ func (ynab *YNABExport) ExportAssignments(context context.Context, w io.Writer) assignment.Group, assignment.Category, assignment.Amount.String() + "€", - "0,00€", - "0,00€", + NewZeroNumeric().String() + "€", + NewZeroNumeric().String() + "€", } err := csv.Write(row) @@ -129,9 +129,9 @@ func GetTransactionRow(transaction GetAllTransactionsForBudgetRow) []string { row = append(row, transaction.Memo) if transaction.Amount.IsPositive() { - row = append(row, "0,00€", transaction.Amount.String()+"€") + row = append(row, NewZeroNumeric().String()+"€", transaction.Amount.String()+"€") } else { - row = append(row, transaction.Amount.String()[1:]+"€", "0,00€") + row = append(row, transaction.Amount.String()[1:]+"€", NewZeroNumeric().String()+"€") } return append(row, string(transaction.Status)) diff --git a/postgres/ynab-import.go b/postgres/ynab-import.go index f54d092..629a844 100644 --- a/postgres/ynab-import.go +++ b/postgres/ynab-import.go @@ -303,27 +303,38 @@ func trimLastChar(s string) string { return s[:len(s)-size] } -func GetAmount(inflow string, outflow string) (Numeric, error) { +func ParseNumeric(text string) (Numeric, error) { // Remove trailing currency - inflow = strings.Replace(trimLastChar(inflow), ",", ".", 1) - outflow = strings.Replace(trimLastChar(outflow), ",", ".", 1) + text = trimLastChar(text) + + // Unify decimal separator + text = strings.Replace(text, ",", ".", 1) num := Numeric{} - err := num.Set(inflow) + err := num.Set(text) if err != nil { - return num, fmt.Errorf("parse inflow %s: %w", inflow, err) + 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 + } + + if !in.IsZero() { + return in, nil } // if inflow is zero, use outflow - if num.Int.Int64() != 0 { - return num, nil - } - - err = num.Set("-" + outflow) + out, err := ParseNumeric("-" + outflow) if err != nil { - return num, fmt.Errorf("parse outflow %s: %w", inflow, err) + return out, err } - return num, nil + return out, nil } func (ynab *YNABImport) GetAccount(context context.Context, name string) (*Account, error) {