Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

341 Zeilen
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. this.changeStep('products') ;
  240. },
  241. productQuantityClick: function(product, quantity) {
  242. console.log(this.products[product.index].quantity_remaining) ;
  243. if( this.products[product.index].quantity_form + quantity >= 0 &&
  244. (this.products[product.index].quantity_form + quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit) ||
  245. !this.products[product.index].quantity_max)
  246. ) {
  247. this.products[product.index].quantity_form += quantity ;
  248. }
  249. },
  250. oneProductOrdered: function() {
  251. for(var key in this.products) {
  252. if(this.products[key].quantity_form > 0) {
  253. return true ;
  254. }
  255. }
  256. return false ;
  257. },
  258. countProductOrdered: function() {
  259. var count = 0 ;
  260. for(var key in this.products) {
  261. if(this.products[key].quantity_form > 0) {
  262. if(this.products[key].unit != 'piece') {
  263. count ++ ;
  264. }
  265. else {
  266. count += this.products[key].quantity_form ;
  267. }
  268. }
  269. }
  270. return count ;
  271. },
  272. priceTotal: function(format) {
  273. var price = 0 ;
  274. for(var key in this.products) {
  275. if(this.products[key].quantity_form > 0) {
  276. price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price ;
  277. }
  278. }
  279. if(format) {
  280. return this.formatPrice(price) ;
  281. }
  282. else {
  283. return price ;
  284. }
  285. },
  286. confirmClick: function() {
  287. this.disableConfirmButton = true ;
  288. var productsArray = {} ;
  289. for(var key in this.products) {
  290. if( this.products[key].quantity_form != null &&
  291. this.products[key].quantity_form > 0) {
  292. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  293. }
  294. }
  295. axios.post('ajax-process', {
  296. Order: {
  297. id_distribution : this.distribution.id,
  298. id_point_sale: this.pointSaleActive.id,
  299. comment: this.comment
  300. },
  301. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  302. products: productsArray,
  303. use_credit: Number(this.useCredit)
  304. }).then(function(response) {
  305. if(response.data.status == 'success') {
  306. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  307. }
  308. });
  309. },
  310. checkProducts: function() {
  311. if(!this.oneProductOrdered()) {
  312. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  313. }
  314. },
  315. checkCreditLimit: function(order) {
  316. var total = this.priceTotal() ;
  317. if(order != null) {
  318. total = this.priceTotal() - order.amount_paid ;
  319. }
  320. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  321. }
  322. }
  323. });