← Back
Editing: AddressController.php
<?php namespace App\Controller\Organization; use App\Breadcrumb\BreadcrumbInterface; use App\Breadcrumb\BreadcrumbItem; use App\Entity\Adresse; use App\Entity\Agency; use App\Entity\Controle; use App\Entity\User; use App\Repository\AdresseRepository; use App\Repository\ControleRepository; use App\Repository\ElevatorBreakdownRepository; use App\Repository\HistoriqueAlerteRepository; use App\Repository\ItemRepository; use App\Repository\ZoneRepository; use App\Service\AddressService; use App\Service\OrganizationService; use App\Service\SolutionService; use App\Service\SupervisorService; use App\Service\Utils; use App\Session\Filter\SessionFiltersContainer; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\NoResultException; use Knp\Component\Pager\PaginatorInterface; 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; #[Route(path: '/organization/address', name: 'organization_address_')] class AddressController extends AbstractController { public function __construct(BreadcrumbInterface $breadcrumb, SupervisorService $supervisorService) { $breadcrumb->addRootItem(BreadcrumbItem::set('Accueil', 'organization_index')); $breadcrumb->addItem(BreadcrumbItem::set('Adresses', 'organization_address_index')); $supervisorService->setSection('address'); } #[Route(path: '/{page}', name: 'index', requirements: ['page' => '\d+'])] public function list( OrganizationService $organizationService, AdresseRepository $adresseRepository, PaginatorInterface $paginator, int $page = 1 ): Response { $query = $adresseRepository->getQueryByAgencies($organizationService->getAgenciesId()); $addresses = $paginator->paginate($query, $page); return $this->render('organization/address/index.html.twig', ['addresses' => $addresses]); } /** * @throws NonUniqueResultException */ #[Route(path: '/detail/{id}.html', name: 'detail', options: ['expose' => true])] public function detail( Adresse $adresse, BreadcrumbInterface $breadcrumb, ItemRepository $itemRepository, ControleRepository $controleRepository ): Response { $breadcrumb->addItem(BreadcrumbItem::set('Détail d\'une adresse')); $avgNote = $controleRepository->getTotalAverageNoteByAddress($adresse); return $this->render( 'organization/address/detail.html.twig', [ 'adresse' => $adresse, 'items' => $itemRepository->fetchItemsByAddress($adresse), 'avgNote' => $avgNote['avgNote'] ?? 0, ] ); } #[Route(path: '/{id}/chart1.json', name: 'control_chart')] public function controlChart( Adresse $adresse, Utils $utils, SessionFiltersContainer $sessionFiltersContainer, ControleRepository $controleRepository ): JsonResponse { $filters = $sessionFiltersContainer->getCurrent(); $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $labels = $utils->getMonthsTimeline($startDate, $endDate); /** @var User $user */ $user = $this->getUser(); $agencyIds = $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->getValues(); $controls = $controleRepository->countControlesByAgenciesByMonth( agenciesId: $agencyIds, groupProvider: false, address: $adresse ); $total = array_reduce($controls, function ($carry, $item) { $carry += $item['nbControls']; return $carry; }, 0); $prevControls = $controleRepository->countControlesByAgenciesByMonth( agenciesId: $agencyIds, groupProvider: false, previous: true, address: $adresse ); $prevTotal = array_reduce($prevControls, function ($carry, $item) { return $carry + $item['nbControls']; }, 0); if ($prevTotal > 0) { $evolution = (($total - $prevTotal) / $prevTotal) * 100; } else { $evolution = $total * 100; } return new JsonResponse([ 'labels' => $labels, 'controls' => $controls, 'total' => $total, 'prevTotal' => $prevTotal, 'evolution' => number_format($evolution, 2), ]); } /** * @throws \Exception */ #[Route(path: '/{id}/chart2.json', name: 'note_chart')] public function noteChart( Adresse $adresse, Utils $utils, SessionFiltersContainer $sessionFiltersContainer, ControleRepository $controleRepository ): JsonResponse { $filters = $sessionFiltersContainer->getCurrent(); $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $total = 0; /** @var User $user */ $user = $this->getUser(); $agencyIds = $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->getValues(); $notes = $controleRepository->averageNoteByAgenciesByMonth( agenciesId: $agencyIds, address: $adresse ); if (count($notes) > 0) { $total = array_reduce($notes, function ($carry, $item) { return $carry + $item['note']; }, 0) / count($notes); } $labels = $utils->getMonthsTimeline($startDate, $endDate); $prevNotes = $controleRepository->averageNoteByAgenciesByMonth( agenciesId: $agencyIds, address: $adresse, previous: true ); if (count($prevNotes) > 0) { $prevTotal = array_reduce($prevNotes, function ($carry, $item) { return $carry + $item['note']; }, 0) / count($prevNotes); } else { $prevTotal = 0; } if ($prevTotal > 0) { $evolution = (($total - $prevTotal) / $prevTotal) * 100; } else { $evolution = $total * 100; } return new JsonResponse([ 'labels' => $labels, 'notes' => $notes, 'total' => number_format($total, 1), 'prevTotal' => $prevTotal, 'evolution' => number_format($evolution, 2), ]); } #[Route(path: '/{id}/chart3.json', name: 'incidence_chart')] public function incidenceChart( Adresse $adresse, Utils $utils, SessionFiltersContainer $sessionFiltersContainer, ControleRepository $controleRepository ): JsonResponse { $filters = $sessionFiltersContainer->getCurrent(); $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $total = 0; /** @var User $user */ $user = $this->getUser(); /** @var int[] $agencyIds */ $agencyIds = $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->getValues(); $notes = $controleRepository->incidenceByAgenciesByMonth( agencies: $agencyIds, adresse: $adresse ); if (count($notes) > 0) { $total = array_reduce($notes, function ($carry, $item) { return $carry + $item['incidence']; }, 0) / count($notes); } $total *= 100; $labels = $utils->getMonthsTimeline($startDate, $endDate); $prevNotes = $controleRepository->incidenceByAgenciesByMonth( agencies: $agencyIds, adresse: $adresse, previous: true ); if (count($prevNotes) > 0) { $prevTotal = array_reduce($prevNotes, function ($carry, $item) { return $carry + $item['incidence']; }, 0) / count($prevNotes); $prevTotal *= 100; if ($prevTotal > 0) { $evolution = (($total - $prevTotal) / $prevTotal) * 100; } else { $evolution = 101; } } else { $prevTotal = 0; $evolution = 0; } return new JsonResponse([ 'labels' => $labels, 'notes' => $notes, 'total' => number_format($total, 1), 'prevTotal' => $prevTotal, 'evolution' => number_format($evolution, 2), ]); } #[Route(path: '/{id}/chart-alerts.json', name: 'chart_alert')] public function alertsChart( Adresse $adresse, Utils $utils, SessionFiltersContainer $sessionFiltersContainer, HistoriqueAlerteRepository $alerteRepository ): JsonResponse { $filters = $sessionFiltersContainer->getCurrent(); $options['address'] = $adresse; $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $labels = $utils->getMonthsTimeline($startDate, $endDate); /** @var User $user */ $user = $this->getUser(); $agencyIds = $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->getValues(); $alerts = $alerteRepository->getByAgenciesByMonth( agenciesId: $agencyIds, options: $options ); $prevAlerts = $alerteRepository->getByAgenciesByMonth( agenciesId: $agencyIds, previous: true, options: $options ); $total = array_reduce($alerts, function ($carry, $item) { return $carry + $item['cnt']; }, 0); $prevTotal = array_reduce($prevAlerts, function ($carry, $item) { return $carry + $item['cnt']; }, 0); $evolution = $total * 100; if ($prevTotal > 0) { $evolution = (($total - $prevTotal) / $prevTotal) * 100; } return new JsonResponse([ 'labels' => $labels, 'alerts' => $alerts, 'total' => $total, 'prevTotal' => $prevTotal, 'evolution' => number_format($evolution, 2), ]); } #[Route(path: '/{id}/chart-average-note.json', name: 'average_note_chart')] public function averageNoteChart( Adresse $adresse, ControleRepository $controleRepository, SessionFiltersContainer $sessionFiltersContainer, Utils $utils ): JsonResponse { $filters = $sessionFiltersContainer->getCurrent(); $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $data = $controleRepository->getSupervisorAverageNoteByAgencyByMonth( organization: $this->getUser()->getAgency(), address: $adresse ); $labels = $utils->getMonthsTimeline($startDate, $endDate); return new JsonResponse([ 'labels' => $labels, 'data' => $data, ]); } #[Route(path: '/alertes/{page}', name: 'alerts')] public function alerts( BreadcrumbInterface $breadcrumb, PaginatorInterface $paginator, HistoriqueAlerteRepository $alerteRepository, SupervisorService $supervisorService, OrganizationService $organizationService, int $page = 1 ): Response { $agenciesId = $organizationService->getAgenciesId(); $query = $alerteRepository->getQueryByAgencies($agenciesId)->orderBy( 'ha.date', 'DESC' ); $alerts = $paginator->paginate($query, $page); $supervisorService->setSection('alerts'); $breadcrumb->addItem(BreadcrumbItem::set('Alertes', 'organization_address_alerts')); return $this->render('organization/address/alerts.html.twig', ['alerts' => $alerts]); } #[Route(path: '/alertes/detail/{id}.html', name: 'alerts_detail')] public function alertDetail( Controle $controle, ZoneRepository $zoneRepository, BreadcrumbInterface $breadcrumb ): Response { $breadcrumb->addItem(BreadcrumbItem::set('Alertes', 'organization_address_alerts')); $breadcrumb->addItem(BreadcrumbItem::set('Détail d\'une alerte')); return $this->render( 'organization/address/alert_detail.html.twig', [ 'controle' => $controle, 'zones' => $zoneRepository->findByControl($controle), ] ); } #[Route(path: '/performance', name: 'performance')] public function performance( Request $request, BreadcrumbInterface $breadcrumb, AdresseRepository $adresseRepository, AddressService $addressService, SolutionService $solutionService, OrganizationService $organizationService ): Response { $agenciesId = $organizationService->getAgenciesId(); $sort = $request->get('perfSort', 'desc'); $addressQuery = $adresseRepository->getQueryByAgencies( agenciesId: $agenciesId, solution: $solutionService->getCurrent() ) ->select('a as addr') ->addSelect('COUNT(c.id) as cnt') ->addSelect('AVG(c.averageNote) as avgNote') ->groupBy('a') ->orderBy('cnt', $sort); $breadcrumb->addItem(BreadcrumbItem::set('Performance', 'organization_address_performance')); return $this->render( 'organization/address/performance.html.twig', ['addresses' => $addressQuery->getQuery()->getResult(), 'addressService' => $addressService] ); } /** * @throws NoResultException * @throws NonUniqueResultException */ #[Route(path: '/chart-sleep.js', name: 'sleep_chart')] public function sleepChart( SessionFiltersContainer $sessionFiltersContainer, AdresseRepository $adresseRepository, Utils $utils, OrganizationService $organizationService ): JsonResponse { $agenciesId = $organizationService->getAgenciesId(); $filters = $sessionFiltersContainer->getCurrent(); $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $uncontrolled = $adresseRepository->addressUncontroledByAgencies($agenciesId); $total = 0; if (count($uncontrolled) > 0) { $total = array_reduce($uncontrolled, function ($carry, $item) { return $carry + $item['percent']; }, 0) / count($uncontrolled); } $labels = $utils->getMonthsTimeline($startDate, $endDate); $prevUncontrolled = $adresseRepository->addressUncontroledByAgencies($agenciesId, true); $prevTotal = 0; $evolution = 100; if (count($prevUncontrolled) > 0) { $prevTotal = array_reduce($prevUncontrolled, function ($carry, $item) { return $carry + $item['percent']; }, 0) / count($prevUncontrolled); $evolution = (($total - $prevTotal) / $prevTotal) * 100; } return new JsonResponse([ 'labels' => $labels, 'uncontrolled' => $uncontrolled, 'total' => number_format($total, 1), 'prevTotal' => $prevTotal, 'evolution' => number_format($evolution, 2), ]); } #[Route(path: '/pannes/{page}', name: 'breakdown')] public function breakdown( ElevatorBreakdownRepository $breakdownRepository, PaginatorInterface $paginator, SupervisorService $supervisorService, BreadcrumbInterface $breadcrumb, OrganizationService $organizationService, $page = 1 ): Response { $agenciesId = $organizationService->getAgenciesId(); $breadcrumb->addItem(BreadcrumbItem::set('Pannes', 'organization_address_breakdown')); $supervisorService->setSection('breakdown'); $query = $breakdownRepository->findAllByAgencies($agenciesId); $breakdowns = $paginator->paginate($query, $page); return $this->render('organization/address/breakdown.html.twig', ['breakdowns' => $breakdowns]); } #[Route(path: '/chart-breakdown.json', name: 'chart_breakdown')] public function breakdownChart( Utils $utils, SessionFiltersContainer $sessionFiltersContainer, ElevatorBreakdownRepository $breakdownRepository, OrganizationService $organizationService ): JsonResponse { $agenciesId = $organizationService->getAgenciesId(); $filters = $sessionFiltersContainer->getCurrent(); $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $labels = $utils->getMonthsTimeline($startDate, $endDate); $breakdowns = $breakdownRepository->getByAgenciesByMonth($agenciesId); $prevBreakdowns = $breakdownRepository->getByAgenciesByMonth( agenciesId: $agenciesId, previous: true ); $total = array_reduce($breakdowns, function ($carry, $item) { return $carry + $item['cnt']; }, 0); $prevTotal = array_reduce($prevBreakdowns, function ($carry, $item) { return $carry + $item['cnt']; }, 0); $evolution = 100; if ($prevTotal > 0) { $evolution = (($total - $prevTotal) / $prevTotal) * 100; } return new JsonResponse([ 'labels' => $labels, 'breakdowns' => $breakdowns, 'total' => $total, 'prevTotal' => $prevTotal, 'evolution' => $evolution, ]); } }
Save File
Cancel