top-clients.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * API Top clients par type de produit (FR10b).
  4. * GET ?type=alevins|larves & optionnel ?cycle=24-25|25-26
  5. */
  6. require_once __DIR__ . '/config.php';
  7. header('Access-Control-Allow-Origin: *');
  8. header('Access-Control-Allow-Methods: GET');
  9. $method = $_SERVER['REQUEST_METHOD'] ?? '';
  10. if ($method !== 'GET') {
  11. sendError('Méthode non autorisée', 405);
  12. exit;
  13. }
  14. $type = isset($_GET['type']) ? strtolower(trim($_GET['type'])) : '';
  15. $cycle = isset($_GET['cycle']) ? trim($_GET['cycle']) : null;
  16. if ($type !== 'alevins' && $type !== 'larves') {
  17. sendError('type requis: alevins ou larves', 400);
  18. exit;
  19. }
  20. if (isMockMode()) {
  21. $clients = loadMock('clients');
  22. $factures = loadMock('factures');
  23. $byClient = [];
  24. foreach ($factures as $f) {
  25. $t = $f['type_produit'] ?? '';
  26. if (strtolower($t) !== $type) continue;
  27. if ($cycle !== null && ($f['cycle'] ?? '') !== $cycle) continue;
  28. $cid = (int) $f['client_id'];
  29. if (!isset($byClient[$cid])) {
  30. $byClient[$cid] = ['client_id' => $cid, 'nom' => '', 'quantites' => 0, 'ca' => 0];
  31. foreach ($clients as $c) {
  32. if ((int) $c['id'] === $cid) { $byClient[$cid]['nom'] = $c['nom']; break; }
  33. }
  34. }
  35. $byClient[$cid]['quantites'] += (float) ($f['quantites'] ?? 0);
  36. $byClient[$cid]['ca'] += (float) $f['montant_ht'];
  37. }
  38. $rows = array_values($byClient);
  39. usort($rows, fn($a, $b) => $b['ca'] <=> $a['ca']);
  40. $totalQte = array_sum(array_column($rows, 'quantites'));
  41. $totalCa = array_sum(array_column($rows, 'ca'));
  42. foreach ($rows as &$r) {
  43. $r['pct_qte'] = $totalQte > 0 ? round(100 * $r['quantites'] / $totalQte, 1) : 0;
  44. $r['pct_ca'] = $totalCa > 0 ? round(100 * $r['ca'] / $totalCa, 1) : 0;
  45. }
  46. sendJson([
  47. 'type' => $type,
  48. 'cycle' => $cycle,
  49. 'top_clients' => $rows,
  50. 'total_quantites' => $totalQte,
  51. 'total_ca' => $totalCa,
  52. ]);
  53. exit;
  54. }
  55. $pdo = getPdo();
  56. $sql = "
  57. SELECT c.id AS client_id, c.nom,
  58. SUM(COALESCE(f.quantites, 0)) AS quantites,
  59. SUM(f.montant_ht) AS ca
  60. FROM clients c
  61. JOIN factures f ON f.client_id = c.id
  62. WHERE LOWER(TRIM(f.type_produit)) = ?
  63. ";
  64. $params = [$type];
  65. if ($cycle !== null && $cycle !== '') {
  66. $sql .= " AND TRIM(f.cycle) = ?";
  67. $params[] = $cycle;
  68. }
  69. $sql .= " GROUP BY c.id, c.nom ORDER BY ca DESC";
  70. $stmt = $pdo->prepare($sql);
  71. $stmt->execute($params);
  72. $rows = $stmt->fetchAll();
  73. $totalQte = array_sum(array_column($rows, 'quantites'));
  74. $totalCa = array_sum(array_column($rows, 'ca'));
  75. foreach ($rows as &$r) {
  76. $r['quantites'] = (float) $r['quantites'];
  77. $r['ca'] = (float) $r['ca'];
  78. $r['pct_qte'] = $totalQte > 0 ? round(100 * $r['quantites'] / $totalQte, 1) : 0;
  79. $r['pct_ca'] = $totalCa > 0 ? round(100 * $r['ca'] / $totalCa, 1) : 0;
  80. }
  81. sendJson([
  82. 'type' => $type,
  83. 'cycle' => $cycle,
  84. 'top_clients' => $rows,
  85. 'total_quantites' => $totalQte,
  86. 'total_ca' => $totalCa,
  87. ]);