| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- /**
- * API Meilleurs clients par type de produit (FR10b).
- * GET ?type=alevins|larves & optionnel ?cycle=24-25|25-26
- */
- require_once __DIR__ . '/config.php';
- header('Access-Control-Allow-Origin: *');
- header('Access-Control-Allow-Methods: GET');
- $method = $_SERVER['REQUEST_METHOD'] ?? '';
- if ($method !== 'GET') {
- sendError('Méthode non autorisée', 405);
- exit;
- }
- $type = isset($_GET['type']) ? strtolower(trim($_GET['type'])) : '';
- $cycle = isset($_GET['cycle']) ? trim($_GET['cycle']) : null;
- if ($type !== 'alevins' && $type !== 'larves') {
- sendError('type requis: alevins ou larves', 400);
- exit;
- }
- if (isMockMode()) {
- $clients = loadMock('clients');
- $factures = loadMock('factures');
- $byClient = [];
- foreach ($factures as $f) {
- $t = $f['type_produit'] ?? '';
- if (strtolower($t) !== $type) continue;
- if ($cycle !== null && ($f['cycle'] ?? '') !== $cycle) continue;
- $cid = (int) $f['client_id'];
- if (!isset($byClient[$cid])) {
- $byClient[$cid] = ['client_id' => $cid, 'nom' => '', 'quantites' => 0, 'ca' => 0];
- foreach ($clients as $c) {
- if ((int) $c['id'] === $cid) { $byClient[$cid]['nom'] = $c['nom']; break; }
- }
- }
- $byClient[$cid]['quantites'] += (float) ($f['quantites'] ?? 0);
- $byClient[$cid]['ca'] += (float) $f['montant_ht'];
- }
- $rows = array_values($byClient);
- usort($rows, fn($a, $b) => $b['ca'] <=> $a['ca']);
- $totalQte = array_sum(array_column($rows, 'quantites'));
- $totalCa = array_sum(array_column($rows, 'ca'));
- foreach ($rows as &$r) {
- $r['pct_qte'] = $totalQte > 0 ? round(100 * $r['quantites'] / $totalQte, 1) : 0;
- $r['pct_ca'] = $totalCa > 0 ? round(100 * $r['ca'] / $totalCa, 1) : 0;
- }
- sendJson([
- 'type' => $type,
- 'cycle' => $cycle,
- 'top_clients' => $rows,
- 'total_quantites' => $totalQte,
- 'total_ca' => $totalCa,
- ]);
- exit;
- }
- $pdo = getPdo();
- $sql = "
- SELECT c.id AS client_id, c.nom,
- SUM(COALESCE(f.quantites, 0)) AS quantites,
- SUM(f.montant_ht) AS ca
- FROM clients c
- JOIN factures f ON f.client_id = c.id
- WHERE LOWER(TRIM(f.type_produit)) = ?
- ";
- $params = [$type];
- if ($cycle !== null && $cycle !== '') {
- $sql .= " AND TRIM(f.cycle) = ?";
- $params[] = $cycle;
- }
- $sql .= " GROUP BY c.id, c.nom ORDER BY ca DESC";
- $stmt = $pdo->prepare($sql);
- $stmt->execute($params);
- $rows = $stmt->fetchAll();
- $totalQte = array_sum(array_column($rows, 'quantites'));
- $totalCa = array_sum(array_column($rows, 'ca'));
- foreach ($rows as &$r) {
- $r['quantites'] = (float) $r['quantites'];
- $r['ca'] = (float) $r['ca'];
- $r['pct_qte'] = $totalQte > 0 ? round(100 * $r['quantites'] / $totalQte, 1) : 0;
- $r['pct_ca'] = $totalCa > 0 ? round(100 * $r['ca'] / $totalCa, 1) : 0;
- }
- sendJson([
- 'type' => $type,
- 'cycle' => $cycle,
- 'top_clients' => $rows,
- 'total_quantites' => $totalQte,
- 'total_ca' => $totalCa,
- ]);
|