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.

321 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.init() ;
  195. this.changeStep('point-sale') ;
  196. }
  197. },
  198. isAvailableDate: function(date) {
  199. for(var key in this.calendar.availableDates) {
  200. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  201. return true ;
  202. }
  203. }
  204. return false ;
  205. },
  206. pointSaleClick: function(event) {
  207. var app = this ;
  208. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  209. var hasCode = event.currentTarget.getAttribute('data-code') ;
  210. if(hasCode) {
  211. axios.get('ajax-validate-code-point-sale',{params: {
  212. idPointSale: idPointSale,
  213. code: this.pointsSaleCodes[idPointSale]
  214. }}).then(function(response) {
  215. if(response.data) {
  216. app.pointsSale[idPointSale].invalid_code = false ;
  217. app.validatePointSale(idPointSale) ;
  218. }
  219. else {
  220. app.pointsSale[idPointSale].invalid_code = true ;
  221. Vue.set(app.pointsSaleCodes, idPointSale, '');
  222. }
  223. }) ;
  224. }
  225. else {
  226. this.validatePointSale(idPointSale) ;
  227. }
  228. },
  229. validatePointSale: function(idPointSale) {
  230. this.pointSaleActive = this.getPointSale(idPointSale) ;
  231. this.useCredit = true ;
  232. this.changeStep('products') ;
  233. },
  234. productQuantityClick: function(product, quantity) {
  235. if( this.products[product.index].quantity_form + quantity >= 0 &&
  236. (this.products[product.index].quantity_form + quantity <= this.products[product.index].quantity_remaining ||
  237. !this.products[product.index].quantity_max)
  238. ) {
  239. this.products[product.index].quantity_form += quantity ;
  240. }
  241. },
  242. oneProductOrdered: function() {
  243. for(var key in this.products) {
  244. if(this.products[key].quantity_form > 0) {
  245. return true ;
  246. }
  247. }
  248. return false ;
  249. },
  250. countProductOrdered: function() {
  251. var count = 0 ;
  252. for(var key in this.products) {
  253. if(this.products[key].quantity_form > 0) {
  254. count += this.products[key].quantity_form ;
  255. }
  256. }
  257. return count ;
  258. },
  259. priceTotal: function(format) {
  260. var price = 0 ;
  261. for(var key in this.products) {
  262. if(this.products[key].quantity_form > 0) {
  263. price += this.products[key].quantity_form * this.products[key].price ;
  264. }
  265. }
  266. if(format) {
  267. return this.formatPrice(price) ;
  268. }
  269. else {
  270. return price ;
  271. }
  272. },
  273. confirmClick: function() {
  274. this.disableConfirmButton = true ;
  275. var productsArray = {} ;
  276. for(var key in this.products) {
  277. if( this.products[key].quantity_form != null &&
  278. this.products[key].quantity_form > 0) {
  279. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  280. }
  281. }
  282. axios.post('ajax-process', {
  283. Order: {
  284. id_distribution : this.distribution.id,
  285. id_point_sale: this.pointSaleActive.id,
  286. comment: this.comment
  287. },
  288. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  289. products: productsArray,
  290. use_credit: Number(this.useCredit)
  291. }).then(function(response) {
  292. if(response.data.status == 'success') {
  293. window.location.href = chat_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  294. }
  295. });
  296. },
  297. checkProducts: function() {
  298. if(!this.oneProductOrdered()) {
  299. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  300. }
  301. }
  302. }
  303. });