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.

731 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: 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. app.delivery = app.order.delivery_home ;
  255. if(app.order.delivery_address && app.order.delivery_address.length > 0) {
  256. app.deliveryAddress = app.order.delivery_address ;
  257. }
  258. app.pointSaleActive = app.getPointSale(response.data.order.id_point_sale) ;
  259. }
  260. else {
  261. app.comment = null ;
  262. app.delivery = false ;
  263. app.deliveryAddress = null ;
  264. if(app.user.address && app.user.address.length > 0) {
  265. app.deliveryAddress = app.user.address ;
  266. }
  267. }
  268. },
  269. isMobile: function() {
  270. var width_window = parseInt($(window).width());
  271. return width_window <= 768;
  272. },
  273. responsive: function() {
  274. var app = this;
  275. app.responsiveApply();
  276. $(window).resize(function() {
  277. app.responsiveApply();
  278. });
  279. },
  280. responsiveApply: function() {
  281. var $td_summary = $('#products tr.total td.summary');
  282. if(this.isMobile()) {
  283. $td_summary.attr('colspan', 3);
  284. }
  285. else {
  286. $td_summary.attr('colspan', 4);
  287. }
  288. },
  289. isChangeState: function(entryPoint, oldStep, newStep) {
  290. return this.producer
  291. && entryPoint == this.producer.option_order_entry_point
  292. && oldStep == this.oldStep
  293. && newStep == this.step ;
  294. },
  295. nextStep: function() {
  296. this.errors = [] ;
  297. var oldStep = this.step ;
  298. var nextStep = null ;
  299. // par point de vente
  300. if(this.producer && this.producer.option_order_entry_point == 'point-sale') {
  301. if(oldStep == 'point-sale') {
  302. nextStep = 'date' ;
  303. }
  304. else if(oldStep == 'date') {
  305. nextStep = 'products' ;
  306. }
  307. }
  308. // par date
  309. else {
  310. if(oldStep == 'date') {
  311. nextStep = 'point-sale' ;
  312. }
  313. else if(oldStep == 'point-sale') {
  314. nextStep = 'products' ;
  315. }
  316. }
  317. if(nextStep) {
  318. this.changeStep(nextStep) ;
  319. }
  320. },
  321. changeStep: function(step) {
  322. this.errors = [] ;
  323. var oldStep = this.step ;
  324. this.oldStep = oldStep ;
  325. if(oldStep == 'products' && step == 'payment') {
  326. this.checkProducts() ;
  327. }
  328. if(!this.errors.length) {
  329. this.step = step ;
  330. window.scroll(0, $('#page-title').position().top - 25) ;
  331. this.init('basic', oldStep, step) ;
  332. }
  333. },
  334. dayClickList: function(event) {
  335. var dateStr = event.currentTarget.getAttribute('data-distribution-date') ;
  336. var dateObject = new Date(dateStr.replace(/-/g, "/")) ;
  337. if(this.isAvailableDate(dateObject)) {
  338. this.dayClickEvent(dateObject) ;
  339. }
  340. },
  341. dayClick: function(day) {
  342. if(this.isAvailableDate(day.date)) {
  343. this.dayClickEvent(day.date) ;
  344. }
  345. },
  346. dayClickEvent: function(date) {
  347. this.date = date ;
  348. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  349. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  350. + this.date.getFullYear() ;
  351. this.nextStep() ;
  352. },
  353. isAvailableDate: function(date) {
  354. for(var key in this.calendar.availableDates) {
  355. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  356. return true ;
  357. }
  358. }
  359. return false ;
  360. },
  361. pointSaleClick: function(event) {
  362. var app = this ;
  363. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  364. var hasCode = event.currentTarget.getAttribute('data-code') ;
  365. if(hasCode) {
  366. axios.get('ajax-validate-code-point-sale',{params: {
  367. idPointSale: idPointSale,
  368. code: this.pointsSaleCodes[idPointSale]
  369. }}).then(function(response) {
  370. if(response.data) {
  371. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = false ;
  372. app.validatePointSale(idPointSale) ;
  373. }
  374. else {
  375. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = true ;
  376. Vue.set(app.pointsSaleCodes, idPointSale, '');
  377. }
  378. }) ;
  379. }
  380. else {
  381. this.validatePointSale(idPointSale) ;
  382. }
  383. },
  384. validatePointSale: function(idPointSale) {
  385. this.pointSaleActive = this.getPointSale(idPointSale) ;
  386. if(this.pointSaleActive.credit_functioning == 'mandatory' || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active)) {
  387. this.useCredit = true ;
  388. }
  389. this.nextStep() ;
  390. },
  391. productQuantityClick: function(product, quantity) {
  392. if( this.products[product.index].quantity_form + quantity >= 0 &&
  393. (this.products[product.index].quantity_form + quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit) ||
  394. !this.products[product.index].quantity_max)
  395. ) {
  396. var theQuantity = parseFloat(this.products[product.index].quantity_form) + parseFloat(quantity);
  397. this.products[product.index].quantity_form = parseFloat(theQuantity.toFixed(2)) ;
  398. }
  399. },
  400. oneProductOrdered: function() {
  401. for(var key in this.products) {
  402. if(this.products[key].quantity_form > 0) {
  403. return true ;
  404. }
  405. }
  406. return false ;
  407. },
  408. countProductOrdered: function() {
  409. var count = 0 ;
  410. for(var key in this.products) {
  411. if(this.products[key].quantity_form > 0) {
  412. if(this.products[key].unit != 'piece') {
  413. count ++ ;
  414. }
  415. else {
  416. count += this.products[key].quantity_form ;
  417. }
  418. }
  419. }
  420. return count ;
  421. },
  422. priceTotal: function(format) {
  423. var price = 0 ;
  424. for(var key in this.products) {
  425. var quantity = this.products[key].quantity_form;
  426. if(quantity > 0) {
  427. price += (quantity / this.products[key].unit_coefficient) * this.getBestProductPrice(this.products[key].id, this.products[key].quantity_form);
  428. }
  429. }
  430. if(format) {
  431. return this.formatPrice(price) ;
  432. }
  433. else {
  434. return numberDecimals(price, 2) ;
  435. }
  436. },
  437. productHasPrice: function(product) {
  438. return product.prices && product.prices.length > 0;
  439. },
  440. productHasPriceWithFromQuantity: function(product) {
  441. if(this.productHasPrice(product)) {
  442. for(var i = 0; i < product.prices.length; i++) {
  443. if(product.prices[i].from_quantity > 0) {
  444. return true;
  445. }
  446. }
  447. }
  448. return false;
  449. },
  450. getBestProductPrice: function(idProduct, theQuantity) {
  451. var thePriceWithTax = 9999;
  452. var product = this.getProduct(idProduct);
  453. var pricesArray = product.prices;
  454. var unitCoefficient = product.unit_coefficient;
  455. if(theQuantity) {
  456. theQuantity = theQuantity / unitCoefficient;
  457. }
  458. for(var i = 0; i < pricesArray.length ; i++) {
  459. var priceWithTax = pricesArray[i].price_with_tax;
  460. var fromQuantity = pricesArray[i].from_quantity;
  461. if(priceWithTax < thePriceWithTax && fromQuantity <= theQuantity) {
  462. thePriceWithTax = priceWithTax;
  463. }
  464. }
  465. if(thePriceWithTax == 9999) {
  466. return 0;
  467. }
  468. else {
  469. return thePriceWithTax;
  470. }
  471. },
  472. confirmClick: function() {
  473. var app = this ;
  474. // delivery
  475. if(app.delivery && !app.deliveryAddress) {
  476. this.errors = [] ;
  477. this.errors.push('Veuillez saisir une adresse de livraison') ;
  478. return false ;
  479. }
  480. // guest form
  481. var $signupGuestForm = $('#signup-guest form') ;
  482. if($signupGuestForm.length > 0 && !$signupGuestForm.valid()) {
  483. $signupGuestForm.submit() ;
  484. return false ;
  485. }
  486. var user = false ;
  487. if(this.producer.option_allow_order_guest && !this.user) {
  488. user = {
  489. email: $('#signupguest-email').val(),
  490. password: $('#signupguest-password').val(),
  491. firstname: $('#signupguest-firstname').val(),
  492. lastname: $('#signupguest-lastname').val(),
  493. phone: $('#signupguest-phone').val(),
  494. } ;
  495. }
  496. // products
  497. var productsArray = {} ;
  498. for(var key in this.products) {
  499. if( this.products[key].quantity_form != null &&
  500. this.products[key].quantity_form > 0) {
  501. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  502. }
  503. }
  504. app.disableConfirmButton = true ;
  505. axios.post('ajax-process', {
  506. Order: {
  507. id_distribution : this.distribution.id,
  508. id_point_sale: this.pointSaleActive.id,
  509. comment: this.comment,
  510. delivery_home: this.delivery,
  511. delivery_address: this.deliveryAddress
  512. },
  513. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  514. products: productsArray,
  515. use_credit: Number(this.useCredit),
  516. user: user
  517. }).then(function(response) {
  518. if(response.data.status == 'success') {
  519. app.errors = [] ;
  520. if(response.data.redirect && response.data.redirect.length > 0) {
  521. window.location.href = response.data.redirect ;
  522. }
  523. else {
  524. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  525. }
  526. }
  527. else {
  528. app.errors = response.data.errors ;
  529. app.disableConfirmButton = false ;
  530. }
  531. });
  532. },
  533. checkProducts: function() {
  534. if(!this.oneProductOrdered()) {
  535. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  536. }
  537. },
  538. checkCreditLimit: function(order) {
  539. var total = this.priceTotal() ;
  540. if(order != null) {
  541. total = this.priceTotal() - order.amount_paid ;
  542. }
  543. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  544. },
  545. countProductsByCategory: function(category) {
  546. var count = 0 ;
  547. for(var i = 0 ; i < this.products.length ; i++) {
  548. if(this.products[i].id_product_category == category.id) {
  549. count ++ ;
  550. }
  551. }
  552. return count ;
  553. },
  554. countSelectedProductsByCategory: function(category) {
  555. var count = 0 ;
  556. for(var key in this.products) {
  557. if(this.products[key].quantity_form > 0 && this.products[key].id_product_category == category.id) {
  558. count ++ ;
  559. }
  560. }
  561. return count ;
  562. },
  563. setCategoryCurrent: function(category, first) {
  564. if(this.categoryCurrent && this.categoryCurrent.id == category.id && !first) {
  565. this.categoryCurrent = null ;
  566. }
  567. else {
  568. this.categoryCurrent = category ;
  569. }
  570. setTimeout(function() {
  571. opendistrib_products();
  572. }, 500);
  573. }
  574. },
  575. computed : {
  576. orderedPointsSale: function() {
  577. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  578. if(a.position > b.position) {
  579. return 1 ;
  580. }
  581. else {
  582. return -1 ;
  583. }
  584. }) ;
  585. return orderedPointsSaleArray ;
  586. }
  587. },
  588. updated: function () {
  589. var app = this;
  590. this.$nextTick(function () {
  591. if(app.step == 'payment' && !app.user && app.producer.option_allow_order_guest) {
  592. $("#signup-guest form").validate({
  593. rules: {
  594. 'SignupForm[email]': {
  595. 'email': true,
  596. 'required': true,
  597. 'minlength': 8,
  598. 'maxlength': 255
  599. },
  600. 'SignupForm[password]': {
  601. 'required': true,
  602. 'minlength': 8,
  603. "maxlength": 255
  604. },
  605. 'SignupForm[firstname]': {
  606. 'required': true,
  607. 'minlength': 2,
  608. "maxlength": 255
  609. },
  610. 'SignupForm[lastname]': {
  611. 'required': true,
  612. 'minlength': 2,
  613. "maxlength": 255
  614. },
  615. 'SignupForm[phone]': {
  616. 'required': true,
  617. 'minlength': 2,
  618. "maxlength": 255
  619. },
  620. },
  621. messages: {
  622. 'SignupForm[email]' : {
  623. 'required': 'Ce champs est requis.',
  624. 'email' : 'Email invalide.'
  625. },
  626. 'SignupForm[password]' : {
  627. 'required': 'Ce champs est requis.',
  628. },
  629. 'SignupForm[firstname]' : {
  630. 'required': 'Ce champs est requis.',
  631. },
  632. 'SignupForm[lastname]' : {
  633. 'required': 'Ce champs est requis.',
  634. },
  635. 'SignupForm[phone]' : {
  636. 'required': 'Ce champs est requis.',
  637. },
  638. }
  639. }) ;
  640. }
  641. });
  642. }
  643. });
  644. Vue.component('step-date',{
  645. props: [
  646. 'step',
  647. 'pointSaleActive',
  648. 'dateFormat',
  649. 'changeStep',
  650. 'producer',
  651. 'first',
  652. ],
  653. data: function() {
  654. return {
  655. } ;
  656. },
  657. template: '#template-step-date',
  658. methods: {
  659. }
  660. }) ;
  661. Vue.component('step-point-sale',{
  662. props: [
  663. 'step',
  664. 'pointSaleActive',
  665. 'changeStep',
  666. 'producer',
  667. 'first',
  668. ],
  669. data: function() {
  670. return {
  671. } ;
  672. },
  673. template: '#template-step-point-sale',
  674. methods: {
  675. }
  676. }) ;