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.

383 lines
15KB

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