<?php
// src/Controller/DashboardController.php
namespace App\Controller;
use App\Repository\CardTransactionRepository;
use App\Repository\PaiementRecetteRepository;
use App\Repository\RecetteRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\DashboardRepository;
use Doctrine\ORM\EntityManagerInterface;
class DashboardController extends AbstractController
{
#[Route('/admin/dashboard', name: 'app_dashboard')]
public function index(
RecetteRepository $recetteRepository,
PaiementRecetteRepository $paiementRepository
): Response {
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN', null, 'Accès non autorisé');
$currentYear = date('Y');
$currentMonth = date('m');
// Statistiques annuelles
$caAnnuel = $recetteRepository->getCAByYear($currentYear);
// Statistiques du mois en cours
$caMoisEnCours = $recetteRepository->getCAByMonth($currentYear, $currentMonth);
// Répartition par type de paiement du mois
$paiementCB = $paiementRepository->findPaiementsCBByMonth($currentYear, $currentMonth);
$paiementEspeces = $paiementRepository->findPaiementsEspecesByMonth($currentYear, $currentMonth);
$acomptes = $recetteRepository->findAcomptesByMonth($currentYear, $currentMonth);
// Données pour le graphique d'évolution mensuelle
$evolutionMensuelle = $recetteRepository->getMonthlyEvolution($currentYear);
// Comparaison avec le mois précédent (même période)
$comparaisonMois = $recetteRepository->getComparaisonMemeJour();
// Meilleurs jours de la semaine
$bestDays = $recetteRepository->getBestDaysOfWeek($currentYear, $currentMonth);
// Meilleur jour du mois
$meilleurJour = $recetteRepository->getMeilleurJourDuMois($currentYear, $currentMonth);
return $this->render('admin/tresorerie.html.twig', [
'caAnnuel' => $caAnnuel,
'caMoisEnCours' => $caMoisEnCours,
'paiementCB' => $paiementCB,
'paiementEspeces' => $paiementEspeces,
'acomptes' => $acomptes,
'evolutionMensuelle' => $evolutionMensuelle,
'comparaisonMois' => $comparaisonMois,
'bestDays' => $bestDays,
'meilleurJour' => $meilleurJour
]);
}
#[Route('/admin/dashboard/especes-details', name: 'app_dashboard_especes_details')]
public function especesDetails(PaiementRecetteRepository $paiementRepository): JsonResponse
{
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN', null, 'Accès non autorisé');
$currentYear = date('Y');
$currentMonth = date('m');
$detailsPaiements = $paiementRepository->findDetailsPaiementsEspeces($currentYear, $currentMonth);
// Formater les données pour le JSON
$formattedData = array_map(function($paiement) {
return [
'id' => $paiement->getId(),
'date' => $paiement->getRecette()->getDate()->format('d/m/Y H:i'),
'client' => $paiement->getRecette()->getNomClient(),
'montant' => number_format($paiement->getSommePayee(), 2, ',', ' '),
'total' => number_format($paiement->getRecette()->getTotal(), 2, ',', ' '),
'acompte' => number_format($paiement->getRecette()->getAcompte(), 2, ',', ' ')
];
}, $detailsPaiements);
// Trier par date croissante (début du mois en premier)
usort($formattedData, function($a, $b) {
$dateA = \DateTime::createFromFormat('d/m/Y H:i', $a['date']);
$dateB = \DateTime::createFromFormat('d/m/Y H:i', $b['date']);
return $dateA <=> $dateB;
});
return $this->json($formattedData);
}
#[Route('/admin/dashboard/cb-details', name: 'app_dashboard_cb_details')]
public function cbDetails(PaiementRecetteRepository $paiementRepository): JsonResponse
{
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN', null, 'Accès non autorisé');
$currentYear = date('Y');
$currentMonth = date('m');
$detailsPaiements = $paiementRepository->findDetailsPaiementsCB($currentYear, $currentMonth);
return $this->json($this->formatPaiements($detailsPaiements));
}
#[Route('/admin/dashboard/acompte-details', name: 'app_dashboard_acompte_details')]
public function acompteDetails(RecetteRepository $recetteRepository): JsonResponse
{
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN', null, 'Accès non autorisé');
$currentYear = date('Y');
$currentMonth = date('m');
$detailsAcomptes = $recetteRepository->findDetailsAcomptes($currentYear, $currentMonth);
return $this->json($this->formatAcomptes($detailsAcomptes));
}
private function formatPaiements($paiements): array
{
return array_map(function($paiement) {
return [
'date' => $paiement->getRecette()->getDate()->format('d/m/Y H:i'),
'client' => $paiement->getRecette()->getNomClient(),
'montant' => number_format($paiement->getSommePayee(), 2, ',', ' ')
];
}, $paiements);
}
private function formatAcomptes($recettes): array
{
return array_map(function($recette) {
return [
'date' => $recette->getDate()->format('d/m/Y H:i'),
'client' => $recette->getNomClient(),
'montant' => number_format($recette->getAcompte(), 2, ',', ' ')
];
}, $recettes);
}
}