Add functions for parsing drivvo CSVs.

This commit is contained in:
Alf Sebastian Houge
2022-04-04 14:55:22 +02:00
parent 15f6539bf7
commit bfaebf17d0
5 changed files with 368 additions and 5 deletions

View File

@@ -9,6 +9,7 @@ import (
func RegisteImportController(router *gin.RouterGroup) { func RegisteImportController(router *gin.RouterGroup) {
router.POST("/import/fuelly", fuellyImport) router.POST("/import/fuelly", fuellyImport)
router.POST("/import/drivvo", drivvoImport)
} }
func fuellyImport(c *gin.Context) { func fuellyImport(c *gin.Context) {
@@ -24,3 +25,17 @@ func fuellyImport(c *gin.Context) {
} }
c.JSON(http.StatusOK, gin.H{}) c.JSON(http.StatusOK, gin.H{})
} }
func drivvoImport(c *gin.Context) {
bytes, err := getFileBytes(c, "file")
if err != nil {
c.JSON(http.StatusUnprocessableEntity, err)
return
}
errors := service.DrivvoImport(bytes, c.MustGet("userId").(string))
if len(errors) > 0 {
c.JSON(http.StatusUnprocessableEntity, gin.H{"errors": errors})
return
}
c.JSON(http.StatusOK, gin.H{})
}

View File

@@ -11,6 +11,188 @@ import (
"github.com/leekchan/accounting" "github.com/leekchan/accounting"
) )
// TODO: Move drivvo stuff to separate file
func DrivvoParseExpenses(content []byte, user *db.User) ([]db.Expense, []string) {
expenseReader := csv.NewReader(bytes.NewReader(content))
expenseReader.Comment = '#'
// Read headers (there is a trailing comma at the end, that's why we have to read the first line)
expenseReader.Read()
expenseReader.FieldsPerRecord = 6
expenseRecords, err := expenseReader.ReadAll()
var errors []string
if err != nil {
errors = append(errors, err.Error())
println(err.Error())
return nil, errors
}
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.ExpenseType = record[3]
expense.UserID = user.ID
expense.Currency = user.Currency
expense.DistanceUnit = user.DistanceUnit
expense.Source = "Drivvo"
expenses = append(expenses, expense)
}
return expenses, errors
}
func DrivvoParseRefuelings(content []byte, user *db.User) ([]db.Fillup, []string) {
refuelingReader := csv.NewReader(bytes.NewReader(content))
refuelingReader.Comment = '#'
refuelingRecords, err := refuelingReader.ReadAll()
var errors []string
if err != nil {
errors = append(errors, err.Error())
println(err.Error())
return nil, errors
}
var fillups []db.Fillup
for index, record := range refuelingRecords {
// Skip column titles
if index == 0 {
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))
}
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.UserID = user.ID
fillup.Currency = user.Currency
fillup.DistanceUnit = user.DistanceUnit
fillup.Source = "Drivvo"
fillups = append(fillups, fillup)
}
return fillups, errors
}
func DrivvoImport(content []byte, userId string) []string {
var errors []string
user, err := GetUserById(userId)
if err != nil {
errors = append(errors, err.Error())
return errors
}
serviceSectionIndex := bytes.Index(content, []byte("#Service"))
endParseIndex := bytes.Index(content, []byte("#Income"))
if endParseIndex == -1 {
endParseIndex = bytes.Index(content, []byte("#Route"))
if endParseIndex == -1 {
endParseIndex = len(content)
}
}
expenseSectionIndex := bytes.Index(content, []byte("#Expense"))
if expenseSectionIndex == -1 {
expenseSectionIndex = endParseIndex
}
fillups, errors := DrivvoParseRefuelings(content[:serviceSectionIndex], user)
_ = fillups
var allExpenses []db.Expense
if serviceSectionIndex != -1 {
services, parseErrors := DrivvoParseExpenses(content[serviceSectionIndex:expenseSectionIndex], user)
if parseErrors != nil {
errors = append(errors, parseErrors...)
}
allExpenses = append(allExpenses, services...)
}
if expenseSectionIndex != endParseIndex {
expenses, parseErrors := DrivvoParseExpenses(content[expenseSectionIndex:endParseIndex], user)
if parseErrors != nil {
errors = append(errors, parseErrors...)
}
allExpenses = append(allExpenses, expenses...)
}
if len(errors) != 0 {
return errors
}
errors = append(errors, "Not implemented")
return errors
}
func FuellyImport(content []byte, userId string) []string { func FuellyImport(content []byte, userId string) []string {
stream := bytes.NewReader(content) stream := bytes.NewReader(content)
reader := csv.NewReader(stream) reader := csv.NewReader(stream)

View File

@@ -410,6 +410,15 @@ export default [
}, },
props: (route) => ({ user: store.state.auth.currentUser || {} }), props: (route) => ({ user: store.state.auth.currentUser || {} }),
}, },
{
path: '/import/drivvo',
name: 'import-drivvo',
component: () => lazyLoadView(import('@views/import-drivvo.vue')),
meta: {
authRequired: true,
},
props: (route) => ({ user: store.state.auth.currentUser || {} }),
},
{ {
path: '/logout', path: '/logout',
name: 'logout', name: 'logout',

View File

@@ -0,0 +1,146 @@
<script>
import Layout from '@layouts/main.vue'
import { mapState } from 'vuex'
import axios from 'axios'
export default {
page: {
title: 'Import Drivvo',
meta: [{ name: 'description', content: 'The Import Drivvo page.' }],
},
components: { Layout },
computed: {
...mapState('utils', ['isMobile']),
uploadButtonLabel() {
if (this.isMobile) {
if (this.file == null) {
return 'Choose Photo'
} else {
return ''
}
} else {
if (this.file == null) {
return 'Choose CSV'
} else {
return ''
}
}
},
},
props: {
user: {
type: Object,
required: true,
},
},
data: function() {
return {
file: null,
tryingToCreate: false,
errors: [],
}
},
methods: {
importDrivvo() {
console.log('Import from drivvo')
if (this.file == null) {
return
}
this.tryingToCreate = true
this.errorMessage = ''
const formData = new FormData()
formData.append('file', this.file, this.file.name)
axios
.post(`/api/import/drivvo`, formData)
.then((data) => {
this.$buefy.toast.open({
message: 'Data Imported Successfully',
type: 'is-success',
duration: 3000,
})
this.file = null
})
.catch((ex) => {
this.$buefy.toast.open({
duration: 5000,
message: 'There was some issue with importing the file. Please check the error message',
position: 'is-bottom',
type: 'is-danger',
})
if (ex.response && ex.response.data.errors) {
this.errors = ex.response.data.errors
}
})
.finally(() => {
this.tryingToCreate = false
})
},
},
}
</script>
<template>
<Layout>
<div class="columns box">
<div class="column">
<h1 class="title">Import from Drivvo</h1>
</div>
</div>
<br />
<div class="columns">
<div class="column">
<!-- TODO: Write about that income and trips are not imported. Also Second and Third fuel are ignored -->
<p class="subtitle"> Steps to import data from Drivvo</p>
<ol>
<li
>Export your data from Fuelly in the CSV format. Steps to do that can be found
<a href="http://docs.fuelly.com/acar-import-export-center" target="_nofollow">here</a>.</li
>
<li>Make sure that you have already created the vehicles in Hammond platform.</li>
<li>Make sure that the Vehicle nickname in Hammond is exactly the same as the name on Fuelly CSV or the import will not work.</li>
<li
>Make sure that the <u>Currency</u> and <u>Distance Unit</u> are set correctly in Hammond. Import will not autodetect Currency from the
CSV but use the one set for the user.</li
>
<li>Similiarly, make sure that the <u>Fuel Unit</u> and <u>Fuel Type</u> are correctly set in the Vehicle.</li>
<li>Once you have checked all these points,just import the CSV below.</li>
<li><b>Make sure that you do not import the file again and that will create repeat entries.</b></li>
</ol>
</div>
</div>
<div class="section box">
<div class="columns">
<div class="column is-two-thirds"> <p class="subtitle">Choose the Drivvo CSV and press the import button.</p></div>
<div class="column is-one-third is-flex is-align-content-center">
<form @submit.prevent="importDrivvo">
<div class="columns"
><div class="column">
<b-field class="file is-primary" :class="{ 'has-name': !!file }">
<b-upload v-model="file" class="file-label" accept=".csv">
<span class="file-cta">
<b-icon class="file-icon" icon="upload"></b-icon>
<span class="file-label">{{ uploadButtonLabel }}</span>
</span>
<span v-if="file" class="file-name" :class="isMobile ? 'file-name-mobile' : 'file-name-desktop'">
{{ file.name }}
</span>
</b-upload>
</b-field>
</div>
<div class="column">
<b-button tag="input" native-type="submit" :disabled="tryingToCreate" type="is-primary" value="Upload File" class="control">
Import
</b-button>
</div></div
>
</form>
</div>
</div>
</div>
<b-message v-if="errors.length" type="is-danger">
<ul>
<li v-for="error in errors" :key="error">{{ error }}</li>
</ul>
</b-message>
</Layout>
</template>

View File

@@ -26,11 +26,22 @@ export default {
> >
<br /> <br />
<div class="columns"> <div class="columns">
<div class="box column is-one-third" to="/import-fuelly"> <div class="column is-one-third">
<h1 class="title">Fuelly</h1> <div class="box">
<p>If you have been using Fuelly to store your vehicle data, export the CSV file from Fuelly and click here to import.</p> <h1 class="title">Fuelly</h1>
<br /> <p>If you have been using Fuelly to store your vehicle data, export the CSV file from Fuelly and click here to import.</p>
<b-button type="is-primary" tag="router-link" to="/import/fuelly">Import</b-button> <br />
<b-button type="is-primary" tag="router-link" to="/import/fuelly">Import</b-button>
</div>
</div>
<div class="column is-one-third" to="/import-fuelly">
<div class="box">
<h1 class="title">Drivvo</h1>
<p>Import data stored in Drivvo to Hammond by exporting the CSV file from Drivvo and uploading it here.</p>
<br />
<b-button type="is-primary" tag="router-link" to="/import/drivvo">Import</b-button>
</div>
</div> </div>
</div> </div>
</Layout> </Layout>