paiements.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * API Paiements - liste par client (FR5) et enregistrement (FR4).
  4. * GET ?client_id= | POST facture_id, montant, date_paiement
  5. */
  6. require_once __DIR__ . '/config.php';
  7. header('Access-Control-Allow-Origin: *');
  8. header('Access-Control-Allow-Methods: GET, POST, DELETE');
  9. header('Access-Control-Allow-Headers: Content-Type');
  10. $method = $_SERVER['REQUEST_METHOD'] ?? '';
  11. if ($method === 'GET') {
  12. $clientId = isset($_GET['client_id']) ? (int) $_GET['client_id'] : 0;
  13. if ($clientId <= 0) {
  14. sendError('client_id requis', 400);
  15. exit;
  16. }
  17. if (isMockMode()) {
  18. $factures = loadMock('factures');
  19. $factureIds = array_column(array_filter($factures, fn($f) => (int) $f['client_id'] === $clientId), 'id');
  20. $paiements = loadMock('paiements');
  21. $rows = [];
  22. foreach ($paiements as $p) {
  23. if (in_array((int) $p['facture_id'], $factureIds)) {
  24. $nFacture = '';
  25. foreach ($factures as $f) {
  26. if ((int) $f['id'] === (int) $p['facture_id']) {
  27. $nFacture = $f['n_facture'];
  28. break;
  29. }
  30. }
  31. $rows[] = [
  32. 'id' => (int) $p['id'],
  33. 'facture_id' => (int) $p['facture_id'],
  34. 'montant' => (float) $p['montant'],
  35. 'date_paiement' => $p['date_paiement'],
  36. 'created_at' => $p['created_at'] ?? null,
  37. 'n_facture' => $nFacture,
  38. ];
  39. }
  40. }
  41. usort($rows, fn($a, $b) => strcmp($b['date_paiement'], $a['date_paiement']));
  42. sendJson($rows);
  43. exit;
  44. }
  45. $pdo = getPdo();
  46. $stmt = $pdo->prepare("
  47. SELECT p.id, p.facture_id, p.montant, p.date_paiement, p.created_at, f.n_facture
  48. FROM paiements p
  49. JOIN factures f ON f.id = p.facture_id
  50. WHERE f.client_id = ?
  51. ORDER BY p.date_paiement DESC
  52. ");
  53. $stmt->execute([$clientId]);
  54. $rows = $stmt->fetchAll();
  55. foreach ($rows as &$r) {
  56. $r['montant'] = (float) $r['montant'];
  57. $r['date_paiement'] = $r['date_paiement'];
  58. $r['created_at'] = $r['created_at'] ? (new DateTime($r['created_at']))->format(DateTime::ATOM) : null;
  59. }
  60. sendJson($rows);
  61. exit;
  62. }
  63. if ($method === 'POST') {
  64. $input = json_decode(file_get_contents('php://input'), true) ?? [];
  65. $factureId = isset($input['facture_id']) ? (int) $input['facture_id'] : 0;
  66. $montant = isset($input['montant']) ? (float) $input['montant'] : 0;
  67. $datePaiement = isset($input['date_paiement']) ? trim($input['date_paiement']) : date('Y-m-d');
  68. if ($factureId <= 0 || $montant <= 0) {
  69. sendError('facture_id et montant (positif) requis', 400);
  70. exit;
  71. }
  72. // Vérifier que la facture existe (bonne pratique ERP)
  73. if (!isMockMode()) {
  74. $pdo = getPdo();
  75. $stmt = $pdo->prepare('SELECT id FROM factures WHERE id = ?');
  76. $stmt->execute([$factureId]);
  77. if (!$stmt->fetch()) {
  78. sendError('Facture introuvable', 404);
  79. exit;
  80. }
  81. }
  82. if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $datePaiement)) {
  83. sendError('date_paiement invalide (YYYY-MM-DD)', 400);
  84. exit;
  85. }
  86. if (isMockMode()) {
  87. $paiements = loadMock('paiements');
  88. $newId = 1;
  89. foreach ($paiements as $p) {
  90. if ((int) $p['id'] >= $newId) $newId = (int) $p['id'] + 1;
  91. }
  92. $now = (new DateTime())->format(DateTime::ATOM);
  93. $row = [
  94. 'id' => $newId,
  95. 'facture_id' => $factureId,
  96. 'montant' => $montant,
  97. 'date_paiement' => $datePaiement,
  98. 'created_at' => $now,
  99. ];
  100. $paiements[] = $row;
  101. saveMock('paiements', $paiements);
  102. sendJson($row);
  103. exit;
  104. }
  105. $pdo = getPdo();
  106. $stmt = $pdo->prepare('INSERT INTO paiements (facture_id, montant, date_paiement) VALUES (?, ?, ?)');
  107. $stmt->execute([$factureId, $montant, $datePaiement]);
  108. $id = (int) $pdo->lastInsertId();
  109. $stmt = $pdo->prepare('SELECT id, facture_id, montant, date_paiement, created_at FROM paiements WHERE id = ?');
  110. $stmt->execute([$id]);
  111. $row = $stmt->fetch();
  112. $row['montant'] = (float) $row['montant'];
  113. $row['created_at'] = $row['created_at'] ? (new DateTime($row['created_at']))->format(DateTime::ATOM) : null;
  114. sendJson($row);
  115. exit;
  116. }
  117. if ($method === 'DELETE') {
  118. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  119. if ($id <= 0) {
  120. sendError('id requis', 400);
  121. exit;
  122. }
  123. if (isMockMode()) {
  124. $paiements = loadMock('paiements');
  125. $found = false;
  126. foreach ($paiements as $i => $p) {
  127. if ((int) $p['id'] === $id) {
  128. unset($paiements[$i]);
  129. $paiements = array_values($paiements);
  130. saveMock('paiements', $paiements);
  131. sendJson(['success' => true, 'id' => $id]);
  132. exit;
  133. }
  134. }
  135. sendError('Paiement non trouvé', 404);
  136. exit;
  137. }
  138. $pdo = getPdo();
  139. $stmt = $pdo->prepare('DELETE FROM paiements WHERE id = ?');
  140. $stmt->execute([$id]);
  141. if ($stmt->rowCount() === 0) {
  142. sendError('Paiement non trouvé', 404);
  143. exit;
  144. }
  145. sendJson(['success' => true, 'id' => $id]);
  146. exit;
  147. }
  148. sendError('Méthode non autorisée', 405);