Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

656 lines
26KB

  1. /**
  2. Copyright distrib (2018)
  3. contact@opendistrib.net
  4. Ce logiciel est un programme informatique servant à aider les producteurs
  5. à distribuer leur production en circuits courts.
  6. Ce logiciel est régi par la licence CeCILL soumise au droit français et
  7. respectant les principes de diffusion des logiciels libres. Vous pouvez
  8. utiliser, modifier et/ou redistribuer ce programme sous les conditions
  9. de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  10. sur le site "http://www.cecill.info".
  11. En contrepartie de l'accessibilité au code source et des droits de copie,
  12. de modification et de redistribution accordés par cette licence, il n'est
  13. offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  14. seule une responsabilité restreinte pèse sur l'auteur du programme, le
  15. titulaire des droits patrimoniaux et les concédants successifs.
  16. A cet égard l'attention de l'utilisateur est attirée sur les risques
  17. associés au chargement, à l'utilisation, à la modification et/ou au
  18. développement et à la reproduction du logiciel par l'utilisateur étant
  19. donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  20. manipuler et qui le réserve donc à des développeurs et des professionnels
  21. avertis possédant des connaissances informatiques approfondies. Les
  22. utilisateurs sont donc invités à charger et tester l'adéquation du
  23. logiciel à leurs besoins dans des conditions permettant d'assurer la
  24. sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  25. à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  26. Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  27. pris connaissance de la licence CeCILL, et que vous en avez accepté les
  28. termes.
  29. */
  30. var app = new Vue({
  31. el: '#app-distribution-index',
  32. data: {
  33. UrlManager: UrlManager,
  34. baseUrl: $('meta[name=baseurl]').attr('content'),
  35. date: null,
  36. dateFormat: null,
  37. loading: true,
  38. distribution: {
  39. active: false,
  40. },
  41. producer: null,
  42. oneDistributionWeekActive: false,
  43. products: [],
  44. countActiveProducts: 0,
  45. pointsSale: [],
  46. meansPayment: [],
  47. idActivePointSale: 0,
  48. idDefaultPointSale: 0,
  49. countActivePointsSale: 0,
  50. countOrdersByPointSale: [],
  51. orders: [],
  52. ordersUpdate: [],
  53. countOrders: 0,
  54. users: [],
  55. deliveryNotes: [],
  56. showModalProducts: false,
  57. showModalPointsSale: false,
  58. showModalFormOrderCreate: false,
  59. orderCreate: null,
  60. showModalFormOrderUpdate: false,
  61. idOrderUpdate: 0,
  62. showViewProduct: false,
  63. idOrderView: 0,
  64. showModalPayment: false,
  65. idOrderPayment: 0,
  66. showLoading: false,
  67. tillerIsSynchro: false,
  68. checkboxSelectAllOrders: false,
  69. messageGenerateDeliveryNoteDisplayed: false,
  70. calendar: {
  71. mode: 'single',
  72. attrs: [],
  73. themeStyles: {
  74. wrapper: {
  75. background: '#F7F7F7',
  76. color: '#333',
  77. border: 'solid 1px #e0e0e0'
  78. },
  79. header: {
  80. padding: '10px 10px',
  81. },
  82. headerHorizontalDivider: {
  83. borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
  84. width: '80%',
  85. },
  86. weekdays: {
  87. color: '#e0e0e0',
  88. fontWeight: '600',
  89. padding: '10px 10px',
  90. fontSize: '2rem'
  91. },
  92. weeks: {
  93. padding: '0 15px 15px 15px',
  94. },
  95. dayContent: function(object) {
  96. var style = {
  97. fontSize: '2rem',
  98. padding: '16px',
  99. };
  100. if(object.isHovered || object.isFocus) {
  101. style.backgroundColor = '#F39C12' ;
  102. }
  103. return style ;
  104. },
  105. },
  106. formats: {
  107. dayPopover: 'DD/MM/YYYY'
  108. }
  109. },
  110. },
  111. mounted: function() {
  112. if($('#distribution-date').size()) {
  113. this.date = new Date($('#distribution-date').html()) ;
  114. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  115. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  116. + this.date.getFullYear() ;
  117. }
  118. this.init() ;
  119. this.loading = false ;
  120. },
  121. methods: {
  122. getDate: function() {
  123. return this.formatDate(this.date) ;
  124. },
  125. formatDate: function(date) {
  126. if(date) {
  127. return date.getFullYear() + '-'
  128. + ('0' + (date.getMonth() +1)).slice(-2) + '-'
  129. + ('0' + date.getDate()).slice(-2) ;
  130. }
  131. return false ;
  132. },
  133. init: function(idActivePointSale) {
  134. var app = this ;
  135. this.showLoading = true ;
  136. axios.get("ajax-infos",{params: {date : this.getDate()}})
  137. .then(function(response) {
  138. app.distribution = response.data.distribution ;
  139. app.producer = response.data.producer ;
  140. app.products = response.data.products ;
  141. app.initCountActiveProducts() ;
  142. app.meansPayment = response.data.means_payment ;
  143. app.oneDistributionWeekActive = response.data.one_distribution_week_active ;
  144. app.countOrders = 0 ;
  145. if(response.data.orders) {
  146. app.orders = JSON.parse(JSON.stringify(response.data.orders)) ;
  147. app.ordersUpdate = JSON.parse(JSON.stringify(response.data.orders)) ;
  148. for(i=0 ; i < app.orders.length ; i++) {
  149. if(!app.orders[i].date_delete) {
  150. app.countOrders ++ ;
  151. }
  152. }
  153. }
  154. else {
  155. app.orders = [] ;
  156. }
  157. if(response.data.order_create) {
  158. app.orderCreate = response.data.order_create ;
  159. app.idDefaultPointSale = app.orderCreate.id_point_sale ;
  160. }
  161. if(response.data.points_sale) {
  162. app.pointsSale = response.data.points_sale ;
  163. app.initPointsSale(idActivePointSale) ;
  164. }
  165. else {
  166. app.pointsSale = [] ;
  167. }
  168. if(response.data.users) {
  169. app.users = response.data.users ;
  170. }
  171. if(response.data.delivery_notes) {
  172. app.deliveryNotes = response.data.delivery_notes ;
  173. }
  174. app.tillerIsSynchro = response.data.tiller_is_synchro ;
  175. app.calendar.attrs = [] ;
  176. var distributions = response.data.distributions ;
  177. if(distributions.length) {
  178. for(var i= 0; i < distributions.length; i++) {
  179. app.calendar.attrs.push({
  180. highlight: {
  181. backgroundColor: '#5cb85c',
  182. },
  183. contentStyle: {
  184. color: 'white',
  185. },
  186. dates: distributions[i].date,
  187. }) ;
  188. }
  189. }
  190. app.showLoading = false ;
  191. app.checkboxSelectAllOrders = false ;
  192. let searchParams = new URLSearchParams(window.location.search) ;
  193. if(searchParams.has('message_generate_bl') && !app.messageGenerateDeliveryNoteDisplayed) {
  194. appAlerts.alert('info','Pour générer un bon de livraison, sélectionnez tout d\'abord un jour et un lieu de distribution.', 6000) ;
  195. app.messageGenerateDeliveryNoteDisplayed = true ;
  196. }
  197. }) ;
  198. },
  199. initCountActiveProducts: function() {
  200. this.countActiveProducts = 0 ;
  201. for(var i= 0; i < this.products.length; i++) {
  202. if(this.products[i].productDistribution[0].active == 1) {
  203. this.countActiveProducts ++ ;
  204. }
  205. }
  206. },
  207. initPointsSale: function(idActivePointSale) {
  208. this.countActivePointsSale = 0 ;
  209. this.setIdActivePointSale(0) ;
  210. for(var i= 0; i < this.pointsSale.length; i++) {
  211. if(this.pointsSale[i].pointSaleDistribution[0].delivery == 1) {
  212. this.countActivePointsSale ++ ;
  213. this.setIdActivePointSale(this.pointsSale[i].id) ;
  214. }
  215. }
  216. if(this.countActivePointsSale > 1) {
  217. this.setIdActivePointSale(0) ;
  218. }
  219. if(idActivePointSale) {
  220. this.setIdActivePointSale(idActivePointSale) ;
  221. }
  222. this.countOrdersByPointSale = [] ;
  223. for(var i = 0; i < this.pointsSale.length ; i++) {
  224. this.countOrdersByPointSale[this.pointsSale[i].id] = 0 ;
  225. }
  226. for(var i = 0; i < this.orders.length ; i++) {
  227. this.countOrdersByPointSale[this.orders[i].id_point_sale] ++ ;
  228. }
  229. },
  230. dayClicked: function(day) {
  231. this.date = day.date ;
  232. this.dateFormat = ('0' + this.date.getDate()).slice(-2)+ '/'
  233. + ('0' + (this.date.getMonth() +1)).slice(-2) + '/'
  234. + this.date.getFullYear() ;
  235. this.init() ;
  236. },
  237. productQuantityMaxChange: function(event) {
  238. axios.get("ajax-process-product-quantity-max",{params: {
  239. idDistribution: this.distribution.id,
  240. idProduct: event.currentTarget.getAttribute('data-id-product'),
  241. quantityMax: event.currentTarget.value
  242. }})
  243. .then(function(response) {
  244. }) ;
  245. },
  246. productActiveClick: function(event) {
  247. var idProduct = event.currentTarget.getAttribute('data-id-product') ;
  248. var activeProduct = event.currentTarget.getAttribute('data-active-product') ;
  249. axios.get("ajax-process-active-product",{params: {
  250. idDistribution: this.distribution.id,
  251. idProduct: idProduct,
  252. active: activeProduct
  253. }})
  254. .then(function(response) {
  255. }) ;
  256. for(i = 0 ; i < this.products.length ; i++) {
  257. if(this.products[i].id == idProduct) {
  258. this.products[i].productDistribution[0].active = activeProduct ;
  259. }
  260. }
  261. this.initCountActiveProducts() ;
  262. },
  263. pointSaleActiveClick: function(event) {
  264. var idPointSale = event.currentTarget.getAttribute('data-id-point-sale') ;
  265. var deliveryPointSale = event.currentTarget.getAttribute('data-delivery-point-sale') ;
  266. axios.get("ajax-process-active-point-sale",{params: {
  267. idDistribution: this.distribution.id,
  268. idPointSale: idPointSale,
  269. delivery: deliveryPointSale
  270. }})
  271. .then(function(response) {
  272. }) ;
  273. for(i = 0 ; i < this.pointsSale.length ; i++) {
  274. if(this.pointsSale[i].id == idPointSale) {
  275. this.pointsSale[i].pointSaleDistribution[0].delivery = deliveryPointSale ;
  276. }
  277. }
  278. this.initPointsSale() ;
  279. },
  280. activeDistribution: function(event) {
  281. var app = this ;
  282. axios.get("ajax-process-active-distribution",{params: {
  283. idDistribution: this.distribution.id,
  284. active: event.currentTarget.getAttribute('data-active')
  285. }})
  286. .then(function(response) {
  287. app.init() ;
  288. }) ;
  289. },
  290. activeWeekDistribution: function(event) {
  291. var app = this ;
  292. axios.get("ajax-process-active-week-distribution",{params: {
  293. date: this.date.getFullYear() + '-'
  294. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  295. + ('0' + this.date.getDate()).slice(-2),
  296. active: event.currentTarget.getAttribute('data-active')
  297. }})
  298. .then(function(response) {
  299. app.init() ;
  300. }) ;
  301. },
  302. pointSaleClick: function(event) {
  303. this.setIdActivePointSale(event.currentTarget.getAttribute('data-id-point-sale')) ;
  304. },
  305. setIdActivePointSale: function(id) {
  306. this.idActivePointSale = id ;
  307. if(!id) {
  308. this.orderCreate.id_point_sale = this.idDefaultPointSale ;
  309. }
  310. else {
  311. this.orderCreate.id_point_sale = id ;
  312. }
  313. },
  314. orderCreatedUpdated: function() {
  315. this.showModalFormOrderCreate = false ;
  316. this.showModalFormOrderUpdate = false ;
  317. this.init(this.idActivePointSale) ;
  318. },
  319. deleteOrderClick: function(event) {
  320. var app = this ;
  321. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  322. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-delete",{params: {
  323. idOrder: idOrder
  324. }})
  325. .then(function(response) {
  326. app.init(app.idActivePointSale) ;
  327. }) ;
  328. },
  329. updateOrderClick: function(event) {
  330. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  331. this.idOrderUpdate = idOrder ;
  332. this.showModalFormOrderUpdate = true ;
  333. this.initModalFormOrder() ;
  334. },
  335. openModalFormOrderCreate: function() {
  336. this.showModalFormOrderCreate = true ;
  337. this.initModalFormOrder() ;
  338. },
  339. initModalFormOrder: function() {
  340. setTimeout(function() {
  341. $('.modal-body').css('height',$(window).height()) ;
  342. $('.modal-body').css('maxHeight','unset') ;
  343. },500);
  344. },
  345. orderPaymentModalClick: function(event) {
  346. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  347. this.idOrderPayment = idOrder ;
  348. this.showModalPayment = true ;
  349. },
  350. orderPaymentClick: function(event) {
  351. var app = this ;
  352. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  353. if(!idOrder) {
  354. idOrder = this.idOrderPayment ;
  355. }
  356. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-payment",{params: {
  357. idOrder: idOrder,
  358. type: event.currentTarget.getAttribute('data-type'),
  359. amount: event.currentTarget.getAttribute('data-amount')
  360. }})
  361. .then(function(response) {
  362. app.init(app.idActivePointSale) ;
  363. }) ;
  364. },
  365. orderViewClick: function(event) {
  366. var currentIdOrderView = event.currentTarget.getAttribute('data-id-order') ;
  367. if(this.idOrderView == currentIdOrderView) {
  368. this.showViewProduct = !this.showViewProduct ;
  369. }
  370. else {
  371. this.showViewProduct = true ;
  372. this.idOrderView = currentIdOrderView ;
  373. }
  374. },
  375. closeModalProducts: function() {
  376. this.showModalProducts = false ;
  377. this.init(this.idActivePointSale) ;
  378. },
  379. cloneOrder: function(order) {
  380. var clone = Object.assign({}, order) ;
  381. clone.productOrder = {} ;
  382. for(var key in order.productOrder) {
  383. clone.productOrder[key] = order.productOrder[key] ;
  384. }
  385. return clone ;
  386. },
  387. addSubscriptions: function() {
  388. var app = this ;
  389. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-process-add-subscriptions",{params: {
  390. date: this.getDate()
  391. }})
  392. .then(function(response) {
  393. app.init(app.idActivePointSale) ;
  394. }) ;
  395. },
  396. synchroTiller: function() {
  397. var app = this ;
  398. this.showLoading = true ;
  399. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-process-synchro-tiller",{params: {
  400. date: this.getDate()
  401. }})
  402. .then(function(response) {
  403. app.init(app.idActivePointSale) ;
  404. }) ;
  405. },
  406. totalActivePointSale: function() {
  407. var total = 0 ;
  408. for(var i = 0; i < this.orders.length ; i++) {
  409. if(this.orders[i].id_point_sale == this.idActivePointSale) {
  410. total += this.orders[i].amount ;
  411. }
  412. }
  413. return formatPrice(total) ;
  414. },
  415. changeSynchroTiller: function(event) {
  416. var app = this ;
  417. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  418. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-change-synchro-tiller",{params: {
  419. idOrder: idOrder,
  420. boolSynchroTiller: event.currentTarget.checked ? 1 : 0
  421. }})
  422. .then(function(response) {
  423. app.init() ;
  424. }) ;
  425. },
  426. selectAllOrdersEvent: function(event) {
  427. var bool = event.currentTarget.checked ;
  428. this.selectAllOrders(bool) ;
  429. },
  430. selectAllOrders: function(bool) {
  431. for(var key in this.orders) {
  432. if(this.orders[key].id_point_sale == this.idActivePointSale) {
  433. this.orders[key].selected = bool ;
  434. }
  435. }
  436. },
  437. oneOrderSelected: function() {
  438. for(var key in this.orders) {
  439. if(this.orders[key].selected == true) {
  440. return true ;
  441. }
  442. }
  443. return false ;
  444. },
  445. generateDeliveryNote: function() {
  446. if(!this.oneOrderSelected()) {
  447. this.selectAllOrders(true) ;
  448. }
  449. if(this.oneOrderSelected()) {
  450. var app = this ;
  451. var idOrders = {} ;
  452. for(var key in this.orders) {
  453. if(this.orders[key].selected == true) {
  454. idOrders[key] = this.orders[key].id ;
  455. }
  456. }
  457. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-generate-delivery-note",{params: {
  458. idOrders: JSON.stringify(idOrders)
  459. }})
  460. .then(function(response) {
  461. appAlerts.alertResponse(response) ;
  462. app.init(app.idActivePointSale) ;
  463. }) ;
  464. }
  465. else {
  466. appAlerts.alert('danger','Veuillez sélectionner au moins une commande pour générer un bon de livraison') ;
  467. }
  468. },
  469. deliveryNoteExist: function(idPointSale) {
  470. return typeof this.deliveryNotes[this.idActivePointSale] != 'undefined' ;
  471. },
  472. payOrders: function() {
  473. var app = this ;
  474. axios.get(UrlManager.getBaseUrlAbsolute()+"cron/pay-orders",{params: {
  475. date: app.getDate()
  476. }})
  477. .then(function(response) {
  478. appAlerts.alertResponse(response) ;
  479. app.init(app.idActivePointSale) ;
  480. }) ;
  481. }
  482. },
  483. });
  484. Vue.component('modal', {
  485. template: '#modal-template'
  486. })
  487. Vue.component('order-form',{
  488. props: ['date', 'pointsSale','meansPayment', 'users', 'products', 'order', 'producer'],
  489. data: function() {
  490. return {
  491. errors: [],
  492. idPointSale: 0,
  493. idUser: 0,
  494. username : '',
  495. comment: '',
  496. baseUrl: $('meta[name=baseurl]').attr('content'),
  497. } ;
  498. },
  499. template: '#order-form-template',
  500. methods: {
  501. checkForm: function() {
  502. this.errors = [] ;
  503. var countProducts = 0 ;
  504. for(var key in this.order.productOrder) {
  505. if(this.order.productOrder[key].quantity > 0) {
  506. countProducts ++ ;
  507. }
  508. }
  509. if(this.order.id_point_sale
  510. && (this.order.id_user > 0 || (this.order.username && this.order.username.length))
  511. && countProducts > 0) {
  512. return true ;
  513. }
  514. if(!this.order.id_point_sale) {
  515. this.errors.push('Veuillez sélectionner un point de vente') ;
  516. }
  517. if((!this.order.id_user || this.order.id_user == 0) && !this.order.username.length) {
  518. this.errors.push('Veuillez sélectionner ou saisir un utilisateur') ;
  519. }
  520. if(!countProducts) {
  521. this.errors.push('Veuillez sélectionner au moins un produit') ;
  522. }
  523. },
  524. submitFormCreate: function(event) {
  525. var app = this ;
  526. if(this.checkForm()) {
  527. var processCredit = event.currentTarget.getAttribute('data-process-credit') ;
  528. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-create",{params: {
  529. date: this.date.getFullYear() + '-'
  530. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  531. + ('0' + this.date.getDate()).slice(-2),
  532. idPointSale: this.order.id_point_sale,
  533. idUser: this.order.id_user,
  534. username: this.order.username,
  535. meanPayment: this.order.mean_payment,
  536. products: JSON.stringify(this.order.productOrder),
  537. comment: this.order.comment,
  538. processCredit: processCredit
  539. }})
  540. .then(function(response) {
  541. app.order.id_point_sale = 0 ;
  542. app.order.id_user = 0 ;
  543. app.order.username = '' ;
  544. app.order.comment = '' ;
  545. for(i=0 ; i<app.order.productOrder.length ; i++) {
  546. app.order.productOrder[i] = 0 ;
  547. }
  548. app.$emit('ordercreatedupdated') ;
  549. }) ;
  550. }
  551. },
  552. submitFormUpdate: function(event) {
  553. var app = this ;
  554. if(this.checkForm()) {
  555. var processCredit = event.currentTarget.getAttribute('data-process-credit') ;
  556. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-update",{params: {
  557. date: this.date.getFullYear() + '-'
  558. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  559. + ('0' + this.date.getDate()).slice(-2),
  560. idOrder: this.order.id,
  561. idPointSale: this.order.id_point_sale,
  562. meanPayment: this.order.mean_payment,
  563. idUser: this.order.id_user,
  564. username: ''+this.order.username,
  565. products: JSON.stringify(this.order.productOrder),
  566. comment: this.order.comment,
  567. processCredit: processCredit
  568. }})
  569. .then(function(response) {
  570. app.$emit('ordercreatedupdated') ;
  571. }) ;
  572. }
  573. },
  574. productQuantityClick: function(id_product, quantity) {
  575. if(!this.order.productOrder[id_product].quantity) {
  576. this.order.productOrder[id_product].quantity = 0 ;
  577. }
  578. if(parseFloat(this.order.productOrder[id_product].quantity) + quantity >= 0) {
  579. var theQuantity = parseFloat(this.order.productOrder[id_product].quantity) + parseFloat(quantity) ;
  580. Vue.set(this.order.productOrder, id_product, {
  581. quantity: theQuantity,
  582. unit: this.order.productOrder[id_product].unit,
  583. price: this.order.productOrder[id_product].price
  584. });
  585. }
  586. },
  587. productPriceChange: function(event) {
  588. var idProduct = event.currentTarget.getAttribute('data-id-product') ;
  589. var price = parseFloat(event.currentTarget.value) ;
  590. if(isNaN(price)) {
  591. price = 0 ;
  592. }
  593. Vue.set(this.order.productOrder, idProduct, {
  594. quantity: this.order.productOrder[idProduct].quantity,
  595. unit: this.order.productOrder[idProduct].unit,
  596. price: price
  597. });
  598. },
  599. userChange: function(event) {
  600. var app = this ;
  601. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-point-sale-favorite",{params: {
  602. idUser: app.order.id_user,
  603. }})
  604. .then(function(response) {
  605. app.order.id_point_sale = response.data.id_favorite_point_sale ;
  606. }) ;
  607. }
  608. }
  609. }) ;