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.

288 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 = response.data.orders ;
  120. if(orders.length) {
  121. for(var i= 0; i < orders.length; i++) {
  122. this.calendar.attrs.push({
  123. highlight: {
  124. backgroundColor: '#018548',
  125. },
  126. popover: {
  127. label: orders[i].pointSale.name + ' / '+this.formatPrice(orders[i].amount_total),
  128. hideIndicator: true
  129. },
  130. dates: orders[i].date_distribution,
  131. }) ;
  132. }
  133. }
  134. if(response.data.distribution) {
  135. this.distribution = response.data.distribution ;
  136. }
  137. if(response.data.points_sale) {
  138. this.pointsSale = [] ;
  139. for(var key in response.data.points_sale) {
  140. this.pointsSale[response.data.points_sale[key].id] = response.data.points_sale[key] ;
  141. this.pointsSaleCodes[response.data.points_sale[key].id] = '' ;
  142. Vue.set(this.pointsSaleCodes, response.data.points_sale[key].id, '');
  143. }
  144. }
  145. if(response.data.products) {
  146. this.products = response.data.products ;
  147. }
  148. this.order = null ;
  149. if(response.data.order) {
  150. this.order = response.data.order ;
  151. this.pointSaleActive = this.getPointSale(response.data.order.id_point_sale) ;
  152. }
  153. else {
  154. this.pointSaleActive = null ;
  155. }
  156. this.loading = false ;
  157. });
  158. },
  159. changeStep: function(step) {
  160. var oldStep = this.step ;
  161. this.step = step ;
  162. window.scroll(0, $('#page-title').position().top - 25) ;
  163. if(oldStep == 'date' && step == 'point-sale') {
  164. this.init() ;
  165. }
  166. },
  167. dayClick: function(day) {
  168. if(this.isAvailableDate(day.date)) {
  169. this.date = day.date ;
  170. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  171. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  172. + this.date.getFullYear() ;
  173. this.init() ;
  174. this.changeStep('point-sale') ;
  175. }
  176. },
  177. isAvailableDate: function(date) {
  178. for(var key in this.calendar.availableDates) {
  179. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  180. return true ;
  181. }
  182. }
  183. return false ;
  184. },
  185. pointSaleClick: function(event) {
  186. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  187. var hasCode = event.currentTarget.getAttribute('data-code') ;
  188. if(hasCode) {
  189. axios.get('ajax-validate-code-point-sale',{params: {
  190. idPointSale: idPointSale,
  191. code: this.pointsSaleCodes[idPointSale]
  192. }}).then(response => {
  193. if(response.data) {
  194. this.pointsSale[idPointSale].invalid_code = false ;
  195. this.validatePointSale(idPointSale) ;
  196. }
  197. else {
  198. this.pointsSale[idPointSale].invalid_code = true ;
  199. Vue.set(this.pointsSaleCodes, idPointSale, '');
  200. }
  201. }) ;
  202. }
  203. else {
  204. this.validatePointSale(idPointSale) ;
  205. }
  206. },
  207. validatePointSale: function(idPointSale) {
  208. this.pointSaleActive = this.getPointSale(idPointSale) ;
  209. this.changeStep('products') ;
  210. },
  211. productQuantityClick: function(product, quantity) {
  212. if( this.products[product.index].quantity_form + quantity >= 0 &&
  213. (this.products[product.index].quantity_form + quantity <= this.products[product.index].quantity_remaining ||
  214. !this.products[product.index].quantity_max)
  215. ) {
  216. this.products[product.index].quantity_form += quantity ;
  217. }
  218. },
  219. oneProductOrdered: function() {
  220. for(var key in this.products) {
  221. if(this.products[key].quantity_form > 0) {
  222. return true ;
  223. }
  224. }
  225. return false ;
  226. },
  227. countProductOrdered: function() {
  228. var count = 0 ;
  229. for(var key in this.products) {
  230. if(this.products[key].quantity_form > 0) {
  231. count += this.products[key].quantity_form ;
  232. }
  233. }
  234. return count ;
  235. },
  236. priceTotal: function(format) {
  237. var price = 0 ;
  238. for(var key in this.products) {
  239. if(this.products[key].quantity_form > 0) {
  240. price += this.products[key].quantity_form * this.products[key].price ;
  241. }
  242. }
  243. if(format) {
  244. return this.formatPrice(price) ;
  245. }
  246. else {
  247. return price ;
  248. }
  249. },
  250. confirmClick: function() {
  251. var productsArray = {} ;
  252. for(var key in this.products) {
  253. if( this.products[key].quantity_form != null &&
  254. this.products[key].quantity_form > 0) {
  255. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  256. }
  257. }
  258. axios.post('ajax-process', {
  259. Order: {
  260. id_distribution : this.distribution.id,
  261. id_point_sale: this.pointSaleActive.id,
  262. comment: this.comment
  263. },
  264. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  265. products: productsArray
  266. }).then(response => {
  267. if(response.data.status == 'success') {
  268. this.orderSuccess = true ;
  269. }
  270. });
  271. }
  272. }
  273. });