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.

297 lines
11KB

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