= $newId) $newId = (int) $e['id'] + 1; } $row = [ 'id' => $newId, 'facture_id' => $factureId, 'nb_echeances' => $nbEcheances, 'date_debut_echeancier' => $dateDebut, 'montant_par_echeance' => $montantParEcheance, 'created_at' => (new DateTime())->format(DateTime::ATOM), ]; $echeanciers[] = $row; saveMock('echeanciers', $echeanciers); sendJson($row); exit; } $pdo = getPdo(); // Vérifier que la facture n'a pas déjà un écheancier $stmt = $pdo->prepare('SELECT id FROM echeanciers WHERE facture_id = ?'); $stmt->execute([$factureId]); if ($stmt->fetch()) { sendError('Cette facture a déjà un écheancier', 400); exit; } // Vérifier que la facture existe et calculer l'encours $stmt = $pdo->prepare(' SELECT f.id, f.montant_ht, COALESCE(SUM(p.montant), 0) AS total_paye FROM factures f LEFT JOIN paiements p ON p.facture_id = f.id WHERE f.id = ? GROUP BY f.id, f.montant_ht '); $stmt->execute([$factureId]); $facture = $stmt->fetch(); if (!$facture) { sendError('Facture introuvable', 404); exit; } $encours = max(0, (float) $facture['montant_ht'] - (float) $facture['total_paye']); if ($encours <= 0.01) { sendError('La facture est déjà soldée (encours = 0)', 400); exit; } $montantParEcheance = round($encours / $nbEcheances, 2); // Insérer l'écheancier avec le montant calculé $stmt = $pdo->prepare('INSERT INTO echeanciers (facture_id, nb_echeances, date_debut_echeancier, montant_par_echeance) VALUES (?, ?, ?, ?)'); $stmt->execute([$factureId, $nbEcheances, $dateDebut, $montantParEcheance]); $id = (int) $pdo->lastInsertId(); $stmt = $pdo->prepare('SELECT id, facture_id, nb_echeances, date_debut_echeancier, montant_par_echeance, created_at FROM echeanciers WHERE id = ?'); $stmt->execute([$id]); $row = $stmt->fetch(); $row['montant_par_echeance'] = (float) $row['montant_par_echeance']; $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()) { $echeanciers = loadMock('echeanciers'); $found = false; foreach ($echeanciers as $i => $e) { if ((int) $e['id'] === $id) { unset($echeanciers[$i]); $echeanciers = array_values($echeanciers); saveMock('echeanciers', $echeanciers); sendJson(['success' => true, 'id' => $id]); exit; } } sendError('Écheancier non trouvé', 404); exit; } $pdo = getPdo(); $stmt = $pdo->prepare('DELETE FROM echeanciers WHERE id = ?'); $stmt->execute([$id]); if ($stmt->rowCount() === 0) { sendError('Écheancier non trouvé', 404); exit; } sendJson(['success' => true, 'id' => $id]); exit; } if ($method !== 'GET') { sendError('Méthode non autorisée', 405); exit; } $clientId = isset($_GET['client_id']) ? (int) $_GET['client_id'] : 0; $factureId = isset($_GET['facture_id']) ? (int) $_GET['facture_id'] : 0; if ($clientId <= 0 && $factureId <= 0) { sendError('client_id ou facture_id requis', 400); exit; } if (isMockMode()) { $echeanciers = loadMock('echeanciers'); $factures = loadMock('factures'); if ($factureId > 0) { $echeanciers = array_filter($echeanciers, fn($e) => (int) $e['facture_id'] === $factureId); } else { $factureIds = array_column(array_filter($factures, fn($f) => (int) $f['client_id'] === $clientId), 'id'); $echeanciers = array_filter($echeanciers, fn($e) => in_array((int) $e['facture_id'], $factureIds)); } sendJson(array_values($echeanciers)); exit; } $pdo = getPdo(); if ($factureId > 0) { $stmt = $pdo->prepare('SELECT id, facture_id, nb_echeances, date_debut_echeancier, montant_par_echeance, created_at FROM echeanciers WHERE facture_id = ?'); $stmt->execute([$factureId]); } else { $stmt = $pdo->prepare(' SELECT e.id, e.facture_id, e.nb_echeances, e.date_debut_echeancier, e.montant_par_echeance, e.created_at FROM echeanciers e INNER JOIN factures f ON f.id = e.facture_id WHERE f.client_id = ? '); $stmt->execute([$clientId]); } $rows = $stmt->fetchAll(); foreach ($rows as &$r) { $r['montant_par_echeance'] = (float) $r['montant_par_echeance']; $r['created_at'] = $r['created_at'] ? (new DateTime($r['created_at']))->format(DateTime::ATOM) : null; } sendJson($rows);