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.

893 lines
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), 300);
  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. this.init('first', true);
  492. }
  493. },
  494. oneProductOrdered: function() {
  495. for(var key in this.products) {
  496. if(this.products[key].quantity_form > 0) {
  497. return true ;
  498. }
  499. }
  500. return false ;
  501. },
  502. countProductOrdered: function() {
  503. var count = 0 ;
  504. for(var key in this.products) {
  505. if(this.products[key].quantity_form > 0) {
  506. if(this.products[key].unit != 'piece') {
  507. count ++ ;
  508. }
  509. else {
  510. count += this.products[key].quantity_form ;
  511. }
  512. }
  513. }
  514. return count ;
  515. },
  516. priceTotal: function(format) {
  517. var price = 0 ;
  518. for(var key in this.products) {
  519. var quantity = this.products[key].quantity_form;
  520. if(quantity > 0) {
  521. price += (quantity / this.products[key].unit_coefficient) * this.getBestProductPrice(this.products[key].id, this.products[key].quantity_form);
  522. }
  523. }
  524. if(format) {
  525. return this.formatPrice(price) ;
  526. }
  527. else {
  528. return numberDecimals(price, 2) ;
  529. }
  530. },
  531. productHasPrice: function(product) {
  532. return product.prices && product.prices.length > 0;
  533. },
  534. productHasPriceWithFromQuantity: function(product) {
  535. if(this.productHasPrice(product)) {
  536. for(var i = 0; i < product.prices.length; i++) {
  537. if(product.prices[i].from_quantity > 0) {
  538. return true;
  539. }
  540. }
  541. }
  542. return false;
  543. },
  544. getBestProductPrice: function(idProduct, theQuantity) {
  545. var thePriceWithTax = 9999;
  546. var product = this.getProduct(idProduct);
  547. var pricesArray = product.prices;
  548. var unitCoefficient = product.unit_coefficient;
  549. if(theQuantity) {
  550. theQuantity = theQuantity / unitCoefficient;
  551. }
  552. for(var i = 0; i < pricesArray.length ; i++) {
  553. var priceWithTax = pricesArray[i].price_with_tax;
  554. var fromQuantity = pricesArray[i].from_quantity;
  555. if(priceWithTax < thePriceWithTax && fromQuantity <= theQuantity) {
  556. thePriceWithTax = priceWithTax;
  557. }
  558. }
  559. if(thePriceWithTax == 9999) {
  560. return 0;
  561. }
  562. else {
  563. return thePriceWithTax;
  564. }
  565. },
  566. isPaymentMethodOnsiteActive: function() {
  567. return this.pointSaleActive && this.pointSaleActive.payment_method_onsite
  568. && !this.isPaymentMethodCreditActiveFunctioningUser(true)
  569. && !this.isPaymentMethodCreditActiveFunctioningMandatory();
  570. },
  571. isPaymentMethodCreditActive: function() {
  572. return this.isPaymentMethodCreditActiveFunctioningOptional()
  573. || this.isPaymentMethodCreditActiveFunctioningMandatory()
  574. || this.isPaymentMethodCreditActiveFunctioningUser(true);
  575. },
  576. isPaymentMethodCreditActiveCheckBase: function() {
  577. return this.producer && this.producer.credit
  578. && this.pointSaleActive && this.pointSaleActive.payment_method_credit;
  579. },
  580. isPaymentMethodCreditActiveFunctioningUser: function(userCreditActive) {
  581. return this.isPaymentMethodCreditActiveCheckBase()
  582. && this.pointSaleActive.credit_functioning == 'user'
  583. && this.user && this.user.credit_active == userCreditActive;
  584. },
  585. isPaymentMethodCreditActiveFunctioningMandatory: function() {
  586. return this.isPaymentMethodCreditActiveCheckBase()
  587. && this.pointSaleActive.credit_functioning == 'mandatory';
  588. },
  589. isPaymentMethodCreditActiveFunctioningOptional: function() {
  590. return this.isPaymentMethodCreditActiveCheckBase()
  591. && this.pointSaleActive.credit_functioning == 'optional';
  592. },
  593. isPaymentMethodOnlineActive: function() {
  594. return this.producer && this.producer.online_payment
  595. && this.pointSaleActive && this.pointSaleActive.payment_method_online
  596. && !this.isPaymentMethodCreditActiveFunctioningUser(true)
  597. && !this.isPaymentMethodCreditActiveFunctioningMandatory()
  598. },
  599. errorCreditMandatoryAndLimit: function() {
  600. return this.user
  601. && this.pointSaleActive
  602. && this.pointSaleActive.payment_method_credit
  603. && (this.pointSaleActive.credit_functioning == 'mandatory'
  604. || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active))
  605. && !this.checkCreditLimit(this.order);
  606. },
  607. confirmClick: function() {
  608. var app = this ;
  609. // delivery
  610. if(app.pointSaleActive.is_home_delivery && !app.deliveryAddress) {
  611. this.errors = [] ;
  612. this.errors.push('Veuillez saisir une adresse de livraison.') ;
  613. opendistrib_scroll('page-title');
  614. return false ;
  615. }
  616. // montant minimum de commande
  617. if(app.pointSaleActive.minimum_order_amount > 0 && app.priceTotal() < app.pointSaleActive.minimum_order_amount) {
  618. this.errors = [] ;
  619. this.errors.push('Le montant minimum de commande est de '+app.formatPrice(app.pointSaleActive.minimum_order_amount)+' pour ce point de vente.') ;
  620. opendistrib_scroll('page-title');
  621. return false ;
  622. }
  623. // guest form
  624. var $signupGuestForm = $('#signup-guest form') ;
  625. if($signupGuestForm.length > 0 && !$signupGuestForm.valid()) {
  626. $signupGuestForm.submit() ;
  627. return false ;
  628. }
  629. var user = false ;
  630. if(this.producer.option_allow_order_guest && !this.user) {
  631. user = {
  632. email: $('#signupguest-email').val(),
  633. password: $('#signupguest-password').val(),
  634. firstname: $('#signupguest-firstname').val(),
  635. lastname: $('#signupguest-lastname').val(),
  636. phone: $('#signupguest-phone').val(),
  637. } ;
  638. }
  639. // products
  640. var productsArray = this.getProductsArray();
  641. app.disableConfirmButton = true ;
  642. axios.post('ajax-process', {
  643. Order: {
  644. id_distribution : this.distribution.id,
  645. id_point_sale: this.pointSaleActive.id,
  646. comment: this.comment,
  647. delivery_home: this.pointSaleActive.is_home_delivery,
  648. delivery_address: this.deliveryAddress
  649. },
  650. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  651. products: productsArray,
  652. payment_method: this.paymentMethod,
  653. user: user
  654. }).then(function(response) {
  655. if(response.data.status == 'success') {
  656. app.errors = [] ;
  657. if(response.data.redirect && response.data.redirect.length > 0) {
  658. window.location.href = response.data.redirect ;
  659. }
  660. else {
  661. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  662. }
  663. }
  664. else {
  665. app.errors = response.data.errors ;
  666. window.scroll(0, $('#page-title').offset().top) ;
  667. app.disableConfirmButton = false ;
  668. }
  669. });
  670. },
  671. getProductsArray: function() {
  672. var productsArray = {} ;
  673. for(var key in this.products) {
  674. if( this.products[key].quantity_form != null &&
  675. this.products[key].quantity_form > 0) {
  676. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  677. }
  678. }
  679. return productsArray;
  680. },
  681. checkProducts: function() {
  682. if(!this.oneProductOrdered()) {
  683. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  684. }
  685. },
  686. checkCreditLimit: function(order) {
  687. var total = this.priceTotal() ;
  688. if(order != null) {
  689. total = this.priceTotal() - order.amount_paid ;
  690. }
  691. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  692. },
  693. isProductAvailable: function(product) {
  694. return product.productDistribution && product.productDistribution[0] && product.productDistribution[0].active == 1;
  695. },
  696. countProductsByCategory: function(category) {
  697. var count = 0 ;
  698. for(var i = 0 ; i < this.products.length ; i++) {
  699. if(this.products[i].id_product_category == category.id && this.isProductAvailable(this.products[i])) {
  700. count ++ ;
  701. }
  702. }
  703. return count ;
  704. },
  705. countSelectedProductsByCategory: function(category) {
  706. var count = 0 ;
  707. for(var key in this.products) {
  708. if(this.products[key].quantity_form > 0 && this.products[key].id_product_category == category.id) {
  709. count ++ ;
  710. }
  711. }
  712. return count ;
  713. },
  714. setCategoryCurrent: function(category, first) {
  715. var idScroll = false;
  716. if(this.categoryCurrent && this.categoryCurrent.id == category.id && !first) {
  717. this.categoryCurrent = null ;
  718. }
  719. else {
  720. this.categoryCurrent = category ;
  721. if(!first) {
  722. idScroll = 'category'+this.categoryCurrent.id;
  723. }
  724. }
  725. setTimeout(function() {
  726. opendistrib_products();
  727. }, 500);
  728. if(idScroll) {
  729. setTimeout(opendistrib_scroll, 250, idScroll);
  730. }
  731. }
  732. },
  733. computed : {
  734. orderedPointsSale: function() {
  735. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  736. if(a.position > b.position) {
  737. return 1 ;
  738. }
  739. else {
  740. return -1 ;
  741. }
  742. }) ;
  743. return orderedPointsSaleArray ;
  744. }
  745. },
  746. updated: function () {
  747. var app = this;
  748. this.$nextTick(function () {
  749. if(app.step == 'products' && !app.user && app.producer.option_allow_order_guest) {
  750. $("#signup-guest form").validate({
  751. rules: {
  752. 'SignupForm[email]': {
  753. 'email': true,
  754. 'required': true,
  755. 'minlength': 8,
  756. 'maxlength': 255
  757. },
  758. 'SignupForm[password]': {
  759. 'required': true,
  760. 'minlength': 8,
  761. "maxlength": 255
  762. },
  763. 'SignupForm[firstname]': {
  764. 'required': true,
  765. 'minlength': 2,
  766. "maxlength": 255
  767. },
  768. 'SignupForm[lastname]': {
  769. 'required': true,
  770. 'minlength': 2,
  771. "maxlength": 255
  772. },
  773. 'SignupForm[phone]': {
  774. 'required': true,
  775. 'minlength': 2,
  776. "maxlength": 255
  777. },
  778. },
  779. messages: {
  780. 'SignupForm[email]' : {
  781. 'required': 'Ce champs est requis.',
  782. 'email' : 'Email invalide.'
  783. },
  784. 'SignupForm[password]' : {
  785. 'required': 'Ce champs est requis.',
  786. },
  787. 'SignupForm[firstname]' : {
  788. 'required': 'Ce champs est requis.',
  789. },
  790. 'SignupForm[lastname]' : {
  791. 'required': 'Ce champs est requis.',
  792. },
  793. 'SignupForm[phone]' : {
  794. 'required': 'Ce champs est requis.',
  795. },
  796. }
  797. }) ;
  798. }
  799. });
  800. }
  801. });
  802. Vue.component('step-date',{
  803. props: [
  804. 'step',
  805. 'pointSaleActive',
  806. 'dateFormat',
  807. 'changeStep',
  808. 'producer',
  809. 'first',
  810. ],
  811. data: function() {
  812. return {
  813. } ;
  814. },
  815. template: '#template-step-date',
  816. methods: {
  817. }
  818. }) ;
  819. Vue.component('step-point-sale',{
  820. props: [
  821. 'step',
  822. 'pointSaleActive',
  823. 'changeStep',
  824. 'producer',
  825. 'first',
  826. ],
  827. data: function() {
  828. return {
  829. } ;
  830. },
  831. template: '#template-step-point-sale',
  832. methods: {
  833. }
  834. }) ;