dashboard.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Tableau de bord : encours par client (tri croissant), synthèse, échéances (FR10, FR11).
  3. * Même structure que la slide : Client, Encours, accord, j out, situation + encadré synthèse.
  4. */
  5. (function () {
  6. function formatDateRapport(str) {
  7. if (!str) return '—';
  8. const d = new Date(str);
  9. return isNaN(d.getTime()) ? str : d.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: '2-digit' });
  10. }
  11. function formatKeur(n) {
  12. if (n == null || isNaN(n)) return '—';
  13. const k = (n / 1000).toFixed(1);
  14. return new Intl.NumberFormat('fr-FR').format(parseFloat(k)) + ' k€';
  15. }
  16. async function loadDashboard() {
  17. hideError();
  18. try {
  19. const data = await apiGet('/reporting.php');
  20. const dateRapport = data.date_rapport || new Date().toISOString().slice(0, 10);
  21. const titreEl = document.getElementById('titre-encours');
  22. if (titreEl) titreEl.textContent = 'Encours clients au ' + formatDateRapport(dateRapport);
  23. const encoursEl = document.getElementById('encours-par-client');
  24. if (!data.encours_par_client || data.encours_par_client.length === 0) {
  25. encoursEl.innerHTML = '<p class="text-gray-500">Aucun encours.</p>';
  26. } else {
  27. encoursEl.innerHTML = `
  28. <table class="min-w-full divide-y divide-gray-200 text-sm">
  29. <thead class="bg-gray-50">
  30. <tr>
  31. <th class="px-4 py-2 text-left text-xs font-medium text-gray-600 uppercase">Client</th>
  32. <th class="px-4 py-2 text-right text-xs font-medium text-gray-600 uppercase">Encours</th>
  33. <th class="px-4 py-2 text-center text-xs font-medium text-gray-600 uppercase">accord</th>
  34. <th class="px-4 py-2 text-center text-xs font-medium text-gray-600 uppercase">j out</th>
  35. <th class="px-4 py-2 text-left text-xs font-medium text-gray-600 uppercase">situation</th>
  36. <th class="px-4 py-2 text-right text-xs font-medium text-gray-600 uppercase">Action</th>
  37. </tr>
  38. </thead>
  39. <tbody class="divide-y divide-gray-200 bg-white">
  40. ${data.encours_par_client.map(c => `
  41. <tr class="hover:bg-gray-50">
  42. <td class="px-4 py-2 font-medium text-gray-900">${escapeHtml(c.nom)}</td>
  43. <td class="px-4 py-2 text-right tabular-nums">${formatMoney(c.encours)}</td>
  44. <td class="px-4 py-2 text-center text-gray-500">—</td>
  45. <td class="px-4 py-2 text-center text-gray-500">—</td>
  46. <td class="px-4 py-2 text-gray-500">—</td>
  47. <td class="px-4 py-2 text-right"><a href="client.html?id=${c.client_id}" class="text-blue-600 hover:underline">Voir</a></td>
  48. </tr>
  49. `).join('')}
  50. </tbody>
  51. </table>
  52. `;
  53. }
  54. const synthèseEl = document.getElementById('synthèse-encours');
  55. if (synthèseEl) {
  56. const encoursTotalK = formatKeur(data.encours_total);
  57. const delaiJ = data.delai_moyen_paiement_j != null ? data.delai_moyen_paiement_j + ' j' : '—';
  58. synthèseEl.innerHTML = `
  59. <div class="mt-4 p-4 bg-gray-50 rounded-lg border border-gray-200">
  60. <p class="text-sm text-gray-700"><strong>Encours client total au ${formatDateRapport(dateRapport)}</strong> : ${encoursTotalK}</p>
  61. <p class="text-sm text-gray-700 mt-1"><strong>Délai moyen de paiement à date</strong> : ${delaiJ}</p>
  62. </div>
  63. `;
  64. }
  65. const echeancesEl = document.getElementById('echeances');
  66. if (!data.echeances_a_venir || data.echeances_a_venir.length === 0) {
  67. echeancesEl.innerHTML = '<p class="text-gray-500">Aucune échéance en attente.</p>';
  68. } else {
  69. echeancesEl.innerHTML = `
  70. <table class="min-w-full divide-y divide-gray-200">
  71. <thead class="bg-gray-50"><tr><th class="px-4 py-2 text-left text-xs font-medium text-gray-600">Client</th><th class="px-4 py-2 text-left text-xs font-medium text-gray-600">N° facture</th><th class="px-4 py-2 text-left text-xs font-medium text-gray-600">Date échéance</th><th class="px-4 py-2 text-right text-xs font-medium text-gray-600">Montant</th><th class="px-4 py-2 text-right text-xs font-medium text-gray-600">Action</th></tr></thead>
  72. <tbody class="divide-y divide-gray-200">
  73. ${data.echeances_a_venir.map(e => {
  74. const dateEch = e.date_echeance || e.date_facture;
  75. const montantEch = e.montant_echeance != null ? e.montant_echeance : e.encours_facture;
  76. const echLabel = e.echeance_num ? `Éch. ${e.echeance_num}/${e.echeance_total}` : '';
  77. return `
  78. <tr class="hover:bg-gray-50">
  79. <td class="px-4 py-2 text-gray-900">${escapeHtml(e.nom)}</td>
  80. <td class="px-4 py-2">${escapeHtml(e.n_facture)}${echLabel ? ' <span class="text-xs text-gray-500">(' + echLabel + ')</span>' : ''}</td>
  81. <td class="px-4 py-2">${formatDate(dateEch)}</td>
  82. <td class="px-4 py-2 text-right">${formatMoney(montantEch)}</td>
  83. <td class="px-4 py-2 text-right"><a href="client.html?id=${e.client_id}" class="text-blue-600 hover:underline">Voir</a></td>
  84. </tr>
  85. `;
  86. }).join('')}
  87. </tbody>
  88. </table>
  89. `;
  90. }
  91. } catch (e) {
  92. showError(e.message);
  93. const titreEl = document.getElementById('titre-encours');
  94. if (titreEl) titreEl.textContent = 'Encours clients';
  95. document.getElementById('encours-par-client').innerHTML = '<p class="text-red-500">Erreur de chargement.</p>';
  96. const syn = document.getElementById('synthèse-encours');
  97. if (syn) syn.innerHTML = '';
  98. document.getElementById('echeances').innerHTML = '<p class="text-red-500">Erreur de chargement.</p>';
  99. }
  100. }
  101. function escapeHtml(text) {
  102. const div = document.createElement('div');
  103. div.textContent = text;
  104. return div.innerHTML;
  105. }
  106. loadDashboard();
  107. })();