echeanciers.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * API Écheanciers - FR4b, FR5b.
  4. * GET ?client_id= ou ?facture_id= | POST pour ajouter un écheancier (facture_id, nb_echeances, date_debut_echeancier).
  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 === 'POST') {
  12. $input = json_decode(file_get_contents('php://input'), true) ?? [];
  13. $factureId = isset($input['facture_id']) ? (int) $input['facture_id'] : 0;
  14. $nbEcheances = isset($input['nb_echeances']) ? (int) $input['nb_echeances'] : 0;
  15. $dateDebut = isset($input['date_debut_echeancier']) ? trim($input['date_debut_echeancier']) : '';
  16. if ($factureId <= 0 || $nbEcheances <= 0 || $dateDebut === '') {
  17. sendError('facture_id, nb_echeances (positif) et date_debut_echeancier (YYYY-MM-DD) requis', 400);
  18. exit;
  19. }
  20. if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateDebut)) {
  21. sendError('date_debut_echeancier invalide (YYYY-MM-DD)', 400);
  22. exit;
  23. }
  24. if (isMockMode()) {
  25. $echeanciers = loadMock('echeanciers');
  26. foreach ($echeanciers as $e) {
  27. if ((int) $e['facture_id'] === $factureId) {
  28. sendError('Cette facture a déjà un écheancier', 400);
  29. exit;
  30. }
  31. }
  32. $factures = loadMock('factures');
  33. $paiements = loadMock('paiements');
  34. $facture = null;
  35. foreach ($factures as $f) {
  36. if ((int) $f['id'] === $factureId) { $facture = $f; break; }
  37. }
  38. if (!$facture) {
  39. sendError('Facture introuvable', 404);
  40. exit;
  41. }
  42. // Calculer l'encours actuel
  43. $totalPaye = 0;
  44. foreach ($paiements as $p) {
  45. if ((int) $p['facture_id'] === $factureId) {
  46. $totalPaye += (float) $p['montant'];
  47. }
  48. }
  49. $encours = max(0, (float) $facture['montant_ht'] - $totalPaye);
  50. if ($encours <= 0.01) {
  51. sendError('La facture est déjà soldée (encours = 0)', 400);
  52. exit;
  53. }
  54. $montantParEcheance = round($encours / $nbEcheances, 2);
  55. $newId = 1;
  56. foreach ($echeanciers as $e) {
  57. if ((int) $e['id'] >= $newId) $newId = (int) $e['id'] + 1;
  58. }
  59. $row = [
  60. 'id' => $newId,
  61. 'facture_id' => $factureId,
  62. 'nb_echeances' => $nbEcheances,
  63. 'date_debut_echeancier' => $dateDebut,
  64. 'montant_par_echeance' => $montantParEcheance,
  65. 'created_at' => (new DateTime())->format(DateTime::ATOM),
  66. ];
  67. $echeanciers[] = $row;
  68. saveMock('echeanciers', $echeanciers);
  69. sendJson($row);
  70. exit;
  71. }
  72. $pdo = getPdo();
  73. // Vérifier que la facture n'a pas déjà un écheancier
  74. $stmt = $pdo->prepare('SELECT id FROM echeanciers WHERE facture_id = ?');
  75. $stmt->execute([$factureId]);
  76. if ($stmt->fetch()) {
  77. sendError('Cette facture a déjà un écheancier', 400);
  78. exit;
  79. }
  80. // Vérifier que la facture existe et calculer l'encours
  81. $stmt = $pdo->prepare('
  82. SELECT f.id, f.montant_ht,
  83. COALESCE(SUM(p.montant), 0) AS total_paye
  84. FROM factures f
  85. LEFT JOIN paiements p ON p.facture_id = f.id
  86. WHERE f.id = ?
  87. GROUP BY f.id, f.montant_ht
  88. ');
  89. $stmt->execute([$factureId]);
  90. $facture = $stmt->fetch();
  91. if (!$facture) {
  92. sendError('Facture introuvable', 404);
  93. exit;
  94. }
  95. $encours = max(0, (float) $facture['montant_ht'] - (float) $facture['total_paye']);
  96. if ($encours <= 0.01) {
  97. sendError('La facture est déjà soldée (encours = 0)', 400);
  98. exit;
  99. }
  100. $montantParEcheance = round($encours / $nbEcheances, 2);
  101. // Insérer l'écheancier avec le montant calculé
  102. $stmt = $pdo->prepare('INSERT INTO echeanciers (facture_id, nb_echeances, date_debut_echeancier, montant_par_echeance) VALUES (?, ?, ?, ?)');
  103. $stmt->execute([$factureId, $nbEcheances, $dateDebut, $montantParEcheance]);
  104. $id = (int) $pdo->lastInsertId();
  105. $stmt = $pdo->prepare('SELECT id, facture_id, nb_echeances, date_debut_echeancier, montant_par_echeance, created_at FROM echeanciers WHERE id = ?');
  106. $stmt->execute([$id]);
  107. $row = $stmt->fetch();
  108. $row['montant_par_echeance'] = (float) $row['montant_par_echeance'];
  109. $row['created_at'] = $row['created_at'] ? (new DateTime($row['created_at']))->format(DateTime::ATOM) : null;
  110. sendJson($row);
  111. exit;
  112. }
  113. if ($method === 'DELETE') {
  114. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  115. if ($id <= 0) {
  116. sendError('id requis', 400);
  117. exit;
  118. }
  119. if (isMockMode()) {
  120. $echeanciers = loadMock('echeanciers');
  121. $found = false;
  122. foreach ($echeanciers as $i => $e) {
  123. if ((int) $e['id'] === $id) {
  124. unset($echeanciers[$i]);
  125. $echeanciers = array_values($echeanciers);
  126. saveMock('echeanciers', $echeanciers);
  127. sendJson(['success' => true, 'id' => $id]);
  128. exit;
  129. }
  130. }
  131. sendError('Écheancier non trouvé', 404);
  132. exit;
  133. }
  134. $pdo = getPdo();
  135. $stmt = $pdo->prepare('DELETE FROM echeanciers WHERE id = ?');
  136. $stmt->execute([$id]);
  137. if ($stmt->rowCount() === 0) {
  138. sendError('Écheancier non trouvé', 404);
  139. exit;
  140. }
  141. sendJson(['success' => true, 'id' => $id]);
  142. exit;
  143. }
  144. if ($method !== 'GET') {
  145. sendError('Méthode non autorisée', 405);
  146. exit;
  147. }
  148. $clientId = isset($_GET['client_id']) ? (int) $_GET['client_id'] : 0;
  149. $factureId = isset($_GET['facture_id']) ? (int) $_GET['facture_id'] : 0;
  150. if ($clientId <= 0 && $factureId <= 0) {
  151. sendError('client_id ou facture_id requis', 400);
  152. exit;
  153. }
  154. if (isMockMode()) {
  155. $echeanciers = loadMock('echeanciers');
  156. $factures = loadMock('factures');
  157. if ($factureId > 0) {
  158. $echeanciers = array_filter($echeanciers, fn($e) => (int) $e['facture_id'] === $factureId);
  159. } else {
  160. $factureIds = array_column(array_filter($factures, fn($f) => (int) $f['client_id'] === $clientId), 'id');
  161. $echeanciers = array_filter($echeanciers, fn($e) => in_array((int) $e['facture_id'], $factureIds));
  162. }
  163. sendJson(array_values($echeanciers));
  164. exit;
  165. }
  166. $pdo = getPdo();
  167. if ($factureId > 0) {
  168. $stmt = $pdo->prepare('SELECT id, facture_id, nb_echeances, date_debut_echeancier, montant_par_echeance, created_at FROM echeanciers WHERE facture_id = ?');
  169. $stmt->execute([$factureId]);
  170. } else {
  171. $stmt = $pdo->prepare('
  172. SELECT e.id, e.facture_id, e.nb_echeances, e.date_debut_echeancier, e.montant_par_echeance, e.created_at
  173. FROM echeanciers e
  174. INNER JOIN factures f ON f.id = e.facture_id
  175. WHERE f.client_id = ?
  176. ');
  177. $stmt->execute([$clientId]);
  178. }
  179. $rows = $stmt->fetchAll();
  180. foreach ($rows as &$r) {
  181. $r['montant_par_echeance'] = (float) $r['montant_par_echeance'];
  182. $r['created_at'] = $r['created_at'] ? (new DateTime($r['created_at']))->format(DateTime::ATOM) : null;
  183. }
  184. sendJson($rows);