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.

287 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. 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)
  111. + Number(this.thursday) + Number(this.friday) + Number(this.saturday)
  112. + Number(this.sunday);
  113. if (count == 1 && this.lastCountDays == 0) {
  114. this.lastCountDays = count;
  115. opendistrib_scroll('step-days');
  116. }
  117. },
  118. checkProductAvailable: function (product) {
  119. var available = product.status &&
  120. (!this.monday || (this.monday && product.monday)) &&
  121. (!this.tuesday || (this.tuesday && product.tuesday)) &&
  122. (!this.wednesday || (this.wednesday && product.wednesday)) &&
  123. (!this.thursday || (this.thursday && product.thursday)) &&
  124. (!this.friday || (this.friday && product.friday)) &&
  125. (!this.saturday || (this.saturday && product.saturday)) &&
  126. (!this.sunday || (this.sunday && product.sunday)) &&
  127. (
  128. (product.available_on_points_sale && !this.checkProductPointSaleInclude(product))
  129. || (!product.available_on_points_sale && this.checkProductPointSaleInclude(product))
  130. )
  131. ;
  132. if (!available) {
  133. product.quantity_form = 0;
  134. }
  135. return available;
  136. },
  137. checkProductPointSaleInclude: function(product) {
  138. return this.idPointSaleActive && product.product_point_sale_array.includes(parseInt(this.idPointSaleActive));
  139. },
  140. checkOneProductAvailable: function () {
  141. var count = 0;
  142. for (key in this.products) {
  143. if (this.checkProductAvailable(this.products[key])) {
  144. count++;
  145. }
  146. }
  147. return count;
  148. },
  149. productQuantityClick: function (product, quantity) {
  150. if (this.products[product.index].quantity_form + quantity >= 0) {
  151. this.products[product.index].quantity_form += quantity;
  152. }
  153. },
  154. oneProductOrdered: function () {
  155. for (var key in this.products) {
  156. if (this.products[key].quantity_form > 0) {
  157. return true;
  158. }
  159. }
  160. return false;
  161. },
  162. formatPrice: formatPrice,
  163. getPriceWithTax: getPriceWithTax,
  164. priceTotal: function (format) {
  165. var price = 0;
  166. for (var key in this.products) {
  167. if (this.products[key].quantity_form > 0) {
  168. price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price_with_tax;
  169. }
  170. }
  171. if (format) {
  172. return this.formatPrice(price);
  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: parseInt(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. comment: this.comment
  205. }
  206. }).then(function (response) {
  207. window.location.href = opendistrib_base_url(true) + 'subscription/index';
  208. });
  209. }
  210. },
  211. checkForm: function () {
  212. var app = this;
  213. this.errors = [];
  214. if (!this.idPointSaleActive) {
  215. this.errors.push('Veuillez sélectionner un point de vente');
  216. } else {
  217. if (this.pointSaleActive.code && this.pointSaleActive.code.length > 0) {
  218. axios.get('ajax-validate-code-point-sale', {
  219. params: {
  220. idPointSale: this.idPointSaleActive,
  221. code: this.pointSaleActive.code
  222. }
  223. }).then(function (response) {
  224. if (response.data) {
  225. app.pointsSale[idPointSale].invalid_code = false;
  226. } else {
  227. app.pointsSale[idPointSale].invalid_code = true;
  228. Vue.set(app.pointsSaleCodes, idPointSale, '');
  229. }
  230. });
  231. }
  232. }
  233. var regexDate = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  234. if (!this.dateBegin) {
  235. this.errors.push('Veuillez sélectionner une date de début');
  236. } else {
  237. if (!regexDate.test(this.formatDate(this.dateBegin))) {
  238. this.errors.push('Mauvais format de date de début');
  239. }
  240. }
  241. if (this.dateEnd && this.dateEnd.length > 0 && !regexDate.test(this.formatDate(this.dateEnd))) {
  242. this.errors.push('Mauvais format de date de fin');
  243. }
  244. if (this.weekFrequency != 1 && this.weekFrequency != 2 &&
  245. this.weekFrequency != 3 && this.weekFrequency != 4) {
  246. this.errors.push('Veuillez sélectionner une périodicité');
  247. }
  248. if (!this.monday && !this.tuesday && !this.wednesday && !this.thursday &&
  249. !this.friday && !this.saturday && !this.sunday) {
  250. this.errors.push('Veuillez sélectionner un jour de distribution');
  251. }
  252. if (!this.oneProductOrdered()) {
  253. this.errors.push('Veuillez choisir au moins un produit');
  254. }
  255. if (this.errors.length) {
  256. window.scroll(0, $('#page-title').position().top - 25);
  257. }
  258. }
  259. }
  260. });