| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- /**
- * API Écheanciers - FR4b, FR5b.
- * GET ?client_id= ou ?facture_id= | POST pour ajouter un écheancier (facture_id, nb_echeances, date_debut_echeancier).
- */
- 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) ?? [];
- $factureId = isset($input['facture_id']) ? (int) $input['facture_id'] : 0;
- $nbEcheances = isset($input['nb_echeances']) ? (int) $input['nb_echeances'] : 0;
- $dateDebut = isset($input['date_debut_echeancier']) ? trim($input['date_debut_echeancier']) : '';
- if ($factureId <= 0 || $nbEcheances <= 0 || $dateDebut === '') {
- sendError('facture_id, nb_echeances (positif) et date_debut_echeancier (YYYY-MM-DD) requis', 400);
- exit;
- }
- if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateDebut)) {
- sendError('date_debut_echeancier invalide (YYYY-MM-DD)', 400);
- exit;
- }
- if (isMockMode()) {
- $echeanciers = loadMock('echeanciers');
- foreach ($echeanciers as $e) {
- if ((int) $e['facture_id'] === $factureId) {
- sendError('Cette facture a déjà un écheancier', 400);
- exit;
- }
- }
- $factures = loadMock('factures');
- $paiements = loadMock('paiements');
- $facture = null;
- foreach ($factures as $f) {
- if ((int) $f['id'] === $factureId) { $facture = $f; break; }
- }
- if (!$facture) {
- sendError('Facture introuvable', 404);
- exit;
- }
- // Calculer l'encours actuel
- $totalPaye = 0;
- foreach ($paiements as $p) {
- if ((int) $p['facture_id'] === $factureId) {
- $totalPaye += (float) $p['montant'];
- }
- }
- $encours = max(0, (float) $facture['montant_ht'] - $totalPaye);
- if ($encours <= 0.01) {
- sendError('La facture est déjà soldée (encours = 0)', 400);
- exit;
- }
- $montantParEcheance = round($encours / $nbEcheances, 2);
- $newId = 1;
- foreach ($echeanciers as $e) {
- if ((int) $e['id'] >= $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);
|