module_shop_order.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package modules
  2. import (
  3. "strings"
  4. "golang-fave/engine/basket"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. func (this *Modules) RegisterAction_ShopOrder() *Action {
  9. return this.newAction(AInfo{
  10. WantDB: true,
  11. Mount: "shop-order",
  12. WantAdmin: false,
  13. }, func(wrap *wrapper.Wrapper) {
  14. if wrap.ShopBasket.ProductsCount(&basket.SBParam{
  15. R: wrap.R,
  16. DB: wrap.DB,
  17. Host: wrap.CurrHost,
  18. Config: wrap.Config,
  19. SessionId: wrap.GetSessionId(),
  20. }) <= 0 {
  21. wrap.Write(`{"error": true, "variable": "ShopOrderErrorBasketEmpty"}`)
  22. return
  23. }
  24. pf_client_last_name := wrap.R.FormValue("client_last_name")
  25. pf_client_first_name := wrap.R.FormValue("client_first_name")
  26. pf_client_second_name := wrap.R.FormValue("client_second_name")
  27. pf_client_phone := wrap.R.FormValue("client_phone")
  28. pf_client_email := wrap.R.FormValue("client_email")
  29. pf_client_delivery_comment := wrap.R.FormValue("client_delivery_comment")
  30. pf_client_order_comment := wrap.R.FormValue("client_order_comment")
  31. if (*wrap.Config).Shop.Orders.RequiredFields.LastName != 0 {
  32. if strings.TrimSpace(pf_client_last_name) == "" {
  33. wrap.Write(`{"error": true, "field": "client_last_name", "variable": "ShopOrderEmptyLastName"}`)
  34. return
  35. }
  36. }
  37. if (*wrap.Config).Shop.Orders.RequiredFields.FirstName != 0 {
  38. if strings.TrimSpace(pf_client_first_name) == "" {
  39. wrap.Write(`{"error": true, "field": "client_first_name", "variable": "ShopOrderEmptyFirstName"}`)
  40. return
  41. }
  42. }
  43. if (*wrap.Config).Shop.Orders.RequiredFields.SecondName != 0 {
  44. if strings.TrimSpace(pf_client_second_name) == "" {
  45. wrap.Write(`{"error": true, "field": "client_second_name", "variable": "ShopOrderEmptySecondName"}`)
  46. return
  47. }
  48. }
  49. if (*wrap.Config).Shop.Orders.RequiredFields.MobilePhone != 0 {
  50. if strings.TrimSpace(pf_client_phone) == "" || !utils.IsValidMobile(pf_client_phone) {
  51. wrap.Write(`{"error": true, "field": "client_phone", "variable": "ShopOrderEmptyMobilePhone"}`)
  52. return
  53. }
  54. }
  55. if (*wrap.Config).Shop.Orders.RequiredFields.EmailAddress != 0 {
  56. if strings.TrimSpace(pf_client_email) == "" || !utils.IsValidEmail(pf_client_email) {
  57. wrap.Write(`{"error": true, "field": "client_email", "variable": "ShopOrderEmptyEmailAddress"}`)
  58. return
  59. }
  60. }
  61. if (*wrap.Config).Shop.Orders.RequiredFields.Delivery != 0 {
  62. if strings.TrimSpace(pf_client_delivery_comment) == "" {
  63. wrap.Write(`{"error": true, "field": "client_delivery_comment", "variable": "ShopOrderEmptyDelivery"}`)
  64. return
  65. }
  66. }
  67. if (*wrap.Config).Shop.Orders.RequiredFields.Comment != 0 {
  68. if strings.TrimSpace(pf_client_order_comment) == "" {
  69. wrap.Write(`{"error": true, "field": "client_order_comment", "variable": "ShopOrderEmptyComment"}`)
  70. return
  71. }
  72. }
  73. // Clear user basket
  74. wrap.ShopBasket.ClearBasket(&basket.SBParam{
  75. R: wrap.R,
  76. DB: wrap.DB,
  77. Host: wrap.CurrHost,
  78. Config: wrap.Config,
  79. SessionId: wrap.GetSessionId(),
  80. })
  81. wrap.Write(`{"error": false, "field": "", "variable": "ShopOrderSuccess"}`)
  82. return
  83. // if !utils.IsNumeric(pf_id) {
  84. // wrap.MsgError(`Inner system error`)
  85. // return
  86. // }
  87. // if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  88. // if _, err := tx.Exec(`
  89. // UPDATE shop_products SET
  90. // parent_id = NULL,
  91. // active = 0
  92. // WHERE
  93. // id = ?
  94. // ;`,
  95. // utils.StrToInt(pf_id),
  96. // ); err != nil {
  97. // return err
  98. // }
  99. // return nil
  100. // }); err != nil {
  101. // wrap.MsgError(err.Error())
  102. // return
  103. // }
  104. // wrap.RecreateProductXmlFile()
  105. // wrap.ResetCacheBlocks()
  106. // // Reload current page
  107. // wrap.Write(`window.location.reload(false);`)
  108. })
  109. }