<br />
<b>Warning</b>:  Undefined variable $auth in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<?php
// ============================================================
//  GET /api/recu.php?paiement_id=XX
//  Génère un reçu PDF de paiement de pension
// ============================================================
require_once __DIR__ . '/config.php';

header('Content-Type: application/json; charset=utf-8');

$auth     = requireAuth();
$eleve_id = (int)$auth['eleve_id'];
$paiement_id  = (int)($_GET['paiement_id'] ?? 0);

if (!$paiement_id) jsonError('paiement_id requis');

$db = getDB();

// Récupérer le paiement (vérifier qu'il appartient bien à cet élève)
$stmt = $db->prepare("
    SELECT
        pp.id, pp.inscription_id, pp.montant_paye, pp.date_paiement, pp.recu_numero,
        pt.nom_fr as tranche_nom, pt.numero_tranche,
        e.nom, e.prenom, e.matricule,
        c.nom as classe_nom,
        c.cycle_nom,
        a.libelle as annee_scolaire,
        ec.valeur  as ecole_nom,
        ec2.valeur as ecole_ville,
        ec3.valeur as ecole_telephone
    FROM pension_paiements pp
    LEFT JOIN pension_tranches pt ON pt.id = pp.tranche_id
    JOIN eleves e ON e.id = pp.eleve_id
    LEFT JOIN inscriptions i ON i.id = pp.inscription_id
    LEFT JOIN classes c ON c.id = i.classe_id
    LEFT JOIN annees_scolaires a ON a.id = i.annee_id
    LEFT JOIN config_systeme ec  ON ec.cle  = 'ecole_nom'
    LEFT JOIN config_systeme ec2 ON ec2.cle = 'ecole_ville'
    LEFT JOIN config_systeme ec3 ON ec3.cle = 'ecole_telephone'
    WHERE pp.id = ? AND pp.eleve_id = ?
    LIMIT 1
");
$stmt->execute([$paiement_id, $eleve_id]);
$p = $stmt->fetch();

if (!$p) jsonError('Paiement introuvable');

// Total de tous les paiements de l'élève
$stmtTotal = $db->prepare("
    SELECT COALESCE(SUM(montant_paye), 0) as total_paye
    FROM pension_paiements
    WHERE eleve_id = ?
");
$stmtTotal->execute([$eleve_id]);
$total_paye = (int)$stmtTotal->fetchColumn();

// Montant total pension : niveau_id + annee_id de l'inscription
$stmtConfig = $db->prepare("
    SELECT pc.montant_total
    FROM pension_config pc
    JOIN inscriptions i ON i.id = ?
    JOIN classes cl ON cl.id = i.classe_id
    WHERE pc.niveau_id = cl.niveau_id
      AND pc.annee_id  = i.annee_id
    LIMIT 1
");
$stmtConfig->execute([$p['inscription_id']]);
$montant_total = (int)($stmtConfig->fetchColumn() ?: 0);
$reste = max(0, $montant_total - $total_paye);

// Retourner les données en JSON (le PDF sera généré côté client)
jsonOk([
    'total_paye'     => $total_paye,
    'montant_total'  => $montant_total,
    'reste_a_payer'  => $reste,
    'recu_numero'    => $p['recu_numero'],
    'montant'        => (int)$p['montant_paye'],
    'date_paiement'  => $p['date_paiement'],
    'tranche'        => $p['tranche_nom'] ?? ('Tranche ' . ($p['numero_tranche'] ?? '')),
    'eleve'          => [
        'nom'        => $p['nom'],
        'prenom'     => $p['prenom'],
        'matricule'  => $p['matricule'],
        'classe'