Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

288 linhas
11KB

  1. var app = new Vue({
  2. el: '#app-subscription-form',
  3. data: {
  4. loading: true,
  5. idSubscription: 0,
  6. pointsSale: [],
  7. idPointSaleActive: 0,
  8. pointSaleActive: null,
  9. pointsSaleCodes: [],
  10. dateBegin: null,
  11. dateEnd: null,
  12. weekFrequency: 1,
  13. autoPayment: true,
  14. monday: false,
  15. tuesday: false,
  16. wednesday: false,
  17. thursday: false,
  18. friday: false,
  19. saturday: false,
  20. sunday: false,
  21. products: [],
  22. errors: [],
  23. disableSubmitButton: false,
  24. lastCountDays: 0,
  25. },
  26. mounted: function() {
  27. this.init();
  28. },
  29. methods: {
  30. init: function() {
  31. if($('#subscription-id').val() != 0) {
  32. this.idSubscription = $('#subscription-id').val() ;
  33. }
  34. this.dateBegin = new Date() ;
  35. axios.get("ajax-infos",{params: {idSubscription : this.idSubscription}})
  36. .then(response => {
  37. this.products = response.data.products ;
  38. this.pointsSale = response.data.points_sale ;
  39. if(this.idSubscription > 0) {
  40. this.validatePointSale(response.data.id_point_sale) ;
  41. this.weekFrequency = response.data.week_frequency ;
  42. this.autoPayment = response.data.auto_payment ;
  43. var arrayDateBegin = response.data.date_begin.split('-') ;
  44. this.dateBegin = new Date(arrayDateBegin[0], arrayDateBegin[1] - 1, arrayDateBegin[2]) ;
  45. if(response.data.date_end && response.data.date_end.length > 0) {
  46. var arrayDateEnd = response.data.date_begin.split('-') ;
  47. this.dateEnd = new Date(arrayDateEnd[0], arrayDateEnd[1] - 1, arrayDateEnd[2]) ;
  48. }
  49. this.monday = response.data.monday ;
  50. this.tuesday = response.data.tuesday ;
  51. this.wednesday = response.data.wednesday ;
  52. this.thursday = response.data.thursday ;
  53. this.friday = response.data.friday ;
  54. this.saturday = response.data.saturday ;
  55. this.sunday = response.data.sunday ;
  56. }
  57. this.loading = false ;
  58. });
  59. },
  60. formatDate: function(date) {
  61. if(date) {
  62. return ('0' + date.getDate()).slice(-2) + '/' + ('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear() ;
  63. }
  64. return false ;
  65. },
  66. pointSaleClick: function(event) {
  67. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  68. var hasCode = event.currentTarget.getAttribute('data-code') ;
  69. if(hasCode) {
  70. axios.get('ajax-validate-code-point-sale',{params: {
  71. idPointSale: idPointSale,
  72. code: this.pointsSaleCodes[idPointSale]
  73. }}).then(response => {
  74. if(response.data) {
  75. this.getPointSale(idPointSale).invalid_code = false ;
  76. this.validatePointSale(idPointSale) ;
  77. }
  78. else {
  79. this.getPointSale(idPointSale).invalid_code = true ;
  80. Vue.set(this.pointsSaleCodes, idPointSale, '');
  81. }
  82. }) ;
  83. }
  84. else {
  85. this.validatePointSale(idPointSale) ;
  86. }
  87. },
  88. validatePointSale: function(idPointSale) {
  89. if(this.idPointSaleActive != idPointSale) {
  90. this.monday = false ;
  91. this.tuesday = false ;
  92. this.wednesday = false ;
  93. this.thursday = false ;
  94. this.friday = false ;
  95. this.saturday = false ;
  96. this.sunday = false ;
  97. }
  98. this.pointSaleActive = this.getPointSale(idPointSale) ;
  99. this.idPointSaleActive = idPointSale ;
  100. boulange_scroll('step-date') ;
  101. },
  102. getPointSale: function(idPointSale) {
  103. for(var key in this.pointsSale) {
  104. if(this.pointsSale[key].id == idPointSale) {
  105. return this.pointsSale[key] ;
  106. }
  107. }
  108. },
  109. dayChange: function() {
  110. console.log(this.monday+' '+this.tuesday+' '+this.wednesday+' '+
  111. this.thursday+' '+this.friday+' '+this.saturday+' '+this.sunday) ;
  112. var count = Number(this.monday) + Number(this.tuesday) + Number(this.wednesday)
  113. + Number(this.thursday) + Number(this.friday) + Number(this.saturday)
  114. + Number(this.sunday) ;
  115. if(count == 1 && this.lastCountDays == 0) {
  116. this.lastCountDays = count ;
  117. boulange_scroll('step-days') ;
  118. }
  119. },
  120. checkProductAvailable: function(product) {
  121. var available = product.active &&
  122. (!this.monday || (this.monday && product.monday)) &&
  123. (!this.tuesday || (this.tuesday && product.tuesday)) &&
  124. (!this.wednesday || (this.wednesday && product.wednesday)) &&
  125. (!this.thursday || (this.thursday && product.thursday)) &&
  126. (!this.friday || (this.friday && product.friday)) &&
  127. (!this.saturday || (this.saturday && product.saturday)) &&
  128. (!this.sunday || (this.sunday && product.sunday)) ;
  129. if(!available) {
  130. product.quantity_form = 0 ;
  131. }
  132. return available ;
  133. },
  134. checkOneProductAvailable: function() {
  135. var count = 0 ;
  136. for(key in this.products) {
  137. if(this.checkProductAvailable(this.products[key])) {
  138. count ++ ;
  139. }
  140. }
  141. return count ;
  142. },
  143. productQuantityClick: function(product, quantity) {
  144. if( this.products[product.index].quantity_form + quantity >= 0) {
  145. this.products[product.index].quantity_form += quantity ;
  146. }
  147. },
  148. oneProductOrdered: function() {
  149. for(var key in this.products) {
  150. if(this.products[key].quantity_form > 0) {
  151. return true ;
  152. }
  153. }
  154. return false ;
  155. },
  156. formatPrice: function(price) {
  157. var isNumberRegExp = new RegExp(/^[-+]?[0-9]+(\.[0-9]+)*$/);
  158. if(isNumberRegExp.test(price) && price > 0) {
  159. return Number(price).toFixed(2).replace('.',',')+' €' ;
  160. }
  161. return '--' ;
  162. },
  163. priceTotal: function(format) {
  164. var price = 0 ;
  165. for(var key in this.products) {
  166. if(this.products[key].quantity_form > 0) {
  167. price += this.products[key].quantity_form * this.products[key].price ;
  168. }
  169. }
  170. if(format) {
  171. return this.formatPrice(price) ;
  172. }
  173. else {
  174. return price ;
  175. }
  176. },
  177. formSubmit: function() {
  178. this.checkForm() ;
  179. if(!this.errors.length && !this.disableSubmitButton) {
  180. this.disableSubmitButton = true ;
  181. var productsArray = {} ;
  182. for(var key in this.products) {
  183. if( this.products[key].quantity_form != null &&
  184. this.products[key].quantity_form > 0) {
  185. productsArray['product_'+this.products[key].id] = this.products[key].quantity_form ;
  186. }
  187. }
  188. axios.post('ajax-process', {
  189. idSubscription: this.idSubscription,
  190. SubscriptionForm: {
  191. id_point_sale: this.idPointSaleActive,
  192. date_begin: this.dateBegin ? this.formatDate(this.dateBegin) : '',
  193. date_end: this.dateEnd ? this.formatDate(this.dateEnd) : '',
  194. week_frequency: this.weekFrequency,
  195. auto_payment: this.autoPayment,
  196. monday: this.monday == true ? 1 : 0,
  197. tuesday: this.tuesday == true ? 1 : 0,
  198. wednesday: this.wednesday == true ? 1 : 0,
  199. thursday: this.thursday == true ? 1 : 0,
  200. friday: this.friday == true ? 1 : 0,
  201. saturday: this.saturday == true ? 1 : 0,
  202. sunday: this.sunday == true ? 1 : 0,
  203. products: productsArray
  204. }
  205. }).then(response => {
  206. window.location.href = chat_base_url(true)+'subscription/index' ;
  207. });
  208. }
  209. },
  210. checkForm: function() {
  211. this.errors = [] ;
  212. if(!this.idPointSaleActive) {
  213. this.errors.push('Veuillez sélectionner un point de vente') ;
  214. }
  215. else {
  216. if(this.pointSaleActive.code && this.pointSaleActive.code.length > 0) {
  217. axios.get('ajax-validate-code-point-sale',{params: {
  218. idPointSale: this.idPointSaleActive,
  219. code: this.pointSaleActive.code
  220. }}).then(response => {
  221. if(response.data) {
  222. this.pointsSale[idPointSale].invalid_code = false ;
  223. }
  224. else {
  225. this.pointsSale[idPointSale].invalid_code = true ;
  226. Vue.set(this.pointsSaleCodes, idPointSale, '');
  227. }
  228. }) ;
  229. }
  230. }
  231. var regexDate = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  232. if(!this.dateBegin) {
  233. this.errors.push('Veuillez sélectionner une date de début') ;
  234. }
  235. else {
  236. if(!regexDate.test(this.formatDate(this.dateBegin))) {
  237. this.errors.push('Mauvais format de date de début') ;
  238. }
  239. }
  240. if(this.dateEnd && this.dateEnd.length > 0 && !regexDate.test(this.formatDate(this.dateEnd))) {
  241. this.errors.push('Mauvais format de date de fin') ;
  242. }
  243. if(this.weekFrequency != 1 && this.weekFrequency != 2 &&
  244. this.weekFrequency != 3 && this.weekFrequency != 4) {
  245. this.errors.push('Veuillez sélectionner une périodicité') ;
  246. }
  247. if(!this.monday && !this.tuesday && !this.wednesday && !this.thursday &&
  248. !this.friday && !this.saturday) {
  249. this.errors.push('Veuillez sélectionner un jour de distribution') ;
  250. }
  251. if(!this.oneProductOrdered()) {
  252. this.errors.push('Veuillez choisir au moins un produit') ;
  253. }
  254. if(this.errors.length) {
  255. window.scroll(0, $('#page-title').position().top - 25) ;
  256. }
  257. }
  258. }
  259. });