From 514d412a202c55ecd84ab172f5a13bf0a2adf33b Mon Sep 17 00:00:00 2001 From: aespin Date: Wed, 9 Oct 2024 16:02:06 +0200 Subject: [PATCH] wip --- TODO | 90 +++++++++++++-------------------- handlers/bundle.go | 9 +++- handlers/index.go | 29 ++++++++++- helper/helper.go | 25 +++++++++ helper/helper_test.go | 42 ++++++++++++++++ storage/storage.go | 9 ++++ templates/barcodes.html | 34 +------------ templates/base.html | 56 ++++++++++++++++++++- templates/bcp.html | 35 +------------ templates/co.html | 35 +------------ templates/hbcp.html | 34 +------------ templates/index.html | 109 +++++++++++++++++++--------------------- templates/lcp.html | 35 +------------ templates/po.html | 35 +------------ types/stats.go | 20 ++++++++ 15 files changed, 283 insertions(+), 314 deletions(-) create mode 100644 helper/helper_test.go create mode 100644 types/stats.go diff --git a/TODO b/TODO index 6b5822e..30f7d1e 100644 --- a/TODO +++ b/TODO @@ -1,56 +1,34 @@ - Grupo6 string `json:"grupo6"` - Po int `json:"po" gorm:"index"` - Co int `json:"co"` - Colada string `json:"colada"` - Calidad string `json:"calidad"` - Matnr string `json:"matnr"` - Dibujo int `json:"dibujo"` - Operador string `json:"operador"` - Serie int `json:"serie"` - Nromatricula string `gorm:"primaryKey" json:"nromatricula"` - NroBulto string `json:"nro_bulto"` - EtiquetaDoble string `json:"etiqueta_doble"` - Fecha int `json:"fecha"` - Turno string `json:"turno"` - Observacion1 string `json:"observacion1"` - Observacion2 string `json:"observacion2"` - Observacion3 string `json:"observacion3"` - PaqueteLongitud float64 `json:"paquete_longitud"` - PaqueteAncho int `json:"paquete_ancho"` - PaqueteAlto float64 `json:"paquete_alto"` - PaquetePeso int `json:"paquete_peso"` - PaqueteNroSecciones int `json:"paquete_nro_secciones"` - PaqueteNroMantos int `json:"paquete_nro_mantos"` - PaqueteNroSeccManto int `json:"paquete_nro_secc_manto"` - SeccionTipo string `gorm:"index" json:"seccion_tipo"` - SeccionLongitud int `json:"seccion_longitud"` - SeccionAncho float64 `json:"seccion_ancho"` - SeccionAlto float64 `json:"seccion_alto"` - Idioma string `json:"idioma"` - Destino int `json:"destino"` - Hora int `json:"hora"` - Horario int `json:"horario"` - Inst string `json:"inst"` - Tren int `json:"tren"` - Normed string `json:"normed"` - Norpro string `json:"norpro"` - Nortol string `json:"nortol"` - Spras string `json:"spras"` - Statu int `json:"statu"` - Crlf string `json:"crlf"` - Maquina int `json:"maquina"` - Padre string `json:"padre"` - Paqpadre string `json:"paqpadre"` - RelevantTime string `json:"relevant_time"` - Desvio float64 `json:"desvio"` - Pesoteorico float64 `json:"pesoteorico"` - PesoteoricoReal float64 `json:"pesoteorico_real"` - DesvioTeoricoReal float64 `json:"desvio_teorico_real"` - FechaImpresion string `json:"fecha_impresion"` - PesoNivel1 float64 `json:"peso_nivel1"` - L3Sended bool `json:"l3_sended,omitempty"` - Confirmed bool `json:"confirmed,omitempty"` - SAP bool `json:"sap,omitempty"` - CreatedAt time.Time `gorm:"->;<-:create" json:"createdat,omitempty"` - UpdatedAt time.Time `json:"updatedat,omitempty"` - DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedat,omitempty"` \ No newline at end of file +
+
+ +
+
\ No newline at end of file diff --git a/handlers/bundle.go b/handlers/bundle.go index c04a13c..8cd5ed0 100644 --- a/handlers/bundle.go +++ b/handlers/bundle.go @@ -37,6 +37,13 @@ func BundleHandler(storage storage.Storager) httprouter.Handle { fechas := r.FormValue("fechas") // loading bed evacuadores := r.FormValue("evacuadores") + // checkbox value + onoff := r.FormValue("confirmed") + // confirmed + confirmed := false + if onoff == "on" { + confirmed = true + } // check form values if codigo != "" { // call storager @@ -62,7 +69,7 @@ func BundleHandler(storage storage.Storager) httprouter.Handle { return } // call storager - data, err := storage.ListBundle(r.Context(), lb, inicio, final) + data, err := storage.ListBundle(r.Context(), lb, inicio, final, confirmed) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/handlers/index.go b/handlers/index.go index 594afd9..b19e1da 100644 --- a/handlers/index.go +++ b/handlers/index.go @@ -3,15 +3,42 @@ package handlers import ( "net/http" "text/template" + "time" + "git.espin.casa/albert/cml04-falcon-ui/helper" "git.espin.casa/albert/cml04-falcon-ui/storage" + "git.espin.casa/albert/cml04-falcon-ui/types" "github.com/julienschmidt/httprouter" ) func IndexHandler(storage storage.Storager) httprouter.Handle { + type PageView struct { + Stats *types.Stats + Bundles []types.BundleData + Barcodes []types.Barcode + } return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + // get current shift dates + dates, err := helper.GetShiftDates(time.Now()) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // + bundles, err := storage.ShiftListBundle(r.Context(), dates[0], dates[1]) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // create view + view := &PageView{ + Stats: &types.Stats{}, + Bundles: bundles, + Barcodes: []types.Barcode{}, + } + t, _ := template.ParseFiles("templates/base.html", "templates/index.html") - err := t.Execute(w, nil) + err = t.Execute(w, view) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/helper/helper.go b/helper/helper.go index 21a6429..e596a21 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -1,6 +1,7 @@ package helper import ( + "fmt" "strings" "time" ) @@ -24,3 +25,27 @@ func Dates(dr string) (start, finish time.Time, err error) { } return } + +func GetShiftDates(inputTime time.Time) ([]time.Time, error) { + if inputTime.Hour() >= 6 && inputTime.Hour() < 14 { + // First shift: 6 to 14 hours + startTime := time.Date(inputTime.Year(), inputTime.Month(), inputTime.Day(), 6, 0, 0, 0, inputTime.Location()) + endTime := time.Date(inputTime.Year(), inputTime.Month(), inputTime.Day(), 14, 0, 0, 0, inputTime.Location()) + + return []time.Time{startTime, endTime}, nil + } else if inputTime.Hour() >= 14 && inputTime.Hour() < 22 { + // Second shift: 14 to 22 hours + startTime := time.Date(inputTime.Year(), inputTime.Month(), inputTime.Day(), 14, 0, 0, 0, inputTime.Location()) + endTime := time.Date(inputTime.Year(), inputTime.Month(), inputTime.Day(), 22, 0, 0, 0, inputTime.Location()) + + return []time.Time{startTime, endTime}, nil + } else if inputTime.Hour() >= 22 || inputTime.AddDate(0, 0, 1).Hour() < 6 { + // Third shift: 22 hours to 6 next day + startTime := time.Date(inputTime.Year(), inputTime.Month(), inputTime.Day(), 22, 0, 0, 0, inputTime.Location()) + endTime := time.Date(inputTime.AddDate(0, 0, 1).Year(), inputTime.AddDate(0, 0, 1).Month(), inputTime.AddDate(0, 0, 1).Day(), 6, 0, 0, 0, inputTime.Location()) + + return []time.Time{startTime, endTime}, nil + } else { + return nil, fmt.Errorf("el horario de entrada no es válido") + } +} diff --git a/helper/helper_test.go b/helper/helper_test.go new file mode 100644 index 0000000..700b23e --- /dev/null +++ b/helper/helper_test.go @@ -0,0 +1,42 @@ +package helper + +import ( + "testing" + "time" +) + +func TestGetShiftDates(t *testing.T) { + // First case 1: First shift + inputTime := time.Date(2023, 1, 15, 7, 0, 0, 0, time.UTC) + expectedStart := time.Date(2023, 1, 15, 6, 0, 0, 0, time.UTC) + expectedEnd := time.Date(2023, 1, 15, 14, 0, 0, 0, time.UTC) + shifts, err := GetShiftDates(inputTime) + if err != nil { + t.Errorf("unexpected error GetShiftDates: %v", err) + } + if shifts[0] != expectedStart || shifts[1] != expectedEnd { + t.Errorf("not expected results: got %v, %v; want %v, %v", shifts[0], shifts[1], expectedStart, expectedEnd) + } + // Second case 1: Second shift + inputTime = time.Date(2023, 1, 15, 14, 0, 0, 0, time.UTC) + expectedStart = time.Date(2023, 1, 15, 14, 0, 0, 0, time.UTC) + expectedEnd = time.Date(2023, 1, 15, 22, 0, 0, 0, time.UTC) + shifts, err = GetShiftDates(inputTime) + if err != nil { + t.Errorf("unexpected error GetShiftDates: %v", err) + } + if shifts[0] != expectedStart || shifts[1] != expectedEnd { + t.Errorf("not expected results: got %v, %v; want %v, %v", shifts[0], shifts[1], expectedStart, expectedEnd) + } + // Third case 3: Third shift + inputTime = time.Date(2023, 1, 15, 23, 0, 0, 0, time.UTC) + expectedStart = time.Date(2023, 1, 15, 22, 0, 0, 0, time.UTC) + expectedEnd = time.Date(2023, 1, 16, 6, 0, 0, 0, time.UTC) + shifts, err = GetShiftDates(inputTime) + if err != nil { + t.Errorf("unexpected error GetShiftDates: %v", err) + } + if shifts[0] != expectedStart || shifts[1] != expectedEnd { + t.Errorf("not expected results: got %v, %v; want %v, %v", shifts[0], shifts[1], expectedStart, expectedEnd) + } +} diff --git a/storage/storage.go b/storage/storage.go index c885371..bb19048 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -24,6 +24,7 @@ type Storager interface { ListBarcode(ctx context.Context, lb types.LoadingBed, inicio, final time.Time) (barcodes []types.Barcode, err error) Bundle(ctx context.Context, ua string) (bundle *types.BundleData, err error) ListBundle(ctx context.Context, lb types.LoadingBed, inicio, final time.Time, confirmed bool) (bundles []types.BundleData, err error) + ShiftListBundle(ctx context.Context, inicio, final time.Time) (bundles []types.BundleData, err error) } type storage struct { @@ -31,6 +32,14 @@ type storage struct { mux sync.RWMutex } +// ShiftListBundle implements Storager. +func (s *storage) ShiftListBundle(ctx context.Context, inicio, final time.Time) (bundles []types.BundleData, err error) { + if err := s.db.Where("created_at >= ? and created_at < ?", inicio, final).Find(&bundles).WithContext(ctx).Error; err != nil { + return nil, err + } + return +} + // ListBundle implements Storager. func (s *storage) ListBundle(ctx context.Context, lb types.LoadingBed, inicio time.Time, final time.Time, confirmed bool) (bundles []types.BundleData, err error) { if lb == types.ALL { diff --git a/templates/barcodes.html b/templates/barcodes.html index b6d0e24..636d20d 100644 --- a/templates/barcodes.html +++ b/templates/barcodes.html @@ -4,38 +4,6 @@
-
- -