module_shop_order.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // var lastID int64 = 0
  74. // if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  75. // // Insert row
  76. // res, err := tx.Exec(
  77. // `INSERT INTO blog_posts SET
  78. // user = ?,
  79. // name = ?,
  80. // alias = ?,
  81. // category = ?,
  82. // briefly = ?,
  83. // content = ?,
  84. // datetime = ?,
  85. // active = ?
  86. // ;`,
  87. // wrap.User.A_id,
  88. // pf_name,
  89. // pf_alias,
  90. // utils.StrToInt(pf_category),
  91. // pf_briefly,
  92. // pf_content,
  93. // utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  94. // utils.StrToInt(pf_active),
  95. // )
  96. // if err != nil {
  97. // return err
  98. // }
  99. // // Get inserted post id
  100. // lastID, err = res.LastInsertId()
  101. // if err != nil {
  102. // return err
  103. // }
  104. // // Block rows
  105. // if _, err := tx.Exec("SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", lastID); err != nil {
  106. // return err
  107. // }
  108. // // Insert post and categories relations
  109. // catids := utils.GetPostArrayInt("cats[]", wrap.R)
  110. // if len(catids) > 0 {
  111. // var catsCount int
  112. // err = tx.QueryRow(`
  113. // SELECT
  114. // COUNT(*)
  115. // FROM
  116. // blog_cats
  117. // WHERE
  118. // id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  119. // FOR UPDATE;`,
  120. // ).Scan(
  121. // &catsCount,
  122. // )
  123. // if *wrap.LogCpError(&err) != nil {
  124. // return err
  125. // }
  126. // if len(catids) != catsCount {
  127. // return errors.New("Inner system error")
  128. // }
  129. // var balkInsertArr []string
  130. // for _, el := range catids {
  131. // balkInsertArr = append(balkInsertArr, `(`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  132. // }
  133. // if _, err = tx.Exec(
  134. // `INSERT INTO blog_cat_post_rel (post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  135. // ); err != nil {
  136. // return err
  137. // }
  138. // }
  139. // return nil
  140. // }); err != nil {
  141. // wrap.MsgError(err.Error())
  142. // return
  143. // }
  144. // Clear user basket
  145. wrap.ShopBasket.ClearBasket(&basket.SBParam{
  146. R: wrap.R,
  147. DB: wrap.DB,
  148. Host: wrap.CurrHost,
  149. Config: wrap.Config,
  150. SessionId: wrap.GetSessionId(),
  151. })
  152. wrap.Write(`{"error": false, "field": "", "variable": "ShopOrderSuccess"}`)
  153. return
  154. // if !utils.IsNumeric(pf_id) {
  155. // wrap.MsgError(`Inner system error`)
  156. // return
  157. // }
  158. // if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  159. // if _, err := tx.Exec(`
  160. // UPDATE shop_products SET
  161. // parent_id = NULL,
  162. // active = 0
  163. // WHERE
  164. // id = ?
  165. // ;`,
  166. // utils.StrToInt(pf_id),
  167. // ); err != nil {
  168. // return err
  169. // }
  170. // return nil
  171. // }); err != nil {
  172. // wrap.MsgError(err.Error())
  173. // return
  174. // }
  175. // wrap.RecreateProductXmlFile()
  176. // wrap.ResetCacheBlocks()
  177. // // Reload current page
  178. // wrap.Write(`window.location.reload(false);`)
  179. })
  180. }