Use zero Numeric for export instead of hardcoding 0,00
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Jan Bader 2022-02-23 21:17:43 +00:00
parent 0478d82c1f
commit 6686904539
2 changed files with 27 additions and 16 deletions

View File

@ -43,8 +43,8 @@ func (ynab *YNABExport) ExportAssignments(context context.Context, w io.Writer)
assignment.Group, assignment.Group,
assignment.Category, assignment.Category,
assignment.Amount.String() + "€", assignment.Amount.String() + "€",
"0,00€", NewZeroNumeric().String() + "€",
"0,00€", NewZeroNumeric().String() + "€",
} }
err := csv.Write(row) err := csv.Write(row)
@ -129,9 +129,9 @@ func GetTransactionRow(transaction GetAllTransactionsForBudgetRow) []string {
row = append(row, transaction.Memo) row = append(row, transaction.Memo)
if transaction.Amount.IsPositive() { if transaction.Amount.IsPositive() {
row = append(row, "0,00€", transaction.Amount.String()+"€") row = append(row, NewZeroNumeric().String()+"€", transaction.Amount.String()+"€")
} else { } 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)) return append(row, string(transaction.Status))

View File

@ -303,27 +303,38 @@ func trimLastChar(s string) string {
return s[:len(s)-size] return s[:len(s)-size]
} }
func GetAmount(inflow string, outflow string) (Numeric, error) { func ParseNumeric(text string) (Numeric, error) {
// Remove trailing currency // Remove trailing currency
inflow = strings.Replace(trimLastChar(inflow), ",", ".", 1) text = trimLastChar(text)
outflow = strings.Replace(trimLastChar(outflow), ",", ".", 1)
// Unify decimal separator
text = strings.Replace(text, ",", ".", 1)
num := Numeric{} num := Numeric{}
err := num.Set(inflow) err := num.Set(text)
if err != nil { 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 inflow is zero, use outflow
if num.Int.Int64() != 0 { out, err := ParseNumeric("-" + outflow)
return num, nil
}
err = num.Set("-" + outflow)
if err != nil { 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) { func (ynab *YNABImport) GetAccount(context context.Context, name string) (*Account, error) {