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

6 years ago
6 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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. var app = new Vue({
  2. el: '#app-order-order',
  3. data() {
  4. return Object.assign({
  5. order: null,
  6. loading: false,
  7. loadingProducts: false,
  8. loadingInit: true,
  9. cancelTokenSource: null,
  10. step: null,
  11. producer: null,
  12. user: null,
  13. date: null,
  14. dateFormat: null,
  15. distributions: [],
  16. distribution: null,
  17. pointsSale: [],
  18. pointSaleActive: null,
  19. pointSaleActiveId: null,
  20. pointsSaleCodes: [],
  21. products: [],
  22. categories: [],
  23. categoryCurrent: null,
  24. comment: '',
  25. creditCheckbox: false,
  26. paymentMethod: 'onsite',
  27. useCredit: false,
  28. errors: [],
  29. disableConfirmButton: false,
  30. delivery: false,
  31. deliveryAddress: null,
  32. urlLogin: '#',
  33. producerOptionOrderEntryPoint: null,
  34. calendar: {
  35. columns: this.isMobile() ? 1 : 2,
  36. mode: 'single',
  37. attrs: [],
  38. availableDates: [],
  39. themeStyles: {
  40. wrapper: {
  41. background: '#F7F7F7',
  42. color: '#333',
  43. border: 'solid 1px #e0e0e0'
  44. },
  45. header: {
  46. padding: '10px 10px',
  47. },
  48. headerHorizontalDivider: {
  49. borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
  50. width: '80%',
  51. },
  52. weekdays: {
  53. color: 'gray',
  54. fontWeight: '600',
  55. padding: '10px 10px',
  56. fontSize: '2rem'
  57. },
  58. weeks: {
  59. padding: '0 15px 15px 15px',
  60. },
  61. dayContent: function (object) {
  62. var style = {
  63. fontSize: '1.4rem',
  64. padding: app.isMobile() ? '18px' : '20px',
  65. };
  66. return style;
  67. },
  68. },
  69. masks: {
  70. //dayPopover: 'DD/MM/YYYY'
  71. dayPopover: ''
  72. }
  73. }
  74. }, window.appInitValues);
  75. },
  76. mounted: function() {
  77. this.initDate();
  78. this.init('first', false);
  79. },
  80. methods: {
  81. initDate: function() {
  82. var dateDefined = $('#order-distribution-date').size() || $('#distribution-date').size() ;
  83. if(dateDefined && (this.producerOptionOrderEntryPoint != 'point-sale' || this.pointSaleActiveId)) {
  84. if($('#order-distribution-date').size()) {
  85. this.date = new Date($('#order-distribution-date').html()) ;
  86. }
  87. else {
  88. this.date = new Date($('#distribution-date').html()) ;
  89. }
  90. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  91. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  92. + this.date.getFullYear() ;
  93. }
  94. },
  95. getDate: function() {
  96. return this.formatDate(this.date) ;
  97. },
  98. formatDate: function(date) {
  99. if(date) {
  100. return date.getFullYear() + '-'
  101. + ('0' + (date.getMonth() +1)).slice(-2) + '-'
  102. + ('0' + date.getDate()).slice(-2) ;
  103. }
  104. return false ;
  105. },
  106. formatPrice: formatPrice,
  107. getPointSale: function(idPointSale) {
  108. for(var key in this.pointsSale) {
  109. if(this.pointsSale[key].id == idPointSale) {
  110. return this.pointsSale[key] ;
  111. }
  112. }
  113. },
  114. getPointSaleKey: function(idPointSale) {
  115. for(var key in this.pointsSale) {
  116. if(this.pointsSale[key].id == idPointSale) {
  117. return key ;
  118. }
  119. }
  120. },
  121. getProduct: function(idProduct) {
  122. for(var key in this.products) {
  123. if(this.products[key].id == idProduct) {
  124. return this.products[key] ;
  125. }
  126. }
  127. },
  128. init: function(type, loadingProducts) {
  129. var app = this ;
  130. if(loadingProducts) {
  131. if(app.cancelTokenSource !== null) {
  132. app.cancelTokenSource.cancel();
  133. }
  134. }
  135. else {
  136. this.loading = true ;
  137. }
  138. if(app.isChangeState('date', 'date', 'point-sale')) {
  139. app.pointSaleActive = null ;
  140. app.products = [] ;
  141. }
  142. app.cancelTokenSource = axios.CancelToken.source();
  143. axios.get("ajax-infos",{
  144. cancelToken: app.cancelTokenSource.token,
  145. params: {
  146. date : this.getDate(),
  147. pointSaleId: this.pointSaleActiveId ? this.pointSaleActiveId : (this.pointSaleActive ? this.pointSaleActive.id : 0),
  148. productsJson: this.getProductsArray(),
  149. loadingProducts: loadingProducts
  150. }})
  151. .catch(function (thrown) {
  152. if (axios.isCancel(thrown)) {
  153. //console.log('Request canceled', thrown.message);
  154. //return Promise.reject(thrown);
  155. }
  156. })
  157. .then(function(response) {
  158. if(response) {
  159. app.calendar.attrs = [];
  160. app.calendar.availableDates = [];
  161. var distributions = response.data.distributions;
  162. app.distributions = distributions;
  163. if (distributions.length) {
  164. var arrayDate;
  165. var highlightStyle = {
  166. style: {
  167. background: 'white',
  168. border: 'solid 2px #198754'
  169. },
  170. contentStyle: {
  171. color: '#198754'
  172. }
  173. };
  174. for (var i = 0; i < distributions.length; i++) {
  175. app.calendar.attrs.push({
  176. highlight: highlightStyle,
  177. dates: distributions[i].date
  178. });
  179. arrayDate = distributions[i].date.split('-');
  180. app.calendar.availableDates.push({
  181. highlight: highlightStyle,
  182. start: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]),
  183. end: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2])
  184. });
  185. }
  186. }
  187. if (response.data.leave_period) {
  188. leavePeriodStartDateArray = response.data.leave_period.start.split('-');
  189. leavePeriodEndDateArray = response.data.leave_period.end.split('-');
  190. app.calendar.attrs.push({
  191. highlight: {
  192. style: {
  193. //background: '#E09F3E'
  194. background: 'gray'
  195. },
  196. contentStyle: {
  197. color: 'white'
  198. }
  199. },
  200. dates: {
  201. start: new Date(leavePeriodStartDateArray[0], leavePeriodStartDateArray[1] - 1, leavePeriodStartDateArray[2]),
  202. end: new Date(leavePeriodEndDateArray[0], leavePeriodEndDateArray[1] - 1, leavePeriodEndDateArray[2])
  203. },
  204. popover: {
  205. label: 'En congé',
  206. hideIndicator: true,
  207. isInteractive: true
  208. },
  209. });
  210. }
  211. if (response.data.distribution) {
  212. app.distribution = response.data.distribution;
  213. }
  214. var orders = [];
  215. if (response.data.orders) {
  216. orders = response.data.orders;
  217. }
  218. if (orders.length) {
  219. for (var i = 0; i < orders.length; i++) {
  220. arrayDate = orders[i].date_distribution.split('-');
  221. var dateOrder = new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]);
  222. if (app.isAvailableDate(dateOrder)) {
  223. app.calendar.attrs.push({
  224. highlight: {
  225. style: {
  226. background: '#198754'
  227. },
  228. contentStyle: {
  229. color: 'white'
  230. }
  231. },
  232. popover: {
  233. label: orders[i].pointSale.name + ' (' + app.formatPrice(orders[i].amount_total) + ')',
  234. hideIndicator: true,
  235. isInteractive: true
  236. },
  237. dates: orders[i].date_distribution
  238. });
  239. }
  240. }
  241. }
  242. app.producer = response.data.producer;
  243. app.user = response.data.user;
  244. app.useCredit = response.data.producer.use_credit_checked_default;
  245. if (response.data.points_sale) {
  246. app.pointsSale = [];
  247. var orderPointSale = 0;
  248. for (var key in response.data.points_sale) {
  249. response.data.points_sale[key].order = orderPointSale++;
  250. app.pointsSale[response.data.points_sale[key].id] = response.data.points_sale[key];
  251. if (!app.pointsSaleCodes[response.data.points_sale[key].id]) {
  252. app.pointsSaleCodes[response.data.points_sale[key].id] = '';
  253. Vue.set(app.pointsSaleCodes, response.data.points_sale[key].id, '');
  254. }
  255. }
  256. }
  257. if (app.pointSaleActiveId) {
  258. app.pointSaleActive = app.getPointSale(app.pointSaleActiveId);
  259. }
  260. if (app.pointSaleActive) {
  261. if (app.producer.credit
  262. && app.pointSaleActive.payment_method_credit
  263. && (app.pointSaleActive.credit_functioning == 'mandatory'
  264. || (app.pointSaleActive.credit_functioning == 'user' && app.user.credit_active)
  265. || (app.pointSaleActive.credit_functioning == 'optional' && response.data.producer.use_credit_checked_default))) {
  266. app.paymentMethod = 'credit';
  267. } else if (app.pointSaleActive.payment_method_onsite) {
  268. app.paymentMethod = 'onsite';
  269. } else if (app.pointSaleActive.payment_method_online) {
  270. app.paymentMethod = 'online';
  271. }
  272. }
  273. if (app.isChangeState('point-sale', 'point-sale', 'date')) {
  274. app.date = null;
  275. app.dateFormat = null;
  276. }
  277. // update order
  278. var updateOrder = false;
  279. if (app.isChangeState('date', 'point-sale', 'products')
  280. || app.isChangeState('date', 'date', 'point-sale')
  281. || app.isChangeState('point-sale', 'date', 'products')
  282. || app.isChangeState('point-sale', 'point-sale', 'date')) {
  283. updateOrder = true;
  284. }
  285. if (updateOrder) {
  286. app.updateOrder(response);
  287. }
  288. if (type == 'first') {
  289. if (app.getDate() && app.pointSaleActive) {
  290. app.step = 'products';
  291. if (response.data.products) {
  292. app.products = response.data.products;
  293. }
  294. app.updateOrder(response);
  295. } else if (app.producer.option_order_entry_point == 'point-sale') {
  296. app.step = 'point-sale';
  297. } else if (app.getDate() && app.producer && app.producer.option_order_entry_point == 'date') {
  298. app.step = 'point-sale';
  299. } else {
  300. app.step = 'date';
  301. }
  302. }
  303. if (response.data.categories) {
  304. app.categories = response.data.categories;
  305. for (keyCategory in response.data.categories) {
  306. var category = response.data.categories[keyCategory];
  307. if (category.id && app.countProductsByCategory(category)) {
  308. app.setCategoryCurrent(category, true);
  309. break;
  310. }
  311. }
  312. }
  313. setTimeout(function () {
  314. app.responsive();
  315. opendistrib_products();
  316. }, 500);
  317. app.loading = false;
  318. app.loadingProducts = false;
  319. app.loadingInit = false;
  320. }
  321. });
  322. },
  323. updateOrder: function(response) {
  324. var app = this;
  325. if(response.data.products) {
  326. app.products = response.data.products;
  327. }
  328. app.order = null ;
  329. if(response.data.order) {
  330. app.order = response.data.order ;
  331. app.comment = app.order.comment ;
  332. if(app.order.delivery_address && app.order.delivery_address.length > 0) {
  333. app.deliveryAddress = app.order.delivery_address ;
  334. }
  335. app.pointSaleActive = app.getPointSale(response.data.order.id_point_sale) ;
  336. }
  337. else {
  338. app.comment = null ;
  339. app.deliveryAddress = null ;
  340. if(app.user.address && app.user.address.length > 0) {
  341. app.deliveryAddress = app.user.address ;
  342. }
  343. }
  344. },
  345. isMobile: function() {
  346. var width_window = parseInt($(window).width());
  347. return width_window <= 768;
  348. },
  349. responsive: function() {
  350. var app = this;
  351. app.responsiveApply();
  352. $(window).resize(function() {
  353. app.responsiveApply();
  354. });
  355. },
  356. responsiveApply: function() {
  357. var $td_summary = $('#products tr.total td.summary');
  358. if(this.isMobile()) {
  359. $td_summary.attr('colspan', 3);
  360. }
  361. else {
  362. $td_summary.attr('colspan', 4);
  363. }
  364. },
  365. isChangeState: function(entryPoint, oldStep, newStep) {
  366. return this.producer
  367. && entryPoint == this.producer.option_order_entry_point
  368. && oldStep == this.oldStep
  369. && newStep == this.step ;
  370. },
  371. nextStep: function() {
  372. this.errors = [] ;
  373. var oldStep = this.step ;
  374. var nextStep = null ;
  375. // par point de vente
  376. if(this.producer && this.producer.option_order_entry_point == 'point-sale') {
  377. if(oldStep == 'point-sale') {
  378. nextStep = 'date' ;
  379. }
  380. else if(oldStep == 'date') {
  381. nextStep = 'products' ;
  382. }
  383. }
  384. // par date
  385. else {
  386. if(oldStep == 'date') {
  387. nextStep = 'point-sale' ;
  388. }
  389. else if(oldStep == 'point-sale') {
  390. nextStep = 'products' ;
  391. }
  392. }
  393. if(nextStep) {
  394. this.changeStep(nextStep) ;
  395. }
  396. },
  397. changeStep: function(step) {
  398. this.errors = [] ;
  399. var oldStep = this.step ;
  400. this.oldStep = oldStep ;
  401. if(oldStep == 'products' && step == 'payment') {
  402. this.checkProducts() ;
  403. }
  404. if(!this.errors.length) {
  405. this.step = step ;
  406. this.init('basic', false) ;
  407. }
  408. },
  409. dayClickList: function(event) {
  410. var dateStr = event.currentTarget.getAttribute('data-distribution-date') ;
  411. var dateObject = new Date(dateStr.replace(/-/g, "/")) ;
  412. if(this.isAvailableDate(dateObject)) {
  413. this.dayClickEvent(dateObject) ;
  414. }
  415. },
  416. dayClick: function(day) {
  417. if(this.isAvailableDate(day.date)) {
  418. this.dayClickEvent(day.date) ;
  419. }
  420. },
  421. dayClickEvent: function(date) {
  422. this.date = date ;
  423. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  424. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  425. + this.date.getFullYear() ;
  426. this.nextStep() ;
  427. },
  428. isAvailableDate: function(date) {
  429. for(var key in this.calendar.availableDates) {
  430. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  431. return true ;
  432. }
  433. }
  434. return false ;
  435. },
  436. pointSaleClick: function(event) {
  437. var app = this ;
  438. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  439. var hasCode = event.currentTarget.getAttribute('data-code') ;
  440. if(hasCode) {
  441. axios.get('ajax-validate-code-point-sale',{params: {
  442. idPointSale: idPointSale,
  443. code: this.pointsSaleCodes[idPointSale]
  444. }}).then(function(response) {
  445. if(response.data) {
  446. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = false ;
  447. app.validatePointSale(idPointSale) ;
  448. }
  449. else {
  450. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = true ;
  451. Vue.set(app.pointsSaleCodes, idPointSale, '');
  452. }
  453. }) ;
  454. }
  455. else {
  456. this.validatePointSale(idPointSale) ;
  457. }
  458. },
  459. validatePointSale: function(idPointSale) {
  460. this.pointSaleActive = this.getPointSale(idPointSale) ;
  461. if(this.pointSaleActive.credit_functioning == 'mandatory' || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active)) {
  462. this.useCredit = true ;
  463. }
  464. this.nextStep() ;
  465. },
  466. productQuantityClick: function(product, quantity) {
  467. if(this.products[product.index].quantity_form + quantity >= 0
  468. && this.products[product.index].quantity_form + quantity <= this.products[product.index].quantity_max * this.products[product.index].coefficient_unit
  469. && (quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit)
  470. || this.products[product.index].quantity_remaining == null)
  471. ) {
  472. var theQuantity = parseFloat(this.products[product.index].quantity_form) + parseFloat(quantity);
  473. this.products[product.index].quantity_form = parseFloat(theQuantity.toFixed(2)) ;
  474. this.init('first', true);
  475. }
  476. },
  477. oneProductOrdered: function() {
  478. for(var key in this.products) {
  479. if(this.products[key].quantity_form > 0) {
  480. return true ;
  481. }
  482. }
  483. return false ;
  484. },
  485. countProductOrdered: function() {
  486. var count = 0 ;
  487. for(var key in this.products) {
  488. if(this.products[key].quantity_form > 0) {
  489. if(this.products[key].unit != 'piece') {
  490. count ++ ;
  491. }
  492. else {
  493. count += this.products[key].quantity_form ;
  494. }
  495. }
  496. }
  497. return count ;
  498. },
  499. priceTotal: function(format) {
  500. var price = 0 ;
  501. for(var key in this.products) {
  502. var quantity = this.products[key].quantity_form;
  503. if(quantity > 0) {
  504. price += (quantity / this.products[key].unit_coefficient) * this.getBestProductPrice(this.products[key].id, this.products[key].quantity_form);
  505. }
  506. }
  507. if(format) {
  508. return this.formatPrice(price) ;
  509. }
  510. else {
  511. return numberDecimals(price, 2) ;
  512. }
  513. },
  514. productHasPrice: function(product) {
  515. return product.prices && product.prices.length > 0;
  516. },
  517. productHasPriceWithFromQuantity: function(product) {
  518. if(this.productHasPrice(product)) {
  519. for(var i = 0; i < product.prices.length; i++) {
  520. if(product.prices[i].from_quantity > 0) {
  521. return true;
  522. }
  523. }
  524. }
  525. return false;
  526. },
  527. getBestProductPrice: function(idProduct, theQuantity) {
  528. var thePriceWithTax = 9999;
  529. var product = this.getProduct(idProduct);
  530. var pricesArray = product.prices;
  531. var unitCoefficient = product.unit_coefficient;
  532. if(theQuantity) {
  533. theQuantity = theQuantity / unitCoefficient;
  534. }
  535. for(var i = 0; i < pricesArray.length ; i++) {
  536. var priceWithTax = pricesArray[i].price_with_tax;
  537. var fromQuantity = pricesArray[i].from_quantity;
  538. if(priceWithTax < thePriceWithTax && fromQuantity <= theQuantity) {
  539. thePriceWithTax = priceWithTax;
  540. }
  541. }
  542. if(thePriceWithTax == 9999) {
  543. return 0;
  544. }
  545. else {
  546. return thePriceWithTax;
  547. }
  548. },
  549. isPaymentMethodOnsiteActive: function() {
  550. return this.pointSaleActive && this.pointSaleActive.payment_method_onsite
  551. && !this.isPaymentMethodCreditActiveFunctioningUser(true)
  552. && !this.isPaymentMethodCreditActiveFunctioningMandatory();
  553. },
  554. isPaymentMethodCreditActive: function() {
  555. return this.isPaymentMethodCreditActiveFunctioningOptional()
  556. || this.isPaymentMethodCreditActiveFunctioningMandatory()
  557. || this.isPaymentMethodCreditActiveFunctioningUser(true);
  558. },
  559. isPaymentMethodCreditActiveCheckBase: function() {
  560. return this.producer && this.producer.credit
  561. && this.pointSaleActive && this.pointSaleActive.payment_method_credit;
  562. },
  563. isPaymentMethodCreditActiveFunctioningUser: function(userCreditActive) {
  564. return this.isPaymentMethodCreditActiveCheckBase()
  565. && this.pointSaleActive.credit_functioning == 'user'
  566. && this.user && this.user.credit_active == userCreditActive;
  567. },
  568. isPaymentMethodCreditActiveFunctioningMandatory: function() {
  569. return this.isPaymentMethodCreditActiveCheckBase()
  570. && this.pointSaleActive.credit_functioning == 'mandatory';
  571. },
  572. isPaymentMethodCreditActiveFunctioningOptional: function() {
  573. return this.isPaymentMethodCreditActiveCheckBase()
  574. && this.pointSaleActive.credit_functioning == 'optional';
  575. },
  576. isPaymentMethodOnlineActive: function() {
  577. return this.producer && this.producer.online_payment
  578. && this.pointSaleActive && this.pointSaleActive.payment_method_online
  579. && !this.isPaymentMethodCreditActiveFunctioningUser(true)
  580. && !this.isPaymentMethodCreditActiveFunctioningMandatory()
  581. },
  582. errorCreditMandatoryAndLimit: function() {
  583. return this.user
  584. && this.pointSaleActive
  585. && this.pointSaleActive.payment_method_credit
  586. && (this.pointSaleActive.credit_functioning == 'mandatory'
  587. || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active))
  588. && !this.checkCreditLimit(this.order);
  589. },
  590. confirmClick: function() {
  591. var app = this ;
  592. // delivery
  593. if(app.pointSaleActive.is_home_delivery && !app.deliveryAddress) {
  594. this.errors = [] ;
  595. this.errors.push('Veuillez saisir une adresse de livraison.') ;
  596. opendistrib_scroll('page-title');
  597. return false ;
  598. }
  599. // montant minimum de commande
  600. if(app.pointSaleActive.minimum_order_amount > 0 && app.priceTotal() < app.pointSaleActive.minimum_order_amount) {
  601. this.errors = [] ;
  602. this.errors.push('Le montant minimum de commande est de '+app.formatPrice(app.pointSaleActive.minimum_order_amount)+' pour ce point de vente.') ;
  603. opendistrib_scroll('page-title');
  604. return false ;
  605. }
  606. // guest form
  607. var $signupGuestForm = $('#signup-guest form') ;
  608. if($signupGuestForm.length > 0 && !$signupGuestForm.valid()) {
  609. $signupGuestForm.submit() ;
  610. return false ;
  611. }
  612. var user = false ;
  613. if(this.producer.option_allow_order_guest && !this.user) {
  614. user = {
  615. email: $('#signupguest-email').val(),
  616. password: $('#signupguest-password').val(),
  617. firstname: $('#signupguest-firstname').val(),
  618. lastname: $('#signupguest-lastname').val(),
  619. phone: $('#signupguest-phone').val(),
  620. } ;
  621. }
  622. // products
  623. var productsArray = this.getProductsArray();
  624. app.disableConfirmButton = true ;
  625. axios.post('ajax-process', {
  626. Order: {
  627. id_distribution : this.distribution.id,
  628. id_point_sale: this.pointSaleActive.id,
  629. comment: this.comment,
  630. delivery_home: this.pointSaleActive.is_home_delivery,
  631. delivery_address: this.deliveryAddress
  632. },
  633. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  634. products: productsArray,
  635. payment_method: this.paymentMethod,
  636. user: user
  637. }).then(function(response) {
  638. if(response.data.status == 'success') {
  639. app.errors = [] ;
  640. if(response.data.redirect && response.data.redirect.length > 0) {
  641. window.location.href = response.data.redirect ;
  642. }
  643. else {
  644. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  645. }
  646. }
  647. else {
  648. app.errors = response.data.errors ;
  649. window.scroll(0, $('#page-title').offset().top) ;
  650. app.disableConfirmButton = false ;
  651. }
  652. });
  653. },
  654. getProductsArray: function() {
  655. var productsArray = {} ;
  656. for(var key in this.products) {
  657. if( this.products[key].quantity_form != null &&
  658. this.products[key].quantity_form > 0) {
  659. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  660. }
  661. }
  662. return productsArray;
  663. },
  664. checkProducts: function() {
  665. if(!this.oneProductOrdered()) {
  666. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  667. }
  668. },
  669. checkCreditLimit: function(order) {
  670. var total = this.priceTotal() ;
  671. if(order != null) {
  672. total = this.priceTotal() - order.amount_paid ;
  673. }
  674. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  675. },
  676. isProductAvailable: function(product) {
  677. return product.productDistribution && product.productDistribution[0] && product.productDistribution[0].active == 1;
  678. },
  679. countProductsByCategory: function(category) {
  680. var count = 0 ;
  681. for(var i = 0 ; i < this.products.length ; i++) {
  682. if(this.products[i].id_product_category == category.id && this.isProductAvailable(this.products[i])) {
  683. count ++ ;
  684. }
  685. }
  686. return count ;
  687. },
  688. countSelectedProductsByCategory: function(category) {
  689. var count = 0 ;
  690. for(var key in this.products) {
  691. if(this.products[key].quantity_form > 0 && this.products[key].id_product_category == category.id) {
  692. count ++ ;
  693. }
  694. }
  695. return count ;
  696. },
  697. setCategoryCurrent: function(category, first) {
  698. var idScroll = false;
  699. if(this.categoryCurrent && this.categoryCurrent.id == category.id && !first) {
  700. this.categoryCurrent = null ;
  701. }
  702. else {
  703. this.categoryCurrent = category ;
  704. if(!first) {
  705. idScroll = 'category'+this.categoryCurrent.id;
  706. }
  707. }
  708. setTimeout(function() {
  709. opendistrib_products();
  710. }, 500);
  711. if(idScroll) {
  712. setTimeout(opendistrib_scroll, 250, idScroll);
  713. }
  714. }
  715. },
  716. computed : {
  717. orderedPointsSale: function() {
  718. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  719. if(a.position > b.position) {
  720. return 1 ;
  721. }
  722. else {
  723. return -1 ;
  724. }
  725. }) ;
  726. return orderedPointsSaleArray ;
  727. }
  728. },
  729. updated: function () {
  730. var app = this;
  731. this.$nextTick(function () {
  732. if(app.step == 'products' && !app.user && app.producer.option_allow_order_guest) {
  733. $("#signup-guest form").validate({
  734. rules: {
  735. 'SignupForm[email]': {
  736. 'email': true,
  737. 'required': true,
  738. 'minlength': 8,
  739. 'maxlength': 255
  740. },
  741. 'SignupForm[password]': {
  742. 'required': true,
  743. 'minlength': 8,
  744. "maxlength": 255
  745. },
  746. 'SignupForm[firstname]': {
  747. 'required': true,
  748. 'minlength': 2,
  749. "maxlength": 255
  750. },
  751. 'SignupForm[lastname]': {
  752. 'required': true,
  753. 'minlength': 2,
  754. "maxlength": 255
  755. },
  756. 'SignupForm[phone]': {
  757. 'required': true,
  758. 'minlength': 2,
  759. "maxlength": 255
  760. },
  761. },
  762. messages: {
  763. 'SignupForm[email]' : {
  764. 'required': 'Ce champs est requis.',
  765. 'email' : 'Email invalide.'
  766. },
  767. 'SignupForm[password]' : {
  768. 'required': 'Ce champs est requis.',
  769. },
  770. 'SignupForm[firstname]' : {
  771. 'required': 'Ce champs est requis.',
  772. },
  773. 'SignupForm[lastname]' : {
  774. 'required': 'Ce champs est requis.',
  775. },
  776. 'SignupForm[phone]' : {
  777. 'required': 'Ce champs est requis.',
  778. },
  779. }
  780. }) ;
  781. }
  782. });
  783. }
  784. });
  785. Vue.component('step-date',{
  786. props: [
  787. 'step',
  788. 'pointSaleActive',
  789. 'dateFormat',
  790. 'changeStep',
  791. 'producer',
  792. 'first',
  793. ],
  794. data: function() {
  795. return {
  796. } ;
  797. },
  798. template: '#template-step-date',
  799. methods: {
  800. }
  801. }) ;
  802. Vue.component('step-point-sale',{
  803. props: [
  804. 'step',
  805. 'pointSaleActive',
  806. 'changeStep',
  807. 'producer',
  808. 'first',
  809. ],
  810. data: function() {
  811. return {
  812. } ;
  813. },
  814. template: '#template-step-point-sale',
  815. methods: {
  816. }
  817. }) ;