Create db.Expense and db.Fillup in one place
Create Expense and Fillup in one place instead of throughout function
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/akhilrex/hammond/db"
|
||||
@@ -30,37 +31,35 @@ func DrivvoParseExpenses(content []byte, user *db.User, vehicle *db.Vehicle) ([]
|
||||
|
||||
var expenses []db.Expense
|
||||
for index, record := range expenseRecords {
|
||||
expense := db.Expense{}
|
||||
|
||||
date, err := time.Parse("2006-01-02 15:04:05", record[1])
|
||||
if err != nil {
|
||||
errors = append(errors, "Found an invalid date/time at service/expense row "+strconv.Itoa(index+1))
|
||||
}
|
||||
expense.Date = date
|
||||
|
||||
totalCost, err := strconv.ParseFloat(record[2], 32)
|
||||
if err != nil {
|
||||
errors = append(errors, "Found and invalid total cost at service/expense row "+strconv.Itoa(index+1))
|
||||
}
|
||||
expense.Amount = float32(totalCost)
|
||||
|
||||
odometer, err := strconv.Atoi(record[0])
|
||||
if err != nil {
|
||||
errors = append(errors, "Found an invalid odometer reading at service/expense row "+strconv.Itoa(index+1))
|
||||
}
|
||||
expense.OdoReading = odometer
|
||||
|
||||
notes := fmt.Sprintf("Location: %s\nNotes: %s\n", record[4], record[5])
|
||||
expense.Comments = notes
|
||||
|
||||
expense.VehicleID = vehicle.ID
|
||||
expense.ExpenseType = record[3]
|
||||
expense.UserID = user.ID
|
||||
expense.Currency = user.Currency
|
||||
expense.DistanceUnit = user.DistanceUnit
|
||||
expense.Source = "Drivvo"
|
||||
|
||||
expenses = append(expenses, expense)
|
||||
expenses = append(expenses, db.Expense{
|
||||
UserID: user.ID,
|
||||
VehicleID: vehicle.ID,
|
||||
Date: date,
|
||||
OdoReading: odometer,
|
||||
Amount: float32(totalCost),
|
||||
ExpenseType: record[3],
|
||||
Currency: user.Currency,
|
||||
DistanceUnit: user.DistanceUnit,
|
||||
Comments: notes,
|
||||
Source: "Drivvo",
|
||||
})
|
||||
}
|
||||
|
||||
return expenses, errors
|
||||
@@ -85,61 +84,60 @@ func DrivvoParseRefuelings(content []byte, user *db.User, vehicle *db.Vehicle) (
|
||||
continue
|
||||
}
|
||||
|
||||
fillup := db.Fillup{}
|
||||
|
||||
date, err := time.Parse("2006-01-02 15:04:05", record[1])
|
||||
if err != nil {
|
||||
errors = append(errors, "Found an invalid date/time at refuel row "+strconv.Itoa(index+1))
|
||||
}
|
||||
fillup.Date = date
|
||||
|
||||
totalCost, err := strconv.ParseFloat(record[4], 32)
|
||||
if err != nil {
|
||||
errors = append(errors, "Found and invalid total cost at refuel row "+strconv.Itoa(index+1))
|
||||
}
|
||||
fillup.TotalAmount = float32(totalCost)
|
||||
|
||||
odometer, err := strconv.Atoi(record[0])
|
||||
if err != nil {
|
||||
errors = append(errors, "Found an invalid odometer reading at refuel row "+strconv.Itoa(index+1))
|
||||
}
|
||||
fillup.OdoReading = odometer
|
||||
|
||||
// TODO: Make optional
|
||||
location := record[17]
|
||||
fillup.FillingStation = location
|
||||
|
||||
pricePerUnit, err := strconv.ParseFloat(record[3], 32)
|
||||
if err != nil {
|
||||
// TODO: Add unit type to error message
|
||||
errors = append(errors, "Found an invalid cost per unit at refuel row "+strconv.Itoa(index+1))
|
||||
unit := strings.ToLower(db.FuelUnitDetails[vehicle.FuelUnit].Long)
|
||||
errors = append(errors, fmt.Sprintf("Found an invalid cost per %s at refuel row %d", unit, index+1))
|
||||
}
|
||||
fillup.PerUnitPrice = float32(pricePerUnit)
|
||||
|
||||
quantity, err := strconv.ParseFloat(record[5], 32)
|
||||
if err != nil {
|
||||
errors = append(errors, "Found an invalid quantity at refuel row "+strconv.Itoa(index+1))
|
||||
}
|
||||
fillup.FuelQuantity = float32(quantity)
|
||||
|
||||
isTankFull := record[6] == "Yes"
|
||||
fillup.IsTankFull = &isTankFull
|
||||
|
||||
// Unfortunatly, drivvo doesn't expose this info in their export
|
||||
fal := false
|
||||
fillup.HasMissedFillup = &fal
|
||||
|
||||
notes := fmt.Sprintf("Reason: %s\nNotes: %s\nFuel: %s\n", record[18], record[19], record[2])
|
||||
fillup.Comments = notes
|
||||
|
||||
fillup.VehicleID = vehicle.ID
|
||||
fillup.FuelUnit = vehicle.FuelUnit
|
||||
fillup.UserID = user.ID
|
||||
fillup.Currency = user.Currency
|
||||
fillup.DistanceUnit = user.DistanceUnit
|
||||
fillup.Source = "Drivvo"
|
||||
fillups = append(fillups, db.Fillup{
|
||||
VehicleID: vehicle.ID,
|
||||
UserID: user.ID,
|
||||
Date: date,
|
||||
HasMissedFillup: &fal,
|
||||
IsTankFull: &isTankFull,
|
||||
FuelQuantity: float32(quantity),
|
||||
PerUnitPrice: float32(pricePerUnit),
|
||||
FillingStation: location,
|
||||
OdoReading: odometer,
|
||||
TotalAmount: float32(totalCost),
|
||||
FuelUnit: vehicle.FuelUnit,
|
||||
Currency: user.Currency,
|
||||
DistanceUnit: user.DistanceUnit,
|
||||
Comments: notes,
|
||||
Source: "Drivvo",
|
||||
})
|
||||
|
||||
fillups = append(fillups, fillup)
|
||||
}
|
||||
return fillups, errors
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user