Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

subscription-form.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. comment: ''
  26. },
  27. mounted: function () {
  28. this.init();
  29. },
  30. methods: {
  31. log: log,
  32. init: function () {
  33. var app = this;
  34. if ($('#subscription-id').val() != 0) {
  35. this.idSubscription = $('#subscription-id').val();
  36. }
  37. this.dateBegin = new Date();
  38. axios.get("ajax-infos", {params: {idSubscription: this.idSubscription}})
  39. .then(function (response) {
  40. app.products = response.data.products;
  41. app.pointsSale = response.data.points_sale;
  42. if (app.idSubscription > 0) {
  43. app.validatePointSale(response.data.id_point_sale);
  44. app.weekFrequency = response.data.week_frequency;
  45. app.autoPayment = response.data.auto_payment;
  46. var arrayDateBegin = response.data.date_begin.split('-');
  47. app.dateBegin = new Date(arrayDateBegin[0], arrayDateBegin[1] - 1, arrayDateBegin[2]);
  48. if (response.data.date_end && response.data.date_end.length > 0) {
  49. var arrayDateEnd = response.data.date_begin.split('-');
  50. app.dateEnd = new Date(arrayDateEnd[0], arrayDateEnd[1] - 1, arrayDateEnd[2]);
  51. }
  52. app.monday = response.data.monday;
  53. app.tuesday = response.data.tuesday;
  54. app.wednesday = response.data.wednesday;
  55. app.thursday = response.data.thursday;
  56. app.friday = response.data.friday;
  57. app.saturday = response.data.saturday;
  58. app.sunday = response.data.sunday;
  59. app.comment = response.data.comment;
  60. }
  61. app.loading = false;
  62. });
  63. },
  64. formatDate: formatDate,
  65. pointSaleClick: function (event) {
  66. var app = this;
  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', {
  71. params: {
  72. idPointSale: idPointSale,
  73. code: this.pointsSaleCodes[idPointSale]
  74. }
  75. }).then(function (response) {
  76. if (response.data) {
  77. app.getPointSale(idPointSale).invalid_code = false;
  78. app.validatePointSale(idPointSale);
  79. } else {
  80. app.getPointSale(idPointSale).invalid_code = true;
  81. Vue.set(app.pointsSaleCodes, idPointSale, '');
  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. opendistrib_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. opendistrib_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: formatPrice,
  157. getPriceWithTax: getPriceWithTax,
  158. priceTotal: function (format) {
  159. var price = 0;
  160. for (var key in this.products) {
  161. if (this.products[key].quantity_form > 0) {
  162. price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price_with_tax;
  163. }
  164. }
  165. if (format) {
  166. return this.formatPrice(price);
  167. } else {
  168. return price;
  169. }
  170. },
  171. formSubmit: function () {
  172. this.checkForm();
  173. if (!this.errors.length && !this.disableSubmitButton) {
  174. this.disableSubmitButton = true;
  175. var productsArray = {};
  176. for (var key in this.products) {
  177. if (this.products[key].quantity_form != null &&
  178. this.products[key].quantity_form > 0) {
  179. productsArray['product_' + this.products[key].id] = this.products[key].quantity_form;
  180. }
  181. }
  182. axios.post('ajax-process', {
  183. idSubscription: this.idSubscription,
  184. SubscriptionForm: {
  185. id_point_sale: this.idPointSaleActive,
  186. date_begin: this.dateBegin ? this.formatDate(this.dateBegin) : '',
  187. date_end: this.dateEnd ? this.formatDate(this.dateEnd) : '',
  188. week_frequency: this.weekFrequency,
  189. auto_payment: this.autoPayment,
  190. monday: this.monday == true ? 1 : 0,
  191. tuesday: this.tuesday == true ? 1 : 0,
  192. wednesday: this.wednesday == true ? 1 : 0,
  193. thursday: this.thursday == true ? 1 : 0,
  194. friday: this.friday == true ? 1 : 0,
  195. saturday: this.saturday == true ? 1 : 0,
  196. sunday: this.sunday == true ? 1 : 0,
  197. products: productsArray,
  198. comment: this.comment
  199. }
  200. }).then(function (response) {
  201. window.location.href = opendistrib_base_url(true) + 'subscription/index';
  202. });
  203. }
  204. },
  205. checkForm: function () {
  206. var app = this;
  207. this.errors = [];
  208. if (!this.idPointSaleActive) {
  209. this.errors.push('Veuillez sélectionner un point de vente');
  210. } else {
  211. if (this.pointSaleActive.code && this.pointSaleActive.code.length > 0) {
  212. axios.get('ajax-validate-code-point-sale', {
  213. params: {
  214. idPointSale: this.idPointSaleActive,
  215. code: this.pointSaleActive.code
  216. }
  217. }).then(function (response) {
  218. if (response.data) {
  219. app.pointsSale[idPointSale].invalid_code = false;
  220. } else {
  221. app.pointsSale[idPointSale].invalid_code = true;
  222. Vue.set(app.pointsSaleCodes, idPointSale, '');
  223. }
  224. });
  225. }
  226. }
  227. var regexDate = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  228. if (!this.dateBegin) {
  229. this.errors.push('Veuillez sélectionner une date de début');
  230. } else {
  231. if (!regexDate.test(this.formatDate(this.dateBegin))) {
  232. this.errors.push('Mauvais format de date de début');
  233. }
  234. }
  235. if (this.dateEnd && this.dateEnd.length > 0 && !regexDate.test(this.formatDate(this.dateEnd))) {
  236. this.errors.push('Mauvais format de date de fin');
  237. }
  238. if (this.weekFrequency != 1 && this.weekFrequency != 2 &&
  239. this.weekFrequency != 3 && this.weekFrequency != 4) {
  240. this.errors.push('Veuillez sélectionner une périodicité');
  241. }
  242. if (!this.monday && !this.tuesday && !this.wednesday && !this.thursday &&
  243. !this.friday && !this.saturday) {
  244. this.errors.push('Veuillez sélectionner un jour de distribution');
  245. }
  246. if (!this.oneProductOrdered()) {
  247. this.errors.push('Veuillez choisir au moins un produit');
  248. }
  249. if (this.errors.length) {
  250. window.scroll(0, $('#page-title').position().top - 25);
  251. }
  252. }
  253. }
  254. });