← Back
Editing: AgentController.php
<?php namespace App\Controller\Organization; use App\Breadcrumb\BreadcrumbInterface; use App\Breadcrumb\BreadcrumbItem; use App\Entity\User; use App\Export\AgentControlCountExporter; use App\Repository\ControleRepository; use App\Repository\UserRepository; use App\Service\OrganizationService; 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\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\Routing\Annotation\Route; #[Route(path: '/organization/agent', name: 'organization_agent_')] class AgentController extends AbstractController { public function __construct(BreadcrumbInterface $breadcrumb, SupervisorService $supervisorService) { $breadcrumb->addRootItem(BreadcrumbItem::set('Accueil', 'organization_index')); $breadcrumb->addItem(BreadcrumbItem::set('Agents', 'organization_agent_index')); $supervisorService->setSection('agent'); } #[Route(path: '/{page}', name: 'index', requirements: ['page' => '\d+'])] public function index( OrganizationService $organizationService, UserRepository $userRepository, PaginatorInterface $paginator, ?int $page = 1 ): Response { $agenciesId = $organizationService->getAgenciesId(); $agentsQuery = $userRepository->getAgentsByAgenciesQueryBuilder($agenciesId); $agents = $paginator->paginate($agentsQuery, $page); return $this->render('organization/agent/index.html.twig', ['agents' => $agents]); } #[Route(path: '/performance.html', name: 'performance')] public function performance( OrganizationService $organizationService, UserRepository $userRepository ): Response { $agenciesId = $organizationService->getAgenciesId(); $averageControleTimes = $userRepository->getAgentsByAgenciesByControlTimeQuery( $agenciesId )->getQuery()->getResult(); $averageControleTimes = array_map(function ($item) { list($hours) = explode(':', $item['doneTime']); if ($hours > 23) { $item['doneTime'] = '23:59:59.0000'; } return $item; }, $averageControleTimes); $controlCountPerAgent = $userRepository->getByAgenciesByControl( agenciesId: $agenciesId, role: 'ROLE_AGENT', sort: ['COUNT(c.id)', 'DESC'] ); return $this->render( 'organization/agent/performance.html.twig', [ 'averageControleTimes' => $averageControleTimes, 'controlCountPerAgent' => $controlCountPerAgent, 'maxControls' => array_reduce($controlCountPerAgent, fn ($carry, $item) => max($item['cnt'], $carry), 0), ] ); } #[Route(path: '/chart-time.json', name: 'chart_time')] public function timeChart( OrganizationService $organizationService, ControleRepository $controleRepository, SessionFiltersContainer $sessionFiltersContainer, Utils $utils ): JsonResponse { $agenciesId = $organizationService->getAgenciesId(); $filters = $sessionFiltersContainer->getCurrent(); $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $labels = $utils->getMonthsTimeline($startDate, $endDate); $times = $controleRepository->getControlsTimeByAgenciesByMonth($agenciesId); $total = 0; if (count($times) > 0) { $total = array_reduce($times, function ($carry, $item) { return $carry + $item['doneTime']; }, 0) / count($times); } $prevTimes = $controleRepository->getControlsTimeByAgenciesByMonth( agenciesId: $agenciesId, previous: true ); $prevTotal = 0; if (count($prevTimes) > 0) { $prevTotal = array_reduce($prevTimes, function ($carry, $item) { return $carry + $item['doneTime']; }, 0) / count($prevTimes); } if ($prevTotal > 0) { $evolution = (($total - $prevTotal) / $prevTotal) * 100; } else { $evolution = 0; } return new JsonResponse([ 'labels' => $labels, 'total' => $total, 'times' => $times, 'evolution' => $evolution, ]); } /** * @throws NoResultException * @throws NonUniqueResultException */ #[Route(path: '/chart-frequence.json', name: 'chart_frequency')] public function frequencyChart( ControleRepository $controleRepository, UserRepository $userRepository, SessionFiltersContainer $sessionFiltersContainer, OrganizationService $organizationService, Utils $utils ): JsonResponse { $agenciesId = $organizationService->getAgenciesId(); $filters = $sessionFiltersContainer->getCurrent(); $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $labels = $utils->getMonthsTimeline($startDate, $endDate); $nbAgents = $userRepository->getAgentsByAgenciesQueryBuilder($agenciesId)->select( 'COUNT(u)' )->getQuery()->getSingleScalarResult(); $controls = $controleRepository->countControlesByAgenciesByMonth($agenciesId, false); $frequencies = []; foreach ($controls as $control) { $frequencies[] = [ 'month' => $control['month'], 'cnt' => $control['nbControls'] / $nbAgents, ]; } $total = 0; if (count($frequencies) > 0) { $total = array_reduce($frequencies, function ($carry, $item) { return $carry + $item['cnt']; }, 0) / count($frequencies); } $prevControls = $controleRepository->countControlesByAgenciesByMonth( agenciesId: $agenciesId, groupProvider: false, previous: true ); $prevFrequencies = []; foreach ($prevControls as $control) { $prevFrequencies[] = [ 'month' => $control['month'], 'cnt' => $control['nbControls'] / $nbAgents, ]; } $prevTotal = 0; if (count($prevFrequencies) > 0) { $prevTotal = array_reduce($prevFrequencies, function ($carry, $item) { return $carry + $item['cnt']; }, 0) / count($prevFrequencies); } if ($prevTotal > 0) { $evolution = (($total - $prevTotal) / $prevTotal) * 100; } else { $evolution = 0; } return new JsonResponse([ 'labels' => $labels, 'total' => $total, 'frequencies' => $frequencies, 'evolution' => $evolution, ]); } #[Route(path: '/chart-recurring.json', name: 'chart_recurring')] public function recurringChart( Utils $utils, SessionFiltersContainer $sessionFiltersContainer, ControleRepository $controleRepository, OrganizationService $organizationService ): JsonResponse { $agenciesId = $organizationService->getAgenciesId(); $filters = $sessionFiltersContainer->getCurrent(); $startDate = $filters->getDateRange()->getDateStart(); $endDate = $filters->getDateRange()->getDateEnd(); $labels = $utils->getMonthsTimeline($startDate, $endDate); $controls = $controleRepository->countControlesByAgenciesByMonth($agenciesId, false); $recurring = []; foreach ($controls as $control) { $recurring[] = [ 'month' => $control['month'], 'cnt' => $control['nbControls'] / $control['nbAddresses'], ]; } $total = array_reduce($recurring, function ($carry, $item) { return $carry + $item['cnt']; }, 0) / count($recurring); $prevControls = $controleRepository->countControlesByAgenciesByMonth( $agenciesId, false, true ); $prevRecurring = []; foreach ($prevControls as $control) { $prevRecurring[] = [ 'month' => $control['month'], 'cnt' => $control['nbControls'] / $control['nbAddresses'], ]; } if (count($prevRecurring) > 0) { $prevTotal = array_reduce($prevRecurring, function ($carry, $item) { return $carry + $item['cnt']; }, 0) / count($prevRecurring); } else { $prevTotal = 0; } if ($prevTotal > 0) { $evolution = (($total - $prevTotal) / $prevTotal) * 100; } else { $evolution = 0; } return new JsonResponse([ 'labels' => $labels, 'total' => $total, 'recurring' => $recurring, 'evolution' => $evolution, ]); } #[Route(path: '/export-control-count.xls', name: 'export_control_count')] public function exportControlCountPerAgent(AgentControlCountExporter $exporter): Response { /** @var User $user */ $user = $this->getUser(); $fileName = ($exporter)($user->getOrganization()->getAgencies()->toArray()); return $this->file($fileName, 'export-controles-agents.xls', ResponseHeaderBag::DISPOSITION_INLINE); } }
Save File
Cancel