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.

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