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.

365 lines
14KB

  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: formatPrice,
  90. getPointSale: function(idPointSale) {
  91. for(var key in this.pointsSale) {
  92. if(this.pointsSale[key].id == idPointSale) {
  93. return this.pointsSale[key] ;
  94. }
  95. }
  96. },
  97. getPointSaleKey: function(idPointSale) {
  98. for(var key in this.pointsSale) {
  99. if(this.pointsSale[key].id == idPointSale) {
  100. return 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.user = response.data.user ;
  111. app.useCredit = response.data.producer.use_credit_checked_default ;
  112. app.calendar.attrs = [] ;
  113. app.calendar.availableDates = [] ;
  114. var distributions = response.data.distributions ;
  115. if(distributions.length) {
  116. app.distributions = distributions ;
  117. var arrayDate ;
  118. for(var i= 0; i < distributions.length; i++) {
  119. app.calendar.attrs.push({
  120. highlight: {
  121. backgroundColor: '#5cb85c',
  122. },
  123. contentStyle: {
  124. color: 'white',
  125. },
  126. dates: distributions[i].date,
  127. }) ;
  128. arrayDate = distributions[i].date.split('-') ;
  129. app.calendar.availableDates.push({
  130. start: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]),
  131. end: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2])
  132. }) ;
  133. }
  134. }
  135. var orders = [] ;
  136. if(response.data.orders) {
  137. orders = response.data.orders ;
  138. }
  139. if(orders.length) {
  140. for(var i= 0; i < orders.length; i++) {
  141. arrayDate = orders[i].date_distribution.split('-') ;
  142. var dateOrder = new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]) ;
  143. if(app.isAvailableDate(dateOrder)) {
  144. app.calendar.attrs.push({
  145. highlight: {
  146. backgroundColor: '#FF7F00'
  147. },
  148. contentStyle: {
  149. color: 'white'
  150. },
  151. popover: {
  152. label: orders[i].pointSale.name + ' / '+app.formatPrice(orders[i].amount_total),
  153. hideIndicator: true
  154. },
  155. dates: orders[i].date_distribution,
  156. }) ;
  157. }
  158. }
  159. }
  160. if(response.data.distribution) {
  161. app.distribution = response.data.distribution ;
  162. }
  163. if(response.data.points_sale) {
  164. app.pointsSale = [] ;
  165. var orderPointSale = 0 ;
  166. for(var key in response.data.points_sale) {
  167. response.data.points_sale[key].order = orderPointSale ++ ;
  168. app.pointsSale[response.data.points_sale[key].id] = response.data.points_sale[key] ;
  169. app.pointsSaleCodes[response.data.points_sale[key].id] = '' ;
  170. Vue.set(app.pointsSaleCodes, response.data.points_sale[key].id, '');
  171. }
  172. }
  173. if(response.data.products) {
  174. app.products = response.data.products ;
  175. }
  176. app.order = null ;
  177. if(response.data.order) {
  178. app.order = response.data.order ;
  179. app.pointSaleActive = app.getPointSale(response.data.order.id_point_sale) ;
  180. }
  181. else {
  182. app.pointSaleActive = null ;
  183. }
  184. app.loading = false ;
  185. });
  186. },
  187. changeStep: function(step) {
  188. this.errors = [] ;
  189. var oldStep = this.step ;
  190. if(oldStep == 'products' && step == 'payment') {
  191. this.checkProducts() ;
  192. }
  193. if(!this.errors.length) {
  194. this.step = step ;
  195. window.scroll(0, $('#page-title').position().top - 25) ;
  196. if(oldStep == 'date' && step == 'point-sale') {
  197. this.init() ;
  198. }
  199. }
  200. },
  201. dayClick: function(day) {
  202. if(this.isAvailableDate(day.date)) {
  203. this.date = day.date ;
  204. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  205. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  206. + this.date.getFullYear() ;
  207. this.changeStep('point-sale') ;
  208. }
  209. },
  210. isAvailableDate: function(date) {
  211. for(var key in this.calendar.availableDates) {
  212. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  213. return true ;
  214. }
  215. }
  216. return false ;
  217. },
  218. pointSaleClick: function(event) {
  219. var app = this ;
  220. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  221. var hasCode = event.currentTarget.getAttribute('data-code') ;
  222. if(hasCode) {
  223. axios.get('ajax-validate-code-point-sale',{params: {
  224. idPointSale: idPointSale,
  225. code: this.pointsSaleCodes[idPointSale]
  226. }}).then(function(response) {
  227. if(response.data) {
  228. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = false ;
  229. app.validatePointSale(idPointSale) ;
  230. }
  231. else {
  232. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = true ;
  233. Vue.set(app.pointsSaleCodes, idPointSale, '');
  234. }
  235. }) ;
  236. }
  237. else {
  238. this.validatePointSale(idPointSale) ;
  239. }
  240. },
  241. validatePointSale: function(idPointSale) {
  242. this.pointSaleActive = this.getPointSale(idPointSale) ;
  243. if(this.pointSaleActive.credit_functioning == 'mandatory' || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active)) {
  244. this.useCredit = true ;
  245. }
  246. this.changeStep('products') ;
  247. },
  248. productQuantityClick: function(product, quantity) {
  249. console.log(this.products[product.index].quantity_remaining) ;
  250. if( this.products[product.index].quantity_form + quantity >= 0 &&
  251. (this.products[product.index].quantity_form + quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit) ||
  252. !this.products[product.index].quantity_max)
  253. ) {
  254. this.products[product.index].quantity_form += quantity ;
  255. }
  256. },
  257. oneProductOrdered: function() {
  258. for(var key in this.products) {
  259. if(this.products[key].quantity_form > 0) {
  260. return true ;
  261. }
  262. }
  263. return false ;
  264. },
  265. countProductOrdered: function() {
  266. var count = 0 ;
  267. for(var key in this.products) {
  268. if(this.products[key].quantity_form > 0) {
  269. if(this.products[key].unit != 'piece') {
  270. count ++ ;
  271. }
  272. else {
  273. count += this.products[key].quantity_form ;
  274. }
  275. }
  276. }
  277. return count ;
  278. },
  279. priceTotal: function(format) {
  280. var price = 0 ;
  281. for(var key in this.products) {
  282. if(this.products[key].quantity_form > 0) {
  283. price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price_with_tax ;
  284. }
  285. }
  286. if(format) {
  287. return this.formatPrice(price) ;
  288. }
  289. else {
  290. return price ;
  291. }
  292. },
  293. confirmClick: function() {
  294. this.disableConfirmButton = true ;
  295. var productsArray = {} ;
  296. for(var key in this.products) {
  297. if( this.products[key].quantity_form != null &&
  298. this.products[key].quantity_form > 0) {
  299. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  300. }
  301. }
  302. axios.post('ajax-process', {
  303. Order: {
  304. id_distribution : this.distribution.id,
  305. id_point_sale: this.pointSaleActive.id,
  306. comment: this.comment
  307. },
  308. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  309. products: productsArray,
  310. use_credit: Number(this.useCredit)
  311. }).then(function(response) {
  312. if(response.data.status == 'success') {
  313. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  314. }
  315. });
  316. },
  317. checkProducts: function() {
  318. if(!this.oneProductOrdered()) {
  319. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  320. }
  321. },
  322. checkCreditLimit: function(order) {
  323. var total = this.priceTotal() ;
  324. if(order != null) {
  325. total = this.priceTotal() - order.amount_paid ;
  326. }
  327. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  328. }
  329. },
  330. computed : {
  331. orderedPointsSale: function() {
  332. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  333. if(a.order > b.order) {
  334. return 1 ;
  335. }
  336. else {
  337. return -1 ;
  338. }
  339. }) ;
  340. return orderedPointsSaleArray ;
  341. }
  342. }
  343. });