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.

317 lines
12KB

  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. date: null,
  9. dateFormat: null,
  10. distributions: [],
  11. distribution: null,
  12. pointsSale: [],
  13. pointSaleActive: null,
  14. pointsSaleCodes: [],
  15. products: [],
  16. comment: '',
  17. creditCheckbox: false,
  18. credit: 0,
  19. errors: [],
  20. disableConfirmButton: false,
  21. calendar: {
  22. mode: 'single',
  23. attrs: [],
  24. availableDates: [],
  25. themeStyles: {
  26. wrapper: {
  27. background: '#F7F7F7',
  28. color: '#333',
  29. border: 'solid 1px #e0e0e0'
  30. },
  31. header: {
  32. padding: '10px 10px',
  33. },
  34. headerHorizontalDivider: {
  35. borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
  36. width: '80%',
  37. },
  38. weekdays: {
  39. color: 'gray',
  40. fontWeight: '600',
  41. padding: '10px 10px',
  42. fontSize: '2rem'
  43. },
  44. weeks: {
  45. padding: '0 15px 15px 15px',
  46. },
  47. dayContent: function(object) {
  48. var style = {
  49. fontSize: '1.5rem',
  50. padding: '20px',
  51. };
  52. return style ;
  53. },
  54. },
  55. formats: {
  56. dayPopover: 'DD/MM/YYYY'
  57. }
  58. },
  59. },
  60. mounted: function() {
  61. if($('#order-distribution-date').size()) {
  62. this.date = new Date($('#order-distribution-date').html()) ;
  63. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  64. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  65. + this.date.getFullYear() ;
  66. this.changeStep('point-sale') ;
  67. }
  68. this.init() ;
  69. this.loadingInit = false ;
  70. },
  71. methods: {
  72. getDate: function() {
  73. return this.formatDate(this.date) ;
  74. },
  75. formatDate: function(date) {
  76. if(date) {
  77. return date.getFullYear() + '-'
  78. + ('0' + (date.getMonth() +1)).slice(-2) + '-'
  79. + ('0' + date.getDate()).slice(-2) ;
  80. }
  81. return false ;
  82. },
  83. formatPrice: function(price) {
  84. var isNumberRegExp = new RegExp(/^[-+]?[0-9]+(\.[0-9]+)*$/);
  85. if(isNumberRegExp.test(price) && price > 0) {
  86. return Number(price).toFixed(2).replace('.',',')+' €' ;
  87. }
  88. return '--' ;
  89. },
  90. getPointSale: function(idPointSale) {
  91. for(var key in this.pointsSale) {
  92. if(this.pointsSale[key].id == idPointSale) {
  93. return this.pointsSale[key] ;
  94. }
  95. }
  96. },
  97. init: function() {
  98. var app = this ;
  99. this.loading = true ;
  100. axios.get("ajax-infos",{params: {date : this.getDate()}})
  101. .then(function(response) {
  102. app.producer = response.data.producer ;
  103. app.credit = response.data.credit ;
  104. app.calendar.attrs = [] ;
  105. app.calendar.availableDates = [] ;
  106. var distributions = response.data.distributions ;
  107. if(distributions.length) {
  108. app.distributions = distributions ;
  109. var arrayDate ;
  110. for(var i= 0; i < distributions.length; i++) {
  111. app.calendar.attrs.push({
  112. highlight: {
  113. backgroundColor: '#5cb85c',
  114. },
  115. contentStyle: {
  116. color: 'white',
  117. },
  118. dates: distributions[i].date,
  119. }) ;
  120. arrayDate = distributions[i].date.split('-') ;
  121. app.calendar.availableDates.push({
  122. start: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]),
  123. end: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2])
  124. }) ;
  125. }
  126. }
  127. var orders = [] ;
  128. if(response.data.orders) {
  129. orders = response.data.orders ;
  130. }
  131. if(orders.length) {
  132. for(var i= 0; i < orders.length; i++) {
  133. app.calendar.attrs.push({
  134. highlight: {
  135. backgroundColor: '#FF7F00'
  136. },
  137. contentStyle: {
  138. color: 'white'
  139. },
  140. popover: {
  141. label: orders[i].pointSale.name + ' / '+app.formatPrice(orders[i].amount_total),
  142. hideIndicator: true
  143. },
  144. dates: orders[i].date_distribution,
  145. }) ;
  146. }
  147. }
  148. if(response.data.distribution) {
  149. app.distribution = response.data.distribution ;
  150. }
  151. if(response.data.points_sale) {
  152. app.pointsSale = [] ;
  153. for(var key in response.data.points_sale) {
  154. app.pointsSale[response.data.points_sale[key].id] = response.data.points_sale[key] ;
  155. app.pointsSaleCodes[response.data.points_sale[key].id] = '' ;
  156. Vue.set(app.pointsSaleCodes, response.data.points_sale[key].id, '');
  157. }
  158. }
  159. if(response.data.products) {
  160. app.products = response.data.products ;
  161. }
  162. app.order = null ;
  163. if(response.data.order) {
  164. app.order = response.data.order ;
  165. app.pointSaleActive = app.getPointSale(response.data.order.id_point_sale) ;
  166. }
  167. else {
  168. app.pointSaleActive = null ;
  169. }
  170. app.loading = false ;
  171. });
  172. },
  173. changeStep: function(step) {
  174. this.errors = [] ;
  175. var oldStep = this.step ;
  176. if(oldStep == 'products' && step == 'payment') {
  177. this.checkProducts() ;
  178. }
  179. if(!this.errors.length) {
  180. this.step = step ;
  181. window.scroll(0, $('#page-title').position().top - 25) ;
  182. if(oldStep == 'date' && step == 'point-sale') {
  183. this.init() ;
  184. }
  185. }
  186. },
  187. dayClick: function(day) {
  188. if(this.isAvailableDate(day.date)) {
  189. this.date = day.date ;
  190. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  191. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  192. + this.date.getFullYear() ;
  193. this.init() ;
  194. this.changeStep('point-sale') ;
  195. }
  196. },
  197. isAvailableDate: function(date) {
  198. for(var key in this.calendar.availableDates) {
  199. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  200. return true ;
  201. }
  202. }
  203. return false ;
  204. },
  205. pointSaleClick: function(event) {
  206. var app = this ;
  207. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  208. var hasCode = event.currentTarget.getAttribute('data-code') ;
  209. if(hasCode) {
  210. axios.get('ajax-validate-code-point-sale',{params: {
  211. idPointSale: idPointSale,
  212. code: this.pointsSaleCodes[idPointSale]
  213. }}).then(function(response) {
  214. if(response.data) {
  215. app.pointsSale[idPointSale].invalid_code = false ;
  216. app.validatePointSale(idPointSale) ;
  217. }
  218. else {
  219. app.pointsSale[idPointSale].invalid_code = true ;
  220. Vue.set(app.pointsSaleCodes, idPointSale, '');
  221. }
  222. }) ;
  223. }
  224. else {
  225. this.validatePointSale(idPointSale) ;
  226. }
  227. },
  228. validatePointSale: function(idPointSale) {
  229. this.pointSaleActive = this.getPointSale(idPointSale) ;
  230. this.changeStep('products') ;
  231. },
  232. productQuantityClick: function(product, quantity) {
  233. if( this.products[product.index].quantity_form + quantity >= 0 &&
  234. (this.products[product.index].quantity_form + quantity <= this.products[product.index].quantity_remaining ||
  235. !this.products[product.index].quantity_max)
  236. ) {
  237. this.products[product.index].quantity_form += quantity ;
  238. }
  239. },
  240. oneProductOrdered: function() {
  241. for(var key in this.products) {
  242. if(this.products[key].quantity_form > 0) {
  243. return true ;
  244. }
  245. }
  246. return false ;
  247. },
  248. countProductOrdered: function() {
  249. var count = 0 ;
  250. for(var key in this.products) {
  251. if(this.products[key].quantity_form > 0) {
  252. count += this.products[key].quantity_form ;
  253. }
  254. }
  255. return count ;
  256. },
  257. priceTotal: function(format) {
  258. var price = 0 ;
  259. for(var key in this.products) {
  260. if(this.products[key].quantity_form > 0) {
  261. price += this.products[key].quantity_form * this.products[key].price ;
  262. }
  263. }
  264. if(format) {
  265. return this.formatPrice(price) ;
  266. }
  267. else {
  268. return price ;
  269. }
  270. },
  271. confirmClick: function() {
  272. this.disableConfirmButton = true ;
  273. var productsArray = {} ;
  274. for(var key in this.products) {
  275. if( this.products[key].quantity_form != null &&
  276. this.products[key].quantity_form > 0) {
  277. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  278. }
  279. }
  280. axios.post('ajax-process', {
  281. Order: {
  282. id_distribution : this.distribution.id,
  283. id_point_sale: this.pointSaleActive.id,
  284. comment: this.comment
  285. },
  286. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  287. products: productsArray
  288. }).then(function(response) {
  289. if(response.data.status == 'success') {
  290. window.location.href = chat_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  291. }
  292. });
  293. },
  294. checkProducts: function() {
  295. if(!this.oneProductOrdered()) {
  296. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  297. }
  298. }
  299. }
  300. });