2022-09-06 19:15:07 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-09-13 04:54:30 +00:00
|
|
|
|
|
|
|
"github.com/hay-kot/content/backend/internal/types"
|
2022-09-06 19:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ErrInvalidCsv = errors.New("invalid csv")
|
|
|
|
|
2022-09-13 04:54:30 +00:00
|
|
|
const NumOfCols = 21
|
|
|
|
|
2022-09-06 19:15:07 +00:00
|
|
|
func parseFloat(s string) float64 {
|
|
|
|
if s == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
f, _ := strconv.ParseFloat(s, 64)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDate(s string) time.Time {
|
|
|
|
if s == "" {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
p, _ := time.Parse("01/02/2006", s)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-09-13 04:54:30 +00:00
|
|
|
func parseBool(s string) bool {
|
|
|
|
switch strings.ToLower(s) {
|
|
|
|
case "true", "yes", "1":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 04:54:30 +00:00
|
|
|
func parseInt(s string) int {
|
|
|
|
i, _ := strconv.Atoi(s)
|
|
|
|
return i
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 04:54:30 +00:00
|
|
|
type csvRow struct {
|
|
|
|
Item types.ItemSummary
|
|
|
|
Location string
|
|
|
|
LabelStr string
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 04:54:30 +00:00
|
|
|
func newCsvRow(row []string) csvRow {
|
|
|
|
return csvRow{
|
|
|
|
Location: row[1],
|
|
|
|
LabelStr: row[2],
|
|
|
|
Item: types.ItemSummary{
|
|
|
|
ImportRef: row[0],
|
|
|
|
Quantity: parseInt(row[3]),
|
|
|
|
Name: row[4],
|
|
|
|
Description: row[5],
|
|
|
|
Insured: parseBool(row[6]),
|
|
|
|
SerialNumber: row[7],
|
|
|
|
ModelNumber: row[8],
|
|
|
|
Manufacturer: row[9],
|
|
|
|
Notes: row[10],
|
|
|
|
PurchaseFrom: row[11],
|
|
|
|
PurchasePrice: parseFloat(row[12]),
|
|
|
|
PurchaseTime: parseDate(row[13]),
|
|
|
|
LifetimeWarranty: parseBool(row[14]),
|
|
|
|
WarrantyExpires: parseDate(row[15]),
|
|
|
|
WarrantyDetails: row[16],
|
|
|
|
SoldTo: row[17],
|
|
|
|
SoldPrice: parseFloat(row[18]),
|
|
|
|
SoldTime: parseDate(row[19]),
|
|
|
|
SoldNotes: row[20],
|
|
|
|
},
|
|
|
|
}
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c csvRow) getLabels() []string {
|
2022-09-13 04:54:30 +00:00
|
|
|
split := strings.Split(c.LabelStr, ";")
|
2022-09-06 19:15:07 +00:00
|
|
|
|
|
|
|
// Trim each
|
|
|
|
for i, s := range split {
|
|
|
|
split[i] = strings.TrimSpace(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove empty
|
|
|
|
for i, s := range split {
|
|
|
|
if s == "" {
|
|
|
|
split = append(split[:i], split[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return split
|
|
|
|
}
|