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:
2022-02-23 21:17:43 +00:00
parent 0478d82c1f
commit 6686904539
2 changed files with 27 additions and 16 deletions

View File

@ -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) {