← Back
Editing: AgentReport.php
<?php namespace App\Report; use App\Entity\Solution; use App\Entity\User; use App\Repository\ControleRepository; use App\Repository\ElevatorBreakdownRepository; use App\Repository\HistoriqueAlerteRepository; use App\Repository\NoteRepository; use App\Service\AgentService; use App\Service\Utils; use App\Session\Filter\ValueObject\DateRange; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\NoResultException; class AgentReport implements ReportInterface { public function __construct( private readonly Utils $utils, private readonly AgentService $agentService, private readonly NoteRepository $noteRepository, private readonly HistoriqueAlerteRepository $historiqueAlerteRepository, private readonly ElevatorBreakdownRepository $elevatorBreakdownRepository, private readonly ControleRepository $controleRepository ) { } public function supports(string $type): bool { return 'agent-report' === $type; } /** * @return array{evo: string, total: int} */ public function getRateControls( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, \DateTimeInterface $prevDateStart, \DateTimeInterface $prevDateEnd, Solution $solution ): array { $total = count($this->agentService->getControlesByRangeDate($user, $dateStart, $dateEnd, $solution)); $prevTotal = count( $this->agentService->getControlesByRangeDate($user, $prevDateStart, $prevDateEnd, $solution) ); return [ 'evo' => number_format($this->utils->calculateEvolution($total, $prevTotal), 2), 'total' => $total, ]; } /** * @return array{evo: string, total: int} */ public function getRateAlerts( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, \DateTimeInterface $prevDateStart, \DateTimeInterface $prevDateEnd, Solution $solution ): array { $total = $this->getRateAlertsByRange($user, $dateStart, $dateEnd, $solution); $prevTotal = $this->getRateAlertsByRange($user, $prevDateStart, $prevDateEnd, $solution); return [ 'evo' => number_format($this->utils->calculateEvolution($total, $prevTotal), 2), 'total' => $total, ]; } private function getRateAlertsByRange( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): float { $historiqueAlertes = $this->historiqueAlerteRepository->findByUser( $user, null, [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ); $diff = $dateEnd->diff($dateStart); $months = 0; if ($diff->y >= 1) { $months += ($diff->y * 12) + $diff->m; } if ($diff->m >= 1) { $months += $diff->m; } $nbHistoriqueAlertes = count($historiqueAlertes); if ($months > 0) { $nbHistoriqueAlertes = number_format(count($historiqueAlertes) / $months, 1); } return (float) $nbHistoriqueAlertes; } /** * @return array{evo: string, total: int} */ public function getRateAverageNote( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, \DateTimeInterface $prevDateStart, \DateTimeInterface $prevDateEnd, Solution $solution ): array { $total = $this->getRateAverageNoteByRange($user, $dateStart, $dateEnd, $solution); $prevTotal = $this->getRateAverageNoteByRange($user, $prevDateStart, $prevDateEnd, $solution); return [ 'evo' => number_format($this->utils->calculateEvolution($total, $prevTotal), 2), 'total' => $total, ]; } /** * @throws NonUniqueResultException * @throws NoResultException */ private function getRateAverageNoteByRange( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): float { return round( $this->noteRepository->averageNoteByAgent( $user, $dateStart, $dateEnd, $solution ) ?? 0, 1 ); } /** * @return array{evo: string|int, total: int} */ public function getRateIncidence( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, \DateTimeInterface $prevDateStart, \DateTimeInterface $prevDateEnd, Solution $solution ): array { $total = $this->getRateIncidencebyRange($user, $dateStart, $dateEnd, $solution); $prevTotal = $this->getRateIncidencebyRange($user, $prevDateStart, $prevDateEnd, $solution); if ($prevTotal <= 0) { return [ 'evo' => 0, 'total' => 0, ]; } return [ 'evo' => number_format((($total - $prevTotal) / $prevTotal) * 100, 2), 'total' => $total, ]; } private function getRateIncidencebyRange( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): float { $notes = $this->controleRepository->incidenceByAgenciesByMonth( agencies: [], user: $user, options: [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ); $total = 0; if (count($notes) > 0) { $total = array_reduce($notes, function ($carry, $item) { return $carry + $item['incidence']; }, 0) / count($notes); } $total *= 100; return $total; } public function getLessNotedControle( User $user, ?\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): array { return $this->controleRepository->getAverageNoteByManagerOrAgent( $user, [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ); } /** * @throws \Exception */ public function getLessNotedItems( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): array { $dateEndTmp = clone $dateEnd; $dateEndTmp->setTime(0, 0, 0); $listeAllNotedItem = $this->noteRepository->noteAllItemsByAgent( $user->getId(), [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'sort' => ['note', 'ASC'], ] ); return array_slice( array_map(function ($item) { if ((float) $item['note'] > 0) { return [ 'avgItemNote' => $item['note'], 'name' => $item['item'], 'nom' => $item['zone'], 'notationType' => $item['notationType'], ]; } }, $listeAllNotedItem), 0, 5 ); } public function getElevatorbreakDownByMonth( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): array { return $this->elevatorBreakdownRepository->getBreakDownByAgenciesByMonth( agenciesId: [], options: [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, 'agent' => $user, ] ); } }
Save File
Cancel