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.

order-order.js 11KB

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