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.

274 lines
10KB

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