= $newId) $newId = (int) $f['id'] + 1; } $row = [ 'id' => $newId, 'client_id' => $clientId, 'n_facture' => $nFacture, 'n_vente' => $nVente ?: null, 'date_facture' => $dateFacture, 'type_produit' => $typeProduit ?: null, 'cycle' => $cycle ?: null, 'quantites' => $quantites, 'qte_livree' => $qteLivree, 'montant_ht' => $montantHt, 'created_at' => (new DateTime())->format(DateTime::ATOM), ]; $factures[] = $row; saveMock('factures', $factures); sendJson($row); exit; } $pdo = getPdo(); $stmt = $pdo->prepare('INSERT INTO factures (client_id, n_facture, n_vente, date_facture, type_produit, cycle, quantites, qte_livree, montant_ht) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'); $stmt->execute([$clientId, $nFacture, $nVente ?: null, $dateFacture, $typeProduit ?: null, $cycle ?: null, $quantites, $qteLivree, $montantHt]); $id = (int) $pdo->lastInsertId(); $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 = ?'); $stmt->execute([$id]); $row = $stmt->fetch(); $row['montant_ht'] = (float) $row['montant_ht']; $row['quantites'] = $row['quantites'] !== null ? (float) $row['quantites'] : null; $row['qte_livree'] = $row['qte_livree'] !== null ? (float) $row['qte_livree'] : null; $row['created_at'] = $row['created_at'] ? (new DateTime($row['created_at']))->format(DateTime::ATOM) : null; sendJson($row); exit; } if ($method === 'PUT') { $input = json_decode(file_get_contents('php://input'), true) ?? []; $id = isset($input['id']) ? (int) $input['id'] : 0; $nFacture = isset($input['n_facture']) ? trim($input['n_facture']) : ''; $nVente = isset($input['n_vente']) ? trim($input['n_vente']) : ''; $dateFacture = isset($input['date_facture']) ? trim($input['date_facture']) : ''; $typeProduit = isset($input['type_produit']) ? trim($input['type_produit']) : null; $cycle = isset($input['cycle']) ? trim($input['cycle']) : null; $quantites = isset($input['quantites']) ? (float) $input['quantites'] : null; $qteLivree = isset($input['qte_livree']) ? (float) $input['qte_livree'] : null; $montantHt = isset($input['montant_ht']) ? (float) $input['montant_ht'] : 0; if ($id <= 0 || $nFacture === '' || $montantHt <= 0) { sendError('id, n_facture et montant_ht (positif) requis', 400); exit; } if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateFacture)) { sendError('date_facture invalide (YYYY-MM-DD)', 400); exit; } if (isMockMode()) { $factures = loadMock('factures'); $found = false; foreach ($factures as &$f) { if ((int) $f['id'] === $id) { $f['n_facture'] = $nFacture; $f['n_vente'] = $nVente ?: null; $f['date_facture'] = $dateFacture; $f['type_produit'] = $typeProduit ?: null; $f['cycle'] = $cycle ?: null; $f['quantites'] = $quantites; $f['qte_livree'] = $qteLivree; $f['montant_ht'] = $montantHt; $found = true; saveMock('factures', $factures); sendJson($f); exit; } } if (!$found) { sendError('Facture non trouvée', 404); exit; } } $pdo = getPdo(); $stmt = $pdo->prepare('UPDATE factures SET n_facture = ?, n_vente = ?, date_facture = ?, type_produit = ?, cycle = ?, quantites = ?, qte_livree = ?, montant_ht = ? WHERE id = ?'); $stmt->execute([$nFacture, $nVente ?: null, $dateFacture, $typeProduit ?: null, $cycle ?: null, $quantites, $qteLivree, $montantHt, $id]); $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 = ?'); $stmt->execute([$id]); $row = $stmt->fetch(); if (!$row) { sendError('Facture non trouvée', 404); exit; } $row['montant_ht'] = (float) $row['montant_ht']; $row['quantites'] = $row['quantites'] !== null ? (float) $row['quantites'] : null; $row['qte_livree'] = $row['qte_livree'] !== null ? (float) $row['qte_livree'] : null; $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()) { $factures = loadMock('factures'); $found = false; foreach ($factures as $i => $f) { if ((int) $f['id'] === $id) { unset($factures[$i]); $factures = array_values($factures); saveMock('factures', $factures); sendJson(['success' => true, 'id' => $id]); exit; } } sendError('Facture non trouvée', 404); exit; } $pdo = getPdo(); $stmt = $pdo->prepare('DELETE FROM factures WHERE id = ?'); $stmt->execute([$id]); if ($stmt->rowCount() === 0) { sendError('Facture non trouvée', 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; if ($clientId <= 0) { sendError('client_id requis', 400); exit; } if (isMockMode()) { $factures = array_filter(loadMock('factures'), fn($f) => (int) $f['client_id'] === $clientId); $paiements = loadMock('paiements'); $rows = []; foreach ($factures as $f) { $totalPaye = 0; foreach ($paiements as $p) { if ((int) $p['facture_id'] === (int) $f['id']) { $totalPaye += (float) $p['montant']; } } $montantHt = (float) $f['montant_ht']; $encours = max(0, $montantHt - $totalPaye); $rows[] = [ 'id' => (int) $f['id'], 'client_id' => (int) $f['client_id'], 'n_facture' => $f['n_facture'], 'n_vente' => $f['n_vente'] ?? null, 'date_facture' => $f['date_facture'], 'type_produit' => $f['type_produit'] ?? null, 'cycle' => $f['cycle'] ?? null, 'quantites' => isset($f['quantites']) ? (float) $f['quantites'] : null, 'qte_livree' => isset($f['qte_livree']) ? (float) $f['qte_livree'] : null, 'montant_ht' => $montantHt, 'created_at' => $f['created_at'] ?? null, 'total_paye' => $totalPaye, 'encours_facture' => $encours, 'etat' => $encours <= 0 ? 'paye' : 'en_attente', ]; } usort($rows, fn($a, $b) => strcmp($b['date_facture'], $a['date_facture'])); sendJson($rows); exit; } $pdo = getPdo(); $stmt = $pdo->prepare(" 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, COALESCE(SUM(p.montant), 0) AS total_paye, GREATEST(0, f.montant_ht - COALESCE(SUM(p.montant), 0)) AS encours_facture FROM factures f LEFT JOIN paiements p ON p.facture_id = f.id WHERE f.client_id = ? GROUP BY f.id ORDER BY f.date_facture DESC "); $stmt->execute([$clientId]); $rows = $stmt->fetchAll(); foreach ($rows as &$r) { $r['total_paye'] = (float) $r['total_paye']; $r['montant_ht'] = (float) $r['montant_ht']; $r['quantites'] = $r['quantites'] !== null ? (float) $r['quantites'] : null; $r['qte_livree'] = $r['qte_livree'] !== null ? (float) $r['qte_livree'] : null; $r['encours_facture'] = (float) $r['encours_facture']; $r['etat'] = $r['encours_facture'] <= 0.01 ? 'paye' : 'en_attente'; $r['created_at'] = $r['created_at'] ? (new DateTime($r['created_at']))->format(DateTime::ATOM) : null; } sendJson($rows);