| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- /**
- * API Clients - liste et détail (FR1, FR2).
- * GET ?q=nom pour recherche, GET ?id= pour un client.
- */
- require_once __DIR__ . '/config.php';
- header('Access-Control-Allow-Origin: *');
- header('Access-Control-Allow-Methods: GET, POST, DELETE');
- header('Access-Control-Allow-Headers: Content-Type');
- $method = $_SERVER['REQUEST_METHOD'] ?? '';
- if ($method === 'POST') {
- $input = json_decode(file_get_contents('php://input'), true) ?? [];
- $nom = isset($input['nom']) ? trim($input['nom']) : '';
- if ($nom === '') {
- sendError('nom requis', 400);
- exit;
- }
- if (isMockMode()) {
- $clients = loadMock('clients');
- $newId = 1;
- foreach ($clients as $c) {
- if ((int) $c['id'] >= $newId) $newId = (int) $c['id'] + 1;
- }
- $row = [
- 'id' => $newId,
- 'nom' => $nom,
- 'created_at' => (new DateTime())->format(DateTime::ATOM),
- ];
- $clients[] = $row;
- saveMock('clients', $clients);
- sendJson($row);
- exit;
- }
- $pdo = getPdo();
- $stmt = $pdo->prepare('INSERT INTO clients (nom) VALUES (?)');
- $stmt->execute([$nom]);
- $id = (int) $pdo->lastInsertId();
- $stmt = $pdo->prepare('SELECT id, nom, created_at FROM clients WHERE id = ?');
- $stmt->execute([$id]);
- $row = $stmt->fetch();
- $row['created_at'] = $row['created_at'] ? (new DateTime($row['created_at']))->format(DateTime::ATOM) : null;
- sendJson($row);
- exit;
- }
- if ($method === 'DELETE') {
- $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
- if ($id <= 0) {
- sendError('id requis', 400);
- exit;
- }
- if (isMockMode()) {
- $clients = loadMock('clients');
- $found = false;
- foreach ($clients as $i => $c) {
- if ((int) $c['id'] === $id) {
- unset($clients[$i]);
- $clients = array_values($clients);
- saveMock('clients', $clients);
- sendJson(['success' => true, 'id' => $id]);
- exit;
- }
- }
- sendError('Client non trouvé', 404);
- exit;
- }
- $pdo = getPdo();
- // Vérifier que le client n'a pas de factures (cascade gérée par la DB mais on peut vérifier)
- $stmt = $pdo->prepare('SELECT COUNT(*) AS nb FROM factures WHERE client_id = ?');
- $stmt->execute([$id]);
- $row = $stmt->fetch();
- if ($row && (int) $row['nb'] > 0) {
- sendError('Impossible de supprimer : ce client a des factures associées', 400);
- exit;
- }
- $stmt = $pdo->prepare('DELETE FROM clients WHERE id = ?');
- $stmt->execute([$id]);
- if ($stmt->rowCount() === 0) {
- sendError('Client non trouvé', 404);
- exit;
- }
- sendJson(['success' => true, 'id' => $id]);
- exit;
- }
- if ($method !== 'GET') {
- sendError('Méthode non autorisée', 405);
- exit;
- }
- $id = isset($_GET['id']) ? (int) $_GET['id'] : null;
- $q = isset($_GET['q']) ? trim($_GET['q']) : '';
- if (isMockMode()) {
- $clients = loadMock('clients');
- if ($id !== null && $id > 0) {
- foreach ($clients as $c) {
- if ((int) $c['id'] === $id) {
- sendJson($c);
- exit;
- }
- }
- sendError('Client non trouvé', 404);
- exit;
- }
- if ($q !== '') {
- $clients = array_values(array_filter($clients, function ($c) use ($q) {
- return stripos($c['nom'], $q) !== false;
- }));
- }
- usort($clients, fn($a, $b) => strcasecmp($a['nom'], $b['nom']));
- sendJson($clients);
- exit;
- }
- $pdo = getPdo();
- if ($id !== null && $id > 0) {
- $stmt = $pdo->prepare('SELECT id, nom, created_at FROM clients WHERE id = ?');
- $stmt->execute([$id]);
- $row = $stmt->fetch();
- if (!$row) {
- sendError('Client non trouvé', 404);
- exit;
- }
- $row['created_at'] = $row['created_at'] ? (new DateTime($row['created_at']))->format(DateTime::ATOM) : null;
- sendJson($row);
- exit;
- }
- $sql = 'SELECT id, nom, created_at FROM clients';
- $params = [];
- if ($q !== '') {
- $sql .= ' WHERE nom LIKE ?';
- $params[] = '%' . $q . '%';
- }
- $sql .= ' ORDER BY nom';
- $stmt = $pdo->prepare($sql);
- $stmt->execute($params);
- $rows = $stmt->fetchAll();
- foreach ($rows as &$r) {
- $r['created_at'] = $r['created_at'] ? (new DateTime($r['created_at']))->format(DateTime::ATOM) : null;
- }
- sendJson($rows);
|