| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- /**
- * API Paiements - liste par client (FR5) et enregistrement (FR4).
- * GET ?client_id= | POST facture_id, montant, date_paiement
- */
- 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 === 'GET') {
- $clientId = isset($_GET['client_id']) ? (int) $_GET['client_id'] : 0;
- if ($clientId <= 0) {
- sendError('client_id requis', 400);
- exit;
- }
- if (isMockMode()) {
- $factures = loadMock('factures');
- $factureIds = array_column(array_filter($factures, fn($f) => (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);
|