<?php
/**
 * Script pour générer un rapport mensuel des paiements pour chaque membre
 */

// Inclusion des fichiers nécessaires
require_once '../config.php';
require_once '../database/init.php';
require_once '../includes/functions.php';

// Vérification de l'authentification et des permissions
requireLogin();
requirePermission('admin');

// Définition de la période du rapport (mois courant)
$startDate = date('Y-m-01');
$endDate = date('Y-m-t');

// Récupération des paiements mensuels pour chaque membre
$query = "SELECT m.id, m.first_name, m.last_name, mp.payment_month, mp.amount_paid, mp.payment_status, mp.payment_date
          FROM monthly_payments mp
          JOIN members m ON mp.member_id = m.id
          WHERE mp.payment_date BETWEEN :start_date AND :end_date
          ORDER BY m.last_name, m.first_name, mp.payment_month";
$params = [':start_date' => $startDate, ':end_date' => $endDate];
$monthlyPayments = fetchAll($query, $params);

// Titre de la page
$pageTitle = 'Rapport Mensuel des Paiements';

// Inclusion de l'en-tête
include '../includes/header.php';
?>

<div class="container mt-4">
    <h1 class="mb-4">Rapport Mensuel des Paiements</h1>
    <table class="table table-bordered">
        <thead class="table-light">
            <tr>
                <th>ID Membre</th>
                <th>Nom Complet</th>
                <th>Mois du Paiement</th>
                <th>Montant Payé</th>
                <th>Statut du Paiement</th>
                <th>Date du Paiement</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($monthlyPayments as $payment): ?>
                <tr>
                    <td><?php echo htmlspecialchars($payment['id']); ?></td>
                    <td><?php echo htmlspecialchars($payment['first_name'] . ' ' . $payment['last_name']); ?></td>
                    <td><?php echo date('F Y', strtotime($payment['payment_month'])); ?></td>
                    <td><?php echo number_format($payment['amount_paid'], 2) . ' د.م.'; ?></td>
                    <td><?php echo htmlspecialchars($payment['payment_status']); ?></td>
                    <td><?php echo date('d/m/Y', strtotime($payment['payment_date'])); ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <button onclick="window.print();" class="btn btn-secondary mt-3">Imprimer le Rapport</button>
</div>

<?php include '../includes/footer.php'; ?>
