You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
12KB

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