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.

733 lines
26KB

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