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.

475 lines
18KB

  1. var app = new Vue({
  2. el: '#app-order-order',
  3. data: {
  4. loading: false,
  5. loadingInit: true,
  6. step: 'date',
  7. producer: null,
  8. user: null,
  9. date: null,
  10. dateFormat: null,
  11. distributions: [],
  12. distribution: null,
  13. pointsSale: [],
  14. pointSaleActive: null,
  15. pointsSaleCodes: [],
  16. products: [],
  17. categories: [],
  18. comment: '',
  19. creditCheckbox: false,
  20. useCredit: false,
  21. errors: [],
  22. disableConfirmButton: false,
  23. calendar: {
  24. mode: 'single',
  25. attrs: [],
  26. availableDates: [],
  27. themeStyles: {
  28. wrapper: {
  29. background: '#F7F7F7',
  30. color: '#333',
  31. border: 'solid 1px #e0e0e0'
  32. },
  33. header: {
  34. padding: '10px 10px',
  35. },
  36. headerHorizontalDivider: {
  37. borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
  38. width: '80%',
  39. },
  40. weekdays: {
  41. color: 'gray',
  42. fontWeight: '600',
  43. padding: '10px 10px',
  44. fontSize: '2rem'
  45. },
  46. weeks: {
  47. padding: '0 15px 15px 15px',
  48. },
  49. dayContent: function(object) {
  50. var style = {
  51. fontSize: '1.5rem',
  52. padding: '20px',
  53. };
  54. return style ;
  55. },
  56. },
  57. formats: {
  58. dayPopover: 'DD/MM/YYYY'
  59. }
  60. },
  61. },
  62. mounted: function() {
  63. if($('#order-distribution-date').size() || $('#distribution-date').size()) {
  64. if($('#order-distribution-date').size()) {
  65. this.date = new Date($('#order-distribution-date').html()) ;
  66. }
  67. else {
  68. this.date = new Date($('#distribution-date').html()) ;
  69. }
  70. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  71. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  72. + this.date.getFullYear() ;
  73. this.changeStep('point-sale') ;
  74. }
  75. this.init() ;
  76. this.loadingInit = false ;
  77. },
  78. methods: {
  79. getDate: function() {
  80. return this.formatDate(this.date) ;
  81. },
  82. formatDate: function(date) {
  83. if(date) {
  84. return date.getFullYear() + '-'
  85. + ('0' + (date.getMonth() +1)).slice(-2) + '-'
  86. + ('0' + date.getDate()).slice(-2) ;
  87. }
  88. return false ;
  89. },
  90. formatPrice: formatPrice,
  91. getPointSale: function(idPointSale) {
  92. for(var key in this.pointsSale) {
  93. if(this.pointsSale[key].id == idPointSale) {
  94. return this.pointsSale[key] ;
  95. }
  96. }
  97. },
  98. getPointSaleKey: function(idPointSale) {
  99. for(var key in this.pointsSale) {
  100. if(this.pointsSale[key].id == idPointSale) {
  101. return key ;
  102. }
  103. }
  104. },
  105. init: function(updateOnlyProducts) {
  106. var app = this ;
  107. this.loading = true ;
  108. axios.get("ajax-infos",{params: {
  109. date : this.getDate(),
  110. pointSaleId: this.pointSaleActive ? this.pointSaleActive.id : 0
  111. }})
  112. .then(function(response) {
  113. if(!updateOnlyProducts) {
  114. app.producer = response.data.producer;
  115. app.user = response.data.user;
  116. app.useCredit = response.data.producer.use_credit_checked_default;
  117. app.calendar.attrs = [];
  118. app.calendar.availableDates = [];
  119. var distributions = response.data.distributions;
  120. if (distributions.length) {
  121. app.distributions = distributions;
  122. var arrayDate;
  123. for (var i = 0; i < distributions.length; i++) {
  124. app.calendar.attrs.push({
  125. highlight: {
  126. backgroundColor: '#5cb85c',
  127. },
  128. contentStyle: {
  129. color: 'white',
  130. },
  131. dates: distributions[i].date,
  132. });
  133. arrayDate = distributions[i].date.split('-');
  134. app.calendar.availableDates.push({
  135. start: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]),
  136. end: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2])
  137. });
  138. }
  139. }
  140. var orders = [];
  141. if (response.data.orders) {
  142. orders = response.data.orders;
  143. }
  144. if (orders.length) {
  145. for (var i = 0; i < orders.length; i++) {
  146. arrayDate = orders[i].date_distribution.split('-');
  147. var dateOrder = new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]);
  148. if (app.isAvailableDate(dateOrder)) {
  149. app.calendar.attrs.push({
  150. highlight: {
  151. backgroundColor: '#FF7F00'
  152. },
  153. contentStyle: {
  154. color: 'white'
  155. },
  156. popover: {
  157. label: orders[i].pointSale.name + ' / ' + app.formatPrice(orders[i].amount_total),
  158. hideIndicator: true
  159. },
  160. dates: orders[i].date_distribution,
  161. });
  162. }
  163. }
  164. }
  165. if (response.data.distribution) {
  166. app.distribution = response.data.distribution;
  167. }
  168. if (response.data.points_sale) {
  169. app.pointsSale = [];
  170. var orderPointSale = 0;
  171. for (var key in response.data.points_sale) {
  172. response.data.points_sale[key].order = orderPointSale++;
  173. app.pointsSale[response.data.points_sale[key].id] = response.data.points_sale[key];
  174. app.pointsSaleCodes[response.data.points_sale[key].id] = '';
  175. Vue.set(app.pointsSaleCodes, response.data.points_sale[key].id, '');
  176. }
  177. }
  178. }
  179. if(response.data.products) {
  180. app.products = response.data.products ;
  181. }
  182. if(response.data.categories) {
  183. app.categories = response.data.categories ;
  184. }
  185. if(!updateOnlyProducts) {
  186. app.order = null ;
  187. if(response.data.order) {
  188. app.order = response.data.order ;
  189. app.pointSaleActive = app.getPointSale(response.data.order.id_point_sale) ;
  190. }
  191. else {
  192. app.pointSaleActive = null ;
  193. }
  194. }
  195. app.loading = false ;
  196. });
  197. },
  198. changeStep: function(step) {
  199. this.errors = [] ;
  200. var oldStep = this.step ;
  201. if(oldStep == 'products' && step == 'payment') {
  202. this.checkProducts() ;
  203. }
  204. if(!this.errors.length) {
  205. this.step = step ;
  206. window.scroll(0, $('#page-title').position().top - 25) ;
  207. if(oldStep == 'date' && step == 'point-sale') {
  208. this.init() ;
  209. }
  210. }
  211. },
  212. dayClickList: function(event) {
  213. var dateStr = event.currentTarget.getAttribute('data-distribution-date') ;
  214. var dateObject = new Date(dateStr.replace(/-/g, "/")) ;
  215. if(this.isAvailableDate(dateObject)) {
  216. this.dayClickEvent(dateObject) ;
  217. }
  218. },
  219. dayClick: function(day) {
  220. if(this.isAvailableDate(day.date)) {
  221. this.dayClickEvent(day.date) ;
  222. }
  223. },
  224. dayClickEvent: function(date) {
  225. this.date = date ;
  226. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  227. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  228. + this.date.getFullYear() ;
  229. this.changeStep('point-sale') ;
  230. },
  231. isAvailableDate: function(date) {
  232. for(var key in this.calendar.availableDates) {
  233. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  234. return true ;
  235. }
  236. }
  237. return false ;
  238. },
  239. pointSaleClick: function(event) {
  240. var app = this ;
  241. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  242. var hasCode = event.currentTarget.getAttribute('data-code') ;
  243. if(hasCode) {
  244. axios.get('ajax-validate-code-point-sale',{params: {
  245. idPointSale: idPointSale,
  246. code: this.pointsSaleCodes[idPointSale]
  247. }}).then(function(response) {
  248. if(response.data) {
  249. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = false ;
  250. app.validatePointSale(idPointSale) ;
  251. }
  252. else {
  253. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = true ;
  254. Vue.set(app.pointsSaleCodes, idPointSale, '');
  255. }
  256. }) ;
  257. }
  258. else {
  259. this.validatePointSale(idPointSale) ;
  260. }
  261. app.init(true) ;
  262. },
  263. validatePointSale: function(idPointSale) {
  264. this.pointSaleActive = this.getPointSale(idPointSale) ;
  265. if(this.pointSaleActive.credit_functioning == 'mandatory' || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active)) {
  266. this.useCredit = true ;
  267. }
  268. this.changeStep('products') ;
  269. },
  270. productQuantityClick: function(product, quantity) {
  271. if( this.products[product.index].quantity_form + quantity >= 0 &&
  272. (this.products[product.index].quantity_form + quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit) ||
  273. !this.products[product.index].quantity_max)
  274. ) {
  275. this.products[product.index].quantity_form += quantity ;
  276. }
  277. },
  278. oneProductOrdered: function() {
  279. for(var key in this.products) {
  280. if(this.products[key].quantity_form > 0) {
  281. return true ;
  282. }
  283. }
  284. return false ;
  285. },
  286. countProductOrdered: function() {
  287. var count = 0 ;
  288. for(var key in this.products) {
  289. if(this.products[key].quantity_form > 0) {
  290. if(this.products[key].unit != 'piece') {
  291. count ++ ;
  292. }
  293. else {
  294. count += this.products[key].quantity_form ;
  295. }
  296. }
  297. }
  298. return count ;
  299. },
  300. priceTotal: function(format) {
  301. var price = 0 ;
  302. for(var key in this.products) {
  303. if(this.products[key].quantity_form > 0) {
  304. price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price_with_tax ;
  305. }
  306. }
  307. if(format) {
  308. return this.formatPrice(price) ;
  309. }
  310. else {
  311. return price ;
  312. }
  313. },
  314. confirmClick: function() {
  315. var app = this ;
  316. var $signupGuestForm = $('#signup-guest form') ;
  317. if($signupGuestForm.length > 0 && !$signupGuestForm.valid()) {
  318. $signupGuestForm.submit() ;
  319. return false ;
  320. }
  321. app.disableConfirmButton = true ;
  322. var productsArray = {} ;
  323. for(var key in this.products) {
  324. if( this.products[key].quantity_form != null &&
  325. this.products[key].quantity_form > 0) {
  326. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  327. }
  328. }
  329. var user = false ;
  330. if(this.producer.option_allow_order_guest && !this.user) {
  331. user = {
  332. email: $('#signupguest-email').val(),
  333. password: $('#signupguest-password').val(),
  334. firstname: $('#signupguest-firstname').val(),
  335. lastname: $('#signupguest-lastname').val(),
  336. phone: $('#signupguest-phone').val(),
  337. } ;
  338. }
  339. axios.post('ajax-process', {
  340. Order: {
  341. id_distribution : this.distribution.id,
  342. id_point_sale: this.pointSaleActive.id,
  343. comment: this.comment
  344. },
  345. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  346. products: productsArray,
  347. use_credit: Number(this.useCredit),
  348. user: user
  349. }).then(function(response) {
  350. if(response.data.status == 'success') {
  351. app.errors = [] ;
  352. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  353. }
  354. else {
  355. app.errors = response.data.errors ;
  356. app.disableConfirmButton = false ;
  357. }
  358. });
  359. },
  360. checkProducts: function() {
  361. if(!this.oneProductOrdered()) {
  362. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  363. }
  364. },
  365. checkCreditLimit: function(order) {
  366. var total = this.priceTotal() ;
  367. if(order != null) {
  368. total = this.priceTotal() - order.amount_paid ;
  369. }
  370. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  371. },
  372. countProductsByCategory: function(category) {
  373. var count = 0 ;
  374. for(var i = 0 ; i < this.products.length ; i++) {
  375. if(this.products[i].id_product_category == category.id) {
  376. count ++ ;
  377. }
  378. }
  379. return count ;
  380. }
  381. },
  382. computed : {
  383. orderedPointsSale: function() {
  384. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  385. if(a.order > b.order) {
  386. return 1 ;
  387. }
  388. else {
  389. return -1 ;
  390. }
  391. }) ;
  392. return orderedPointsSaleArray ;
  393. }
  394. },
  395. updated: function () {
  396. var app = this;
  397. this.$nextTick(function () {
  398. if(app.step == 'payment' && !app.user && app.producer.option_allow_order_guest) {
  399. $("#signup-guest form").validate({
  400. rules: {
  401. 'SignupForm[email]': {
  402. 'email': true,
  403. 'required': true,
  404. 'minlength': 8,
  405. 'maxlength': 255
  406. },
  407. 'SignupForm[password]': {
  408. 'required': true,
  409. 'minlength': 8,
  410. "maxlength": 255
  411. },
  412. 'SignupForm[firstname]': {
  413. 'required': true,
  414. 'minlength': 2,
  415. "maxlength": 255
  416. },
  417. 'SignupForm[lastname]': {
  418. 'required': true,
  419. 'minlength': 2,
  420. "maxlength": 255
  421. },
  422. 'SignupForm[phone]': {
  423. 'required': true,
  424. 'minlength': 2,
  425. "maxlength": 255
  426. },
  427. },
  428. messages: {
  429. 'SignupForm[email]' : {
  430. 'required': 'Ce champs est requis.',
  431. 'email' : 'Email invalide.'
  432. },
  433. 'SignupForm[password]' : {
  434. 'required': 'Ce champs est requis.',
  435. },
  436. 'SignupForm[firstname]' : {
  437. 'required': 'Ce champs est requis.',
  438. },
  439. 'SignupForm[lastname]' : {
  440. 'required': 'Ce champs est requis.',
  441. },
  442. 'SignupForm[phone]' : {
  443. 'required': 'Ce champs est requis.',
  444. },
  445. }
  446. }) ;
  447. }
  448. });
  449. }
  450. });