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.

277 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. axios.post('ajax-process', {
  179. idSubscription: this.idSubscription,
  180. SubscriptionForm: {
  181. id_point_sale: this.idPointSaleActive,
  182. date_begin: this.dateBegin ? this.formatDate(this.dateBegin) : '',
  183. date_end: this.dateEnd ? this.formatDate(this.dateEnd) : '',
  184. week_frequency: this.weekFrequency,
  185. auto_payment: this.autoPayment,
  186. monday: this.monday == true ? 1 : 0,
  187. tuesday: this.tuesday == true ? 1 : 0,
  188. wednesday: this.wednesday == true ? 1 : 0,
  189. thursday: this.thursday == true ? 1 : 0,
  190. friday: this.friday == true ? 1 : 0,
  191. saturday: this.saturday == true ? 1 : 0,
  192. sunday: this.sunday == true ? 1 : 0,
  193. products: productsArray,
  194. comment: this.comment
  195. }
  196. }).then(function (response) {
  197. window.location.href = opendistrib_base_url(true) + 'subscription/index';
  198. });
  199. }
  200. },
  201. checkForm: function () {
  202. var app = this;
  203. this.errors = [];
  204. if (!this.idPointSaleActive) {
  205. this.errors.push('Veuillez sélectionner un point de vente');
  206. } else {
  207. if (this.pointSaleActive.code && this.pointSaleActive.code.length > 0) {
  208. axios.get('ajax-validate-code-point-sale', {
  209. params: {
  210. idPointSale: this.idPointSaleActive,
  211. code: this.pointSaleActive.code
  212. }
  213. }).then(function (response) {
  214. if (response.data) {
  215. app.pointsSale[idPointSale].invalid_code = false;
  216. } else {
  217. app.pointsSale[idPointSale].invalid_code = true;
  218. Vue.set(app.pointsSaleCodes, idPointSale, '');
  219. }
  220. });
  221. }
  222. }
  223. var regexDate = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  224. if (!this.dateBegin) {
  225. this.errors.push('Veuillez sélectionner une date de début');
  226. } else {
  227. if (!regexDate.test(this.formatDate(this.dateBegin))) {
  228. this.errors.push('Mauvais format de date de début');
  229. }
  230. }
  231. if (this.dateEnd && this.dateEnd.length > 0 && !regexDate.test(this.formatDate(this.dateEnd))) {
  232. this.errors.push('Mauvais format de date de fin');
  233. }
  234. if (this.weekFrequency != 1 && this.weekFrequency != 2 &&
  235. this.weekFrequency != 3 && this.weekFrequency != 4) {
  236. this.errors.push('Veuillez sélectionner une périodicité');
  237. }
  238. if (!this.monday && !this.tuesday && !this.wednesday && !this.thursday &&
  239. !this.friday && !this.saturday && !this.sunday) {
  240. this.errors.push('Veuillez sélectionner un jour de distribution');
  241. }
  242. if (!this.oneProductOrdered()) {
  243. this.errors.push('Veuillez choisir au moins un produit');
  244. }
  245. if (this.errors.length) {
  246. window.scroll(0, $('#page-title').position().top - 25);
  247. }
  248. }
  249. }
  250. });