src/Controller/BilanCarboneController.php line 34
<?php
namespace App\Controller;
use App\Entity\Company;
use App\Entity\BCCompany;
use App\Entity\BCFamille;
use App\Entity\CompanyLabelNRAxe;
use App\Form\CompanyLabelNRTIRType;
use App\Repository\BCFamilleRepository;
use App\Repository\CompanyLabelNRTIRRepository;
use App\Service\ChartService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use App\Service\GrantService;
class BilanCarboneController extends AbstractController
{
protected $requestStack;
private GrantService $grantService;
public function __construct(RequestStack $requestStack, GrantService $grantService)
{
$this->requestStack = $requestStack;
$this->grantService = $grantService;
}
#[Route('/bilan-carbone/{company}', name: 'app_bc_index', defaults: ["company" => null])]
public function index(Company $company = null, Request $request, EntityManagerInterface $em, CompanyLabelNRTIRRepository $companyLabelNRTIRRepository, BCFamilleRepository $BCFamilleRepository): Response
{
$user = $this->getUser();
$type = 'accompanist';
if (!$company) {
if ($user->getCompanyMember()) {
$company = $user->getCompanyMember()->getCompany();
$type = 'self';
} else {
return $this->redirectToRoute('app_home');
}
}
$bilans = $company->getBCCompanies();
$lastBilan = $bilans->last();
$otherBilans = $bilans->slice(0, -1);
$total = [];
$familiesData = [];
$families = $BCFamilleRepository->findAll();
foreach ($families as $family) {
$familiesData[] = [
'entity' => $family,
'pluralName' => $family->getPluralName(),
'totalFabrication' => null,
'totalUsage' => null,
'total' => null,
];
}
if ($lastBilan) {
$lastBilanData = $lastBilan->getBilanData($families, true); // true pour activer l'arrondi si pris en compte dans la méthode
} else {
$lastBilanData = null;
}
usort($otherBilans, function ($a, $b) {
return $b->getCreatedAt() <=> $a->getCreatedAt();
});
$allBilansObject = [];
foreach ($otherBilans as $bilan) {
$allBilansObject[] = [
"id" => $bilan->getId(),
"name" => $bilan->getName(),
"date" => $bilan->getCreatedAt()->format('d/m/Y'),
"type" => $bilan->isType() ? "Simulation" : "Bilan",
];
}
return $this->render('bilan_carbone/index.html.twig', [
'controller_name' => 'BilanCarboneController',
'company' => $company,
'lastBilan' => $lastBilanData,
'otherBilans' => $otherBilans,
'allBilans' => $allBilansObject,
'bilans' => $bilans,
]);
}
#[Route('/bilan-carbone/bilan/nouveau/{company}', name: 'app_bc_create', defaults: ["company" => null])]
public function create(Company $company = null, Request $request, BCFamilleRepository $bCFamilleRepository, EntityManagerInterface $em, CompanyLabelNRTIRRepository $companyLabelNRTIRRepository): Response
{
$user = $this->getUser();
$type = 'accompanist';
if (!$company) {
if ($user->getCompanyMember()) {
$company = $user->getCompanyMember()->getCompany();
$type = 'self';
} else {
return $this->redirectToRoute('app_home');
}
}
$simulation = $request->query->get('simulation');
$families = $bCFamilleRepository->findAll();
return $this->render('bilan_carbone/create.html.twig', [
'controller_name' => 'BilanCarboneController',
'company' => $company,
'families' => $families,
'simulation' => $simulation ? true : false,
]);
}
#[Route('/bilan-carbone/bilan/modifier/{id}', name: 'app_bc_edit')]
public function edit(BCCompany $bilan, Request $request, EntityManagerInterface $em, BCFamilleRepository $bCFamilleRepository, CompanyLabelNRTIRRepository $companyLabelNRTIRRepository): Response
{
$type = 'accompanist';
$user = $this->getUser();
$company = $bilan->getCompany();
$response = $this->grantService->checkUser($user, 'company', $company);
if ($response) {
return $response;
}
$families = $bCFamilleRepository->findAll();
return $this->render('bilan_carbone/create.html.twig', [
'controller_name' => 'BilanCarboneController',
'company' => $company,
'bilan' => $bilan,
'families' => $families,
'simulation' => $bilan->isType() ? true : false,
]);
}
#[Route('/bilan-carbone/bilan/supprimer/{id}', name: 'app_bc_delete')]
public function delete(BCCompany $bilan, Request $request, EntityManagerInterface $em, CompanyLabelNRTIRRepository $companyLabelNRTIRRepository): Response
{
$company = $bilan->getCompany();
$user = $this->getUser();
$type = 'accompanist';
if (!$company) {
if ($user->getCompanyMember()) {
$company = $user->getCompanyMember()->getCompany();
$type = 'self';
} else {
return $this->redirectToRoute('app_home');
}
}
if ($bilan) {
$em->remove($bilan);
$em->flush();
$this->addFlash('success', 'Le bilan a bien été supprimé.');
}
return $this->redirectToRoute('app_bc_index', ['company' => $company->getId()]);
}
#[Route('/bilan-carbone/bilan/{bilan}', name: 'app_bc_show', defaults: ["company" => null, "bilan" => null])]
public function show(BCCompany $bilan, BCFamilleRepository $BCFamilleRepository, ChartService $chartService): Response
{
$company = $bilan->getCompany();
$response = $this->grantService->checkUser($this->getUser(), 'company', $company);
if ($response) {
return $response;
}
$families = $BCFamilleRepository->findAll();
$bilanData = $bilan->getBilanData($families);
$labelsFamily = array_column($bilanData['families'], 'labelChartFamilyImpact');
$dataFamilyImpact = array_map(fn($val) => round($val), array_column($bilanData['families'], 'total'));
$chartFamilyImpact = $chartService->newPieChart("Données (kgCO2e)", $labelsFamily, $dataFamilyImpact, $bilanData["type"]);
$labelsFabrication = [
'Achats neuf - ' . round($bilanData['percentageFabricationNew']) . "%",
'Achats occasion - ' . round($bilanData['percentageFabricationOccasion']) . "%",
'Usage - ' . round($bilanData['percentageUsage']) . "%"
];
$dataFabrication = [
round($bilanData['totalFabricationNew']),
round($bilanData['totalFabricationOccasion']),
round($bilanData['totalUsage'])
];
$chartFabricationUsage = $chartService->newPieChart("Données (kgCO2e)", $labelsFabrication, $dataFabrication, $bilanData["type"]);
$labelsFamiliesStacked = [];
$dataFabricationNew = [];
$dataFabricationOccasion = [];
$dataUsage = [];
foreach ($bilanData['families'] as $key => $family) {
$labelsFamiliesStacked[] = $family['pluralName'];
$dataFabricationNew[] = round($family['totalFabricationNew']);
$dataFabricationOccasion[] = round($family['totalFabricationOccasion']);
$dataUsage[] = round($family['totalUsage']);
}
$datasets = [
[
'label' => "Achats neuf",
'data' => $dataFabricationNew,
'backgroundColor' => $bilanData["type"] == "Simulation" ? 'rgba(86, 39, 143, 0.8)' : 'rgba(0, 115, 148, 0.8)'
],
[
'label' => "Achats occasion",
'data' => $dataFabricationOccasion,
'backgroundColor' => $bilanData["type"] == "Simulation" ? 'rgba(0, 115, 148, 0.8)' : 'rgba(86, 39, 143, 0.8)'
],
[
'label' => "Usage",
'data' => $dataUsage,
'backgroundColor' => 'rgba(96, 190, 160, 0.8)'
]
];
$chartStackedBar = $chartService->newStackedBarChart(
$labelsFamiliesStacked,
$datasets
);
$factorImpactTotalData = [];
$factorImpactPerUnitData = [];
foreach ($bilan->getBCData() as $data) {
$impactTotal = round($data->getTotalImpact());
$quantityTotal = $data->getQuantityNew() + $data->getQuantityOccasion();
$impactPerUnit = ($quantityTotal > 0) ? round($impactTotal / $quantityTotal) : null;
if ($impactTotal !== null) {
$factorImpactTotalData[] = [
'name' => $data->getFacteur()->getBCModele()->getName(),
'type' => $data->getFacteur()->getBCModele()->getBCType()->getName(),
'impact' => $impactTotal,
];
}
if ($impactPerUnit !== null) {
$factorImpactPerUnitData[] = [
'name' => $data->getFacteur()->getBCModele()->getName(),
'type' => $data->getFacteur()->getBCModele()->getBCType()->getName(),
'impactPerUnit' => $impactPerUnit,
];
}
}
usort($factorImpactTotalData, fn($a, $b) => $b['impact'] <=> $a['impact']);
usort($factorImpactPerUnitData, fn($a, $b) => $b['impactPerUnit'] <=> $a['impactPerUnit']);
$top5TotalFactors = array_slice($factorImpactTotalData, 0, 5);
$top5PerUnitFactors = array_slice($factorImpactPerUnitData, 0, 5);
return $this->render('bilan_carbone/show.html.twig', [
'company' => $company,
'bilan' => $bilanData,
'chartFamilyImpact' => $chartFamilyImpact,
'chartFabricationUsage' => $chartFabricationUsage,
'chartStackedBar' => $chartStackedBar,
'top5TotalFactors' => $top5TotalFactors,
'top5PerUnitFactors' => $top5PerUnitFactors,
]);
}
}