factures.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * API Factures - liste par client (FR3), ajout manuel (FR3b).
  4. * La facturation provient de Pennylane ; l’API sert au suivi (saisie manuelle des factures).
  5. * GET ?client_id= | POST pour ajouter une facture (suivi).
  6. */
  7. require_once __DIR__ . '/config.php';
  8. header('Access-Control-Allow-Origin: *');
  9. header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
  10. header('Access-Control-Allow-Headers: Content-Type');
  11. $method = $_SERVER['REQUEST_METHOD'] ?? '';
  12. if ($method === 'POST') {
  13. $input = json_decode(file_get_contents('php://input'), true) ?? [];
  14. $clientId = isset($input['client_id']) ? (int) $input['client_id'] : 0;
  15. $nFacture = isset($input['n_facture']) ? trim($input['n_facture']) : '';
  16. $nVente = isset($input['n_vente']) ? trim($input['n_vente']) : '';
  17. $dateFacture = isset($input['date_facture']) ? trim($input['date_facture']) : date('Y-m-d');
  18. $typeProduit = isset($input['type_produit']) ? trim($input['type_produit']) : null;
  19. $cycle = isset($input['cycle']) ? trim($input['cycle']) : null;
  20. $quantites = isset($input['quantites']) ? (float) $input['quantites'] : null;
  21. $qteLivree = isset($input['qte_livree']) ? (float) $input['qte_livree'] : null;
  22. $montantHt = isset($input['montant_ht']) ? (float) $input['montant_ht'] : 0;
  23. if ($clientId <= 0 || $nFacture === '' || $montantHt <= 0) {
  24. sendError('client_id, n_facture et montant_ht (positif) requis', 400);
  25. exit;
  26. }
  27. if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateFacture)) {
  28. sendError('date_facture invalide (YYYY-MM-DD)', 400);
  29. exit;
  30. }
  31. if (isMockMode()) {
  32. $factures = loadMock('factures');
  33. $newId = 1;
  34. foreach ($factures as $f) {
  35. if ((int) $f['id'] >= $newId) $newId = (int) $f['id'] + 1;
  36. }
  37. $row = [
  38. 'id' => $newId,
  39. 'client_id' => $clientId,
  40. 'n_facture' => $nFacture,
  41. 'n_vente' => $nVente ?: null,
  42. 'date_facture' => $dateFacture,
  43. 'type_produit' => $typeProduit ?: null,
  44. 'cycle' => $cycle ?: null,
  45. 'quantites' => $quantites,
  46. 'qte_livree' => $qteLivree,
  47. 'montant_ht' => $montantHt,
  48. 'created_at' => (new DateTime())->format(DateTime::ATOM),
  49. ];
  50. $factures[] = $row;
  51. saveMock('factures', $factures);
  52. sendJson($row);
  53. exit;
  54. }
  55. $pdo = getPdo();
  56. $stmt = $pdo->prepare('INSERT INTO factures (client_id, n_facture, n_vente, date_facture, type_produit, cycle, quantites, qte_livree, montant_ht) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
  57. $stmt->execute([$clientId, $nFacture, $nVente ?: null, $dateFacture, $typeProduit ?: null, $cycle ?: null, $quantites, $qteLivree, $montantHt]);
  58. $id = (int) $pdo->lastInsertId();
  59. $stmt = $pdo->prepare('SELECT id, client_id, n_facture, n_vente, date_facture, type_produit, cycle, quantites, qte_livree, montant_ht, created_at FROM factures WHERE id = ?');
  60. $stmt->execute([$id]);
  61. $row = $stmt->fetch();
  62. $row['montant_ht'] = (float) $row['montant_ht'];
  63. $row['quantites'] = $row['quantites'] !== null ? (float) $row['quantites'] : null;
  64. $row['qte_livree'] = $row['qte_livree'] !== null ? (float) $row['qte_livree'] : null;
  65. $row['created_at'] = $row['created_at'] ? (new DateTime($row['created_at']))->format(DateTime::ATOM) : null;
  66. sendJson($row);
  67. exit;
  68. }
  69. if ($method === 'PUT') {
  70. $input = json_decode(file_get_contents('php://input'), true) ?? [];
  71. $id = isset($input['id']) ? (int) $input['id'] : 0;
  72. $nFacture = isset($input['n_facture']) ? trim($input['n_facture']) : '';
  73. $nVente = isset($input['n_vente']) ? trim($input['n_vente']) : '';
  74. $dateFacture = isset($input['date_facture']) ? trim($input['date_facture']) : '';
  75. $typeProduit = isset($input['type_produit']) ? trim($input['type_produit']) : null;
  76. $cycle = isset($input['cycle']) ? trim($input['cycle']) : null;
  77. $quantites = isset($input['quantites']) ? (float) $input['quantites'] : null;
  78. $qteLivree = isset($input['qte_livree']) ? (float) $input['qte_livree'] : null;
  79. $montantHt = isset($input['montant_ht']) ? (float) $input['montant_ht'] : 0;
  80. if ($id <= 0 || $nFacture === '' || $montantHt <= 0) {
  81. sendError('id, n_facture et montant_ht (positif) requis', 400);
  82. exit;
  83. }
  84. if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateFacture)) {
  85. sendError('date_facture invalide (YYYY-MM-DD)', 400);
  86. exit;
  87. }
  88. if (isMockMode()) {
  89. $factures = loadMock('factures');
  90. $found = false;
  91. foreach ($factures as &$f) {
  92. if ((int) $f['id'] === $id) {
  93. $f['n_facture'] = $nFacture;
  94. $f['n_vente'] = $nVente ?: null;
  95. $f['date_facture'] = $dateFacture;
  96. $f['type_produit'] = $typeProduit ?: null;
  97. $f['cycle'] = $cycle ?: null;
  98. $f['quantites'] = $quantites;
  99. $f['qte_livree'] = $qteLivree;
  100. $f['montant_ht'] = $montantHt;
  101. $found = true;
  102. saveMock('factures', $factures);
  103. sendJson($f);
  104. exit;
  105. }
  106. }
  107. if (!$found) {
  108. sendError('Facture non trouvée', 404);
  109. exit;
  110. }
  111. }
  112. $pdo = getPdo();
  113. $stmt = $pdo->prepare('UPDATE factures SET n_facture = ?, n_vente = ?, date_facture = ?, type_produit = ?, cycle = ?, quantites = ?, qte_livree = ?, montant_ht = ? WHERE id = ?');
  114. $stmt->execute([$nFacture, $nVente ?: null, $dateFacture, $typeProduit ?: null, $cycle ?: null, $quantites, $qteLivree, $montantHt, $id]);
  115. $stmt = $pdo->prepare('SELECT id, client_id, n_facture, n_vente, date_facture, type_produit, cycle, quantites, qte_livree, montant_ht, created_at FROM factures WHERE id = ?');
  116. $stmt->execute([$id]);
  117. $row = $stmt->fetch();
  118. if (!$row) {
  119. sendError('Facture non trouvée', 404);
  120. exit;
  121. }
  122. $row['montant_ht'] = (float) $row['montant_ht'];
  123. $row['quantites'] = $row['quantites'] !== null ? (float) $row['quantites'] : null;
  124. $row['qte_livree'] = $row['qte_livree'] !== null ? (float) $row['qte_livree'] : null;
  125. $row['created_at'] = $row['created_at'] ? (new DateTime($row['created_at']))->format(DateTime::ATOM) : null;
  126. sendJson($row);
  127. exit;
  128. }
  129. if ($method === 'DELETE') {
  130. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  131. if ($id <= 0) {
  132. sendError('id requis', 400);
  133. exit;
  134. }
  135. if (isMockMode()) {
  136. $factures = loadMock('factures');
  137. $found = false;
  138. foreach ($factures as $i => $f) {
  139. if ((int) $f['id'] === $id) {
  140. unset($factures[$i]);
  141. $factures = array_values($factures);
  142. saveMock('factures', $factures);
  143. sendJson(['success' => true, 'id' => $id]);
  144. exit;
  145. }
  146. }
  147. sendError('Facture non trouvée', 404);
  148. exit;
  149. }
  150. $pdo = getPdo();
  151. $stmt = $pdo->prepare('DELETE FROM factures WHERE id = ?');
  152. $stmt->execute([$id]);
  153. if ($stmt->rowCount() === 0) {
  154. sendError('Facture non trouvée', 404);
  155. exit;
  156. }
  157. sendJson(['success' => true, 'id' => $id]);
  158. exit;
  159. }
  160. if ($method !== 'GET') {
  161. sendError('Méthode non autorisée', 405);
  162. exit;
  163. }
  164. $clientId = isset($_GET['client_id']) ? (int) $_GET['client_id'] : 0;
  165. if ($clientId <= 0) {
  166. sendError('client_id requis', 400);
  167. exit;
  168. }
  169. if (isMockMode()) {
  170. $factures = array_filter(loadMock('factures'), fn($f) => (int) $f['client_id'] === $clientId);
  171. $paiements = loadMock('paiements');
  172. $rows = [];
  173. foreach ($factures as $f) {
  174. $totalPaye = 0;
  175. foreach ($paiements as $p) {
  176. if ((int) $p['facture_id'] === (int) $f['id']) {
  177. $totalPaye += (float) $p['montant'];
  178. }
  179. }
  180. $montantHt = (float) $f['montant_ht'];
  181. $encours = max(0, $montantHt - $totalPaye);
  182. $rows[] = [
  183. 'id' => (int) $f['id'],
  184. 'client_id' => (int) $f['client_id'],
  185. 'n_facture' => $f['n_facture'],
  186. 'n_vente' => $f['n_vente'] ?? null,
  187. 'date_facture' => $f['date_facture'],
  188. 'type_produit' => $f['type_produit'] ?? null,
  189. 'cycle' => $f['cycle'] ?? null,
  190. 'quantites' => isset($f['quantites']) ? (float) $f['quantites'] : null,
  191. 'qte_livree' => isset($f['qte_livree']) ? (float) $f['qte_livree'] : null,
  192. 'montant_ht' => $montantHt,
  193. 'created_at' => $f['created_at'] ?? null,
  194. 'total_paye' => $totalPaye,
  195. 'encours_facture' => $encours,
  196. 'etat' => $encours <= 0 ? 'paye' : 'en_attente',
  197. ];
  198. }
  199. usort($rows, fn($a, $b) => strcmp($b['date_facture'], $a['date_facture']));
  200. sendJson($rows);
  201. exit;
  202. }
  203. $pdo = getPdo();
  204. $stmt = $pdo->prepare("
  205. SELECT f.id, f.client_id, f.n_facture, f.n_vente, f.date_facture, f.type_produit, f.cycle, f.quantites, f.qte_livree, f.montant_ht, f.created_at,
  206. COALESCE(SUM(p.montant), 0) AS total_paye,
  207. GREATEST(0, f.montant_ht - COALESCE(SUM(p.montant), 0)) AS encours_facture
  208. FROM factures f
  209. LEFT JOIN paiements p ON p.facture_id = f.id
  210. WHERE f.client_id = ?
  211. GROUP BY f.id
  212. ORDER BY f.date_facture DESC
  213. ");
  214. $stmt->execute([$clientId]);
  215. $rows = $stmt->fetchAll();
  216. foreach ($rows as &$r) {
  217. $r['total_paye'] = (float) $r['total_paye'];
  218. $r['montant_ht'] = (float) $r['montant_ht'];
  219. $r['quantites'] = $r['quantites'] !== null ? (float) $r['quantites'] : null;
  220. $r['qte_livree'] = $r['qte_livree'] !== null ? (float) $r['qte_livree'] : null;
  221. $r['encours_facture'] = (float) $r['encours_facture'];
  222. $r['etat'] = $r['encours_facture'] <= 0.01 ? 'paye' : 'en_attente';
  223. $r['created_at'] = $r['created_at'] ? (new DateTime($r['created_at']))->format(DateTime::ATOM) : null;
  224. }
  225. sendJson($rows);