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.

762 lines
28KB

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