= $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);