Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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