delete quick entry and some stats
This commit is contained in:
@@ -19,6 +19,8 @@ func RegisterFilesController(router *gin.RouterGroup) {
|
||||
router.GET("/me/quickEntries", getMyQuickEntries)
|
||||
router.GET("/quickEntries/:id", getQuickEntryById)
|
||||
router.POST("/quickEntries/:id/process", setQuickEntryAsProcessed)
|
||||
router.DELETE("/quickEntries/:id", deleteQuickEntryById)
|
||||
|
||||
router.GET("/attachments/:id/file", getAttachmentFile)
|
||||
}
|
||||
|
||||
@@ -76,6 +78,21 @@ func getQuickEntryById(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||
}
|
||||
}
|
||||
|
||||
func deleteQuickEntryById(c *gin.Context) {
|
||||
var searchByIdQuery models.SearchByIdQuery
|
||||
|
||||
if c.ShouldBindUri(&searchByIdQuery) == nil {
|
||||
err := service.DeleteQuickEntryById(searchByIdQuery.Id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnprocessableEntity, common.NewError("deleteQuickEntryById", err))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusNoContent, gin.H{})
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||
}
|
||||
}
|
||||
func setQuickEntryAsProcessed(c *gin.Context) {
|
||||
var searchByIdQuery models.SearchByIdQuery
|
||||
|
||||
|
||||
37
server/controllers/reports.go
Normal file
37
server/controllers/reports.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/akhilrex/hammond/common"
|
||||
"github.com/akhilrex/hammond/models"
|
||||
"github.com/akhilrex/hammond/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterReportsController(router *gin.RouterGroup) {
|
||||
router.GET("/vehicles/:id/mileage", getMileageForVehicle)
|
||||
}
|
||||
|
||||
func getMileageForVehicle(c *gin.Context) {
|
||||
|
||||
var searchByIdQuery models.SearchByIdQuery
|
||||
|
||||
if err := c.ShouldBindUri(&searchByIdQuery); err == nil {
|
||||
var model models.MileageQueryModel
|
||||
err := c.BindQuery(&model)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnprocessableEntity, common.NewError("getMileageForVehicle", err))
|
||||
return
|
||||
}
|
||||
|
||||
fillups, err := service.GetMileageByVehicleId(searchByIdQuery.Id, model.Since)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnprocessableEntity, common.NewError("getMileageForVehicle", err))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, fillups)
|
||||
} else {
|
||||
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
|
||||
}
|
||||
}
|
||||
@@ -160,6 +160,11 @@ func GetFillupsByVehicleId(id string) (*[]Fillup, error) {
|
||||
result := DB.Preload(clause.Associations).Order("date desc").Find(&obj, &Fillup{VehicleID: id})
|
||||
return &obj, result.Error
|
||||
}
|
||||
func GetFillupsByVehicleIdSince(id string, since time.Time) (*[]Fillup, error) {
|
||||
var obj []Fillup
|
||||
result := DB.Where("date >= ? AND vehicle_id = ?", since, id).Preload(clause.Associations).Order("date desc").Find(&obj)
|
||||
return &obj, result.Error
|
||||
}
|
||||
func FindFillups(condition interface{}) (*[]Fillup, error) {
|
||||
|
||||
var model []Fillup
|
||||
@@ -237,6 +242,10 @@ func GetQuickEntryById(id string) (*QuickEntry, error) {
|
||||
result := DB.Preload(clause.Associations).First(&quickEntry, "id=?", id)
|
||||
return &quickEntry, result.Error
|
||||
}
|
||||
func DeleteQuickEntryById(id string) error {
|
||||
result := DB.Where("id=?", id).Delete(&QuickEntry{})
|
||||
return result.Error
|
||||
}
|
||||
func UpdateQuickEntry(entry *QuickEntry) error {
|
||||
return DB.Save(entry).Error
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ func main() {
|
||||
controllers.RegisterVehicleController(router)
|
||||
controllers.RegisterFilesController(router)
|
||||
controllers.RegisteImportController(router)
|
||||
controllers.RegisterReportsController(router)
|
||||
|
||||
go assetEnv()
|
||||
go intiCron()
|
||||
|
||||
38
server/models/report.go
Normal file
38
server/models/report.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/akhilrex/hammond/db"
|
||||
)
|
||||
|
||||
type MileageModel struct {
|
||||
Date time.Time `form:"date" json:"date" binding:"required" time_format:"2006-01-02"`
|
||||
VehicleID string `form:"vehicleId" json:"vehicleId" binding:"required"`
|
||||
FuelUnit db.FuelUnit `form:"fuelUnit" json:"fuelUnit" binding:"required"`
|
||||
FuelQuantity float32 `form:"fuelQuantity" json:"fuelQuantity" binding:"required"`
|
||||
PerUnitPrice float32 `form:"perUnitPrice" json:"perUnitPrice" binding:"required"`
|
||||
Currency string `json:"currency"`
|
||||
|
||||
Mileage float32 `form:"mileage" json:"mileage" binding:"mileage"`
|
||||
CostPerMile float32 `form:"costPerMile" json:"costPerMile" binding:"costPerMile"`
|
||||
OdoReading int `form:"odoReading" json:"odoReading" binding:"odoReading"`
|
||||
}
|
||||
|
||||
func (v *MileageModel) FuelUnitDetail() db.EnumDetail {
|
||||
return db.FuelUnitDetails[v.FuelUnit]
|
||||
}
|
||||
func (b *MileageModel) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
MileageModel
|
||||
FuelUnitDetail db.EnumDetail `json:"fuelUnitDetail"`
|
||||
}{
|
||||
MileageModel: *b,
|
||||
FuelUnitDetail: b.FuelUnitDetail(),
|
||||
})
|
||||
}
|
||||
|
||||
type MileageQueryModel struct {
|
||||
Since time.Time `json:"since" query:"since" form:"since"`
|
||||
}
|
||||
@@ -58,6 +58,9 @@ func GetQuickEntriesForUser(userId, sorting string) (*[]db.QuickEntry, error) {
|
||||
func GetQuickEntryById(id string) (*db.QuickEntry, error) {
|
||||
return db.GetQuickEntryById(id)
|
||||
}
|
||||
func DeleteQuickEntryById(id string) error {
|
||||
return db.DeleteQuickEntryById(id)
|
||||
}
|
||||
func SetQuickEntryAsProcessed(id string) error {
|
||||
return db.SetQuickEntryAsProcessed(id, time.Now())
|
||||
|
||||
|
||||
@@ -97,22 +97,23 @@ func FuellyImport(content []byte, userId string) []string {
|
||||
)
|
||||
|
||||
isTankFull := record[6] == "Full"
|
||||
|
||||
fal := false
|
||||
fillups = append(fillups, db.Fillup{
|
||||
VehicleID: vehicle.ID,
|
||||
FuelUnit: vehicle.FuelUnit,
|
||||
FuelQuantity: quantity,
|
||||
PerUnitPrice: rate,
|
||||
TotalAmount: totalCost,
|
||||
OdoReading: odoreading,
|
||||
IsTankFull: &isTankFull,
|
||||
Comments: notes,
|
||||
FillingStation: location,
|
||||
UserID: userId,
|
||||
Date: date,
|
||||
Currency: user.Currency,
|
||||
DistanceUnit: user.DistanceUnit,
|
||||
Source: "Fuelly",
|
||||
VehicleID: vehicle.ID,
|
||||
FuelUnit: vehicle.FuelUnit,
|
||||
FuelQuantity: quantity,
|
||||
PerUnitPrice: rate,
|
||||
TotalAmount: totalCost,
|
||||
OdoReading: odoreading,
|
||||
IsTankFull: &isTankFull,
|
||||
Comments: notes,
|
||||
FillingStation: location,
|
||||
HasMissedFillup: &fal,
|
||||
UserID: userId,
|
||||
Date: date,
|
||||
Currency: user.Currency,
|
||||
DistanceUnit: user.DistanceUnit,
|
||||
Source: "Fuelly",
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
52
server/service/reportService.go
Normal file
52
server/service/reportService.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/akhilrex/hammond/db"
|
||||
"github.com/akhilrex/hammond/models"
|
||||
)
|
||||
|
||||
func GetMileageByVehicleId(vehicleId string, since time.Time) (mileage []models.MileageModel, err error) {
|
||||
data, err := db.GetFillupsByVehicleIdSince(vehicleId, since)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fillups := make([]db.Fillup, len(*data))
|
||||
copy(fillups, *data)
|
||||
|
||||
var mileages []models.MileageModel
|
||||
|
||||
for i := 0; i < len(fillups)-1; i++ {
|
||||
last := i + 1
|
||||
|
||||
currentFillup := fillups[i]
|
||||
lastFillup := fillups[last]
|
||||
|
||||
mileage := models.MileageModel{
|
||||
Date: currentFillup.Date,
|
||||
VehicleID: currentFillup.VehicleID,
|
||||
FuelUnit: currentFillup.FuelUnit,
|
||||
FuelQuantity: currentFillup.FuelQuantity,
|
||||
PerUnitPrice: currentFillup.PerUnitPrice,
|
||||
OdoReading: currentFillup.OdoReading,
|
||||
Currency: currentFillup.Currency,
|
||||
Mileage: 0,
|
||||
CostPerMile: 0,
|
||||
}
|
||||
|
||||
if currentFillup.IsTankFull != nil && *currentFillup.IsTankFull && (currentFillup.HasMissedFillup == nil || !(*currentFillup.HasMissedFillup)) {
|
||||
distance := float32(currentFillup.OdoReading - lastFillup.OdoReading)
|
||||
mileage.Mileage = distance / currentFillup.FuelQuantity
|
||||
mileage.CostPerMile = distance / currentFillup.TotalAmount
|
||||
|
||||
}
|
||||
|
||||
mileages = append(mileages, mileage)
|
||||
}
|
||||
if mileages == nil {
|
||||
mileages = make([]models.MileageModel, 0)
|
||||
}
|
||||
return mileages, nil
|
||||
}
|
||||
Reference in New Issue
Block a user