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.

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