module_shop_act_order.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.Config).Shop.Orders.Enabled <= 0 {
  15. wrap.Write(`{"error": true, "variable": "ShopOrderErrorDisabled"}`)
  16. return
  17. }
  18. if wrap.ShopBasket.ProductsCount(&basket.SBParam{
  19. R: wrap.R,
  20. DB: wrap.DB,
  21. Host: wrap.CurrHost,
  22. Config: wrap.Config,
  23. SessionId: wrap.GetSessionId(),
  24. }) <= 0 {
  25. wrap.Write(`{"error": true, "variable": "ShopOrderErrorBasketEmpty"}`)
  26. return
  27. }
  28. pf_client_last_name := wrap.R.FormValue("client_last_name")
  29. pf_client_first_name := wrap.R.FormValue("client_first_name")
  30. pf_client_middle_name := wrap.R.FormValue("client_middle_name")
  31. pf_client_phone := wrap.R.FormValue("client_phone")
  32. pf_client_email := wrap.R.FormValue("client_email")
  33. pf_client_delivery_comment := wrap.R.FormValue("client_delivery_comment")
  34. pf_client_order_comment := wrap.R.FormValue("client_order_comment")
  35. if (*wrap.Config).Shop.Orders.RequiredFields.LastName != 0 {
  36. if strings.TrimSpace(pf_client_last_name) == "" {
  37. wrap.Write(`{"error": true, "field": "client_last_name", "variable": "ShopOrderEmptyLastName"}`)
  38. return
  39. }
  40. }
  41. if (*wrap.Config).Shop.Orders.RequiredFields.FirstName != 0 {
  42. if strings.TrimSpace(pf_client_first_name) == "" {
  43. wrap.Write(`{"error": true, "field": "client_first_name", "variable": "ShopOrderEmptyFirstName"}`)
  44. return
  45. }
  46. }
  47. if (*wrap.Config).Shop.Orders.RequiredFields.MiddleName != 0 {
  48. if strings.TrimSpace(pf_client_middle_name) == "" {
  49. wrap.Write(`{"error": true, "field": "client_middle_name", "variable": "ShopOrderEmptyMiddleName"}`)
  50. return
  51. }
  52. }
  53. if (*wrap.Config).Shop.Orders.RequiredFields.MobilePhone != 0 {
  54. if strings.TrimSpace(pf_client_phone) == "" {
  55. wrap.Write(`{"error": true, "field": "client_phone", "variable": "ShopOrderEmptyMobilePhone"}`)
  56. return
  57. }
  58. if !utils.IsValidMobile(pf_client_phone) {
  59. wrap.Write(`{"error": true, "field": "client_phone", "variable": "ShopOrderNotCorrectMobilePhone"}`)
  60. return
  61. }
  62. }
  63. if (*wrap.Config).Shop.Orders.RequiredFields.EmailAddress != 0 {
  64. if strings.TrimSpace(pf_client_email) == "" {
  65. wrap.Write(`{"error": true, "field": "client_email", "variable": "ShopOrderEmptyEmailAddress"}`)
  66. return
  67. }
  68. if !utils.IsValidEmail(pf_client_email) {
  69. wrap.Write(`{"error": true, "field": "client_email", "variable": "ShopOrderNotCorrectEmailAddress"}`)
  70. return
  71. }
  72. }
  73. if (*wrap.Config).Shop.Orders.RequiredFields.Delivery != 0 {
  74. if strings.TrimSpace(pf_client_delivery_comment) == "" {
  75. wrap.Write(`{"error": true, "field": "client_delivery_comment", "variable": "ShopOrderEmptyDelivery"}`)
  76. return
  77. }
  78. }
  79. if (*wrap.Config).Shop.Orders.RequiredFields.Comment != 0 {
  80. if strings.TrimSpace(pf_client_order_comment) == "" {
  81. wrap.Write(`{"error": true, "field": "client_order_comment", "variable": "ShopOrderEmptyComment"}`)
  82. return
  83. }
  84. }
  85. bdata := wrap.ShopBasket.GetAll(&basket.SBParam{
  86. R: wrap.R,
  87. DB: wrap.DB,
  88. Host: wrap.CurrHost,
  89. Config: wrap.Config,
  90. SessionId: wrap.GetSessionId(),
  91. })
  92. var lastID int64 = 0
  93. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  94. // Insert row
  95. res, err := tx.Exec(
  96. `INSERT INTO shop_orders SET
  97. create_datetime = ?,
  98. update_datetime = ?,
  99. currency_id = ?,
  100. currency_name = ?,
  101. currency_coefficient = ?,
  102. currency_code = ?,
  103. currency_symbol = ?,
  104. client_last_name = ?,
  105. client_first_name = ?,
  106. client_middle_name = ?,
  107. client_phone = ?,
  108. client_email = ?,
  109. client_delivery_comment = ?,
  110. client_order_comment = ?,
  111. status = ?
  112. ;`,
  113. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  114. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  115. bdata.Currency.Id,
  116. bdata.Currency.Name,
  117. bdata.Currency.Coefficient,
  118. bdata.Currency.Code,
  119. bdata.Currency.Symbol,
  120. pf_client_last_name,
  121. pf_client_first_name,
  122. pf_client_middle_name,
  123. pf_client_phone,
  124. pf_client_email,
  125. pf_client_delivery_comment,
  126. pf_client_order_comment,
  127. 0,
  128. )
  129. if err != nil {
  130. return err
  131. }
  132. // Get inserted order id
  133. lastID, err = res.LastInsertId()
  134. if err != nil {
  135. return err
  136. }
  137. // Insert order products
  138. for _, product := range *(*bdata).Products {
  139. if _, err = tx.Exec(
  140. `INSERT INTO shop_order_products (id, order_id, product_id, price, quantity) VALUES (NULL, ?, ?, ?, ?);`,
  141. lastID, product.A_product_id, product.A_price, product.A_quantity,
  142. ); err != nil {
  143. return err
  144. }
  145. }
  146. // Send notify email
  147. if (*wrap.Config).Shop.Orders.NotifyEmail != "" {
  148. if err := wrap.SendEmail(
  149. (*wrap.Config).Shop.Orders.NotifyEmail,
  150. "❤️ New Order ("+wrap.Host+":"+wrap.Port+")",
  151. "You have new order in shop on host: <a href=\"http://"+wrap.Host+":"+wrap.Port+"/\">http://"+wrap.Host+":"+wrap.Port+"/</a>",
  152. ); err != nil {
  153. return err
  154. }
  155. }
  156. return nil
  157. }); err != nil {
  158. wrap.Write(`{"error": true, "variable": "ShopOrderErrorSomethingWrong"}`)
  159. return
  160. }
  161. // Clear user basket
  162. wrap.ShopBasket.ClearBasket(&basket.SBParam{
  163. R: wrap.R,
  164. DB: wrap.DB,
  165. Host: wrap.CurrHost,
  166. Config: wrap.Config,
  167. SessionId: wrap.GetSessionId(),
  168. })
  169. wrap.Write(`{"error": false, "field": "", "variable": "ShopOrderSuccess"}`)
  170. return
  171. })
  172. }