src/Controller/BilanCarboneController.php line 34

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Company;
  4. use App\Entity\BCCompany;
  5. use App\Entity\BCFamille;
  6. use App\Entity\CompanyLabelNRAxe;
  7. use App\Form\CompanyLabelNRTIRType;
  8. use App\Repository\BCFamilleRepository;
  9. use App\Repository\CompanyLabelNRTIRRepository;
  10. use App\Service\ChartService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Bundle\SecurityBundle\Security;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use App\Service\GrantService;
  19. class BilanCarboneController extends AbstractController
  20. {
  21.     protected $requestStack;
  22.     private GrantService $grantService;
  23.     public function __construct(RequestStack $requestStackGrantService $grantService)
  24.     {
  25.         $this->requestStack $requestStack;
  26.         $this->grantService $grantService;
  27.     }
  28.     #[Route('/bilan-carbone/{company}'name'app_bc_index'defaults: ["company" => null])]
  29.     public function index(Company $company nullRequest $requestEntityManagerInterface $emCompanyLabelNRTIRRepository $companyLabelNRTIRRepositoryBCFamilleRepository $BCFamilleRepository): Response
  30.     {
  31.         $user $this->getUser();
  32.         $type 'accompanist';
  33.         if (!$company) {
  34.             if ($user->getCompanyMember()) {
  35.                 $company $user->getCompanyMember()->getCompany();
  36.                 $type 'self';
  37.             } else {
  38.                 return $this->redirectToRoute('app_home');
  39.             }
  40.         }
  41.         $bilans $company->getBCCompanies();
  42.         $lastBilan $bilans->last();
  43.         $otherBilans $bilans->slice(0, -1);
  44.         $total = [];
  45.         $familiesData = [];
  46.         $families $BCFamilleRepository->findAll();
  47.         foreach ($families as $family) {
  48.             $familiesData[] = [
  49.                 'entity' => $family,
  50.                 'pluralName' => $family->getPluralName(),
  51.                 'totalFabrication' => null,
  52.                 'totalUsage' => null,
  53.                 'total' => null,
  54.             ];
  55.         }
  56.         if ($lastBilan) {
  57.             $lastBilanData $lastBilan->getBilanData($familiestrue); // true pour activer l'arrondi si pris en compte dans la méthode
  58.         } else {
  59.             $lastBilanData null;
  60.         }
  61.         usort($otherBilans, function ($a$b) {
  62.             return $b->getCreatedAt() <=> $a->getCreatedAt();
  63.         });
  64.         $allBilansObject = [];
  65.         foreach ($otherBilans as $bilan) {
  66.             $allBilansObject[] = [
  67.                 "id" => $bilan->getId(),
  68.                 "name" => $bilan->getName(),
  69.                 "date" => $bilan->getCreatedAt()->format('d/m/Y'),
  70.                 "type" => $bilan->isType() ? "Simulation" "Bilan",
  71.             ];
  72.         }
  73.         return $this->render('bilan_carbone/index.html.twig', [
  74.             'controller_name' => 'BilanCarboneController',
  75.             'company' => $company,
  76.             'lastBilan' => $lastBilanData,
  77.             'otherBilans' => $otherBilans,
  78.             'allBilans' => $allBilansObject,
  79.             'bilans' => $bilans,
  80.         ]);
  81.     }
  82.     #[Route('/bilan-carbone/bilan/nouveau/{company}'name'app_bc_create'defaults: ["company" => null])]
  83.     public function create(Company $company nullRequest $requestBCFamilleRepository $bCFamilleRepositoryEntityManagerInterface $emCompanyLabelNRTIRRepository $companyLabelNRTIRRepository): Response
  84.     {
  85.         $user $this->getUser();
  86.         $type 'accompanist';
  87.         if (!$company) {
  88.             if ($user->getCompanyMember()) {
  89.                 $company $user->getCompanyMember()->getCompany();
  90.                 $type 'self';
  91.             } else {
  92.                 return $this->redirectToRoute('app_home');
  93.             }
  94.         }
  95.         $simulation $request->query->get('simulation');
  96.         $families $bCFamilleRepository->findAll();
  97.         return $this->render('bilan_carbone/create.html.twig', [
  98.             'controller_name' => 'BilanCarboneController',
  99.             'company' => $company,
  100.             'families' => $families,
  101.             'simulation' => $simulation true false,
  102.         ]);
  103.     }
  104.     #[Route('/bilan-carbone/bilan/modifier/{id}'name'app_bc_edit')]
  105.     public function edit(BCCompany $bilanRequest $requestEntityManagerInterface $emBCFamilleRepository $bCFamilleRepositoryCompanyLabelNRTIRRepository $companyLabelNRTIRRepository): Response
  106.     {
  107.         $type 'accompanist';
  108.         $user $this->getUser();
  109.         $company $bilan->getCompany();
  110.         $response $this->grantService->checkUser($user'company'$company);
  111.         if ($response) {
  112.             return $response;
  113.         }
  114.         $families $bCFamilleRepository->findAll();
  115.         return $this->render('bilan_carbone/create.html.twig', [
  116.             'controller_name' => 'BilanCarboneController',
  117.             'company' => $company,
  118.             'bilan' => $bilan,
  119.             'families' => $families,
  120.             'simulation' => $bilan->isType() ? true false,
  121.         ]);
  122.     }
  123.     #[Route('/bilan-carbone/bilan/supprimer/{id}'name'app_bc_delete')]
  124.     public function delete(BCCompany $bilanRequest $requestEntityManagerInterface $emCompanyLabelNRTIRRepository $companyLabelNRTIRRepository): Response
  125.     {
  126.         $company $bilan->getCompany();
  127.         $user $this->getUser();
  128.         $type 'accompanist';
  129.         if (!$company) {
  130.             if ($user->getCompanyMember()) {
  131.                 $company $user->getCompanyMember()->getCompany();
  132.                 $type 'self';
  133.             } else {
  134.                 return $this->redirectToRoute('app_home');
  135.             }
  136.         }
  137.         if ($bilan) {
  138.             $em->remove($bilan);
  139.             $em->flush();
  140.             $this->addFlash('success''Le bilan a bien été supprimé.');
  141.         }
  142.         return $this->redirectToRoute('app_bc_index', ['company' => $company->getId()]);
  143.     }
  144.     #[Route('/bilan-carbone/bilan/{bilan}'name'app_bc_show'defaults: ["company" => null"bilan" => null])]
  145.     public function show(BCCompany $bilanBCFamilleRepository $BCFamilleRepositoryChartService $chartService): Response
  146.     {
  147.         $company $bilan->getCompany();
  148.         $response $this->grantService->checkUser($this->getUser(), 'company'$company);
  149.         if ($response) {
  150.             return $response;
  151.         }
  152.         $families $BCFamilleRepository->findAll();
  153.         $bilanData $bilan->getBilanData($families);
  154.         $labelsFamily array_column($bilanData['families'], 'labelChartFamilyImpact');
  155.         $dataFamilyImpact array_map(fn($val) => round($val), array_column($bilanData['families'], 'total'));
  156.         $chartFamilyImpact $chartService->newPieChart("Données (kgCO2e)"$labelsFamily$dataFamilyImpact$bilanData["type"]);
  157.         $labelsFabrication = [
  158.             'Achats neuf - ' round($bilanData['percentageFabricationNew']) . "%",
  159.             'Achats occasion - ' round($bilanData['percentageFabricationOccasion']) . "%",
  160.             'Usage - ' round($bilanData['percentageUsage']) . "%"
  161.         ];
  162.         $dataFabrication = [
  163.             round($bilanData['totalFabricationNew']),
  164.             round($bilanData['totalFabricationOccasion']),
  165.             round($bilanData['totalUsage'])
  166.         ];
  167.         $chartFabricationUsage $chartService->newPieChart("Données (kgCO2e)"$labelsFabrication$dataFabrication$bilanData["type"]);
  168.         $labelsFamiliesStacked = [];
  169.         $dataFabricationNew = [];
  170.         $dataFabricationOccasion = [];
  171.         $dataUsage = [];
  172.         foreach ($bilanData['families'] as $key => $family) {
  173.             $labelsFamiliesStacked[] = $family['pluralName'];
  174.             $dataFabricationNew[] = round($family['totalFabricationNew']);
  175.             $dataFabricationOccasion[] = round($family['totalFabricationOccasion']);
  176.             $dataUsage[] = round($family['totalUsage']);
  177.         }
  178.         $datasets = [
  179.             [
  180.                 'label' => "Achats neuf",
  181.                 'data' => $dataFabricationNew,
  182.                 'backgroundColor' => $bilanData["type"] == "Simulation" 'rgba(86, 39, 143, 0.8)' 'rgba(0, 115, 148, 0.8)'
  183.             ],
  184.             [
  185.                 'label' => "Achats occasion",
  186.                 'data' => $dataFabricationOccasion,
  187.                 'backgroundColor' => $bilanData["type"] == "Simulation" 'rgba(0, 115, 148, 0.8)' 'rgba(86, 39, 143, 0.8)'
  188.             ],
  189.             [
  190.                 'label' => "Usage",
  191.                 'data' => $dataUsage,
  192.                 'backgroundColor' =>  'rgba(96, 190, 160, 0.8)'
  193.             ]
  194.         ];
  195.         $chartStackedBar $chartService->newStackedBarChart(
  196.             $labelsFamiliesStacked,
  197.             $datasets
  198.         );
  199.         $factorImpactTotalData = [];
  200.         $factorImpactPerUnitData = [];
  201.         foreach ($bilan->getBCData() as $data) {
  202.             $impactTotal round($data->getTotalImpact());
  203.             $quantityTotal $data->getQuantityNew() + $data->getQuantityOccasion();
  204.             $impactPerUnit = ($quantityTotal 0) ? round($impactTotal $quantityTotal) : null;
  205.             if ($impactTotal !== null) {
  206.                 $factorImpactTotalData[] = [
  207.                     'name' => $data->getFacteur()->getBCModele()->getName(),
  208.                     'type' => $data->getFacteur()->getBCModele()->getBCType()->getName(),
  209.                     'impact' => $impactTotal,
  210.                 ];
  211.             }
  212.             if ($impactPerUnit !== null) {
  213.                 $factorImpactPerUnitData[] = [
  214.                     'name' => $data->getFacteur()->getBCModele()->getName(),
  215.                     'type' => $data->getFacteur()->getBCModele()->getBCType()->getName(),
  216.                     'impactPerUnit' => $impactPerUnit,
  217.                 ];
  218.             }
  219.         }
  220.         usort($factorImpactTotalData, fn($a$b) => $b['impact'] <=> $a['impact']);
  221.         usort($factorImpactPerUnitData, fn($a$b) => $b['impactPerUnit'] <=> $a['impactPerUnit']);
  222.         $top5TotalFactors array_slice($factorImpactTotalData05);
  223.         $top5PerUnitFactors array_slice($factorImpactPerUnitData05);
  224.         return $this->render('bilan_carbone/show.html.twig', [
  225.             'company' => $company,
  226.             'bilan' => $bilanData,
  227.             'chartFamilyImpact' => $chartFamilyImpact,
  228.             'chartFabricationUsage' => $chartFabricationUsage,
  229.             'chartStackedBar' => $chartStackedBar,
  230.             'top5TotalFactors' => $top5TotalFactors,
  231.             'top5PerUnitFactors' => $top5PerUnitFactors,
  232.         ]);
  233.     }
  234. }