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.

278 lines
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. 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. var count = Number(this.monday) + Number(this.tuesday) + Number(this.wednesday) + Number(this.thursday) + Number(this.friday) + Number(this.saturday) + Number(this.sunday);
  111. if (count == 1 && this.lastCountDays == 0) {
  112. this.lastCountDays = count;
  113. opendistrib_scroll('step-days');
  114. }
  115. },
  116. checkProductAvailable: function (product) {
  117. var available = product.active &&
  118. (!this.monday || (this.monday && product.monday)) &&
  119. (!this.tuesday || (this.tuesday && product.tuesday)) &&
  120. (!this.wednesday || (this.wednesday && product.wednesday)) &&
  121. (!this.thursday || (this.thursday && product.thursday)) &&
  122. (!this.friday || (this.friday && product.friday)) &&
  123. (!this.saturday || (this.saturday && product.saturday)) &&
  124. (!this.sunday || (this.sunday && product.sunday));
  125. if (!available) {
  126. product.quantity_form = 0;
  127. }
  128. return available;
  129. },
  130. checkOneProductAvailable: function () {
  131. var count = 0;
  132. for (key in this.products) {
  133. if (this.checkProductAvailable(this.products[key])) {
  134. count++;
  135. }
  136. }
  137. return count;
  138. },
  139. productQuantityClick: function (product, quantity) {
  140. if (this.products[product.index].quantity_form + quantity >= 0) {
  141. this.products[product.index].quantity_form += quantity;
  142. }
  143. },
  144. oneProductOrdered: function () {
  145. for (var key in this.products) {
  146. if (this.products[key].quantity_form > 0) {
  147. return true;
  148. }
  149. }
  150. return false;
  151. },
  152. formatPrice: formatPrice,
  153. getPriceWithTax: getPriceWithTax,
  154. priceTotal: function (format) {
  155. var price = 0;
  156. for (var key in this.products) {
  157. if (this.products[key].quantity_form > 0) {
  158. price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price_with_tax;
  159. }
  160. }
  161. if (format) {
  162. return this.formatPrice(price);
  163. } else {
  164. return price;
  165. }
  166. },
  167. formSubmit: function () {
  168. this.checkForm();
  169. if (!this.errors.length && !this.disableSubmitButton) {
  170. this.disableSubmitButton = true;
  171. var productsArray = {};
  172. for (var key in this.products) {
  173. if (this.products[key].quantity_form != null &&
  174. this.products[key].quantity_form > 0) {
  175. productsArray['product_' + this.products[key].id] = this.products[key].quantity_form;
  176. }
  177. }
  178. console.log(this.autoPayment);
  179. axios.post('ajax-process', {
  180. idSubscription: this.idSubscription,
  181. SubscriptionForm: {
  182. id_point_sale: this.idPointSaleActive,
  183. date_begin: this.dateBegin ? this.formatDate(this.dateBegin) : '',
  184. date_end: this.dateEnd ? this.formatDate(this.dateEnd) : '',
  185. week_frequency: this.weekFrequency,
  186. auto_payment: this.autoPayment ? 1 : 0,
  187. monday: this.monday == true ? 1 : 0,
  188. tuesday: this.tuesday == true ? 1 : 0,
  189. wednesday: this.wednesday == true ? 1 : 0,
  190. thursday: this.thursday == true ? 1 : 0,
  191. friday: this.friday == true ? 1 : 0,
  192. saturday: this.saturday == true ? 1 : 0,
  193. sunday: this.sunday == true ? 1 : 0,
  194. products: productsArray,
  195. comment: this.comment
  196. }
  197. }).then(function (response) {
  198. window.location.href = opendistrib_base_url(true) + 'subscription/index';
  199. });
  200. }
  201. },
  202. checkForm: function () {
  203. var app = this;
  204. this.errors = [];
  205. if (!this.idPointSaleActive) {
  206. this.errors.push('Veuillez sélectionner un point de vente');
  207. } else {
  208. if (this.pointSaleActive.code && this.pointSaleActive.code.length > 0) {
  209. axios.get('ajax-validate-code-point-sale', {
  210. params: {
  211. idPointSale: this.idPointSaleActive,
  212. code: this.pointSaleActive.code
  213. }
  214. }).then(function (response) {
  215. if (response.data) {
  216. app.pointsSale[idPointSale].invalid_code = false;
  217. } else {
  218. app.pointsSale[idPointSale].invalid_code = true;
  219. Vue.set(app.pointsSaleCodes, idPointSale, '');
  220. }
  221. });
  222. }
  223. }
  224. var regexDate = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  225. if (!this.dateBegin) {
  226. this.errors.push('Veuillez sélectionner une date de début');
  227. } else {
  228. if (!regexDate.test(this.formatDate(this.dateBegin))) {
  229. this.errors.push('Mauvais format de date de début');
  230. }
  231. }
  232. if (this.dateEnd && this.dateEnd.length > 0 && !regexDate.test(this.formatDate(this.dateEnd))) {
  233. this.errors.push('Mauvais format de date de fin');
  234. }
  235. if (this.weekFrequency != 1 && this.weekFrequency != 2 &&
  236. this.weekFrequency != 3 && this.weekFrequency != 4) {
  237. this.errors.push('Veuillez sélectionner une périodicité');
  238. }
  239. if (!this.monday && !this.tuesday && !this.wednesday && !this.thursday &&
  240. !this.friday && !this.saturday && !this.sunday) {
  241. this.errors.push('Veuillez sélectionner un jour de distribution');
  242. }
  243. if (!this.oneProductOrdered()) {
  244. this.errors.push('Veuillez choisir au moins un produit');
  245. }
  246. if (this.errors.length) {
  247. window.scroll(0, $('#page-title').position().top - 25);
  248. }
  249. }
  250. }
  251. });