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.

737 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. 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. columns: 2,
  31. mode: 'single',
  32. attrs: [],
  33. availableDates: [],
  34. themeStyles: {
  35. wrapper: {
  36. background: '#F7F7F7',
  37. color: '#333',
  38. border: 'solid 1px #e0e0e0'
  39. },
  40. header: {
  41. padding: '10px 10px',
  42. },
  43. headerHorizontalDivider: {
  44. borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
  45. width: '80%',
  46. },
  47. weekdays: {
  48. color: 'gray',
  49. fontWeight: '600',
  50. padding: '10px 10px',
  51. fontSize: '2rem'
  52. },
  53. weeks: {
  54. padding: '0 15px 15px 15px',
  55. },
  56. dayContent: function (object) {
  57. var style = {
  58. fontSize: '1.4rem',
  59. padding: app.isMobile() ? '18px' : '20px',
  60. };
  61. return style;
  62. },
  63. },
  64. formats: {
  65. dayPopover: 'DD/MM/YYYY'
  66. }
  67. }
  68. }, window.appInitValues);
  69. },
  70. mounted: function() {
  71. let fr = new Intl.Locale("fr-FR");
  72. var dateDefined = $('#order-distribution-date').size() || $('#distribution-date').size() ;
  73. if(dateDefined) {
  74. if($('#order-distribution-date').size()) {
  75. this.date = new Date($('#order-distribution-date').html()) ;
  76. }
  77. else {
  78. this.date = new Date($('#distribution-date').html()) ;
  79. }
  80. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  81. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  82. + this.date.getFullYear() ;
  83. }
  84. this.init('first') ;
  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. setTimeout(function() {
  260. app.responsive();
  261. opendistrib_products();
  262. }, 500);
  263. app.loading = false ;
  264. app.loadingInit = false ;
  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.delivery && !app.deliveryAddress) {
  474. this.errors = [] ;
  475. this.errors.push('Veuillez saisir une adresse de livraison') ;
  476. return false ;
  477. }
  478. // guest form
  479. var $signupGuestForm = $('#signup-guest form') ;
  480. if($signupGuestForm.length > 0 && !$signupGuestForm.valid()) {
  481. $signupGuestForm.submit() ;
  482. return false ;
  483. }
  484. var user = false ;
  485. if(this.producer.option_allow_order_guest && !this.user) {
  486. user = {
  487. email: $('#signupguest-email').val(),
  488. password: $('#signupguest-password').val(),
  489. firstname: $('#signupguest-firstname').val(),
  490. lastname: $('#signupguest-lastname').val(),
  491. phone: $('#signupguest-phone').val(),
  492. } ;
  493. }
  494. // products
  495. var productsArray = {} ;
  496. for(var key in this.products) {
  497. if( this.products[key].quantity_form != null &&
  498. this.products[key].quantity_form > 0) {
  499. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  500. }
  501. }
  502. app.disableConfirmButton = true ;
  503. axios.post('ajax-process', {
  504. Order: {
  505. id_distribution : this.distribution.id,
  506. id_point_sale: this.pointSaleActive.id,
  507. comment: this.comment,
  508. delivery_home: this.delivery,
  509. delivery_address: this.deliveryAddress
  510. },
  511. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  512. products: productsArray,
  513. use_credit: Number(this.useCredit),
  514. user: user
  515. }).then(function(response) {
  516. if(response.data.status == 'success') {
  517. app.errors = [] ;
  518. if(response.data.redirect && response.data.redirect.length > 0) {
  519. window.location.href = response.data.redirect ;
  520. }
  521. else {
  522. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  523. }
  524. }
  525. else {
  526. app.errors = response.data.errors ;
  527. app.disableConfirmButton = false ;
  528. }
  529. });
  530. },
  531. checkProducts: function() {
  532. if(!this.oneProductOrdered()) {
  533. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  534. }
  535. },
  536. checkCreditLimit: function(order) {
  537. var total = this.priceTotal() ;
  538. if(order != null) {
  539. total = this.priceTotal() - order.amount_paid ;
  540. }
  541. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  542. },
  543. countProductsByCategory: function(category) {
  544. var count = 0 ;
  545. for(var i = 0 ; i < this.products.length ; i++) {
  546. if(this.products[i].id_product_category == category.id) {
  547. count ++ ;
  548. }
  549. }
  550. return count ;
  551. },
  552. countSelectedProductsByCategory: function(category) {
  553. var count = 0 ;
  554. for(var key in this.products) {
  555. if(this.products[key].quantity_form > 0 && this.products[key].id_product_category == category.id) {
  556. count ++ ;
  557. }
  558. }
  559. return count ;
  560. },
  561. setCategoryCurrent: function(category) {
  562. if(this.categoryCurrent && this.categoryCurrent.id == category.id) {
  563. this.categoryCurrent = null ;
  564. }
  565. else {
  566. this.categoryCurrent = category ;
  567. }
  568. setTimeout(function() {
  569. opendistrib_products();
  570. }, 500);
  571. }
  572. },
  573. computed : {
  574. orderedPointsSale: function() {
  575. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  576. if(a.position > b.position) {
  577. return 1 ;
  578. }
  579. else {
  580. return -1 ;
  581. }
  582. }) ;
  583. return orderedPointsSaleArray ;
  584. }
  585. },
  586. updated: function () {
  587. var app = this;
  588. this.$nextTick(function () {
  589. if(app.step == 'payment' && !app.user && app.producer.option_allow_order_guest) {
  590. $("#signup-guest form").validate({
  591. rules: {
  592. 'SignupForm[email]': {
  593. 'email': true,
  594. 'required': true,
  595. 'minlength': 8,
  596. 'maxlength': 255
  597. },
  598. 'SignupForm[password]': {
  599. 'required': true,
  600. 'minlength': 8,
  601. "maxlength": 255
  602. },
  603. 'SignupForm[firstname]': {
  604. 'required': true,
  605. 'minlength': 2,
  606. "maxlength": 255
  607. },
  608. 'SignupForm[lastname]': {
  609. 'required': true,
  610. 'minlength': 2,
  611. "maxlength": 255
  612. },
  613. 'SignupForm[phone]': {
  614. 'required': true,
  615. 'minlength': 2,
  616. "maxlength": 255
  617. },
  618. },
  619. messages: {
  620. 'SignupForm[email]' : {
  621. 'required': 'Ce champs est requis.',
  622. 'email' : 'Email invalide.'
  623. },
  624. 'SignupForm[password]' : {
  625. 'required': 'Ce champs est requis.',
  626. },
  627. 'SignupForm[firstname]' : {
  628. 'required': 'Ce champs est requis.',
  629. },
  630. 'SignupForm[lastname]' : {
  631. 'required': 'Ce champs est requis.',
  632. },
  633. 'SignupForm[phone]' : {
  634. 'required': 'Ce champs est requis.',
  635. },
  636. }
  637. }) ;
  638. }
  639. });
  640. }
  641. });
  642. Vue.component('step-date',{
  643. props: [
  644. 'step',
  645. 'pointSaleActive',
  646. 'dateFormat',
  647. 'changeStep',
  648. 'producer',
  649. 'first',
  650. ],
  651. data: function() {
  652. return {
  653. } ;
  654. },
  655. template: '#template-step-date',
  656. methods: {
  657. }
  658. }) ;
  659. Vue.component('step-point-sale',{
  660. props: [
  661. 'step',
  662. 'pointSaleActive',
  663. 'changeStep',
  664. 'producer',
  665. 'first',
  666. ],
  667. data: function() {
  668. return {
  669. } ;
  670. },
  671. template: '#template-step-point-sale',
  672. methods: {
  673. }
  674. }) ;