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.

374 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. dayClickList: function(event) {
  202. var dateStr = event.currentTarget.getAttribute('data-distribution-date') ;
  203. var dateObject = new Date(dateStr) ;
  204. if(this.isAvailableDate(dateObject)) {
  205. this.dayClickEvent(dateObject) ;
  206. }
  207. },
  208. dayClick: function(day) {
  209. if(this.isAvailableDate(day.date)) {
  210. this.dayClickEvent(day.date) ;
  211. }
  212. },
  213. dayClickEvent: function(date) {
  214. this.date = date ;
  215. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  216. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  217. + this.date.getFullYear() ;
  218. this.changeStep('point-sale') ;
  219. },
  220. isAvailableDate: function(date) {
  221. for(var key in this.calendar.availableDates) {
  222. if(date.getTime() == this.calendar.availableDates[key].start.getTime()) {
  223. return true ;
  224. }
  225. }
  226. return false ;
  227. },
  228. pointSaleClick: function(event) {
  229. var app = this ;
  230. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  231. var hasCode = event.currentTarget.getAttribute('data-code') ;
  232. if(hasCode) {
  233. axios.get('ajax-validate-code-point-sale',{params: {
  234. idPointSale: idPointSale,
  235. code: this.pointsSaleCodes[idPointSale]
  236. }}).then(function(response) {
  237. if(response.data) {
  238. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = false ;
  239. app.validatePointSale(idPointSale) ;
  240. }
  241. else {
  242. app.pointsSale[app.getPointSaleKey(idPointSale)].invalid_code = true ;
  243. Vue.set(app.pointsSaleCodes, idPointSale, '');
  244. }
  245. }) ;
  246. }
  247. else {
  248. this.validatePointSale(idPointSale) ;
  249. }
  250. },
  251. validatePointSale: function(idPointSale) {
  252. this.pointSaleActive = this.getPointSale(idPointSale) ;
  253. if(this.pointSaleActive.credit_functioning == 'mandatory' || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active)) {
  254. this.useCredit = true ;
  255. }
  256. this.changeStep('products') ;
  257. },
  258. productQuantityClick: function(product, quantity) {
  259. console.log(this.products[product.index].quantity_remaining) ;
  260. if( this.products[product.index].quantity_form + quantity >= 0 &&
  261. (this.products[product.index].quantity_form + quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit) ||
  262. !this.products[product.index].quantity_max)
  263. ) {
  264. this.products[product.index].quantity_form += quantity ;
  265. }
  266. },
  267. oneProductOrdered: function() {
  268. for(var key in this.products) {
  269. if(this.products[key].quantity_form > 0) {
  270. return true ;
  271. }
  272. }
  273. return false ;
  274. },
  275. countProductOrdered: function() {
  276. var count = 0 ;
  277. for(var key in this.products) {
  278. if(this.products[key].quantity_form > 0) {
  279. if(this.products[key].unit != 'piece') {
  280. count ++ ;
  281. }
  282. else {
  283. count += this.products[key].quantity_form ;
  284. }
  285. }
  286. }
  287. return count ;
  288. },
  289. priceTotal: function(format) {
  290. var price = 0 ;
  291. for(var key in this.products) {
  292. if(this.products[key].quantity_form > 0) {
  293. price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price_with_tax ;
  294. }
  295. }
  296. if(format) {
  297. return this.formatPrice(price) ;
  298. }
  299. else {
  300. return price ;
  301. }
  302. },
  303. confirmClick: function() {
  304. this.disableConfirmButton = true ;
  305. var productsArray = {} ;
  306. for(var key in this.products) {
  307. if( this.products[key].quantity_form != null &&
  308. this.products[key].quantity_form > 0) {
  309. productsArray[this.products[key].id] = this.products[key].quantity_form ;
  310. }
  311. }
  312. axios.post('ajax-process', {
  313. Order: {
  314. id_distribution : this.distribution.id,
  315. id_point_sale: this.pointSaleActive.id,
  316. comment: this.comment
  317. },
  318. code_point_sale: this.pointsSaleCodes[this.pointSaleActive.id],
  319. products: productsArray,
  320. use_credit: Number(this.useCredit)
  321. }).then(function(response) {
  322. if(response.data.status == 'success') {
  323. window.location.href = opendistrib_base_url(true)+'order/confirm?idOrder='+response.data.idOrder ;
  324. }
  325. });
  326. },
  327. checkProducts: function() {
  328. if(!this.oneProductOrdered()) {
  329. this.errors.push('Veuillez sélectionner au moins un produit.') ;
  330. }
  331. },
  332. checkCreditLimit: function(order) {
  333. var total = this.priceTotal() ;
  334. if(order != null) {
  335. total = this.priceTotal() - order.amount_paid ;
  336. }
  337. return this.producer.credit_limit == null || (this.producer.credit_limit != null && (this.user.credit - total >= this.producer.credit_limit)) ;
  338. }
  339. },
  340. computed : {
  341. orderedPointsSale: function() {
  342. var orderedPointsSaleArray = this.pointsSale.sort(function(a, b) {
  343. if(a.order > b.order) {
  344. return 1 ;
  345. }
  346. else {
  347. return -1 ;
  348. }
  349. }) ;
  350. return orderedPointsSaleArray ;
  351. }
  352. }
  353. });