module_shop_act_order.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/basket"
  5. "golang-fave/engine/consts"
  6. "golang-fave/engine/utils"
  7. "golang-fave/engine/wrapper"
  8. )
  9. func (this *Modules) RegisterAction_ShopOrder() *Action {
  10. return this.newAction(AInfo{
  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 := utils.Trim(wrap.R.FormValue("client_last_name"))
  29. pf_client_first_name := utils.Trim(wrap.R.FormValue("client_first_name"))
  30. pf_client_middle_name := utils.Trim(wrap.R.FormValue("client_middle_name"))
  31. pf_client_phone := utils.Trim(wrap.R.FormValue("client_phone"))
  32. pf_client_email := utils.Trim(wrap.R.FormValue("client_email"))
  33. pf_client_delivery_comment := utils.Trim(wrap.R.FormValue("client_delivery_comment"))
  34. pf_client_order_comment := utils.Trim(wrap.R.FormValue("client_order_comment"))
  35. if (*wrap.Config).Shop.Orders.RequiredFields.LastName != 0 {
  36. if 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 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 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 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 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 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 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(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  94. // Insert row
  95. res, err := tx.Exec(
  96. ctx,
  97. `INSERT INTO fave_shop_orders SET
  98. create_datetime = ?,
  99. update_datetime = ?,
  100. currency_id = ?,
  101. currency_name = ?,
  102. currency_coefficient = ?,
  103. currency_code = ?,
  104. currency_symbol = ?,
  105. client_last_name = ?,
  106. client_first_name = ?,
  107. client_middle_name = ?,
  108. client_phone = ?,
  109. client_email = ?,
  110. client_delivery_comment = ?,
  111. client_order_comment = ?,
  112. status = ?
  113. ;`,
  114. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  115. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  116. bdata.Currency.Id,
  117. bdata.Currency.Name,
  118. bdata.Currency.Coefficient,
  119. bdata.Currency.Code,
  120. bdata.Currency.Symbol,
  121. pf_client_last_name,
  122. pf_client_first_name,
  123. pf_client_middle_name,
  124. pf_client_phone,
  125. pf_client_email,
  126. pf_client_delivery_comment,
  127. pf_client_order_comment,
  128. 0,
  129. )
  130. if err != nil {
  131. return err
  132. }
  133. // Get inserted order id
  134. lastID, err = res.LastInsertId()
  135. if err != nil {
  136. return err
  137. }
  138. // Insert order products
  139. for _, product := range *(*bdata).Products {
  140. if _, err = tx.Exec(
  141. ctx,
  142. `INSERT INTO fave_shop_order_products (id, order_id, product_id, price, quantity) VALUES (NULL, ?, ?, ?, ?);`,
  143. lastID, product.A_product_id, product.A_price, product.A_quantity,
  144. ); err != nil {
  145. return err
  146. }
  147. }
  148. // Send notify email to owner
  149. if (*wrap.Config).Shop.Orders.NotifyEmail != "" {
  150. if err := wrap.SendEmailTemplated(
  151. (*wrap.Config).Shop.Orders.NotifyEmail,
  152. (*wrap.Config).Shop.Orders.NewOrderEmailThemeCp+" #"+utils.Int64ToStr(lastID)+" ("+wrap.Host+":"+wrap.Port+")",
  153. "email-new-order-admin",
  154. consts.TmplEmailOrder{
  155. Basket: bdata,
  156. Client: consts.TmplOrderClient{
  157. LastName: pf_client_last_name,
  158. FirstName: pf_client_first_name,
  159. MiddleName: pf_client_middle_name,
  160. Phone: pf_client_phone,
  161. Email: pf_client_email,
  162. DeliveryComment: pf_client_delivery_comment,
  163. OrderComment: pf_client_order_comment,
  164. },
  165. Else: consts.TmplOrderElse{
  166. OrderId: lastID,
  167. Subject: (*wrap.Config).Shop.Orders.NewOrderEmailThemeCp + " #" + utils.Int64ToStr(lastID) + " (" + wrap.Host + ":" + wrap.Port + ")",
  168. CpOrderLink: "http://" + wrap.Host + ":" + wrap.Port + "/cp/shop/orders-modify/" + utils.Int64ToStr(lastID) + "/",
  169. },
  170. },
  171. ); err != nil {
  172. return err
  173. }
  174. }
  175. // Send notify email to client
  176. if pf_client_email != "" {
  177. if err := wrap.SendEmailTemplated(
  178. pf_client_email,
  179. (*wrap.Config).Shop.Orders.NewOrderEmailThemeUser+" #"+utils.Int64ToStr(lastID),
  180. "email-new-order-user",
  181. consts.TmplEmailOrder{
  182. Basket: bdata,
  183. Client: consts.TmplOrderClient{
  184. LastName: pf_client_last_name,
  185. FirstName: pf_client_first_name,
  186. MiddleName: pf_client_middle_name,
  187. Phone: pf_client_phone,
  188. Email: pf_client_email,
  189. DeliveryComment: pf_client_delivery_comment,
  190. OrderComment: pf_client_order_comment,
  191. },
  192. Else: consts.TmplOrderElse{
  193. OrderId: lastID,
  194. Subject: (*wrap.Config).Shop.Orders.NewOrderEmailThemeUser + " #" + utils.Int64ToStr(lastID),
  195. CpOrderLink: "http://" + wrap.Host + ":" + wrap.Port + "/cp/shop/orders-modify/" + utils.Int64ToStr(lastID) + "/",
  196. },
  197. },
  198. ); err != nil {
  199. return err
  200. }
  201. }
  202. return nil
  203. }); err != nil {
  204. wrap.Write(`{"error": true, "variable": "ShopOrderErrorSomethingWrong"}`)
  205. return
  206. }
  207. // Clear user basket
  208. wrap.ShopBasket.ClearBasket(&basket.SBParam{
  209. R: wrap.R,
  210. DB: wrap.DB,
  211. Host: wrap.CurrHost,
  212. Config: wrap.Config,
  213. SessionId: wrap.GetSessionId(),
  214. })
  215. wrap.Write(`{"error": false, "field": "", "variable": "ShopOrderSuccess"}`)
  216. return
  217. })
  218. }