clients.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * API Clients - liste et détail (FR1, FR2).
  4. * GET ?q=nom pour recherche, GET ?id= pour un client.
  5. */
  6. require_once __DIR__ . '/config.php';
  7. header('Access-Control-Allow-Origin: *');
  8. header('Access-Control-Allow-Methods: GET, POST, DELETE');
  9. header('Access-Control-Allow-Headers: Content-Type');
  10. $method = $_SERVER['REQUEST_METHOD'] ?? '';
  11. if ($method === 'POST') {
  12. $input = json_decode(file_get_contents('php://input'), true) ?? [];
  13. $nom = isset($input['nom']) ? trim($input['nom']) : '';
  14. if ($nom === '') {
  15. sendError('nom requis', 400);
  16. exit;
  17. }
  18. if (isMockMode()) {
  19. $clients = loadMock('clients');
  20. $newId = 1;
  21. foreach ($clients as $c) {
  22. if ((int) $c['id'] >= $newId) $newId = (int) $c['id'] + 1;
  23. }
  24. $row = [
  25. 'id' => $newId,
  26. 'nom' => $nom,
  27. 'created_at' => (new DateTime())->format(DateTime::ATOM),
  28. ];
  29. $clients[] = $row;
  30. saveMock('clients', $clients);
  31. sendJson($row);
  32. exit;
  33. }
  34. $pdo = getPdo();
  35. $stmt = $pdo->prepare('INSERT INTO clients (nom) VALUES (?)');
  36. $stmt->execute([$nom]);
  37. $id = (int) $pdo->lastInsertId();
  38. $stmt = $pdo->prepare('SELECT id, nom, created_at FROM clients WHERE id = ?');
  39. $stmt->execute([$id]);
  40. $row = $stmt->fetch();
  41. $row['created_at'] = $row['created_at'] ? (new DateTime($row['created_at']))->format(DateTime::ATOM) : null;
  42. sendJson($row);
  43. exit;
  44. }
  45. if ($method === 'DELETE') {
  46. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  47. if ($id <= 0) {
  48. sendError('id requis', 400);
  49. exit;
  50. }
  51. if (isMockMode()) {
  52. $clients = loadMock('clients');
  53. $found = false;
  54. foreach ($clients as $i => $c) {
  55. if ((int) $c['id'] === $id) {
  56. unset($clients[$i]);
  57. $clients = array_values($clients);
  58. saveMock('clients', $clients);
  59. sendJson(['success' => true, 'id' => $id]);
  60. exit;
  61. }
  62. }
  63. sendError('Client non trouvé', 404);
  64. exit;
  65. }
  66. $pdo = getPdo();
  67. // Vérifier que le client n'a pas de factures (cascade gérée par la DB mais on peut vérifier)
  68. $stmt = $pdo->prepare('SELECT COUNT(*) AS nb FROM factures WHERE client_id = ?');
  69. $stmt->execute([$id]);
  70. $row = $stmt->fetch();
  71. if ($row && (int) $row['nb'] > 0) {
  72. sendError('Impossible de supprimer : ce client a des factures associées', 400);
  73. exit;
  74. }
  75. $stmt = $pdo->prepare('DELETE FROM clients WHERE id = ?');
  76. $stmt->execute([$id]);
  77. if ($stmt->rowCount() === 0) {
  78. sendError('Client non trouvé', 404);
  79. exit;
  80. }
  81. sendJson(['success' => true, 'id' => $id]);
  82. exit;
  83. }
  84. if ($method !== 'GET') {
  85. sendError('Méthode non autorisée', 405);
  86. exit;
  87. }
  88. $id = isset($_GET['id']) ? (int) $_GET['id'] : null;
  89. $q = isset($_GET['q']) ? trim($_GET['q']) : '';
  90. if (isMockMode()) {
  91. $clients = loadMock('clients');
  92. if ($id !== null && $id > 0) {
  93. foreach ($clients as $c) {
  94. if ((int) $c['id'] === $id) {
  95. sendJson($c);
  96. exit;
  97. }
  98. }
  99. sendError('Client non trouvé', 404);
  100. exit;
  101. }
  102. if ($q !== '') {
  103. $clients = array_values(array_filter($clients, function ($c) use ($q) {
  104. return stripos($c['nom'], $q) !== false;
  105. }));
  106. }
  107. usort($clients, fn($a, $b) => strcasecmp($a['nom'], $b['nom']));
  108. sendJson($clients);
  109. exit;
  110. }
  111. $pdo = getPdo();
  112. if ($id !== null && $id > 0) {
  113. $stmt = $pdo->prepare('SELECT id, nom, created_at FROM clients WHERE id = ?');
  114. $stmt->execute([$id]);
  115. $row = $stmt->fetch();
  116. if (!$row) {
  117. sendError('Client non trouvé', 404);
  118. exit;
  119. }
  120. $row['created_at'] = $row['created_at'] ? (new DateTime($row['created_at']))->format(DateTime::ATOM) : null;
  121. sendJson($row);
  122. exit;
  123. }
  124. $sql = 'SELECT id, nom, created_at FROM clients';
  125. $params = [];
  126. if ($q !== '') {
  127. $sql .= ' WHERE nom LIKE ?';
  128. $params[] = '%' . $q . '%';
  129. }
  130. $sql .= ' ORDER BY nom';
  131. $stmt = $pdo->prepare($sql);
  132. $stmt->execute($params);
  133. $rows = $stmt->fetchAll();
  134. foreach ($rows as &$r) {
  135. $r['created_at'] = $r['created_at'] ? (new DateTime($r['created_at']))->format(DateTime::ATOM) : null;
  136. }
  137. sendJson($rows);