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.

684 lines
25KB

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