| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- (function () {
- const typeEl = document.getElementById('type');
- const cycleEl = document.getElementById('cycle');
- const topListEl = document.getElementById('top-list');
- const titleEl = document.getElementById('title-section');
- const totauxEl = document.getElementById('totaux');
- async function load() {
- hideError();
- const type = typeEl.value;
- const cycle = cycleEl.value || null;
- try {
- const params = { type };
- if (cycle) params.cycle = cycle;
- const data = await apiGet('/top-clients.php', params);
- const label = type === 'alevins' ? 'Alevins' : 'Larves';
- titleEl.textContent = 'Top clients ' + label + (cycle ? ' (cycle ' + cycle + ')' : '');
- if (!data.top_clients || data.top_clients.length === 0) {
- topListEl.innerHTML = '<p class="text-gray-500">Aucune donnée pour ce filtre.</p>';
- totauxEl.textContent = '';
- return;
- }
- topListEl.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-right text-xs font-medium text-gray-600">Quantités</th>
- <th class="px-4 py-2 text-right text-xs font-medium text-gray-600">% qté</th>
- <th class="px-4 py-2 text-right text-xs font-medium text-gray-600">CA HT</th>
- <th class="px-4 py-2 text-right text-xs font-medium text-gray-600">% CA</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.top_clients.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">${formatNumber(c.quantites)}</td>
- <td class="px-4 py-2 text-right">${c.pct_qte} %</td>
- <td class="px-4 py-2 text-right">${formatMoney(c.ca)}</td>
- <td class="px-4 py-2 text-right">${c.pct_ca} %</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>
- `;
- totauxEl.textContent = 'Total quantités : ' + formatNumber(data.total_quantites) + ' — Total CA HT : ' + formatMoney(data.total_ca);
- } catch (e) {
- showError(e.message);
- topListEl.innerHTML = '<p class="text-red-500">Erreur de chargement.</p>';
- totauxEl.textContent = '';
- }
- }
- function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
- }
- document.getElementById('btn-refresh').addEventListener('click', load);
- typeEl.addEventListener('change', load);
- cycleEl.addEventListener('change', load);
- load();
- })();
|