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