top-clients.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. (function () {
  2. const typeEl = document.getElementById('type');
  3. const cycleEl = document.getElementById('cycle');
  4. const topListEl = document.getElementById('top-list');
  5. const titleEl = document.getElementById('title-section');
  6. const totauxEl = document.getElementById('totaux');
  7. async function load() {
  8. hideError();
  9. const type = typeEl.value;
  10. const cycle = cycleEl.value || null;
  11. try {
  12. const params = { type };
  13. if (cycle) params.cycle = cycle;
  14. const data = await apiGet('/top-clients.php', params);
  15. const label = type === 'alevins' ? 'Alevins' : 'Larves';
  16. titleEl.textContent = 'Meilleurs clients ' + label + (cycle ? ' (cycle ' + cycle + ')' : '');
  17. if (!data.top_clients || data.top_clients.length === 0) {
  18. topListEl.innerHTML = '<p class="text-gray-500">Aucune donnée pour ce filtre.</p>';
  19. totauxEl.textContent = '';
  20. return;
  21. }
  22. topListEl.innerHTML = `
  23. <table class="min-w-full divide-y divide-gray-200">
  24. <thead class="bg-gray-50">
  25. <tr>
  26. <th class="px-4 py-2 text-left text-xs font-medium text-gray-600">Client</th>
  27. <th class="px-4 py-2 text-right text-xs font-medium text-gray-600">Quantités</th>
  28. <th class="px-4 py-2 text-right text-xs font-medium text-gray-600">% qté</th>
  29. <th class="px-4 py-2 text-right text-xs font-medium text-gray-600">CA HT</th>
  30. <th class="px-4 py-2 text-right text-xs font-medium text-gray-600">% CA</th>
  31. <th class="px-4 py-2 text-right text-xs font-medium text-gray-600">Actions</th>
  32. </tr>
  33. </thead>
  34. <tbody class="divide-y divide-gray-200">
  35. ${data.top_clients.map(c => `
  36. <tr class="hover:bg-gray-50">
  37. <td class="px-4 py-2 font-medium text-gray-900">${escapeHtml(c.nom)}</td>
  38. <td class="px-4 py-2 text-right">${formatNumber(c.quantites)}</td>
  39. <td class="px-4 py-2 text-right">${c.pct_qte} %</td>
  40. <td class="px-4 py-2 text-right">${formatMoney(c.ca)}</td>
  41. <td class="px-4 py-2 text-right">${c.pct_ca} %</td>
  42. <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>
  43. </tr>
  44. `).join('')}
  45. </tbody>
  46. </table>
  47. `;
  48. totauxEl.textContent = 'Total quantités : ' + formatNumber(data.total_quantites) + ' — Total CA HT : ' + formatMoney(data.total_ca);
  49. } catch (e) {
  50. showError(e.message);
  51. topListEl.innerHTML = '<p class="text-red-500">Erreur de chargement.</p>';
  52. totauxEl.textContent = '';
  53. }
  54. }
  55. function escapeHtml(text) {
  56. const div = document.createElement('div');
  57. div.textContent = text;
  58. return div.innerHTML;
  59. }
  60. document.getElementById('btn-refresh').addEventListener('click', load);
  61. typeEl.addEventListener('change', load);
  62. cycleEl.addEventListener('change', load);
  63. load();
  64. })();