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.

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