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.

738 lines
27KB

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