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.

381 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(updateOnlyProducts) {
  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. if(!updateOnlyProducts) {
  113. app.producer = response.data.producer;
  114. app.user = response.data.user;
  115. app.useCredit = response.data.producer.use_credit_checked_default;
  116. app.calendar.attrs = [];
  117. app.calendar.availableDates = [];
  118. var distributions = response.data.distributions;
  119. if (distributions.length) {
  120. app.distributions = distributions;
  121. var arrayDate;
  122. for (var i = 0; i < distributions.length; i++) {
  123. app.calendar.attrs.push({
  124. highlight: {
  125. backgroundColor: '#5cb85c',
  126. },
  127. contentStyle: {
  128. color: 'white',
  129. },
  130. dates: distributions[i].date,
  131. });
  132. arrayDate = distributions[i].date.split('-');
  133. app.calendar.availableDates.push({
  134. start: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]),
  135. end: new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2])
  136. });
  137. }
  138. }
  139. var orders = [];
  140. if (response.data.orders) {
  141. orders = response.data.orders;
  142. }
  143. if (orders.length) {
  144. for (var i = 0; i < orders.length; i++) {
  145. arrayDate = orders[i].date_distribution.split('-');
  146. var dateOrder = new Date(arrayDate[0], arrayDate[1] - 1, arrayDate[2]);
  147. if (app.isAvailableDate(dateOrder)) {
  148. app.calendar.attrs.push({
  149. highlight: {
  150. backgroundColor: '#FF7F00'
  151. },
  152. contentStyle: {
  153. color: 'white'
  154. },
  155. popover: {
  156. label: orders[i].pointSale.name + ' / ' + app.formatPrice(orders[i].amount_total),
  157. hideIndicator: true
  158. },
  159. dates: orders[i].date_distribution,
  160. });
  161. }
  162. }
  163. }
  164. if (response.data.distribution) {
  165. app.distribution = response.data.distribution;
  166. }
  167. if (response.data.points_sale) {
  168. app.pointsSale = [];
  169. var orderPointSale = 0;
  170. for (var key in response.data.points_sale) {
  171. response.data.points_sale[key].order = orderPointSale++;
  172. app.pointsSale[response.data.points_sale[key].id] = response.data.points_sale[key];
  173. app.pointsSaleCodes[response.data.points_sale[key].id] = '';
  174. Vue.set(app.pointsSaleCodes, response.data.points_sale[key].id, '');
  175. }
  176. }
  177. }
  178. if(response.data.products) {
  179. app.products = response.data.products ;
  180. }
  181. if(!updateOnlyProducts) {
  182. app.order = null ;
  183. if(response.data.order) {
  184. app.order = response.data.order ;
  185. app.pointSaleActive = app.getPointSale(response.data.order.id_point_sale) ;
  186. }
  187. else {
  188. app.pointSaleActive = null ;
  189. }
  190. }
  191. app.loading = false ;
  192. });
  193. },
  194. changeStep: function(step) {
  195. this.errors = [] ;
  196. var oldStep = this.step ;
  197. if(oldStep == 'products' && step == 'payment') {
  198. this.checkProducts() ;
  199. }
  200. if(!this.errors.length) {
  201. this.step = step ;
  202. window.scroll(0, $('#page-title').position().top - 25) ;
  203. if(oldStep == 'date' && step == 'point-sale') {
  204. this.init() ;
  205. }
  206. }
  207. },
  208. dayClickList: function(event) {
  209. var dateStr = event.currentTarget.getAttribute('data-distribution-date') ;
  210. var dateObject = new Date(dateStr.replace(/-/g, "/")) ;
  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. app.init(true) ;
  258. },
  259. validatePointSale: function(idPointSale) {
  260. this.pointSaleActive = this.getPointSale(idPointSale) ;
  261. if(this.pointSaleActive.credit_functioning == 'mandatory' || (this.pointSaleActive.credit_functioning == 'user' && this.user.credit_active)) {
  262. this.useCredit = true ;
  263. }
  264. this.changeStep('products') ;
  265. },
  266. productQuantityClick: function(product, quantity) {
  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. });