module_shop_order.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. bdata := wrap.ShopBasket.GetAll(&basket.SBParam{
  74. R: wrap.R,
  75. DB: wrap.DB,
  76. Host: wrap.CurrHost,
  77. Config: wrap.Config,
  78. SessionId: wrap.GetSessionId(),
  79. })
  80. var lastID int64 = 0
  81. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  82. // Insert row
  83. res, err := tx.Exec(
  84. `INSERT INTO shop_orders SET
  85. create_datetime = ?,
  86. update_datetime = ?,
  87. currency_id = ?,
  88. currency_name = ?,
  89. currency_coefficient = ?,
  90. currency_code = ?,
  91. currency_symbol = ?,
  92. client_last_name = ?,
  93. client_first_name = ?,
  94. client_second_name = ?,
  95. client_phone = ?,
  96. client_email = ?,
  97. client_delivery_comment = ?,
  98. client_order_comment = ?,
  99. status = ?
  100. ;`,
  101. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  102. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  103. bdata.Currency.Id,
  104. bdata.Currency.Name,
  105. bdata.Currency.Coefficient,
  106. bdata.Currency.Code,
  107. bdata.Currency.Symbol,
  108. pf_client_last_name,
  109. pf_client_first_name,
  110. pf_client_second_name,
  111. pf_client_phone,
  112. pf_client_email,
  113. pf_client_delivery_comment,
  114. pf_client_order_comment,
  115. 0,
  116. )
  117. if err != nil {
  118. return err
  119. }
  120. // Get inserted order id
  121. lastID, err = res.LastInsertId()
  122. if err != nil {
  123. return err
  124. }
  125. // Insert order products
  126. for _, product := range *(*bdata).Products {
  127. if _, err = tx.Exec(
  128. `INSERT INTO shop_order_products (id, order_id, product_id, price, quantity) VALUES (NULL, ?, ?, ?, ?);`,
  129. lastID, product.A_product_id, product.A_price, product.A_quantity,
  130. ); err != nil {
  131. return err
  132. }
  133. }
  134. return nil
  135. }); err != nil {
  136. wrap.Write(`{"error": true, "variable": "ShopOrderErrorSomethingWrong"}`)
  137. return
  138. }
  139. // Clear user basket
  140. wrap.ShopBasket.ClearBasket(&basket.SBParam{
  141. R: wrap.R,
  142. DB: wrap.DB,
  143. Host: wrap.CurrHost,
  144. Config: wrap.Config,
  145. SessionId: wrap.GetSessionId(),
  146. })
  147. wrap.Write(`{"error": false, "field": "", "variable": "ShopOrderSuccess"}`)
  148. return
  149. })
  150. }