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.

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