| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- /**
- * Tableau de bord : encours par client (tri croissant), synthèse, échéances (FR10, FR11).
- * Même structure que la slide : Client, Encours, accord, j out, situation + encadré synthèse.
- */
- (function () {
- function formatDateRapport(str) {
- if (!str) return '—';
- const d = new Date(str);
- return isNaN(d.getTime()) ? str : d.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: '2-digit' });
- }
- function formatKeur(n) {
- if (n == null || isNaN(n)) return '—';
- const k = (n / 1000).toFixed(1);
- return new Intl.NumberFormat('fr-FR').format(parseFloat(k)) + ' k€';
- }
- async function loadDashboard() {
- hideError();
- try {
- const data = await apiGet('/reporting.php');
- const dateRapport = data.date_rapport || new Date().toISOString().slice(0, 10);
- const titreEl = document.getElementById('titre-encours');
- if (titreEl) titreEl.textContent = 'Encours clients au ' + formatDateRapport(dateRapport);
- const encoursEl = document.getElementById('encours-par-client');
- if (!data.encours_par_client || data.encours_par_client.length === 0) {
- encoursEl.innerHTML = '<p class="text-gray-500">Aucun encours.</p>';
- } else {
- encoursEl.innerHTML = `
- <table class="min-w-full divide-y divide-gray-200 text-sm">
- <thead class="bg-gray-50">
- <tr>
- <th class="px-4 py-2 text-left text-xs font-medium text-gray-600 uppercase">Client</th>
- <th class="px-4 py-2 text-right text-xs font-medium text-gray-600 uppercase">Encours</th>
- <th class="px-4 py-2 text-center text-xs font-medium text-gray-600 uppercase">accord</th>
- <th class="px-4 py-2 text-center text-xs font-medium text-gray-600 uppercase">j out</th>
- <th class="px-4 py-2 text-left text-xs font-medium text-gray-600 uppercase">situation</th>
- <th class="px-4 py-2 text-right text-xs font-medium text-gray-600 uppercase">Action</th>
- </tr>
- </thead>
- <tbody class="divide-y divide-gray-200 bg-white">
- ${data.encours_par_client.map(c => `
- <tr class="hover:bg-gray-50">
- <td class="px-4 py-2 font-medium text-gray-900">${escapeHtml(c.nom)}</td>
- <td class="px-4 py-2 text-right tabular-nums">${formatMoney(c.encours)}</td>
- <td class="px-4 py-2 text-center text-gray-500">—</td>
- <td class="px-4 py-2 text-center text-gray-500">—</td>
- <td class="px-4 py-2 text-gray-500">—</td>
- <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>
- </tr>
- `).join('')}
- </tbody>
- </table>
- `;
- }
- const synthèseEl = document.getElementById('synthèse-encours');
- if (synthèseEl) {
- const encoursTotalK = formatKeur(data.encours_total);
- const delaiJ = data.delai_moyen_paiement_j != null ? data.delai_moyen_paiement_j + ' j' : '—';
- synthèseEl.innerHTML = `
- <div class="mt-4 p-4 bg-gray-50 rounded-lg border border-gray-200">
- <p class="text-sm text-gray-700"><strong>Encours client total au ${formatDateRapport(dateRapport)}</strong> : ${encoursTotalK}</p>
- <p class="text-sm text-gray-700 mt-1"><strong>Délai moyen de paiement à date</strong> : ${delaiJ}</p>
- </div>
- `;
- }
- const echeancesEl = document.getElementById('echeances');
- if (!data.echeances_a_venir || data.echeances_a_venir.length === 0) {
- echeancesEl.innerHTML = '<p class="text-gray-500">Aucune échéance en attente.</p>';
- } else {
- echeancesEl.innerHTML = `
- <table class="min-w-full divide-y divide-gray-200">
- <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>
- <tbody class="divide-y divide-gray-200">
- ${data.echeances_a_venir.map(e => {
- const dateEch = e.date_echeance || e.date_facture;
- const montantEch = e.montant_echeance != null ? e.montant_echeance : e.encours_facture;
- const echLabel = e.echeance_num ? `Éch. ${e.echeance_num}/${e.echeance_total}` : '';
- return `
- <tr class="hover:bg-gray-50">
- <td class="px-4 py-2 text-gray-900">${escapeHtml(e.nom)}</td>
- <td class="px-4 py-2">${escapeHtml(e.n_facture)}${echLabel ? ' <span class="text-xs text-gray-500">(' + echLabel + ')</span>' : ''}</td>
- <td class="px-4 py-2">${formatDate(dateEch)}</td>
- <td class="px-4 py-2 text-right">${formatMoney(montantEch)}</td>
- <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>
- </tr>
- `;
- }).join('')}
- </tbody>
- </table>
- `;
- }
- } catch (e) {
- showError(e.message);
- const titreEl = document.getElementById('titre-encours');
- if (titreEl) titreEl.textContent = 'Encours clients';
- document.getElementById('encours-par-client').innerHTML = '<p class="text-red-500">Erreur de chargement.</p>';
- const syn = document.getElementById('synthèse-encours');
- if (syn) syn.innerHTML = '';
- document.getElementById('echeances').innerHTML = '<p class="text-red-500">Erreur de chargement.</p>';
- }
- }
- function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
- }
- loadDashboard();
- })();
|