(int) $f['client_id'] === $clientId), 'id'); $paiements = loadMock('paiements'); $rows = []; foreach ($paiements as $p) { if (in_array((int) $p['facture_id'], $factureIds)) { $nFacture = ''; foreach ($factures as $f) { if ((int) $f['id'] === (int) $p['facture_id']) { $nFacture = $f['n_facture']; break; } } $rows[] = [ 'id' => (int) $p['id'], 'facture_id' => (int) $p['facture_id'], 'montant' => (float) $p['montant'], 'date_paiement' => $p['date_paiement'], 'created_at' => $p['created_at'] ?? null, 'n_facture' => $nFacture, ]; } } usort($rows, fn($a, $b) => strcmp($b['date_paiement'], $a['date_paiement'])); sendJson($rows); exit; } $pdo = getPdo(); $stmt = $pdo->prepare(" SELECT p.id, p.facture_id, p.montant, p.date_paiement, p.created_at, f.n_facture FROM paiements p JOIN factures f ON f.id = p.facture_id WHERE f.client_id = ? ORDER BY p.date_paiement DESC "); $stmt->execute([$clientId]); $rows = $stmt->fetchAll(); foreach ($rows as &$r) { $r['montant'] = (float) $r['montant']; $r['date_paiement'] = $r['date_paiement']; $r['created_at'] = $r['created_at'] ? (new DateTime($r['created_at']))->format(DateTime::ATOM) : null; } sendJson($rows); exit; } if ($method === 'POST') { $input = json_decode(file_get_contents('php://input'), true) ?? []; $factureId = isset($input['facture_id']) ? (int) $input['facture_id'] : 0; $montant = isset($input['montant']) ? (float) $input['montant'] : 0; $datePaiement = isset($input['date_paiement']) ? trim($input['date_paiement']) : date('Y-m-d'); if ($factureId <= 0 || $montant <= 0) { sendError('facture_id et montant (positif) requis', 400); exit; } // Vérifier que la facture existe (bonne pratique ERP) if (!isMockMode()) { $pdo = getPdo(); $stmt = $pdo->prepare('SELECT id FROM factures WHERE id = ?'); $stmt->execute([$factureId]); if (!$stmt->fetch()) { sendError('Facture introuvable', 404); exit; } } if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $datePaiement)) { sendError('date_paiement invalide (YYYY-MM-DD)', 400); exit; } if (isMockMode()) { $paiements = loadMock('paiements'); $newId = 1; foreach ($paiements as $p) { if ((int) $p['id'] >= $newId) $newId = (int) $p['id'] + 1; } $now = (new DateTime())->format(DateTime::ATOM); $row = [ 'id' => $newId, 'facture_id' => $factureId, 'montant' => $montant, 'date_paiement' => $datePaiement, 'created_at' => $now, ]; $paiements[] = $row; saveMock('paiements', $paiements); sendJson($row); exit; } $pdo = getPdo(); $stmt = $pdo->prepare('INSERT INTO paiements (facture_id, montant, date_paiement) VALUES (?, ?, ?)'); $stmt->execute([$factureId, $montant, $datePaiement]); $id = (int) $pdo->lastInsertId(); $stmt = $pdo->prepare('SELECT id, facture_id, montant, date_paiement, created_at FROM paiements WHERE id = ?'); $stmt->execute([$id]); $row = $stmt->fetch(); $row['montant'] = (float) $row['montant']; $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()) { $paiements = loadMock('paiements'); $found = false; foreach ($paiements as $i => $p) { if ((int) $p['id'] === $id) { unset($paiements[$i]); $paiements = array_values($paiements); saveMock('paiements', $paiements); sendJson(['success' => true, 'id' => $id]); exit; } } sendError('Paiement non trouvé', 404); exit; } $pdo = getPdo(); $stmt = $pdo->prepare('DELETE FROM paiements WHERE id = ?'); $stmt->execute([$id]); if ($stmt->rowCount() === 0) { sendError('Paiement non trouvé', 404); exit; } sendJson(['success' => true, 'id' => $id]); exit; } sendError('Méthode non autorisée', 405);