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.

466 line
17KB

  1. var app = new Vue({
  2. el: '#app-distribution-index',
  3. data: {
  4. baseUrl: $('meta[name=baseurl]').attr('content'),
  5. date: null,
  6. dateFormat: null,
  7. loading: true,
  8. distribution: {
  9. active: false,
  10. },
  11. oneDistributionWeekActive: false,
  12. products: [],
  13. countActiveProducts: 0,
  14. pointsSale: [],
  15. idActivePointSale: 0,
  16. idDefaultPointSale: 0,
  17. countActivePointsSale: 0,
  18. countOrdersByPointSale: [],
  19. orders: [],
  20. ordersUpdate: [],
  21. countOrders: 0,
  22. users: [],
  23. showModalProducts: false,
  24. showModalPointsSale: false,
  25. showModalFormOrderCreate: false,
  26. orderCreate: null,
  27. showModalFormOrderUpdate: false,
  28. idOrderUpdate: 0,
  29. showViewProduct: false,
  30. idOrderView: 0,
  31. showModalPayment: false,
  32. idOrderPayment: 0,
  33. showLoading: false,
  34. calendar: {
  35. mode: 'single',
  36. attrs: [],
  37. themeStyles: {
  38. wrapper: {
  39. background: '#F7F7F7',
  40. color: '#333',
  41. border: 'solid 1px #e0e0e0'
  42. },
  43. header: {
  44. padding: '10px 10px',
  45. },
  46. headerHorizontalDivider: {
  47. borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
  48. width: '80%',
  49. },
  50. weekdays: {
  51. color: '#e0e0e0',
  52. fontWeight: '600',
  53. padding: '10px 10px',
  54. fontSize: '2rem'
  55. },
  56. weeks: {
  57. padding: '0 15px 15px 15px',
  58. },
  59. dayContent: function(object) {
  60. var style = {
  61. fontSize: '2rem',
  62. padding: '16px',
  63. };
  64. if(object.isHovered || object.isFocus) {
  65. style.backgroundColor = '#F39C12' ;
  66. }
  67. return style ;
  68. },
  69. },
  70. formats: {
  71. dayPopover: 'DD/MM/YYYY'
  72. }
  73. },
  74. },
  75. mounted: function() {
  76. if($('#distribution-date').size()) {
  77. this.date = new Date($('#distribution-date').html()) ;
  78. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  79. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  80. + this.date.getFullYear() ;
  81. }
  82. this.init() ;
  83. this.loading = false ;
  84. },
  85. methods: {
  86. getDate: function() {
  87. return this.formatDate(this.date) ;
  88. },
  89. formatDate: function(date) {
  90. if(date) {
  91. return date.getFullYear() + '-'
  92. + ('0' + (date.getMonth() +1)).slice(-2) + '-'
  93. + ('0' + date.getDate()).slice(-2) ;
  94. }
  95. return false ;
  96. },
  97. init: function(idActivePointSale) {
  98. var app = this ;
  99. this.showLoading = true ;
  100. axios.get("ajax-infos",{params: {date : this.getDate()}})
  101. .then(function(response) {
  102. app.distribution = response.data.distribution ;
  103. app.products = response.data.products ;
  104. app.initCountActiveProducts() ;
  105. app.oneDistributionWeekActive = response.data.one_distribution_week_active ;
  106. app.countOrders = 0 ;
  107. if(response.data.orders) {
  108. app.orders = JSON.parse(JSON.stringify(response.data.orders)) ;
  109. app.ordersUpdate = JSON.parse(JSON.stringify(response.data.orders)) ;
  110. for(i=0 ; i < app.orders.length ; i++) {
  111. if(!app.orders[i].date_delete) {
  112. app.countOrders ++ ;
  113. }
  114. }
  115. }
  116. else {
  117. app.orders = [] ;
  118. }
  119. if(response.data.order_create) {
  120. app.orderCreate = response.data.order_create ;
  121. app.idDefaultPointSale = app.orderCreate.id_point_sale ;
  122. }
  123. if(response.data.points_sale) {
  124. app.pointsSale = response.data.points_sale ;
  125. app.initPointsSale(idActivePointSale) ;
  126. }
  127. else {
  128. app.pointsSale = [] ;
  129. }
  130. if(response.data.users) {
  131. app.users = response.data.users ;
  132. }
  133. app.calendar.attrs = [] ;
  134. var distributions = response.data.distributions ;
  135. if(distributions.length) {
  136. for(var i= 0; i < distributions.length; i++) {
  137. app.calendar.attrs.push({
  138. highlight: {
  139. backgroundColor: '#5cb85c',
  140. },
  141. contentStyle: {
  142. color: 'white',
  143. },
  144. dates: distributions[i].date,
  145. }) ;
  146. }
  147. }
  148. app.showLoading = false ;
  149. }) ;
  150. },
  151. initCountActiveProducts: function() {
  152. this.countActiveProducts = 0 ;
  153. for(var i= 0; i < this.products.length; i++) {
  154. if(this.products[i].productDistribution[0].active == 1) {
  155. this.countActiveProducts ++ ;
  156. }
  157. }
  158. },
  159. initPointsSale: function(idActivePointSale) {
  160. this.countActivePointsSale = 0 ;
  161. this.setIdActivePointSale(0) ;
  162. for(var i= 0; i < this.pointsSale.length; i++) {
  163. if(this.pointsSale[i].pointSaleDistribution[0].delivery == 1) {
  164. this.countActivePointsSale ++ ;
  165. this.setIdActivePointSale(this.pointsSale[i].id) ;
  166. }
  167. }
  168. if(this.countActivePointsSale > 1) {
  169. this.setIdActivePointSale(0) ;
  170. }
  171. if(idActivePointSale) {
  172. this.setIdActivePointSale(idActivePointSale) ;
  173. }
  174. this.countOrdersByPointSale = [] ;
  175. for(var i = 0; i < this.pointsSale.length ; i++) {
  176. this.countOrdersByPointSale[this.pointsSale[i].id] = 0 ;
  177. }
  178. for(var i = 0; i < this.orders.length ; i++) {
  179. this.countOrdersByPointSale[this.orders[i].id_point_sale] ++ ;
  180. }
  181. },
  182. dayClicked: function(day) {
  183. this.date = day.date ;
  184. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  185. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  186. + this.date.getFullYear() ;
  187. this.init() ;
  188. },
  189. productQuantityMaxChange: function(event) {
  190. axios.get("ajax-process-product-quantity-max",{params: {
  191. idDistribution: this.distribution.id,
  192. idProduct: event.currentTarget.getAttribute('data-id-product'),
  193. quantityMax: event.currentTarget.value
  194. }})
  195. .then(function(response) {
  196. }) ;
  197. },
  198. productActiveClick: function(event) {
  199. var idProduct = event.currentTarget.getAttribute('data-id-product') ;
  200. var activeProduct = event.currentTarget.getAttribute('data-active-product') ;
  201. axios.get("ajax-process-active-product",{params: {
  202. idDistribution: this.distribution.id,
  203. idProduct: idProduct,
  204. active: activeProduct
  205. }})
  206. .then(function(response) {
  207. }) ;
  208. for(i = 0 ; i < this.products.length ; i++) {
  209. if(this.products[i].id == idProduct) {
  210. this.products[i].productDistribution[0].active = activeProduct ;
  211. }
  212. }
  213. this.initCountActiveProducts() ;
  214. },
  215. pointSaleActiveClick: function(event) {
  216. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  217. var deliveryPointSale = event.currentTarget.getAttribute('data-delivery-point-sale') ;
  218. axios.get("ajax-process-active-point-sale",{params: {
  219. idDistribution: this.distribution.id,
  220. idPointSale: idPointSale,
  221. delivery: deliveryPointSale
  222. }})
  223. .then(function(response) {
  224. }) ;
  225. for(i = 0 ; i < this.pointsSale.length ; i++) {
  226. if(this.pointsSale[i].id == idPointSale) {
  227. this.pointsSale[i].pointSaleDistribution[0].delivery = deliveryPointSale ;
  228. }
  229. }
  230. this.initPointsSale() ;
  231. },
  232. activeDistribution: function(event) {
  233. var app = this ;
  234. axios.get("ajax-process-active-distribution",{params: {
  235. idDistribution: this.distribution.id,
  236. active: event.currentTarget.getAttribute('data-active')
  237. }})
  238. .then(function(response) {
  239. app.init() ;
  240. }) ;
  241. },
  242. activeWeekDistribution: function(event) {
  243. var app = this ;
  244. axios.get("ajax-process-active-week-distribution",{params: {
  245. date: this.date.getFullYear() + '-'
  246. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  247. + ('0' + this.date.getDate()).slice(-2),
  248. active: event.currentTarget.getAttribute('data-active')
  249. }})
  250. .then(function(response) {
  251. app.init() ;
  252. }) ;
  253. },
  254. pointSaleClick: function(event) {
  255. this.setIdActivePointSale(event.currentTarget.getAttribute('data-id-point-sale')) ;
  256. },
  257. setIdActivePointSale: function(id) {
  258. this.idActivePointSale = id ;
  259. if(!id) {
  260. this.orderCreate.id_point_sale = this.idDefaultPointSale ;
  261. }
  262. else {
  263. this.orderCreate.id_point_sale = id ;
  264. }
  265. },
  266. orderCreatedUpdated: function() {
  267. this.showModalFormOrderCreate = false ;
  268. this.showModalFormOrderUpdate = false ;
  269. this.init(this.idActivePointSale) ;
  270. },
  271. deleteOrderClick: function(event) {
  272. var app = this ;
  273. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  274. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-delete",{params: {
  275. idOrder: idOrder
  276. }})
  277. .then(function(response) {
  278. app.init(app.idActivePointSale) ;
  279. }) ;
  280. },
  281. updateOrderClick: function(event) {
  282. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  283. this.idOrderUpdate = idOrder ;
  284. this.showModalFormOrderUpdate = true ;
  285. },
  286. orderPaymentModalClick: function(event) {
  287. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  288. this.idOrderPayment = idOrder ;
  289. this.showModalPayment = true ;
  290. },
  291. orderPaymentClick: function(event) {
  292. var app = this ;
  293. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  294. if(!idOrder) {
  295. idOrder = this.idOrderPayment ;
  296. }
  297. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-payment",{params: {
  298. idOrder: idOrder,
  299. type: event.currentTarget.getAttribute('data-type'),
  300. amount: event.currentTarget.getAttribute('data-amount')
  301. }})
  302. .then(function(response) {
  303. app.init(app.idActivePointSale) ;
  304. }) ;
  305. },
  306. orderViewClick: function(event) {
  307. var currentIdOrderView = event.currentTarget.getAttribute('data-id-order') ;
  308. if(this.idOrderView == currentIdOrderView) {
  309. this.showViewProduct = !this.showViewProduct ;
  310. }
  311. else {
  312. this.showViewProduct = true ;
  313. this.idOrderView = currentIdOrderView ;
  314. }
  315. },
  316. closeModalProducts: function() {
  317. this.showModalProducts = false ;
  318. this.init(this.idActivePointSale) ;
  319. },
  320. cloneOrder: function(order) {
  321. var clone = Object.assign({}, order) ;
  322. clone.productOrder = {} ;
  323. for(var key in order.productOrder) {
  324. clone.productOrder[key] = order.productOrder[key] ;
  325. }
  326. return clone ;
  327. }
  328. },
  329. });
  330. Vue.component('modal', {
  331. template: '#modal-template'
  332. })
  333. Vue.component('order-form',{
  334. props: ['date', 'pointsSale', 'users', 'products', 'order'],
  335. data: function() {
  336. return {
  337. errors: [],
  338. idPointSale: 0,
  339. idUser: 0,
  340. username : '',
  341. comment: '',
  342. } ;
  343. },
  344. template: '#order-form-template',
  345. methods: {
  346. checkForm: function() {
  347. this.errors = [] ;
  348. var countProducts = 0 ;
  349. for(var key in this.order.productOrder) {
  350. if(this.order.productOrder[key] > 0) {
  351. countProducts ++ ;
  352. }
  353. }
  354. if(this.order.id_point_sale
  355. && (this.order.id_user || (this.order.username && this.order.username.length))
  356. && countProducts > 0) {
  357. return true ;
  358. }
  359. if(!this.order.id_point_sale) {
  360. this.errors.push('Veuillez sélectionner un point de vente') ;
  361. }
  362. if(!this.order.id_user && !this.order.username) {
  363. this.errors.push('Veuillez sélectionner ou saisir un utilisateur') ;
  364. }
  365. if(!countProducts) {
  366. this.errors.push('Veuillez sélectionner au moins un produit') ;
  367. }
  368. },
  369. submitFormCreate: function(event) {
  370. var app = this ;
  371. if(this.checkForm()) {
  372. var processCredit = event.currentTarget.getAttribute('data-process-credit') ;
  373. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-create",{params: {
  374. date: this.date.getFullYear() + '-'
  375. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  376. + ('0' + this.date.getDate()).slice(-2),
  377. idPointSale: this.order.id_point_sale,
  378. idUser: this.order.id_user,
  379. username: this.order.username,
  380. products: JSON.stringify(this.order.productOrder),
  381. comment: this.order.comment,
  382. processCredit: processCredit
  383. }})
  384. .then(function(response) {
  385. app.order.id_point_sale = 0 ;
  386. app.order.id_user = 0 ;
  387. app.order.username = '' ;
  388. app.order.comment = '' ;
  389. for(i=0 ; i<app.order.productOrder.length ; i++) {
  390. app.order.productOrder[i] = 0 ;
  391. }
  392. app.$emit('ordercreatedupdated') ;
  393. }) ;
  394. }
  395. },
  396. submitFormUpdate: function(event) {
  397. var app = this ;
  398. if(this.checkForm()) {
  399. var processCredit = event.currentTarget.getAttribute('data-process-credit') ;
  400. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-update",{params: {
  401. date: this.date.getFullYear() + '-'
  402. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  403. + ('0' + this.date.getDate()).slice(-2),
  404. idOrder: this.order.id,
  405. idPointSale: this.order.id_point_sale,
  406. idUser: this.order.id_user,
  407. username: ''+this.order.username,
  408. products: JSON.stringify(this.order.productOrder),
  409. comment: this.order.comment,
  410. processCredit: processCredit
  411. }})
  412. .then(function(response) {
  413. app.$emit('ordercreatedupdated') ;
  414. }) ;
  415. }
  416. },
  417. productQuantityClick: function(id_product, quantity) {
  418. if(this.order.productOrder[id_product] + quantity >= 0) {
  419. var theQuantity = this.order.productOrder[id_product] + quantity ;
  420. Vue.set(this.order.productOrder, id_product, theQuantity);
  421. }
  422. }
  423. }
  424. }) ;