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.

order-order.js 26KB

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