func (dIn *ClientDeviceCheckIn) Validate(db *gorm.DB) { if dIn.FromReportItemID == 0 { db.AddError(validations.NewError(dIn, "FromReportItemID", "送检的设备不能为空")) } if dIn.ToWarehouseID == 0 { db.AddError(validations.NewError(dIn, "ToWhomID", "送检设备还回仓库不能为空")) } if dIn.Quantity <= 0 { db.AddError(validations.NewError(dIn, "Quantity", "还回送检设备的数量要大于0")) } if dIn.ByWhomID == 0 { db.AddError(validations.NewError(dIn, "ByWhomID", "请选择操作员")) } from, to, _, cdIn, _, err := fromToDevice(dIn.FromReportItemID, dIn.ToWarehouseID, "Warehouse") if err != nil { db.AddError(validations.NewError(dIn, "FromReportItemID", err.Error())) return } dIn.DeviceName = cdIn.DeviceName dIn.ClientName = cdIn.ClientName dIn.ToWarehouseName = to.HolderName() dIn.FromDeviceCheckCompanyName = from.HolderName() byWhom, _ := holderByIDType(dIn.ByWhomID, "Employee") dIn.ByWhomName = byWhom.HolderName() }
func (cdOut *ClientDeviceOut) Validate(db *gorm.DB) { if cdOut.ReportItemID == 0 { db.AddError(validations.NewError(cdOut, "ReportItemID", "选择收入过的客户设备")) } if cdOut.ByWhomID == 0 { db.AddError(validations.NewError(cdOut, "ByWhomID", "请选择操作员")) } _, _, _, cdIn, ri, err := fromToDevice(cdOut.ReportItemID, 0, "Employee") if err != nil { db.AddError(validations.NewError(cdOut, "ReportItemID", err.Error())) return } if int(cdIn.Quantity) != ri.Count { db.AddError(validations.NewError(cdOut, "ReportItemID", "该客户设备有送检中设备,不能还回。")) return } cdOut.DeviceName = cdIn.DeviceName cdOut.ClientName = cdIn.ClientName cdOut.Quantity = cdIn.Quantity cdOut.WarehouseName = cdIn.WarehouseName }
func (dOut *DeviceOut) Validate(db *gorm.DB) { if dOut.FromReportItemID == 0 { db.AddError(validations.NewError(dOut, "FromReportItemID", "带出设备不能为空")) } if dOut.ToWhomID == 0 { db.AddError(validations.NewError(dOut, "ToWhomID", "设备带出人不能为空")) } if dOut.Quantity <= 0 { db.AddError(validations.NewError(dOut, "Quantity", "带出设备的数量要大于0")) } if dOut.ByWhomID == 0 { db.AddError(validations.NewError(dOut, "ByWhomID", "请选择操作员")) } from, to, d, _, _, err := fromToDevice(dOut.FromReportItemID, dOut.ToWhomID, "Employee") if err != nil { db.AddError(validations.NewError(dOut, "FromReportItemID", err.Error())) return } dOut.DeviceName = d.Name dOut.ToWhomName = to.HolderName() dOut.FromWarehouseName = from.HolderName() byWhom, _ := holderByIDType(dOut.ByWhomID, "Employee") dOut.ByWhomName = byWhom.HolderName() }
func (cOut *ConsumableOut) Validate(db *gorm.DB) { if cOut.ReportItemID == 0 { db.AddError(validations.NewError(cOut, "ReportItemID", "消耗品不能为空")) } if cOut.ToWhomID == 0 { db.AddError(validations.NewError(cOut, "ToWhomID", "消耗品使用人不能为空")) } if cOut.Quantity <= 0 { db.AddError(validations.NewError(cOut, "Quantity", "带出消耗品的数量要大于0")) } if cOut.ByWhomID == 0 { db.AddError(validations.NewError(cOut, "ByWhomID", "请选择操作员")) } from, to, d, _, _, err := fromToDevice(cOut.ReportItemID, cOut.ToWhomID, "Employee") if err != nil { db.AddError(validations.NewError(cOut, "FromReportItemID", err.Error())) return } cOut.DeviceName = d.Name cOut.ToWhomName = to.HolderName() cOut.WarehouseName = from.HolderName() byWhom, _ := holderByIDType(cOut.ByWhomID, "Employee") cOut.ByWhomName = byWhom.HolderName() }
func (dOut *ClientDeviceCheckOut) Validate(db *gorm.DB) { if dOut.FromReportItemID == 0 { db.AddError(validations.NewError(dOut, "FromReportItemID", "送检设备不能为空")) } if dOut.ToDeviceCheckCompanyID == 0 { db.AddError(validations.NewError(dOut, "ToDeviceCheckCompanyID", "送检公司不能为空")) } if dOut.Quantity <= 0 { db.AddError(validations.NewError(dOut, "Quantity", "送检设备的数量要大于0")) } if dOut.ByWhomID == 0 { db.AddError(validations.NewError(dOut, "ByWhomID", "请选择操作员")) } from, to, _, cdIn, _, err := fromToDevice(dOut.FromReportItemID, dOut.ToDeviceCheckCompanyID, "DeviceCheckCompany") if err != nil { db.AddError(validations.NewError(dOut, "FromReportItemID", err.Error())) return } dOut.DeviceName = cdIn.DeviceName dOut.ClientName = cdIn.ClientName dOut.ToDeviceCheckCompanyName = to.HolderName() dOut.FromWarehouseName = from.HolderName() byWhom, _ := holderByIDType(dOut.ByWhomID, "Employee") dOut.ByWhomName = byWhom.HolderName() }
func (color Color) Validate(db *gorm.DB) { if strings.TrimSpace(color.Name) == "" { db.AddError(validations.NewError(color, "Name", "Name can not be empty")) } if strings.TrimSpace(color.Code) == "" { db.AddError(validations.NewError(color, "Code", "Code can not be empty")) } }
func (size Size) Validate(db *gorm.DB) { if strings.TrimSpace(size.Name) == "" { db.AddError(validations.NewError(size, "Name", "Name can not be empty")) } if strings.TrimSpace(size.Code) == "" { db.AddError(validations.NewError(size, "Code", "Code can not be empty")) } }
func (product Product) Validate(db *gorm.DB) { if strings.TrimSpace(product.Name) == "" { db.AddError(validations.NewError(product, "Name", "Name can not be empty")) } if strings.TrimSpace(product.Code) == "" { db.AddError(validations.NewError(product, "Code", "Code can not be empty")) } }
func (device *Device) Validate(db *gorm.DB) { var deviceInDb Device db.Where("code = ?", device.Code).First(&deviceInDb) if deviceInDb.ID != 0 && deviceInDb.ID != device.ID { db.AddError(validations.NewError(device, "Code", "代码已经存在了,不能重复")) } if device.Name == "" { db.AddError(validations.NewError(device, "Name", "设备名称不能为空")) } catName := "" for _, dc := range DeviceCategories { if fmt.Sprintf("%d", device.CategoryID) == dc[0] { catName = dc[1] break } } // panic(fmt.Sprintf("%d", d.CategoryID) + catName) device.CategoryName = catName }
func (cdIn *ClientDeviceIn) Validate(db *gorm.DB) { if len(strings.TrimSpace(cdIn.DeviceName)) == 0 { db.AddError(validations.NewError(cdIn, "DeviceName", "收入设备名不能为空")) } if len(strings.TrimSpace(cdIn.ClientName)) == 0 { db.AddError(validations.NewError(cdIn, "ClientName", "收入客户名不能为空")) } if cdIn.WarehouseID == 0 { db.AddError(validations.NewError(cdIn, "WharehouseID", "收入到的仓库不能为空")) } if cdIn.Quantity <= 0 { db.AddError(validations.NewError(cdIn, "Quantity", "收入设备的数量要大于0")) } if cdIn.ByWhomID == 0 { db.AddError(validations.NewError(cdIn, "ByWhomID", "请选择操作员")) } wh, _ := holderByIDType(cdIn.WarehouseID, "Warehouse") cdIn.WarehouseName = wh.HolderName() byWhom, _ := holderByIDType(cdIn.ByWhomID, "Employee") cdIn.ByWhomName = byWhom.HolderName() }
func (cIn *ConsumableIn) Validate(db *gorm.DB) { if cIn.ReportItemID == 0 { db.AddError(validations.NewError(cIn, "ReportItemID", "购买的消耗品不能为空")) } if cIn.Quantity <= 0 { db.AddError(validations.NewError(cIn, "Quantity", "购买的数量要大于0")) } if cIn.ByWhomID == 0 { db.AddError(validations.NewError(cIn, "ByWhomID", "请选择操作员")) } from, _, d, _, _, err := fromToDevice(cIn.ReportItemID, 0, "Employee") if err != nil { db.AddError(validations.NewError(cIn, "ReportItemID", err.Error())) return } cIn.DeviceName = d.Name cIn.WarehouseName = from.HolderName() byWhom, _ := holderByIDType(cIn.ByWhomID, "Employee") cIn.ByWhomName = byWhom.HolderName() }
func init() { Admin = admin.New(&qor.Config{DB: db.Publish.DraftDB()}) Admin.SetAuth(Auth{}) assetManager := Admin.AddResource(&admin.AssetManager{}, &admin.Config{Invisible: true}) product := Admin.AddResource(&models.Product{}, &admin.Config{Menu: []string{"Product Management"}}) product.Meta(&admin.Meta{Name: "MadeCountry", Type: "select_one", Collection: Countries}) product.Meta(&admin.Meta{Name: "Description", Type: "rich_editor", Resource: assetManager}) for _, country := range Countries { var country = country product.Scope(&admin.Scope{Name: country, Group: "Made Country", Handle: func(db *gorm.DB, ctx *qor.Context) *gorm.DB { return db.Where("made_country = ?", country) }}) } product.IndexAttrs("-ColorVariations") Admin.AddResource(&models.Color{}, &admin.Config{Menu: []string{"Product Management"}}) Admin.AddResource(&models.Size{}, &admin.Config{Menu: []string{"Product Management"}}) Admin.AddResource(&models.Category{}, &admin.Config{Menu: []string{"Product Management"}}) Admin.AddResource(&models.Order{}, &admin.Config{Menu: []string{"Order Management"}}) store := Admin.AddResource(&models.Store{}, &admin.Config{Menu: []string{"Store Management"}}) store.IndexAttrs("-Latitude", "-Longitude") store.AddValidator(func(record interface{}, metaValues *resource.MetaValues, context *qor.Context) error { if meta := metaValues.Get("Name"); meta != nil { if name := utils.ToString(meta.Value); strings.TrimSpace(name) == "" { return validations.NewError(record, "Name", "Name can't be blank") } } return nil }) Admin.AddResource(config.Config.I18n, &admin.Config{Menu: []string{"Site Management"}}) user := Admin.AddResource(&models.User{}) user.IndexAttrs("ID", "Email", "Name", "Gender", "Role") Admin.AddResource(db.Publish) }
func (language *Language) Validate(db *gorm.DB) error { if language.Code == "invalid" { return validations.NewError(language, "Code", "invalid language") } return nil }
func (address *Address) Validate(db *gorm.DB) { if address.Address == "invalid" { db.AddError(validations.NewError(address, "Address", "invalid address")) } }
func (card *CreditCard) Validate(db *gorm.DB) { if !regexp.MustCompile("^(\\d){13,16}$").MatchString(card.Number) { db.AddError(validations.NewError(card, "Number", "invalid card number")) } }
func (user *User) Validate(db *gorm.DB) { if user.Name == "invalid" { db.AddError(validations.NewError(user, "Name", "invalid user name")) } }
func init() { Admin = admin.New(&qor.Config{DB: db.Publish.DraftDB()}) Admin.SetSiteName("Qor DEMO") Admin.SetAuth(Auth{}) // Add Dashboard Admin.AddMenu(&admin.Menu{Name: "Dashboard", Link: "/admin"}) // Add Asset Manager, for rich editor assetManager := Admin.AddResource(&admin.AssetManager{}, &admin.Config{Invisible: true}) // Add Product product := Admin.AddResource(&models.Product{}, &admin.Config{Menu: []string{"Product Management"}}) product.Meta(&admin.Meta{Name: "MadeCountry", Type: "select_one", Collection: Countries}) product.Meta(&admin.Meta{Name: "Description", Type: "rich_editor", Resource: assetManager}) sizeVariation := Admin.NewResource(&models.SizeVariation{}, &admin.Config{Invisible: true}) sizeVariation.NewAttrs("-ColorVariation") sizeVariation.EditAttrs("-ColorVariation") colorVariation := Admin.NewResource(&models.ColorVariation{}, &admin.Config{Invisible: true}) colorVariation.Meta(&admin.Meta{Name: "SizeVariations", Resource: sizeVariation}) colorVariation.NewAttrs("-Product") colorVariation.EditAttrs("-Product") product.Meta(&admin.Meta{Name: "ColorVariations", Resource: colorVariation}) product.SearchAttrs("Name", "Code", "Category.Name", "Brand.Name") for _, country := range Countries { var country = country product.Scope(&admin.Scope{Name: country, Group: "Made Country", Handle: func(db *gorm.DB, ctx *qor.Context) *gorm.DB { return db.Where("made_country = ?", country) }}) } product.IndexAttrs("-ColorVariations") Admin.AddResource(&models.Color{}, &admin.Config{Menu: []string{"Product Management"}}) Admin.AddResource(&models.Size{}, &admin.Config{Menu: []string{"Product Management"}}) Admin.AddResource(&models.Category{}, &admin.Config{Menu: []string{"Product Management"}}) Admin.AddResource(&models.Collection{}, &admin.Config{Menu: []string{"Product Management"}}) // Add Order orderItem := Admin.NewResource(&models.OrderItem{}) orderItem.Meta(&admin.Meta{Name: "SizeVariation", Type: "select_one", Collection: sizeVariationCollection}) order := Admin.AddResource(&models.Order{}, &admin.Config{Menu: []string{"Order Management"}}) order.Meta(&admin.Meta{Name: "ShippingAddress", Type: "single_edit"}) order.Meta(&admin.Meta{Name: "BillingAddress", Type: "single_edit"}) order.Meta(&admin.Meta{Name: "OrderItems", Resource: orderItem}) activity.RegisterActivityMeta(order) // define scopes for Order for _, state := range []string{"checkout", "cancelled", "paid", "paid_cancelled", "processing", "shipped", "returned"} { var state = state order.Scope(&admin.Scope{ Name: state, Label: strings.Title(strings.Replace(state, "_", " ", -1)), Group: "Order Status", Handle: func(db *gorm.DB, context *qor.Context) *gorm.DB { return db.Where(models.Order{Transition: transition.Transition{State: state}}) }, }) } order.IndexAttrs("-DiscountValue", "-OrderItems", "-AbandonedReason") order.NewAttrs("-DiscountValue", "-AbandonedReason") order.EditAttrs("-DiscountValue", "-AbandonedReason") order.ShowAttrs("-DiscountValue", "-AbandonedReason") order.SearchAttrs("User.Name", "User.Email", "ShippingAddress.ContactName", "ShippingAddress.Address1", "ShippingAddress.Address2") // Define another resource for same model abandonedOrder := Admin.AddResource(&models.Order{}, &admin.Config{Name: "Abandoned Order", Menu: []string{"Order Management"}}) abandonedOrder.Meta(&admin.Meta{Name: "ShippingAddress", Type: "single_edit"}) abandonedOrder.Meta(&admin.Meta{Name: "BillingAddress", Type: "single_edit"}) // Define default scope for abandoned orders abandonedOrder.Scope(&admin.Scope{ Default: true, Handle: func(db *gorm.DB, context *qor.Context) *gorm.DB { return db.Where("abandoned_reason IS NOT NULL AND abandoned_reason <> ?", "") }, }) // Define scopes for abandoned orders for _, amount := range []int{5000, 10000, 20000} { var amount = amount abandonedOrder.Scope(&admin.Scope{ Name: fmt.Sprint(amount), Group: "Amount Greater Than", Handle: func(db *gorm.DB, context *qor.Context) *gorm.DB { return db.Where("payment_amount > ?", amount) }, }) } abandonedOrder.IndexAttrs("-ShippingAddress", "-BillingAddress", "-DiscountValue", "-OrderItems") abandonedOrder.NewAttrs("-DiscountValue") abandonedOrder.EditAttrs("-DiscountValue") abandonedOrder.ShowAttrs("-DiscountValue") // Add Store store := Admin.AddResource(&models.Store{}, &admin.Config{Menu: []string{"Store Management"}}) store.AddValidator(func(record interface{}, metaValues *resource.MetaValues, context *qor.Context) error { if meta := metaValues.Get("Name"); meta != nil { if name := utils.ToString(meta.Value); strings.TrimSpace(name) == "" { return validations.NewError(record, "Name", "Name can't be blank") } } return nil }) // Add Translations Admin.AddResource(config.Config.I18n, &admin.Config{Menu: []string{"Site Management"}}) // Add Setting Admin.AddResource(&models.Setting{}, &admin.Config{Singleton: true}) // Add User user := Admin.AddResource(&models.User{}) user.IndexAttrs("ID", "Email", "Name", "Gender", "Role") // Add Publish Admin.AddResource(db.Publish, &admin.Config{Singleton: true}) // Add Seo Admin.AddResource(&models.Seo{}, &admin.Config{Name: "Meta Data", Singleton: true}) // Add Search Center Admin.AddSearchResource(order, user, product) initFuncMap() initRouter() }
func (category Category) Validate(db *gorm.DB) { if strings.TrimSpace(category.Name) == "" { db.AddError(validations.NewError(category, "Name", "Name can not be empty")) } }