module_shop_order.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package modules
  2. import (
  3. "fmt"
  4. "strings"
  5. "golang-fave/engine/basket"
  6. "golang-fave/engine/wrapper"
  7. "golang-fave/utils"
  8. )
  9. func (this *Modules) RegisterAction_ShopOrder() *Action {
  10. return this.newAction(AInfo{
  11. WantDB: true,
  12. Mount: "shop-order",
  13. WantAdmin: false,
  14. }, func(wrap *wrapper.Wrapper) {
  15. if wrap.ShopBasket.ProductsCount(&basket.SBParam{
  16. R: wrap.R,
  17. DB: wrap.DB,
  18. Host: wrap.CurrHost,
  19. Config: wrap.Config,
  20. SessionId: wrap.GetSessionId(),
  21. }) <= 0 {
  22. wrap.Write(`{"error": true, "variable": "ShopOrderErrorBasketEmpty"}`)
  23. return
  24. }
  25. pf_client_last_name := wrap.R.FormValue("client_last_name")
  26. pf_client_first_name := wrap.R.FormValue("client_first_name")
  27. pf_client_second_name := wrap.R.FormValue("client_second_name")
  28. pf_client_phone := wrap.R.FormValue("client_phone")
  29. pf_client_email := wrap.R.FormValue("client_email")
  30. pf_client_delivery_comment := wrap.R.FormValue("client_delivery_comment")
  31. pf_client_order_comment := wrap.R.FormValue("client_order_comment")
  32. if (*wrap.Config).Shop.Orders.RequiredFields.LastName != 0 {
  33. if strings.TrimSpace(pf_client_last_name) == "" {
  34. wrap.Write(`{"error": true, "field": "client_last_name", "variable": "ShopOrderEmptyLastName"}`)
  35. return
  36. }
  37. }
  38. if (*wrap.Config).Shop.Orders.RequiredFields.FirstName != 0 {
  39. if strings.TrimSpace(pf_client_first_name) == "" {
  40. wrap.Write(`{"error": true, "field": "client_first_name", "variable": "ShopOrderEmptyFirstName"}`)
  41. return
  42. }
  43. }
  44. if (*wrap.Config).Shop.Orders.RequiredFields.SecondName != 0 {
  45. if strings.TrimSpace(pf_client_second_name) == "" {
  46. wrap.Write(`{"error": true, "field": "client_second_name", "variable": "ShopOrderEmptySecondName"}`)
  47. return
  48. }
  49. }
  50. if (*wrap.Config).Shop.Orders.RequiredFields.MobilePhone != 0 {
  51. if strings.TrimSpace(pf_client_phone) == "" || !utils.IsValidMobile(pf_client_phone) {
  52. wrap.Write(`{"error": true, "field": "client_phone", "variable": "ShopOrderEmptyMobilePhone"}`)
  53. return
  54. }
  55. }
  56. if (*wrap.Config).Shop.Orders.RequiredFields.EmailAddress != 0 {
  57. if strings.TrimSpace(pf_client_email) == "" || !utils.IsValidEmail(pf_client_email) {
  58. wrap.Write(`{"error": true, "field": "client_email", "variable": "ShopOrderEmptyEmailAddress"}`)
  59. return
  60. }
  61. }
  62. if (*wrap.Config).Shop.Orders.RequiredFields.Delivery != 0 {
  63. if strings.TrimSpace(pf_client_delivery_comment) == "" {
  64. wrap.Write(`{"error": true, "field": "client_delivery_comment", "variable": "ShopOrderEmptyDelivery"}`)
  65. return
  66. }
  67. }
  68. if (*wrap.Config).Shop.Orders.RequiredFields.Comment != 0 {
  69. if strings.TrimSpace(pf_client_order_comment) == "" {
  70. wrap.Write(`{"error": true, "field": "client_order_comment", "variable": "ShopOrderEmptyComment"}`)
  71. return
  72. }
  73. }
  74. bdata := wrap.ShopBasket.GetAll(&basket.SBParam{
  75. R: wrap.R,
  76. DB: wrap.DB,
  77. Host: wrap.CurrHost,
  78. Config: wrap.Config,
  79. SessionId: wrap.GetSessionId(),
  80. })
  81. fmt.Printf("bdata: %+v\n", (*bdata))
  82. // var lastID int64 = 0
  83. // if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  84. // // Insert row
  85. // res, err := tx.Exec(
  86. // `INSERT INTO blog_posts SET
  87. // user = ?,
  88. // name = ?,
  89. // alias = ?,
  90. // category = ?,
  91. // briefly = ?,
  92. // content = ?,
  93. // datetime = ?,
  94. // active = ?
  95. // ;`,
  96. // wrap.User.A_id,
  97. // pf_name,
  98. // pf_alias,
  99. // utils.StrToInt(pf_category),
  100. // pf_briefly,
  101. // pf_content,
  102. // utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  103. // utils.StrToInt(pf_active),
  104. // )
  105. // if err != nil {
  106. // return err
  107. // }
  108. // // Get inserted post id
  109. // lastID, err = res.LastInsertId()
  110. // if err != nil {
  111. // return err
  112. // }
  113. // // Block rows
  114. // if _, err := tx.Exec("SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", lastID); err != nil {
  115. // return err
  116. // }
  117. // // Insert post and categories relations
  118. // catids := utils.GetPostArrayInt("cats[]", wrap.R)
  119. // if len(catids) > 0 {
  120. // var catsCount int
  121. // err = tx.QueryRow(`
  122. // SELECT
  123. // COUNT(*)
  124. // FROM
  125. // blog_cats
  126. // WHERE
  127. // id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  128. // FOR UPDATE;`,
  129. // ).Scan(
  130. // &catsCount,
  131. // )
  132. // if *wrap.LogCpError(&err) != nil {
  133. // return err
  134. // }
  135. // if len(catids) != catsCount {
  136. // return errors.New("Inner system error")
  137. // }
  138. // var balkInsertArr []string
  139. // for _, el := range catids {
  140. // balkInsertArr = append(balkInsertArr, `(`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  141. // }
  142. // if _, err = tx.Exec(
  143. // `INSERT INTO blog_cat_post_rel (post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
  144. // ); err != nil {
  145. // return err
  146. // }
  147. // }
  148. // return nil
  149. // }); err != nil {
  150. // wrap.MsgError(err.Error())
  151. // return
  152. // }
  153. // Clear user basket
  154. wrap.ShopBasket.ClearBasket(&basket.SBParam{
  155. R: wrap.R,
  156. DB: wrap.DB,
  157. Host: wrap.CurrHost,
  158. Config: wrap.Config,
  159. SessionId: wrap.GetSessionId(),
  160. })
  161. wrap.Write(`{"error": false, "field": "", "variable": "ShopOrderSuccess"}`)
  162. return
  163. // if !utils.IsNumeric(pf_id) {
  164. // wrap.MsgError(`Inner system error`)
  165. // return
  166. // }
  167. // if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  168. // if _, err := tx.Exec(`
  169. // UPDATE shop_products SET
  170. // parent_id = NULL,
  171. // active = 0
  172. // WHERE
  173. // id = ?
  174. // ;`,
  175. // utils.StrToInt(pf_id),
  176. // ); err != nil {
  177. // return err
  178. // }
  179. // return nil
  180. // }); err != nil {
  181. // wrap.MsgError(err.Error())
  182. // return
  183. // }
  184. // wrap.RecreateProductXmlFile()
  185. // wrap.ResetCacheBlocks()
  186. // // Reload current page
  187. // wrap.Write(`window.location.reload(false);`)
  188. })
  189. }