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

5 years ago
5 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
5 years ago
3 years ago
5 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. var dateDefined = $('#order-distribution-date').size() || $('#distribution-date').size() ;
  69. if(dateDefined) {
  70. if($('#order-distribution-date').size()) {
  71. this.date = new Date($('#order-distribution-date').html()) ;
  72. }
  73. else {
  74. this.date = new Date($('#distribution-date').html()) ;
  75. }
  76. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  77. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  78. + this.date.getFullYear() ;
  79. }
  80. this.init('first') ;
  81. this.loadingInit = false ;
  82. },
  83. methods: {
  84. getDate: function() {
  85. return this.formatDate(this.date) ;
  86. },
  87. formatDate: function(date) {
  88. if(date) {
  89. return date.getFullYear() + '-'
  90. + ('0' + (date.getMonth() +1)).slice(-2) + '-'
  91. + ('0' + date.getDate()).slice(-2) ;
  92. }
  93. return false ;
  94. },
  95. formatPrice: formatPrice,
  96. getPointSale: function(idPointSale) {
  97. for(var key in this.pointsSale) {
  98. if(this.pointsSale[key].id == idPointSale) {
  99. return this.pointsSale[key] ;
  100. }
  101. }
  102. },
  103. getPointSaleKey: function(idPointSale) {
  104. for(var key in this.pointsSale) {
  105. if(this.pointsSale[key].id == idPointSale) {
  106. return key ;
  107. }
  108. }
  109. },
  110. init: function(type, oldStep, step) {
  111. var app = this ;
  112. this.loading = true ;
  113. if(app.isChangeState('date', 'date', 'point-sale')) {
  114. app.pointSaleActive = null ;
  115. app.products = [] ;
  116. }
  117. axios.get("ajax-infos",{params: {
  118. date : this.getDate(),
  119. pointSaleId: this.pointSaleActive ? this.pointSaleActive.id : 0
  120. }})
  121. .then(function(response) {
  122. // distributions
  123. var distributions = response.data.distributions;
  124. app.calendar.attrs = [];
  125. app.calendar.availableDates = [];
  126. if (distributions.length) {
  127. app.distributions = distributions;
  128. var arrayDate;
  129. for (var i = 0; i < distributions.length; i++) {
  130. app.calendar.attrs.push({
  131. highlight: {
  132. backgroundColor: '#5cb85c',
  133. },
  134. contentStyle: {
  135. color: 'white',
  136. },
  137. dates: distributions[i].date,
  138. });
  139. arrayDate = distributions[i].date.split('-');
  140. app.calendar.availableDates.push({
  141. start: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]),
  142. end: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2])
  143. });
  144. }
  145. }
  146. // distribution
  147. if (response.data.distribution) {
  148. app.distribution = response.data.distribution;
  149. }
  150. // liste commandes
  151. var orders = [];
  152. if (response.data.orders) {
  153. orders = response.data.orders;
  154. }
  155. if (orders.length) {
  156. for (var i = 0; i < orders.length; i++) {
  157. arrayDate = orders[i].date_distribution.split('-');
  158. var dateOrder = new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]);
  159. if (app.isAvailableDate(dateOrder)) {
  160. app.calendar.attrs.push({
  161. highlight: {
  162. backgroundColor: '#FF7F00'
  163. },
  164. contentStyle: {
  165. color: 'white'
  166. },
  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. });
  173. }
  174. }
  175. }
  176. // catégories
  177. if(response.data.categories) {
  178. app.categories = response.data.categories ;
  179. app.setCategoryCurrent(response.data.categories[0]) ;
  180. }
  181. // producer
  182. app.producer = response.data.producer;
  183. // user
  184. app.user = response.data.user;
  185. // use credit
  186. app.useCredit = response.data.producer.use_credit_checked_default;
  187. // points de vente
  188. if (response.data.points_sale) {
  189. app.pointsSale = [];
  190. var orderPointSale = 0;
  191. for (var key in response.data.points_sale) {
  192. response.data.points_sale[key].order = orderPointSale++;
  193. app.pointsSale[response.data.points_sale[key].id] = response.data.points_sale[key];
  194. if(!app.pointsSaleCodes[response.data.points_sale[key].id]) {
  195. app.pointsSaleCodes[response.data.points_sale[key].id] = '';
  196. Vue.set(app.pointsSaleCodes, response.data.points_sale[key].id, '');
  197. }
  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 if(app.getDate() && app.producer && app.producer.option_order_entry_point == 'date') {
  242. app.step = 'point-sale' ;
  243. }
  244. else {
  245. app.step = 'date' ;
  246. }
  247. }
  248. app.loading = false ;
  249. });
  250. },
  251. isChangeState: function(entryPoint, oldStep, newStep) {
  252. return this.producer
  253. && entryPoint == this.producer.option_order_entry_point
  254. && oldStep == this.oldStep
  255. && newStep == this.step ;
  256. },
  257. nextStep: function() {
  258. this.errors = [] ;
  259. var oldStep = this.step ;
  260. var nextStep = null ;
  261. // par point de vente
  262. if(this.producer && this.producer.option_order_entry_point == 'point-sale') {
  263. if(oldStep == 'point-sale') {
  264. nextStep = 'date' ;
  265. }
  266. else if(oldStep == 'date') {
  267. nextStep = 'products' ;
  268. }
  269. }
  270. // par date
  271. else {
  272. if(oldStep == 'date') {
  273. nextStep = 'point-sale' ;
  274. }
  275. else if(oldStep == 'point-sale') {
  276. nextStep = 'products' ;
  277. }
  278. }
  279. if(nextStep) {
  280. this.changeStep(nextStep) ;
  281. }
  282. },
  283. changeStep: function(step) {
  284. this.errors = [] ;
  285. var oldStep = this.step ;
  286. this.oldStep = oldStep ;
  287. if(oldStep == 'products' && step == 'payment') {
  288. this.checkProducts() ;
  289. }
  290. if(!this.errors.length) {
  291. this.step = step ;
  292. window.scroll(0, $('#page-title').position().top - 25) ;
  293. this.init('basic', oldStep, step) ;
  294. }
  295. },
  296. dayClickList: function(event) {
  297. var dateStr = event.currentTarget.getAttribute('data-distribution-date') ;
  298. var dateObject = new Date(dateStr.replace(/-/g, "/")) ;
  299. if(this.isAvailableDate(dateObject)) {
  300. this.dayClickEvent(dateObject) ;
  301. }
  302. },
  303. dayClick: function(day) {
  304. if(this.isAvailableDate(day.date)) {
  305. this.dayClickEvent(day.date) ;
  306. }
  307. },
  308. dayClickEvent: function(date) {
  309. this.date = date ;
  310. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  311. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  312. + this.date.getFullYear() ;
  313. this.nextStep() ;
  314. },
  315. isAvailableDate: function(date) {
  316. for(var key in this.calendar.availableDates) {
  317. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  318. return true ;
  319. }
  320. }
  321. return false ;
  322. },
  323. pointSaleClick: function(event) {
  324. var app = this ;
  325. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  326. var hasCode = event.currentTarget.getAttribute('data-code') ;
  327. if(hasCode) {
  328. axios.get('ajax-validate-code-point-sale',{params: {
  329. idPointSale: idPointSale,
  330. code: this.pointsSaleCodes[idPointSale]
  331. }}).then(function(response) {
  332. if(response.data) {
  333. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = false ;
  334. app.validatePointSale(idPointSale) ;
  335. }
  336. else {
  337. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = true ;
  338. Vue.set(app.pointsSaleCodes, idPointSale, '');
  339. }
  340. }) ;
  341. }
  342. else {
  343. this.validatePointSale(idPointSale) ;
  344. }
  345. },
  346. validatePointSale: function(idPointSale) {
  347. this.pointSaleActive = this.getPointSale(idPointSale) ;
  348. if(this.pointSaleActive.credit_functioning == 'mandatory' || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active)) {
  349. this.useCredit = true ;
  350. }
  351. this.nextStep() ;
  352. },
  353. productQuantityClick: function(product, quantity) {
  354. if( this.products[product.index].quantity_form + quantity >= 0 &&
  355. (this.products[product.index].quantity_form + quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit) ||
  356. !this.products[product.index].quantity_max)
  357. ) {
  358. this.products[product.index].quantity_form += quantity ;
  359. }
  360. },
  361. oneProductOrdered: function() {
  362. for(var key in this.products) {
  363. if(this.products[key].quantity_form > 0) {
  364. return true ;
  365. }
  366. }
  367. return false ;
  368. },
  369. countProductOrdered: function() {
  370. var count = 0 ;
  371. for(var key in this.products) {
  372. if(this.products[key].quantity_form > 0) {
  373. if(this.products[key].unit != 'piece') {
  374. count ++ ;
  375. }
  376. else {
  377. count += this.products[key].quantity_form ;
  378. }
  379. }
  380. }
  381. return count ;
  382. },
  383. priceTotal: function(format) {
  384. var price = 0 ;
  385. for(var key in this.products) {
  386. if(this.products[key].quantity_form > 0) {
  387. price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price_with_tax ;
  388. }
  389. }
  390. if(format) {
  391. return this.formatPrice(price) ;
  392. }
  393. else {
  394. return price ;
  395. }
  396. },
  397. confirmClick: function() {
  398. var app = this ;
  399. // delivery
  400. if(app.delivery && !app.deliveryAddress) {
  401. this.errors = [] ;
  402. this.errors.push('Veuillez saisir une adresse de livraison') ;
  403. return false ;
  404. }
  405. // guest form
  406. var $signupGuestForm = $('#signup-guest form') ;
  407. if($signupGuestForm.length > 0 && !$signupGuestForm.valid()) {
  408. $signupGuestForm.submit() ;
  409. return false ;
  410. }
  411. var user = false ;
  412. if(this.producer.option_allow_order_guest && !this.user) {
  413. user = {
  414. email: $('#signupguest-email').val(),
  415. password: $('#signupguest-password').val(),
  416. firstname: $('#signupguest-firstname').val(),
  417. lastname: $('#signupguest-lastname').val(),
  418. phone: $('#signupguest-phone').val(),
  419. } ;
  420. }
  421. // products
  422. var productsArray = {} ;
  423. for(var key in this.products) {
  424. if( this.products[key].quantity_form != null &&
  425. this.products[key].quantity_form > 0) {
  426. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  427. }
  428. }
  429. app.disableConfirmButton = true ;
  430. axios.post('ajax-process', {
  431. Order: {
  432. id_distribution : this.distribution.id,
  433. id_point_sale: this.pointSaleActive.id,
  434. comment: this.comment,
  435. delivery_home: this.delivery,
  436. delivery_address: this.deliveryAddress
  437. },
  438. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  439. products: productsArray,
  440. use_credit: Number(this.useCredit),
  441. user: user
  442. }).then(function(response) {
  443. if(response.data.status == 'success') {
  444. app.errors = [] ;
  445. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  446. }
  447. else {
  448. app.errors = response.data.errors ;
  449. app.disableConfirmButton = false ;
  450. }
  451. });
  452. },
  453. checkProducts: function() {
  454. if(!this.oneProductOrdered()) {
  455. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  456. }
  457. },
  458. checkCreditLimit: function(order) {
  459. var total = this.priceTotal() ;
  460. if(order != null) {
  461. total = this.priceTotal() - order.amount_paid ;
  462. }
  463. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  464. },
  465. countProductsByCategory: function(category) {
  466. var count = 0 ;
  467. for(var i = 0 ; i < this.products.length ; i++) {
  468. if(this.products[i].id_product_category == category.id) {
  469. count ++ ;
  470. }
  471. }
  472. return count ;
  473. },
  474. countSelectedProductsByCategory: function(category) {
  475. var count = 0 ;
  476. for(var key in this.products) {
  477. if(this.products[key].quantity_form > 0 && this.products[key].id_product_category == category.id) {
  478. count ++ ;
  479. }
  480. }
  481. return count ;
  482. },
  483. setCategoryCurrent: function(category) {
  484. if(this.categoryCurrent && this.categoryCurrent.id == category.id) {
  485. this.categoryCurrent = null ;
  486. }
  487. else {
  488. this.categoryCurrent = category ;
  489. }
  490. }
  491. },
  492. computed : {
  493. orderedPointsSale: function() {
  494. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  495. if(a.order > b.order) {
  496. return 1 ;
  497. }
  498. else {
  499. return -1 ;
  500. }
  501. }) ;
  502. return orderedPointsSaleArray ;
  503. }
  504. },
  505. updated: function () {
  506. var app = this;
  507. this.$nextTick(function () {
  508. if(app.step == 'payment' && !app.user && app.producer.option_allow_order_guest) {
  509. $("#signup-guest form").validate({
  510. rules: {
  511. 'SignupForm[email]': {
  512. 'email': true,
  513. 'required': true,
  514. 'minlength': 8,
  515. 'maxlength': 255
  516. },
  517. 'SignupForm[password]': {
  518. 'required': true,
  519. 'minlength': 8,
  520. "maxlength": 255
  521. },
  522. 'SignupForm[firstname]': {
  523. 'required': true,
  524. 'minlength': 2,
  525. "maxlength": 255
  526. },
  527. 'SignupForm[lastname]': {
  528. 'required': true,
  529. 'minlength': 2,
  530. "maxlength": 255
  531. },
  532. 'SignupForm[phone]': {
  533. 'required': true,
  534. 'minlength': 2,
  535. "maxlength": 255
  536. },
  537. },
  538. messages: {
  539. 'SignupForm[email]' : {
  540. 'required': 'Ce champs est requis.',
  541. 'email' : 'Email invalide.'
  542. },
  543. 'SignupForm[password]' : {
  544. 'required': 'Ce champs est requis.',
  545. },
  546. 'SignupForm[firstname]' : {
  547. 'required': 'Ce champs est requis.',
  548. },
  549. 'SignupForm[lastname]' : {
  550. 'required': 'Ce champs est requis.',
  551. },
  552. 'SignupForm[phone]' : {
  553. 'required': 'Ce champs est requis.',
  554. },
  555. }
  556. }) ;
  557. }
  558. });
  559. }
  560. });
  561. Vue.component('step-date',{
  562. props: [
  563. 'step',
  564. 'pointSaleActive',
  565. 'dateFormat',
  566. 'changeStep',
  567. 'producer',
  568. 'first',
  569. ],
  570. data: function() {
  571. return {
  572. } ;
  573. },
  574. template: '#template-step-date',
  575. methods: {
  576. }
  577. }) ;
  578. Vue.component('step-point-sale',{
  579. props: [
  580. 'step',
  581. 'pointSaleActive',
  582. 'changeStep',
  583. 'producer',
  584. 'first',
  585. ],
  586. data: function() {
  587. return {
  588. } ;
  589. },
  590. template: '#template-step-point-sale',
  591. methods: {
  592. }
  593. }) ;