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.

338 lines
13KB

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