order_config.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package admin
  2. import (
  3. "time"
  4. "github.com/qor5/admin/example/models"
  5. "github.com/qor5/admin/presets"
  6. "github.com/qor5/ui/vuetify"
  7. "github.com/qor5/ui/vuetifyx"
  8. "github.com/qor5/web"
  9. h "github.com/theplant/htmlgo"
  10. "gorm.io/gorm"
  11. )
  12. const (
  13. OrderCodeAttr = "ID"
  14. CreatedDateAttr = "CreatedAt"
  15. CheckInDateAttr = "ConfirmedAt"
  16. StatusAttr = "Status"
  17. PaymentMethodAttr = "PaymentMethod"
  18. DeliveryMethodAttr = "DeliveryMethod"
  19. SourceAttr = "Source"
  20. OrderItemsAttr = "OrderItems"
  21. ActionsAttr = "Actions"
  22. )
  23. func configOrder(pb *presets.Builder, db *gorm.DB) {
  24. b := pb.Model(&models.Order{})
  25. // listing
  26. lb := b.Listing(
  27. OrderCodeAttr,
  28. CreatedDateAttr,
  29. CheckInDateAttr,
  30. PaymentMethodAttr,
  31. StatusAttr,
  32. SourceAttr,
  33. )
  34. lb.Field(CreatedDateAttr).ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  35. return h.Td(h.Text(field.Value(obj).(time.Time).Local().Format("2006-01-02 15:04:05")))
  36. }).Label("Date Created")
  37. lb.Field(CheckInDateAttr).Label("Check In Date").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  38. pTime := field.Value(obj)
  39. if pTime != nil {
  40. return h.Td(h.Text((*pTime.(*time.Time)).Local().Format("2006-01-02 15:04:05")))
  41. } else {
  42. return h.Td(h.Text(""))
  43. }
  44. })
  45. lb.Field(StatusAttr).ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  46. status := field.Value(obj).(models.OrderStatus)
  47. if status == "" {
  48. return h.Td()
  49. }
  50. return h.Td(GetColoredStatus(status))
  51. })
  52. lb.FilterDataFunc(func(ctx *web.EventContext) vuetifyx.FilterData {
  53. statusOptions := []*vuetifyx.SelectItem{}
  54. for _, status := range models.OrderStatuses {
  55. statusOptions = append(statusOptions, &vuetifyx.SelectItem{Value: string(status), Text: string(status)})
  56. }
  57. return []*vuetifyx.FilterItem{
  58. {
  59. Key: "created_at",
  60. Label: "Created At",
  61. ItemType: vuetifyx.ItemTypeDate,
  62. SQLCondition: `created_at %s ?`,
  63. },
  64. {
  65. Key: "status",
  66. Label: "Status",
  67. ItemType: vuetifyx.ItemTypeMultipleSelect,
  68. SQLCondition: `status %s ?`,
  69. Options: statusOptions,
  70. },
  71. }
  72. })
  73. lb.Action("Export").ButtonCompFunc(func(ctx *web.EventContext) h.HTMLComponent {
  74. return vuetify.VBtn("Export").
  75. Color("primary").
  76. Depressed(true).
  77. Class("ml-2").
  78. Href(exportOrdersURL)
  79. })
  80. lb.BulkAction("Change status").ComponentFunc(func(selectedIds []string, ctx *web.EventContext) h.HTMLComponent {
  81. vErr := &web.ValidationErrors{}
  82. if ctx.Flash != nil {
  83. vErr = ctx.Flash.(*web.ValidationErrors)
  84. }
  85. return h.Div(
  86. vuetify.VCardText(
  87. vuetify.VAutocomplete().Label("Status").
  88. FieldName("status").
  89. Items(models.OrderStatuses).
  90. Attach(false).ErrorMessages(vErr.GetFieldErrors("status")...),
  91. ),
  92. )
  93. }).UpdateFunc(func(selectedIds []string, ctx *web.EventContext) (err error) {
  94. vErr := &web.ValidationErrors{}
  95. status := ctx.R.FormValue("status")
  96. if status == "" {
  97. vErr.FieldError("status", "Please select status")
  98. ctx.Flash = vErr
  99. return nil
  100. }
  101. if err := db.Model(&models.Order{}).Where("id IN (?)", selectedIds).Update("status", status).Error; err != nil {
  102. return err
  103. }
  104. return
  105. })
  106. // detailing
  107. b.RightDrawerWidth("50%")
  108. orderDetailing := b.Detailing(
  109. // ActionsAttr,
  110. &presets.FieldsSection{
  111. Title: "Basic Information",
  112. Rows: [][]string{
  113. {OrderCodeAttr, CreatedDateAttr},
  114. {StatusAttr, CheckInDateAttr},
  115. {PaymentMethodAttr, DeliveryMethodAttr},
  116. {SourceAttr},
  117. },
  118. },
  119. ).Drawer(true)
  120. orderDetailing.Field(OrderCodeAttr).Label("Order ID")
  121. orderDetailing.Field(CreatedDateAttr).Label("Check In Date").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  122. order := obj.(*models.Order)
  123. v := order.CreatedAt.Local().Format("2006-01-02 15:04:05")
  124. return vuetifyx.VXReadonlyField().
  125. Label(field.Label).
  126. Value(v)
  127. })
  128. orderDetailing.Field(StatusAttr).ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  129. return vuetifyx.VXReadonlyField(GetColoredStatus(obj.(*models.Order).Status)).
  130. Label(field.Label)
  131. })
  132. orderDetailing.Field(CheckInDateAttr).Label("Check In Date").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  133. order := obj.(*models.Order)
  134. var v string
  135. if order.ConfirmedAt != nil {
  136. v = order.ConfirmedAt.Local().Format("2006-01-02 15:04:05")
  137. }
  138. return vuetifyx.VXReadonlyField().
  139. Label(field.Label).
  140. Value(v)
  141. })
  142. orderDetailing.Field(PaymentMethodAttr).Label("Payment Method").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  143. order := obj.(*models.Order)
  144. return vuetifyx.VXReadonlyField().
  145. Label(field.Label).
  146. Value(order.PaymentMethod)
  147. })
  148. orderDetailing.Field(DeliveryMethodAttr).Label("Fulfilment").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  149. order := obj.(*models.Order)
  150. return vuetifyx.VXReadonlyField().
  151. Label(field.Label).
  152. Value(order.DeliveryMethod)
  153. })
  154. }
  155. func GetColoredStatus(status models.OrderStatus) h.HTMLComponent {
  156. color, ok := models.OrderStatusColorMap[status]
  157. if !ok {
  158. return h.Text(string(status))
  159. }
  160. return vuetify.VChip(h.Text(string(status))).Color(color).Dark(true)
  161. }