<?php
namespace App\Controller\Backend;
use App\Entity\Entreprise;
use App\Entity\Region;
use App\Repository\CotisationRepository;
use App\Repository\EnterpriseRepository;
use App\Repository\RegionRepository;
use DateTime;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
use Symfony\UX\Chartjs\Model\Chart;
#[IsGranted('ROLE_USER', message: "Cher utilisateur FFCB, Vous n'avez pas l'autorisation pour cette action !")]
#[Route('/admin')]
class DefaultController extends AbstractController
{
#[Route(path: '/', name: 'app_homepage')]
public function index(
Request $request,
EnterpriseRepository $enterpriseRepository,
CotisationRepository $cotisationRepository,
RegionRepository $regionRepository,
ChartBuilderInterface $chartBuilder
)
{
$colors = [
'rgb(255, 99, 132)',
'rgb(255, 159, 64)',
'rgb(255, 205, 86)',
'rgb(75, 192, 192)',
'rgb(54, 162, 235)',
'rgb(153, 102, 255)',
'rgb(201, 203, 207)',
'rgb(50, 59, 116)'
];
$cotisationLimit = new DateTime((new DateTime())->format('Y').'-07-01');
$now = new DateTime();
if ($now <= $cotisationLimit) {
$nowYear = ((int) (new \DateTime())->format('Y')) - 1;
} else {
$nowYear = (int) (new \DateTime())->format('Y');
}
$nowYear = (int) (new \DateTime())->format('Y');
$mapSelectedYear = $request->query->get('year', $nowYear);
$regions = $regionRepository->findAll();
$memberPerRegion = [];
$count = 0;
/**
* @var int $key
* @var Region $region
*/
foreach ($regions as $key => $region) {
$countMembers = $enterpriseRepository->countMemberPerRegion($mapSelectedYear, $region->getId());
$memberPerRegion[$key] = [
'countMember' => $countMembers !== null ? $countMembers[1] : 0,
'name' => $region->getNom(),
'id' => $region->getId()
];
if ($countMembers !== null) {
$count += $countMembers[1];
}
}
$membersPerYear = $cotisationRepository->getMemberPerYear($nowYear);
$chartMemberPerYearLabel = [];
$chartMemberPerYearData = [];
foreach ($membersPerYear as $item) {
$chartMemberPerYearLabel[] = $item['year'];
$chartMemberPerYearData[] = $item['countMember'];
}
$chartMemberPerYear = $chartBuilder->createChart(Chart::TYPE_BAR);
$chartMemberPerYear->setData([
'labels' => $chartMemberPerYearLabel,
'datasets' => [
[
'backgroundColor' => [
'rgba(0, 33, 215, 0.7)',
'rgba(127, 255, 0, 0.7)',
'rgba(255, 92, 0, 0.7)',
],
'borderColor' => [
'rgb(0, 33, 215)',
'rgb(127, 255, 0)',
'rgb(255, 92, 0)',
],
'data' => $chartMemberPerYearData,
],
],
]);
$chartMemberPerYear->setOptions([
'plugins' => [
'legend' => [
'display' => false,
'title' => false
]
]
]);
$chartFirmByType = $chartBuilder->createChart(Chart::TYPE_DOUGHNUT);
$countFirmByType = $enterpriseRepository->countFirmByType();
$countFirmByTypeLabels = [];
$countFirmByTypeData = [];
foreach ($countFirmByType as $key => $item) {
$countFirmByTypeLabels[] = $item['name'];
$countFirmByTypeData[] = $item['countFirm'];
}
$chartFirmByType->setData([
'labels' => $countFirmByTypeLabels,
'datasets' => [[
'label' => 'Nb entreprise par type',
'data' => $countFirmByTypeData,
'backgroundColor' => $colors,
'hoverOffset' => 4
]]
]);
return $this->render('backend/default/index.html.twig', [
'menu' => 'home',
'chartMemberPerYear' => $chartMemberPerYear,
'chartFirmByType' => $chartFirmByType,
'memberPerRegion' => $memberPerRegion,
'nowYear' => $nowYear,
'mapSelectedYear' => $mapSelectedYear
]);
}
#[Route('/region-details/{id}', name: 'home_region_details', methods: ['POST'])]
public function regionDetails(Region $region, Request $request, EnterpriseRepository $enterpriseRepository): JsonResponse
{
$datas = json_decode($request->getContent(), true);
$members = $enterpriseRepository->findMemberPerRegion($region, $datas['year']);
$results = [
'region' => $region->getNom(),
'datas' => []
];
/** @var Entreprise $member */
foreach ($members as $member) {
$results['datas'][] = [
'id' => $member->getId(),
'department' => $member->getDepartement()->getNom(),
'name' => $member->getRaisonSocial(),
'url' => $this->generateUrl('show_entreprise', ['id' => $member->getId()])
];
}
return new JsonResponse($results);
}
}