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.

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