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.

order-order.js 14KB

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