Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

order-order.js 12KB

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