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 23KB

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
6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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.5rem',
  56. padding: '20px',
  57. };
  58. return style;
  59. },
  60. },
  61. formats: {
  62. dayPopover: 'DD/MM/YYYY'
  63. }
  64. }
  65. }, window.appInitValues);
  66. },
  67. mounted: function() {
  68. if($('#order-distribution-date').size() || $('#distribution-date').size()) {
  69. if($('#order-distribution-date').size()) {
  70. this.date = new Date($('#order-distribution-date').html()) ;
  71. }
  72. else {
  73. this.date = new Date($('#distribution-date').html()) ;
  74. }
  75. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  76. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  77. + this.date.getFullYear() ;
  78. if(this.producer.option_order_entry_point == 'date') {
  79. this.changeStep('point-sale') ;
  80. }
  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. // use 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. app.pointsSaleCodes[response.data.points_sale[key].id] = '';
  197. Vue.set(app.pointsSaleCodes, response.data.points_sale[key].id, '');
  198. }
  199. }
  200. if(app.isChangeState('point-sale', 'point-sale', 'date')) {
  201. app.date = null ;
  202. app.dateFormat = null ;
  203. }
  204. // update order
  205. var updateOrder = false ;
  206. if(app.isChangeState('date', 'point-sale', 'products')
  207. || app.isChangeState('date', 'date', 'point-sale')
  208. || app.isChangeState('point-sale', 'date', 'products')
  209. || app.isChangeState('point-sale', 'point-sale', 'date')) {
  210. updateOrder = true ;
  211. }
  212. if(updateOrder) {
  213. // products
  214. if(response.data.products) {
  215. app.products = response.data.products ;
  216. }
  217. // order
  218. app.order = null ;
  219. if(response.data.order) {
  220. app.order = response.data.order ;
  221. app.comment = app.order.comment ;
  222. app.delivery = app.order.delivery_home ;
  223. if(app.order.delivery_address && app.order.delivery_address.length > 0) {
  224. app.deliveryAddress = app.order.delivery_address ;
  225. }
  226. app.pointSaleActive = app.getPointSale(response.data.order.id_point_sale) ;
  227. }
  228. else {
  229. app.comment = null ;
  230. app.delivery = false ;
  231. app.deliveryAddress = null ;
  232. if(app.user.address && app.user.address.length > 0) {
  233. app.deliveryAddress = app.user.address ;
  234. }
  235. }
  236. }
  237. if(type == 'first') {
  238. if(app.producer.option_order_entry_point == 'point-sale') {
  239. app.step = 'point-sale' ;
  240. }
  241. else {
  242. app.step = 'date' ;
  243. }
  244. }
  245. app.loading = false ;
  246. });
  247. },
  248. isChangeState: function(entryPoint, oldStep, newStep) {
  249. return this.producer
  250. && entryPoint == this.producer.option_order_entry_point
  251. && oldStep == this.oldStep
  252. && newStep == this.step ;
  253. },
  254. nextStep: function() {
  255. this.errors = [] ;
  256. var oldStep = this.step ;
  257. var nextStep = null ;
  258. // par point de vente
  259. if(this.producer && this.producer.option_order_entry_point == 'point-sale') {
  260. if(oldStep == 'point-sale') {
  261. nextStep = 'date' ;
  262. }
  263. else if(oldStep == 'date') {
  264. nextStep = 'products' ;
  265. }
  266. }
  267. // par date
  268. else {
  269. if(oldStep == 'date') {
  270. nextStep = 'point-sale' ;
  271. }
  272. else if(oldStep == 'point-sale') {
  273. nextStep = 'products' ;
  274. }
  275. }
  276. if(nextStep) {
  277. this.changeStep(nextStep) ;
  278. }
  279. },
  280. changeStep: function(step) {
  281. this.errors = [] ;
  282. var oldStep = this.step ;
  283. this.oldStep = oldStep ;
  284. if(oldStep == 'products' && step == 'payment') {
  285. this.checkProducts() ;
  286. }
  287. if(!this.errors.length) {
  288. this.step = step ;
  289. window.scroll(0, $('#page-title').position().top - 25) ;
  290. this.init('basic', oldStep, step) ;
  291. }
  292. },
  293. dayClickList: function(event) {
  294. var dateStr = event.currentTarget.getAttribute('data-distribution-date') ;
  295. var dateObject = new Date(dateStr.replace(/-/g, "/")) ;
  296. if(this.isAvailableDate(dateObject)) {
  297. this.dayClickEvent(dateObject) ;
  298. }
  299. },
  300. dayClick: function(day) {
  301. if(this.isAvailableDate(day.date)) {
  302. this.dayClickEvent(day.date) ;
  303. }
  304. },
  305. dayClickEvent: function(date) {
  306. this.date = date ;
  307. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  308. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  309. + this.date.getFullYear() ;
  310. this.nextStep() ;
  311. },
  312. isAvailableDate: function(date) {
  313. for(var key in this.calendar.availableDates) {
  314. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  315. return true ;
  316. }
  317. }
  318. return false ;
  319. },
  320. pointSaleClick: function(event) {
  321. var app = this ;
  322. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  323. var hasCode = event.currentTarget.getAttribute('data-code') ;
  324. if(hasCode) {
  325. axios.get('ajax-validate-code-point-sale',{params: {
  326. idPointSale: idPointSale,
  327. code: this.pointsSaleCodes[idPointSale]
  328. }}).then(function(response) {
  329. if(response.data) {
  330. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = false ;
  331. app.validatePointSale(idPointSale) ;
  332. }
  333. else {
  334. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = true ;
  335. Vue.set(app.pointsSaleCodes, idPointSale, '');
  336. }
  337. }) ;
  338. }
  339. else {
  340. this.validatePointSale(idPointSale) ;
  341. }
  342. },
  343. validatePointSale: function(idPointSale) {
  344. this.pointSaleActive = this.getPointSale(idPointSale) ;
  345. if(this.pointSaleActive.credit_functioning == 'mandatory' || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active)) {
  346. this.useCredit = true ;
  347. }
  348. this.nextStep() ;
  349. },
  350. productQuantityClick: function(product, quantity) {
  351. if( this.products[product.index].quantity_form + quantity >= 0 &&
  352. (this.products[product.index].quantity_form + quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit) ||
  353. !this.products[product.index].quantity_max)
  354. ) {
  355. this.products[product.index].quantity_form += quantity ;
  356. }
  357. },
  358. oneProductOrdered: function() {
  359. for(var key in this.products) {
  360. if(this.products[key].quantity_form > 0) {
  361. return true ;
  362. }
  363. }
  364. return false ;
  365. },
  366. countProductOrdered: function() {
  367. var count = 0 ;
  368. for(var key in this.products) {
  369. if(this.products[key].quantity_form > 0) {
  370. if(this.products[key].unit != 'piece') {
  371. count ++ ;
  372. }
  373. else {
  374. count += this.products[key].quantity_form ;
  375. }
  376. }
  377. }
  378. return count ;
  379. },
  380. priceTotal: function(format) {
  381. var price = 0 ;
  382. for(var key in this.products) {
  383. if(this.products[key].quantity_form > 0) {
  384. price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price_with_tax ;
  385. }
  386. }
  387. if(format) {
  388. return this.formatPrice(price) ;
  389. }
  390. else {
  391. return price ;
  392. }
  393. },
  394. confirmClick: function() {
  395. var app = this ;
  396. // delivery
  397. if(app.delivery && !app.deliveryAddress) {
  398. this.errors = [] ;
  399. this.errors.push('Veuillez saisir une adresse de livraison') ;
  400. return false ;
  401. }
  402. // guest form
  403. var $signupGuestForm = $('#signup-guest form') ;
  404. if($signupGuestForm.length > 0 && !$signupGuestForm.valid()) {
  405. $signupGuestForm.submit() ;
  406. return false ;
  407. }
  408. var user = false ;
  409. if(this.producer.option_allow_order_guest && !this.user) {
  410. user = {
  411. email: $('#signupguest-email').val(),
  412. password: $('#signupguest-password').val(),
  413. firstname: $('#signupguest-firstname').val(),
  414. lastname: $('#signupguest-lastname').val(),
  415. phone: $('#signupguest-phone').val(),
  416. } ;
  417. }
  418. // products
  419. var productsArray = {} ;
  420. for(var key in this.products) {
  421. if( this.products[key].quantity_form != null &&
  422. this.products[key].quantity_form > 0) {
  423. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  424. }
  425. }
  426. app.disableConfirmButton = true ;
  427. axios.post('ajax-process', {
  428. Order: {
  429. id_distribution : this.distribution.id,
  430. id_point_sale: this.pointSaleActive.id,
  431. comment: this.comment,
  432. delivery_home: this.delivery,
  433. delivery_address: this.deliveryAddress
  434. },
  435. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  436. products: productsArray,
  437. use_credit: Number(this.useCredit),
  438. user: user
  439. }).then(function(response) {
  440. if(response.data.status == 'success') {
  441. app.errors = [] ;
  442. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  443. }
  444. else {
  445. app.errors = response.data.errors ;
  446. app.disableConfirmButton = false ;
  447. }
  448. });
  449. },
  450. checkProducts: function() {
  451. if(!this.oneProductOrdered()) {
  452. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  453. }
  454. },
  455. checkCreditLimit: function(order) {
  456. var total = this.priceTotal() ;
  457. if(order != null) {
  458. total = this.priceTotal() - order.amount_paid ;
  459. }
  460. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  461. },
  462. countProductsByCategory: function(category) {
  463. var count = 0 ;
  464. for(var i = 0 ; i < this.products.length ; i++) {
  465. if(this.products[i].id_product_category == category.id) {
  466. count ++ ;
  467. }
  468. }
  469. return count ;
  470. },
  471. countSelectedProductsByCategory: function(category) {
  472. var count = 0 ;
  473. for(var key in this.products) {
  474. if(this.products[key].quantity_form > 0 && this.products[key].id_product_category == category.id) {
  475. count ++ ;
  476. }
  477. }
  478. return count ;
  479. },
  480. setCategoryCurrent: function(category) {
  481. if(this.categoryCurrent && this.categoryCurrent.id == category.id) {
  482. this.categoryCurrent = null ;
  483. }
  484. else {
  485. this.categoryCurrent = category ;
  486. }
  487. }
  488. },
  489. computed : {
  490. orderedPointsSale: function() {
  491. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  492. if(a.order > b.order) {
  493. return 1 ;
  494. }
  495. else {
  496. return -1 ;
  497. }
  498. }) ;
  499. return orderedPointsSaleArray ;
  500. }
  501. },
  502. updated: function () {
  503. var app = this;
  504. this.$nextTick(function () {
  505. if(app.step == 'payment' && !app.user && app.producer.option_allow_order_guest) {
  506. $("#signup-guest form").validate({
  507. rules: {
  508. 'SignupForm[email]': {
  509. 'email': true,
  510. 'required': true,
  511. 'minlength': 8,
  512. 'maxlength': 255
  513. },
  514. 'SignupForm[password]': {
  515. 'required': true,
  516. 'minlength': 8,
  517. "maxlength": 255
  518. },
  519. 'SignupForm[firstname]': {
  520. 'required': true,
  521. 'minlength': 2,
  522. "maxlength": 255
  523. },
  524. 'SignupForm[lastname]': {
  525. 'required': true,
  526. 'minlength': 2,
  527. "maxlength": 255
  528. },
  529. 'SignupForm[phone]': {
  530. 'required': true,
  531. 'minlength': 2,
  532. "maxlength": 255
  533. },
  534. },
  535. messages: {
  536. 'SignupForm[email]' : {
  537. 'required': 'Ce champs est requis.',
  538. 'email' : 'Email invalide.'
  539. },
  540. 'SignupForm[password]' : {
  541. 'required': 'Ce champs est requis.',
  542. },
  543. 'SignupForm[firstname]' : {
  544. 'required': 'Ce champs est requis.',
  545. },
  546. 'SignupForm[lastname]' : {
  547. 'required': 'Ce champs est requis.',
  548. },
  549. 'SignupForm[phone]' : {
  550. 'required': 'Ce champs est requis.',
  551. },
  552. }
  553. }) ;
  554. }
  555. });
  556. }
  557. });
  558. Vue.component('step-date',{
  559. props: [
  560. 'step',
  561. 'pointSaleActive',
  562. 'dateFormat',
  563. 'changeStep',
  564. 'producer',
  565. 'first',
  566. ],
  567. data: function() {
  568. return {
  569. } ;
  570. },
  571. template: '#template-step-date',
  572. methods: {
  573. }
  574. }) ;
  575. Vue.component('step-point-sale',{
  576. props: [
  577. 'step',
  578. 'pointSaleActive',
  579. 'changeStep',
  580. 'producer',
  581. 'first',
  582. ],
  583. data: function() {
  584. return {
  585. } ;
  586. },
  587. template: '#template-step-point-sale',
  588. methods: {
  589. }
  590. }) ;