diff --git a/assets/js/barcode.js b/assets/js/barcode.js index 19148f8..20660d9 100644 --- a/assets/js/barcode.js +++ b/assets/js/barcode.js @@ -7,10 +7,5 @@ $(document).ready(function () { isRange:true, color:"link", minuteSteps:1}); - // To access to bulmaCalendar instance of an element - $('#submit').click(function() { - const dateInput = $('#inicio').val(); - //$('#selected-date').text(`Fecha seleccionada: ${dateInput}`); - }); }); diff --git a/assets/js/bundle.js b/assets/js/bundle.js new file mode 100644 index 0000000..2199a8f --- /dev/null +++ b/assets/js/bundle.js @@ -0,0 +1,10 @@ +$(document).ready(function () { + // Initialize all input of date type. + const calendar = bulmaCalendar.attach('[type="date"]',{ + weekStart: 1, + dateFormat: 'dd/MM/yyyy', + type:"datetime", + isRange:true, + color:"link", + minuteSteps:1}); +}); diff --git a/handlers/barcodes.go b/handlers/barcodes.go index 80fc0c5..4922070 100644 --- a/handlers/barcodes.go +++ b/handlers/barcodes.go @@ -36,12 +36,6 @@ func BarcodesHandler(storage storage.Storager) httprouter.Handle { codigo := r.FormValue("codigo") // date range fechas := r.FormValue("fechas") - // get range dates - inicio, final, err := helper.Dates(fechas) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } // loading bed evacuadores := r.FormValue("evacuadores") // check form values @@ -55,6 +49,12 @@ func BarcodesHandler(storage storage.Storager) httprouter.Handle { // append barcode to holder Barcodes = append(Barcodes, *data) } else { + // get range dates + inicio, final, err := helper.Dates(fechas) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } // get loading bed from string lb, ok := types.MapLoadingBed[evacuadores] // if loading bed not found diff --git a/handlers/bundle.go b/handlers/bundle.go index d916f21..c04a13c 100644 --- a/handlers/bundle.go +++ b/handlers/bundle.go @@ -1,10 +1,13 @@ package handlers import ( + "fmt" "net/http" "text/template" + "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" ) @@ -20,24 +23,60 @@ func BundleHandler(storage storage.Storager) httprouter.Handle { } } - // if r.Method == http.MethodPost { - // // barcodes holder - // Bundles := []types.BundleData{} - // // parse form data from formulary - // if err := r.ParseForm(); err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // } - // // barcode - // codigo := r.FormValue("codigo") - // // date range - // fechas := r.FormValue("fechas") - // // get range dates - // inicio, final, err := helper.Dates(fechas) - // if err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // return - // } - // } - + // post method + if r.Method == http.MethodPost { + // bundles holder + Bundles := []types.BundleData{} + // parse form data from formulary + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + // ua + codigo := r.FormValue("ua") + // date range + fechas := r.FormValue("fechas") + // loading bed + evacuadores := r.FormValue("evacuadores") + // check form values + if codigo != "" { + // call storager + data, err := storage.Bundle(r.Context(), codigo) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // append barcode to holder + Bundles = append(Bundles, *data) + } else { + // get range dates + inicio, final, err := helper.Dates(fechas) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // get loading bed from string + lb, ok := types.MapLoadingBed[evacuadores] + // if loading bed not found + if !ok { + http.Error(w, fmt.Errorf("loading bed value not found").Error(), http.StatusInternalServerError) + return + } + // call storager + data, err := storage.ListBundle(r.Context(), lb, inicio, final) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + //set barcodes + Bundles = data + } + // parse template files + t, _ := template.ParseFiles("templates/base.html", "templates/bundle.html") + // execute templates + if err := t.Execute(w, Bundles); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } } } diff --git a/storage/storage.go b/storage/storage.go index c40ef26..e4b4707 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -34,11 +34,11 @@ type storage struct { // ListBundle implements Storager. func (s *storage) ListBundle(ctx context.Context, lb types.LoadingBed, inicio time.Time, final time.Time) (bundles []types.BundleData, err error) { if lb == types.ALL { - if err := s.db.Where("created_at >= ? and created_at < ?", inicio, final).Find(bundles).WithContext(ctx).Error; err != nil { + if err := s.db.Where("created_at >= ? and created_at < ?", inicio, final).Find(&bundles).WithContext(ctx).Error; err != nil { return nil, err } } else { - if err := s.db.Where("loading_bed = ? and created_at >= ? and created_at < ?", lb, inicio, final).Find(bundles).WithContext(ctx).Error; err != nil { + if err := s.db.Where("loading_bed = ? and created_at >= ? and created_at < ?", lb, inicio, final).Find(&bundles).WithContext(ctx).Error; err != nil { return nil, err } } @@ -47,7 +47,7 @@ func (s *storage) ListBundle(ctx context.Context, lb types.LoadingBed, inicio ti // Bundle implements Storager. func (s *storage) Bundle(ctx context.Context, ua string) (bundle *types.BundleData, err error) { - if err := s.db.First(bundle, ua).WithContext(ctx).Error; err != nil { + if err := s.db.First(&bundle, ua).WithContext(ctx).Error; err != nil { return nil, err } return diff --git a/templates/barcodes.html b/templates/barcodes.html index a016534..b6d0e24 100644 --- a/templates/barcodes.html +++ b/templates/barcodes.html @@ -115,8 +115,8 @@ {{ range . }} {{ .Barcode }} - {{ .LoadingBed }} - {{ .CreatedAt }} + {{ .FormatLoadingBed }} + {{ .FormatCreatedAt }} {{ end }} @@ -127,7 +127,5 @@ {{ end }} - + {{ end }} \ No newline at end of file diff --git a/templates/bundle.html b/templates/bundle.html index 718a758..7d55dc3 100644 --- a/templates/bundle.html +++ b/templates/bundle.html @@ -40,11 +40,59 @@
+ {{ if .}}
- {{ end }} + {{ end }} + {{ end }} \ No newline at end of file diff --git a/types/barcode.go b/types/barcode.go index 32bcf46..3104bd3 100644 --- a/types/barcode.go +++ b/types/barcode.go @@ -22,6 +22,12 @@ var MapLoadingBed = map[string]LoadingBed{ "3": ALL, } +var MapFormatBed = map[LoadingBed]string{ + ATA12: "ATA12", + ATA345: "ATA345", + ALL: "TODOS", +} + type Barcode struct { Barcode string `gorm:"primaryKey"` LoadingBed LoadingBed @@ -30,10 +36,14 @@ type Barcode struct { DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedat,omitempty"` } -// func (b *Barcode) FormatCreatedAt() string { -// return b.CreatedAt.Format("02/01/2006 15:04:03") -// } +func (b *Barcode) FormatLoadingBed() string { + return MapFormatBed[b.LoadingBed] +} -// func (b *Barcode) FormatUpdateAt() string { -// return b.UpdatedAt.Format("02/01/2006 15:04:03") -// } +func (b *Barcode) FormatCreatedAt() string { + return b.CreatedAt.Format("02/01/2006 15:04:03") +} + +func (b *Barcode) FormatUpdateAt() string { + return b.UpdatedAt.Format("02/01/2006 15:04:03") +} diff --git a/types/bundle.go b/types/bundle.go index 53045bf..7f74f8e 100644 --- a/types/bundle.go +++ b/types/bundle.go @@ -70,95 +70,6 @@ func (BundleData) TableName() string { return "bundles" } -func (b *BundleData) BeforeSave(tx *gorm.DB) error { - // calculate and assign teorical weight - PesoTeorico(b) - // calculation of the deviation of the actual weight from the theoretical weight - if err := Desvio(b); err != nil { - return err - } - // calculation of the "real" theorical weight means lenght + the lenght of the disc saw (10mm) - PesoTeoricoReal(b) - if err := DesvioReal(b); err != nil { - return err - } - // // calculation of the deviation between theorical and SAP - // if err := PesoDesvioSAP(b); err != nil { - // return err - // } - return nil -} - -func PesoTeorico(b *BundleData) { - // get the lenght of the bundle - bl := b.PaqueteLongitud - // weight per meter - bw := b.PaqueteAlto - // number of pieces - bp := b.PaqueteNroSecciones - // calculate theoretical weight - bt := bl * bw * float64(bp) - // set value - b.Pesoteorico = bt -} - -func Desvio(b *BundleData) error { - // check if theorical is 0 - if b.Pesoteorico == 0 { - return fmt.Errorf("theorical weight is zero") - } - // calculate deviation - dv := float64(b.PaquetePeso) - (b.Pesoteorico) - // % of deviation - pdv := (dv / float64(b.Pesoteorico)) * 100 - // set value - b.Desvio = pdv - // done - return nil -} - -func PesoTeoricoReal(b *BundleData) { - // get the lenght of the bundle - bl := b.PaqueteLongitud + 0.01 // added 10 mm - // weight per meter - bw := b.PaqueteAlto - // number of pieces - bp := b.PaqueteNroSecciones - // calculate theoretical weight - bt := bl * bw * float64(bp) - // set value - b.PesoteoricoReal = bt -} - -func DesvioReal(b *BundleData) error { - // check if theorical is 0 - if b.PesoteoricoReal == 0 { - return fmt.Errorf("theorical weight is zero") - } - // calculate deviation - dv := float64(b.PaquetePeso) - (b.PesoteoricoReal) - // % of deviation - pdv := (dv / float64(b.PesoteoricoReal)) * 100 - // set value - b.DesvioTeoricoReal = pdv - // done - return nil -} - -func PesoDesvioSAP(b *BundleData) error { - // check if theorical is 0 - if b.Pesoteorico == 0 { - return fmt.Errorf("theorical weight is zero") - } - // check if SAP deviation is 0 - if b.PesoNivel1 == 0 { - return fmt.Errorf("SAP deviation not found") - } - // get deviation - dvs := b.PesoNivel1 - // calculates weight - res := b.Pesoteorico + (b.Pesoteorico * (dvs / 100)) - // set weight value - b.PesoNivel1 = res - return nil +func (b *BundleData) FormatDesvio() string { + return fmt.Sprintf("%.3f", b.Desvio) }