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.

900 satır
36KB

  1. var app = new Vue({
  2. el: '#app-order-order',
  3. data() {
  4. return Object.assign({
  5. order: null,
  6. timerAjaxInfosProducts: false,
  7. xhr: false,
  8. loading: false,
  9. loadingProducts: false,
  10. loadingInit: true,
  11. cancelTokenSource: null,
  12. step: null,
  13. producer: null,
  14. user: null,
  15. date: null,
  16. dateFormat: null,
  17. distributions: [],
  18. distribution: null,
  19. pointsSale: [],
  20. pointSaleActive: null,
  21. pointSaleActiveId: null,
  22. pointsSaleCodes: [],
  23. products: [],
  24. categories: [],
  25. categoryCurrent: null,
  26. comment: '',
  27. creditCheckbox: false,
  28. paymentMethod: 'onsite',
  29. useCredit: false,
  30. errors: [],
  31. disableConfirmButton: false,
  32. delivery: false,
  33. deliveryAddress: null,
  34. urlLogin: '#',
  35. producerOptionOrderEntryPoint: null,
  36. calendar: {
  37. columns: this.isMobile() ? 1 : 2,
  38. mode: 'single',
  39. attrs: [],
  40. availableDates: [],
  41. themeStyles: {
  42. wrapper: {
  43. background: '#F7F7F7',
  44. color: '#333',
  45. border: 'solid 1px #e0e0e0'
  46. },
  47. header: {
  48. padding: '10px 10px',
  49. },
  50. headerHorizontalDivider: {
  51. borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
  52. width: '80%',
  53. },
  54. weekdays: {
  55. color: 'gray',
  56. fontWeight: '600',
  57. padding: '10px 10px',
  58. fontSize: '2rem'
  59. },
  60. weeks: {
  61. padding: '0 15px 15px 15px',
  62. },
  63. dayContent: function (object) {
  64. var style = {
  65. fontSize: '1.4rem',
  66. padding: app.isMobile() ? '18px' : '20px',
  67. };
  68. return style;
  69. },
  70. },
  71. masks: {
  72. //dayPopover: 'DD/MM/YYYY'
  73. dayPopover: ''
  74. }
  75. }
  76. }, window.appInitValues);
  77. },
  78. mounted: function() {
  79. this.initDate();
  80. this.init('first', false);
  81. },
  82. methods: {
  83. initDate: function() {
  84. var dateDefined = $('#order-distribution-date').size() || $('#distribution-date').size() ;
  85. if(dateDefined && (this.producerOptionOrderEntryPoint != 'point-sale' || this.pointSaleActiveId)) {
  86. if($('#order-distribution-date').size()) {
  87. this.date = new Date($('#order-distribution-date').html()) ;
  88. }
  89. else {
  90. this.date = new Date($('#distribution-date').html()) ;
  91. }
  92. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  93. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  94. + this.date.getFullYear() ;
  95. }
  96. },
  97. getDate: function() {
  98. return this.formatDate(this.date) ;
  99. },
  100. formatDate: function(date) {
  101. if(date) {
  102. return date.getFullYear() + '-'
  103. + ('0' + (date.getMonth() +1)).slice(-2) + '-'
  104. + ('0' + date.getDate()).slice(-2) ;
  105. }
  106. return false ;
  107. },
  108. formatPrice: formatPrice,
  109. getPointSale: function(idPointSale) {
  110. for(var key in this.pointsSale) {
  111. if(this.pointsSale[key].id == idPointSale) {
  112. return this.pointsSale[key] ;
  113. }
  114. }
  115. },
  116. getPointSaleKey: function(idPointSale) {
  117. for(var key in this.pointsSale) {
  118. if(this.pointsSale[key].id == idPointSale) {
  119. return key ;
  120. }
  121. }
  122. },
  123. getProduct: function(idProduct) {
  124. for(var key in this.products) {
  125. if(this.products[key].id == idProduct) {
  126. return this.products[key] ;
  127. }
  128. }
  129. },
  130. init: function(type, loadingProducts) {
  131. var app = this ;
  132. if(loadingProducts) {
  133. if (app.timerAjaxInfosProducts) {
  134. clearTimeout(app.timerAjaxInfosProducts);
  135. }
  136. app.timerAjaxInfosProducts = setTimeout(function(app) {
  137. app.loadingProducts = true ;
  138. if(app.xhr) {
  139. app.xhr.abort();
  140. }
  141. app.xhr = $.get('ajax-infos', {
  142. date : app.getDate(),
  143. pointSaleId: app.pointSaleActiveId ? app.pointSaleActiveId : (app.pointSaleActive ? app.pointSaleActive.id : 0),
  144. productsJson: JSON.stringify(app.getProductsArray()),
  145. loadingProducts: loadingProducts
  146. }, function(response) {
  147. app.products = response.products;
  148. app.loadingProducts = false;
  149. }, 'json');
  150. }.bind(this, app), 500);
  151. return;
  152. }
  153. else {
  154. this.loading = true ;
  155. }
  156. if(app.isChangeState('date', 'date', 'point-sale')) {
  157. app.pointSaleActive = null ;
  158. app.products = [] ;
  159. }
  160. //app.cancelTokenSource = axios.CancelToken.source();
  161. axios.get("ajax-infos",{
  162. //cancelToken: app.cancelTokenSource.token,
  163. params: {
  164. date : this.getDate(),
  165. pointSaleId: this.pointSaleActiveId ? this.pointSaleActiveId : (this.pointSaleActive ? this.pointSaleActive.id : 0),
  166. productsJson: this.getProductsArray(),
  167. loadingProducts: loadingProducts
  168. }})
  169. .catch(function (thrown) {
  170. if (axios.isCancel(thrown)) {
  171. }
  172. })
  173. .then(function(response) {
  174. if(response) {
  175. app.calendar.attrs = [];
  176. app.calendar.availableDates = [];
  177. var distributions = response.data.distributions;
  178. app.distributions = distributions;
  179. if (distributions.length) {
  180. var arrayDate;
  181. var highlightStyle = {
  182. style: {
  183. background: 'white',
  184. border: 'solid 2px #198754'
  185. },
  186. contentStyle: {
  187. color: '#198754'
  188. }
  189. };
  190. for (var i = 0; i < distributions.length; i++) {
  191. app.calendar.attrs.push({
  192. highlight: highlightStyle,
  193. dates: distributions[i].date
  194. });
  195. arrayDate = distributions[i].date.split('-');
  196. app.calendar.availableDates.push({
  197. highlight: highlightStyle,
  198. start: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]),
  199. end: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2])
  200. });
  201. }
  202. }
  203. if (response.data.leave_period) {
  204. leavePeriodStartDateArray = response.data.leave_period.start.split('-');
  205. leavePeriodEndDateArray = response.data.leave_period.end.split('-');
  206. app.calendar.attrs.push({
  207. highlight: {
  208. style: {
  209. //background: '#E09F3E'
  210. background: 'gray'
  211. },
  212. contentStyle: {
  213. color: 'white'
  214. }
  215. },
  216. dates: {
  217. start: new Date(leavePeriodStartDateArray[0], leavePeriodStartDateArray[1] - 1, leavePeriodStartDateArray[2]),
  218. end: new Date(leavePeriodEndDateArray[0], leavePeriodEndDateArray[1] - 1, leavePeriodEndDateArray[2])
  219. },
  220. popover: {
  221. label: 'En congé',
  222. hideIndicator: true,
  223. isInteractive: true
  224. },
  225. });
  226. }
  227. if (response.data.distribution) {
  228. app.distribution = response.data.distribution;
  229. }
  230. var orders = [];
  231. if (response.data.orders) {
  232. orders = response.data.orders;
  233. }
  234. if (orders.length) {
  235. for (var i = 0; i < orders.length; i++) {
  236. arrayDate = orders[i].date_distribution.split('-');
  237. var dateOrder = new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]);
  238. if (app.isAvailableDate(dateOrder)) {
  239. app.calendar.attrs.push({
  240. highlight: {
  241. style: {
  242. background: '#198754'
  243. },
  244. contentStyle: {
  245. color: 'white'
  246. }
  247. },
  248. popover: {
  249. label: orders[i].pointSale.name + ' (' + app.formatPrice(orders[i].amount_total) + ')',
  250. hideIndicator: true,
  251. isInteractive: true
  252. },
  253. dates: orders[i].date_distribution
  254. });
  255. }
  256. }
  257. }
  258. app.producer = response.data.producer;
  259. app.user = response.data.user;
  260. app.useCredit = response.data.producer.use_credit_checked_default;
  261. if (response.data.points_sale) {
  262. app.pointsSale = [];
  263. var orderPointSale = 0;
  264. for (var key in response.data.points_sale) {
  265. response.data.points_sale[key].order = orderPointSale++;
  266. app.pointsSale[response.data.points_sale[key].id] = response.data.points_sale[key];
  267. if (!app.pointsSaleCodes[response.data.points_sale[key].id]) {
  268. app.pointsSaleCodes[response.data.points_sale[key].id] = '';
  269. Vue.set(app.pointsSaleCodes, response.data.points_sale[key].id, '');
  270. }
  271. }
  272. }
  273. if (app.pointSaleActiveId) {
  274. app.pointSaleActive = app.getPointSale(app.pointSaleActiveId);
  275. }
  276. if (app.pointSaleActive) {
  277. if (app.producer.credit
  278. && app.pointSaleActive.payment_method_credit
  279. && (app.pointSaleActive.credit_functioning == 'mandatory'
  280. || (app.pointSaleActive.credit_functioning == 'user' && app.user.credit_active)
  281. || (app.pointSaleActive.credit_functioning == 'optional' && response.data.producer.use_credit_checked_default))) {
  282. app.paymentMethod = 'credit';
  283. } else if (app.pointSaleActive.payment_method_onsite) {
  284. app.paymentMethod = 'onsite';
  285. } else if (app.pointSaleActive.payment_method_online) {
  286. app.paymentMethod = 'online';
  287. }
  288. }
  289. if (app.isChangeState('point-sale', 'point-sale', 'date')) {
  290. app.date = null;
  291. app.dateFormat = null;
  292. }
  293. // update order
  294. var updateOrder = false;
  295. if (app.isChangeState('date', 'point-sale', 'products')
  296. || app.isChangeState('date', 'date', 'point-sale')
  297. || app.isChangeState('point-sale', 'date', 'products')
  298. || app.isChangeState('point-sale', 'point-sale', 'date')) {
  299. updateOrder = true;
  300. }
  301. if (updateOrder) {
  302. app.updateOrder(response);
  303. }
  304. if (type == 'first') {
  305. if (app.getDate() && app.pointSaleActive) {
  306. app.step = 'products';
  307. if (response.data.products) {
  308. app.products = response.data.products;
  309. }
  310. app.updateOrder(response);
  311. } else if (app.producer.option_order_entry_point == 'point-sale') {
  312. app.step = 'point-sale';
  313. } else if (app.getDate() && app.producer && app.producer.option_order_entry_point == 'date') {
  314. app.step = 'point-sale';
  315. } else {
  316. app.step = 'date';
  317. }
  318. }
  319. if (response.data.categories) {
  320. app.categories = response.data.categories;
  321. for (keyCategory in response.data.categories) {
  322. var category = response.data.categories[keyCategory];
  323. if (category.id && app.countProductsByCategory(category)) {
  324. app.setCategoryCurrent(category, true);
  325. break;
  326. }
  327. }
  328. }
  329. setTimeout(function () {
  330. app.responsive();
  331. opendistrib_products();
  332. }, 500);
  333. app.loading = false;
  334. app.loadingProducts = false;
  335. app.loadingInit = false;
  336. }
  337. });
  338. },
  339. updateOrder: function(response) {
  340. var app = this;
  341. if(response.data.products) {
  342. app.products = response.data.products;
  343. }
  344. app.order = null ;
  345. if(response.data.order) {
  346. app.order = response.data.order ;
  347. app.comment = app.order.comment ;
  348. if(app.order.delivery_address && app.order.delivery_address.length > 0) {
  349. app.deliveryAddress = app.order.delivery_address ;
  350. }
  351. app.pointSaleActive = app.getPointSale(response.data.order.id_point_sale) ;
  352. }
  353. else {
  354. app.comment = null ;
  355. app.deliveryAddress = null ;
  356. if(app.user.address && app.user.address.length > 0) {
  357. app.deliveryAddress = app.user.address ;
  358. }
  359. }
  360. },
  361. isMobile: function() {
  362. var width_window = parseInt($(window).width());
  363. return width_window <= 768;
  364. },
  365. responsive: function() {
  366. var app = this;
  367. app.responsiveApply();
  368. $(window).resize(function() {
  369. app.responsiveApply();
  370. });
  371. },
  372. responsiveApply: function() {
  373. var $td_summary = $('#products tr.total td.summary');
  374. if(this.isMobile()) {
  375. $td_summary.attr('colspan', 3);
  376. }
  377. else {
  378. $td_summary.attr('colspan', 4);
  379. }
  380. },
  381. isChangeState: function(entryPoint, oldStep, newStep) {
  382. return this.producer
  383. && entryPoint == this.producer.option_order_entry_point
  384. && oldStep == this.oldStep
  385. && newStep == this.step ;
  386. },
  387. nextStep: function() {
  388. this.errors = [] ;
  389. var oldStep = this.step ;
  390. var nextStep = null ;
  391. // par point de vente
  392. if(this.producer && this.producer.option_order_entry_point == 'point-sale') {
  393. if(oldStep == 'point-sale') {
  394. nextStep = 'date' ;
  395. }
  396. else if(oldStep == 'date') {
  397. nextStep = 'products' ;
  398. }
  399. }
  400. // par date
  401. else {
  402. if(oldStep == 'date') {
  403. nextStep = 'point-sale' ;
  404. }
  405. else if(oldStep == 'point-sale') {
  406. nextStep = 'products' ;
  407. }
  408. }
  409. if(nextStep) {
  410. this.changeStep(nextStep) ;
  411. }
  412. },
  413. changeStep: function(step) {
  414. this.errors = [] ;
  415. var oldStep = this.step ;
  416. this.oldStep = oldStep ;
  417. if(oldStep == 'products' && step == 'payment') {
  418. this.checkProducts() ;
  419. }
  420. if(!this.errors.length) {
  421. this.step = step ;
  422. this.init('basic', false) ;
  423. }
  424. },
  425. dayClickList: function(event) {
  426. var dateStr = event.currentTarget.getAttribute('data-distribution-date') ;
  427. var dateObject = new Date(dateStr.replace(/-/g, "/")) ;
  428. if(this.isAvailableDate(dateObject)) {
  429. this.dayClickEvent(dateObject) ;
  430. }
  431. },
  432. dayClick: function(day) {
  433. if(this.isAvailableDate(day.date)) {
  434. this.dayClickEvent(day.date) ;
  435. }
  436. },
  437. dayClickEvent: function(date) {
  438. this.date = date ;
  439. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  440. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  441. + this.date.getFullYear() ;
  442. this.nextStep() ;
  443. },
  444. isAvailableDate: function(date) {
  445. for(var key in this.calendar.availableDates) {
  446. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  447. return true ;
  448. }
  449. }
  450. return false ;
  451. },
  452. pointSaleClick: function(event) {
  453. var app = this ;
  454. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  455. var hasCode = event.currentTarget.getAttribute('data-code') ;
  456. if(hasCode) {
  457. axios.get('ajax-validate-code-point-sale',{params: {
  458. idPointSale: idPointSale,
  459. code: this.pointsSaleCodes[idPointSale]
  460. }}).then(function(response) {
  461. if(response.data) {
  462. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = false ;
  463. app.validatePointSale(idPointSale) ;
  464. }
  465. else {
  466. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = true ;
  467. Vue.set(app.pointsSaleCodes, idPointSale, '');
  468. }
  469. }) ;
  470. }
  471. else {
  472. this.validatePointSale(idPointSale) ;
  473. }
  474. },
  475. validatePointSale: function(idPointSale) {
  476. this.pointSaleActive = this.getPointSale(idPointSale) ;
  477. if(this.pointSaleActive.credit_functioning == 'mandatory' || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active)) {
  478. this.useCredit = true ;
  479. }
  480. this.nextStep() ;
  481. },
  482. productQuantityClick: function(product, quantity) {
  483. if(this.products[product.index].quantity_form + quantity >= 0
  484. && (this.products[product.index].quantity_max == null
  485. || this.products[product.index].quantity_form + quantity <= this.products[product.index].quantity_max * this.products[product.index].coefficient_unit)
  486. && (this.products[product.index].quantity_remaining == null
  487. || (quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit)))
  488. ) {
  489. var theQuantity = parseFloat(this.products[product.index].quantity_form) + parseFloat(quantity);
  490. this.products[product.index].quantity_form = parseFloat(theQuantity.toFixed(2)) ;
  491. if(this.producer.feature_product_accessory_enabled) {
  492. this.init('first', true);
  493. }
  494. }
  495. },
  496. oneProductOrdered: function() {
  497. for(var key in this.products) {
  498. if(this.isProductAvailable(this.products[key])
  499. && this.products[key].quantity_form > 0) {
  500. return true ;
  501. }
  502. }
  503. return false ;
  504. },
  505. countProductOrdered: function() {
  506. var count = 0 ;
  507. for(var key in this.products) {
  508. if(this.isProductAvailable(this.products[key])
  509. && this.products[key].quantity_form > 0) {
  510. if(this.products[key].unit != 'piece') {
  511. count ++ ;
  512. }
  513. else {
  514. count += this.products[key].quantity_form ;
  515. }
  516. }
  517. }
  518. return count ;
  519. },
  520. priceTotal: function(format) {
  521. var price = 0 ;
  522. for(var key in this.products) {
  523. var quantity = this.products[key].quantity_form;
  524. if(this.isProductAvailable(this.products[key]) && quantity > 0) {
  525. price += (quantity / this.products[key].unit_coefficient) * this.getBestProductPrice(this.products[key].id, this.products[key].quantity_form);
  526. }
  527. }
  528. if(format) {
  529. return this.formatPrice(price) ;
  530. }
  531. else {
  532. return numberDecimals(price, 2) ;
  533. }
  534. },
  535. productHasPrice: function(product) {
  536. return product.prices && product.prices.length > 0;
  537. },
  538. productHasPriceWithFromQuantity: function(product) {
  539. if(this.productHasPrice(product)) {
  540. for(var i = 0; i < product.prices.length; i++) {
  541. if(product.prices[i].from_quantity > 0) {
  542. return true;
  543. }
  544. }
  545. }
  546. return false;
  547. },
  548. getBestProductPrice: function(idProduct, theQuantity) {
  549. var thePriceWithTax = 9999;
  550. var product = this.getProduct(idProduct);
  551. var pricesArray = product.prices;
  552. var unitCoefficient = product.unit_coefficient;
  553. if(theQuantity) {
  554. theQuantity = theQuantity / unitCoefficient;
  555. }
  556. for(var i = 0; i < pricesArray.length ; i++) {
  557. var priceWithTax = pricesArray[i].price_with_tax;
  558. var fromQuantity = pricesArray[i].from_quantity;
  559. if(priceWithTax < thePriceWithTax && fromQuantity <= theQuantity) {
  560. thePriceWithTax = priceWithTax;
  561. }
  562. }
  563. if(thePriceWithTax == 9999) {
  564. return 0;
  565. }
  566. else {
  567. return thePriceWithTax;
  568. }
  569. },
  570. isPaymentMethodOnsiteActive: function() {
  571. return this.pointSaleActive && this.pointSaleActive.payment_method_onsite
  572. && !this.isPaymentMethodCreditActiveFunctioningUser(true)
  573. && !this.isPaymentMethodCreditActiveFunctioningMandatory();
  574. },
  575. isPaymentMethodCreditActive: function() {
  576. return this.isPaymentMethodCreditActiveFunctioningOptional()
  577. || this.isPaymentMethodCreditActiveFunctioningMandatory()
  578. || this.isPaymentMethodCreditActiveFunctioningUser(true);
  579. },
  580. isPaymentMethodCreditActiveCheckBase: function() {
  581. return this.producer && this.producer.credit
  582. && this.pointSaleActive && this.pointSaleActive.payment_method_credit;
  583. },
  584. isPaymentMethodCreditActiveFunctioningUser: function(userCreditActive) {
  585. return this.isPaymentMethodCreditActiveCheckBase()
  586. && this.pointSaleActive.credit_functioning == 'user'
  587. && this.user && this.user.credit_active == userCreditActive;
  588. },
  589. isPaymentMethodCreditActiveFunctioningMandatory: function() {
  590. return this.isPaymentMethodCreditActiveCheckBase()
  591. && this.pointSaleActive.credit_functioning == 'mandatory';
  592. },
  593. isPaymentMethodCreditActiveFunctioningOptional: function() {
  594. return this.isPaymentMethodCreditActiveCheckBase()
  595. && this.pointSaleActive.credit_functioning == 'optional';
  596. },
  597. isPaymentMethodOnlineActive: function() {
  598. return this.producer && this.producer.online_payment
  599. && this.pointSaleActive && this.pointSaleActive.payment_method_online
  600. && !this.isPaymentMethodCreditActiveFunctioningUser(true)
  601. && !this.isPaymentMethodCreditActiveFunctioningMandatory()
  602. },
  603. errorCreditMandatoryAndLimit: function() {
  604. return this.user
  605. && this.pointSaleActive
  606. && this.pointSaleActive.payment_method_credit
  607. && (this.pointSaleActive.credit_functioning == 'mandatory'
  608. || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active))
  609. && !this.checkCreditLimit(this.order);
  610. },
  611. confirmClick: function() {
  612. var app = this ;
  613. // delivery
  614. if(app.pointSaleActive.is_home_delivery && !app.deliveryAddress) {
  615. this.errors = [] ;
  616. this.errors.push('Veuillez saisir une adresse de livraison.') ;
  617. opendistrib_scroll('page-title');
  618. return false ;
  619. }
  620. // montant minimum de commande
  621. if(app.pointSaleActive.minimum_order_amount > 0 && app.priceTotal() < app.pointSaleActive.minimum_order_amount) {
  622. this.errors = [] ;
  623. this.errors.push('Le montant minimum de commande est de '+app.formatPrice(app.pointSaleActive.minimum_order_amount)+' pour ce point de vente.') ;
  624. opendistrib_scroll('page-title');
  625. return false ;
  626. }
  627. // guest form
  628. var $signupGuestForm = $('#signup-guest form') ;
  629. if($signupGuestForm.length > 0 && !$signupGuestForm.valid()) {
  630. $signupGuestForm.submit() ;
  631. return false ;
  632. }
  633. var user = false ;
  634. if(this.producer.option_allow_order_guest && !this.user) {
  635. user = {
  636. email: $('#signupguest-email').val(),
  637. password: $('#signupguest-password').val(),
  638. firstname: $('#signupguest-firstname').val(),
  639. lastname: $('#signupguest-lastname').val(),
  640. phone: $('#signupguest-phone').val(),
  641. } ;
  642. }
  643. // products
  644. var productsArray = this.getProductsArray();
  645. app.disableConfirmButton = true ;
  646. axios.post('ajax-process', {
  647. Order: {
  648. id_distribution : this.distribution.id,
  649. id_point_sale: this.pointSaleActive.id,
  650. comment: this.comment,
  651. delivery_home: this.pointSaleActive.is_home_delivery,
  652. delivery_address: this.deliveryAddress
  653. },
  654. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  655. products: productsArray,
  656. payment_method: this.paymentMethod,
  657. user: user
  658. }).then(function(response) {
  659. if(response.data.status == 'success') {
  660. app.errors = [] ;
  661. if(response.data.redirect && response.data.redirect.length > 0) {
  662. window.location.href = response.data.redirect ;
  663. }
  664. else {
  665. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  666. }
  667. }
  668. else {
  669. app.errors = response.data.errors ;
  670. window.scroll(0, $('#page-title').offset().top) ;
  671. app.disableConfirmButton = false ;
  672. }
  673. });
  674. },
  675. getProductsArray: function() {
  676. var productsArray = {} ;
  677. for(var key in this.products) {
  678. if( this.isProductAvailable(this.products[key])
  679. && this.products[key].quantity_form != null
  680. && this.products[key].quantity_form > 0) {
  681. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  682. }
  683. }
  684. return productsArray;
  685. },
  686. checkProducts: function() {
  687. if(!this.oneProductOrdered()) {
  688. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  689. }
  690. },
  691. checkCreditLimit: function(order) {
  692. var total = this.priceTotal() ;
  693. if(order != null) {
  694. total = this.priceTotal() - order.amount_paid ;
  695. }
  696. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  697. },
  698. isProductAvailable: function(product) {
  699. return product.productDistribution && product.productDistribution[0] && product.productDistribution[0].active == 1;
  700. },
  701. countProductsByCategory: function(category) {
  702. var count = 0 ;
  703. for(var i = 0 ; i < this.products.length ; i++) {
  704. if(this.products[i].id_product_category == category.id && this.isProductAvailable(this.products[i])) {
  705. count ++ ;
  706. }
  707. }
  708. return count ;
  709. },
  710. countSelectedProductsByCategory: function(category) {
  711. var count = 0 ;
  712. for(var key in this.products) {
  713. if(this.products[key].quantity_form > 0 && this.products[key].id_product_category == category.id) {
  714. count ++ ;
  715. }
  716. }
  717. return count ;
  718. },
  719. setCategoryCurrent: function(category, first) {
  720. var idScroll = false;
  721. if(this.categoryCurrent && this.categoryCurrent.id == category.id && !first) {
  722. this.categoryCurrent = null ;
  723. }
  724. else {
  725. this.categoryCurrent = category ;
  726. if(!first) {
  727. idScroll = 'category'+this.categoryCurrent.id;
  728. }
  729. }
  730. setTimeout(function() {
  731. opendistrib_products();
  732. }, 500);
  733. if(idScroll) {
  734. setTimeout(opendistrib_scroll, 250, idScroll);
  735. }
  736. }
  737. },
  738. computed : {
  739. orderedPointsSale: function() {
  740. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  741. if(a.position > b.position) {
  742. return 1 ;
  743. }
  744. else {
  745. return -1 ;
  746. }
  747. }) ;
  748. return orderedPointsSaleArray ;
  749. }
  750. },
  751. updated: function () {
  752. var app = this;
  753. this.$nextTick(function () {
  754. if(app.step == 'products' && !app.user && app.producer.option_allow_order_guest) {
  755. $("#signup-guest form").validate({
  756. rules: {
  757. 'SignupForm[email]': {
  758. 'email': true,
  759. 'required': true,
  760. 'minlength': 8,
  761. 'maxlength': 255
  762. },
  763. 'SignupForm[password]': {
  764. 'required': true,
  765. 'minlength': 8,
  766. "maxlength": 255
  767. },
  768. 'SignupForm[firstname]': {
  769. 'required': true,
  770. 'minlength': 2,
  771. "maxlength": 255
  772. },
  773. 'SignupForm[lastname]': {
  774. 'required': true,
  775. 'minlength': 2,
  776. "maxlength": 255
  777. },
  778. 'SignupForm[phone]': {
  779. 'required': true,
  780. 'minlength': 2,
  781. "maxlength": 255
  782. },
  783. },
  784. messages: {
  785. 'SignupForm[email]' : {
  786. 'required': 'Ce champs est requis.',
  787. 'email' : 'Email invalide.'
  788. },
  789. 'SignupForm[password]' : {
  790. 'required': 'Ce champs est requis.',
  791. },
  792. 'SignupForm[firstname]' : {
  793. 'required': 'Ce champs est requis.',
  794. },
  795. 'SignupForm[lastname]' : {
  796. 'required': 'Ce champs est requis.',
  797. },
  798. 'SignupForm[phone]' : {
  799. 'required': 'Ce champs est requis.',
  800. },
  801. }
  802. }) ;
  803. }
  804. });
  805. }
  806. });
  807. Vue.component('step-date',{
  808. props: [
  809. 'step',
  810. 'pointSaleActive',
  811. 'dateFormat',
  812. 'changeStep',
  813. 'producer',
  814. 'first',
  815. ],
  816. data: function() {
  817. return {
  818. } ;
  819. },
  820. template: '#template-step-date',
  821. methods: {
  822. }
  823. }) ;
  824. Vue.component('step-point-sale',{
  825. props: [
  826. 'step',
  827. 'pointSaleActive',
  828. 'changeStep',
  829. 'producer',
  830. 'first',
  831. ],
  832. data: function() {
  833. return {
  834. } ;
  835. },
  836. template: '#template-step-point-sale',
  837. methods: {
  838. }
  839. }) ;