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.

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