618 lines
24KB

  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. },
  334. orderPaymentModalClick: function(event) {
  335. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  336. this.idOrderPayment = idOrder ;
  337. this.showModalPayment = true ;
  338. },
  339. orderPaymentClick: function(event) {
  340. var app = this ;
  341. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  342. if(!idOrder) {
  343. idOrder = this.idOrderPayment ;
  344. }
  345. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-payment",{params: {
  346. idOrder: idOrder,
  347. type: event.currentTarget.getAttribute('data-type'),
  348. amount: event.currentTarget.getAttribute('data-amount')
  349. }})
  350. .then(function(response) {
  351. app.init(app.idActivePointSale) ;
  352. }) ;
  353. },
  354. orderViewClick: function(event) {
  355. var currentIdOrderView = event.currentTarget.getAttribute('data-id-order') ;
  356. if(this.idOrderView == currentIdOrderView) {
  357. this.showViewProduct = !this.showViewProduct ;
  358. }
  359. else {
  360. this.showViewProduct = true ;
  361. this.idOrderView = currentIdOrderView ;
  362. }
  363. },
  364. closeModalProducts: function() {
  365. this.showModalProducts = false ;
  366. this.init(this.idActivePointSale) ;
  367. },
  368. cloneOrder: function(order) {
  369. var clone = Object.assign({}, order) ;
  370. clone.productOrder = {} ;
  371. for(var key in order.productOrder) {
  372. clone.productOrder[key] = order.productOrder[key] ;
  373. }
  374. return clone ;
  375. },
  376. addSubscriptions: function() {
  377. var app = this ;
  378. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-process-add-subscriptions",{params: {
  379. date: this.getDate()
  380. }})
  381. .then(function(response) {
  382. app.init(app.idActivePointSale) ;
  383. }) ;
  384. },
  385. synchroTiller: function() {
  386. var app = this ;
  387. this.showLoading = true ;
  388. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-process-synchro-tiller",{params: {
  389. date: this.getDate()
  390. }})
  391. .then(function(response) {
  392. app.init(app.idActivePointSale) ;
  393. }) ;
  394. },
  395. totalActivePointSale: function() {
  396. var total = 0 ;
  397. for(var i = 0; i < this.orders.length ; i++) {
  398. if(this.orders[i].id_point_sale == this.idActivePointSale) {
  399. total += this.orders[i].amount ;
  400. }
  401. }
  402. return formatPrice(total) ;
  403. },
  404. changeSynchroTiller: function(event) {
  405. var app = this ;
  406. var idOrder = event.currentTarget.getAttribute('data-id-order') ;
  407. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-change-synchro-tiller",{params: {
  408. idOrder: idOrder,
  409. boolSynchroTiller: event.currentTarget.checked ? 1 : 0
  410. }})
  411. .then(function(response) {
  412. app.init() ;
  413. }) ;
  414. },
  415. selectAllOrdersEvent: function(event) {
  416. var bool = event.currentTarget.checked ;
  417. this.selectAllOrders(bool) ;
  418. },
  419. selectAllOrders: function(bool) {
  420. for(var key in this.orders) {
  421. if(this.orders[key].id_point_sale == this.idActivePointSale) {
  422. this.orders[key].selected = bool ;
  423. }
  424. }
  425. },
  426. oneOrderSelected: function() {
  427. for(var key in this.orders) {
  428. if(this.orders[key].selected == true) {
  429. return true ;
  430. }
  431. }
  432. return false ;
  433. },
  434. generateDeliveryNote: function() {
  435. if(!this.oneOrderSelected()) {
  436. this.selectAllOrders(true) ;
  437. }
  438. if(this.oneOrderSelected()) {
  439. var app = this ;
  440. var idOrders = {} ;
  441. for(var key in this.orders) {
  442. if(this.orders[key].selected == true) {
  443. idOrders[key] = this.orders[key].id ;
  444. }
  445. }
  446. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-generate-delivery-note",{params: {
  447. idOrders: JSON.stringify(idOrders)
  448. }})
  449. .then(function(response) {
  450. appAlerts.alertResponse(response) ;
  451. app.init(app.idActivePointSale) ;
  452. }) ;
  453. }
  454. else {
  455. appAlerts.alert('danger','Veuillez sélectionner au moins une commande pour générer un bon de livraison') ;
  456. }
  457. },
  458. deliveryNoteExist: function(idPointSale) {
  459. return typeof this.deliveryNotes[this.idActivePointSale] != 'undefined' ;
  460. }
  461. },
  462. });
  463. Vue.component('modal', {
  464. template: '#modal-template'
  465. })
  466. Vue.component('order-form',{
  467. props: ['date', 'pointsSale','meansPayment', 'users', 'products', 'order', 'producer'],
  468. data: function() {
  469. return {
  470. errors: [],
  471. idPointSale: 0,
  472. idUser: 0,
  473. username : '',
  474. comment: '',
  475. baseUrl: $('meta[name=baseurl]').attr('content'),
  476. } ;
  477. },
  478. template: '#order-form-template',
  479. methods: {
  480. checkForm: function() {
  481. this.errors = [] ;
  482. var countProducts = 0 ;
  483. for(var key in this.order.productOrder) {
  484. if(this.order.productOrder[key].quantity > 0) {
  485. countProducts ++ ;
  486. }
  487. }
  488. if(this.order.id_point_sale
  489. && (this.order.id_user > 0 || (this.order.username && this.order.username.length))
  490. && countProducts > 0) {
  491. return true ;
  492. }
  493. if(!this.order.id_point_sale) {
  494. this.errors.push('Veuillez sélectionner un point de vente') ;
  495. }
  496. if((!this.order.id_user || this.order.id_user == 0) && !this.order.username.length) {
  497. this.errors.push('Veuillez sélectionner ou saisir un utilisateur') ;
  498. }
  499. if(!countProducts) {
  500. this.errors.push('Veuillez sélectionner au moins un produit') ;
  501. }
  502. },
  503. submitFormCreate: function(event) {
  504. var app = this ;
  505. if(this.checkForm()) {
  506. var processCredit = event.currentTarget.getAttribute('data-process-credit') ;
  507. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-create",{params: {
  508. date: this.date.getFullYear() + '-'
  509. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  510. + ('0' + this.date.getDate()).slice(-2),
  511. idPointSale: this.order.id_point_sale,
  512. idUser: this.order.id_user,
  513. username: this.order.username,
  514. meanPayment: this.order.mean_payment,
  515. products: JSON.stringify(this.order.productOrder),
  516. comment: this.order.comment,
  517. processCredit: processCredit
  518. }})
  519. .then(function(response) {
  520. app.order.id_point_sale = 0 ;
  521. app.order.id_user = 0 ;
  522. app.order.username = '' ;
  523. app.order.comment = '' ;
  524. for(i=0 ; i<app.order.productOrder.length ; i++) {
  525. app.order.productOrder[i] = 0 ;
  526. }
  527. app.$emit('ordercreatedupdated') ;
  528. }) ;
  529. }
  530. },
  531. submitFormUpdate: function(event) {
  532. var app = this ;
  533. if(this.checkForm()) {
  534. var processCredit = event.currentTarget.getAttribute('data-process-credit') ;
  535. axios.get(UrlManager.getBaseUrlAbsolute()+"order/ajax-update",{params: {
  536. date: this.date.getFullYear() + '-'
  537. + ('0' + (this.date.getMonth() +1)).slice(-2) + '-'
  538. + ('0' + this.date.getDate()).slice(-2),
  539. idOrder: this.order.id,
  540. idPointSale: this.order.id_point_sale,
  541. meanPayment: this.order.mean_payment,
  542. idUser: this.order.id_user,
  543. username: ''+this.order.username,
  544. products: JSON.stringify(this.order.productOrder),
  545. comment: this.order.comment,
  546. processCredit: processCredit
  547. }})
  548. .then(function(response) {
  549. app.$emit('ordercreatedupdated') ;
  550. }) ;
  551. }
  552. },
  553. productQuantityClick: function(id_product, quantity) {
  554. if(!this.order.productOrder[id_product].quantity) {
  555. this.order.productOrder[id_product].quantity = 0 ;
  556. }
  557. if(parseFloat(this.order.productOrder[id_product].quantity) + quantity >= 0) {
  558. var theQuantity = parseFloat(this.order.productOrder[id_product].quantity) + parseFloat(quantity) ;
  559. Vue.set(this.order.productOrder, id_product, {quantity: theQuantity, unit: this.order.productOrder[id_product].unit});
  560. }
  561. },
  562. userChange: function(event) {
  563. var app = this ;
  564. axios.get(UrlManager.getBaseUrlAbsolute()+"distribution/ajax-point-sale-favorite",{params: {
  565. idUser: app.order.id_user,
  566. }})
  567. .then(function(response) {
  568. app.order.id_point_sale = response.data.id_favorite_point_sale ;
  569. }) ;
  570. }
  571. }
  572. }) ;