← Back
Editing: ManagerReport.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\ItemRepository; use App\Service\AgentService; use App\Service\UserService; use App\Service\Utils; use App\Session\Filter\ValueObject\DateRange; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\NoResultException; class ManagerReport implements ReportInterface { public function __construct( private readonly Utils $utils, private readonly AgentService $agentService, private readonly UserService $userService, private readonly HistoriqueAlerteRepository $historiqueAlerteRepository, private readonly ItemRepository $itemRepository, private readonly ElevatorBreakdownRepository $elevatorBreakdownRepository, private readonly ControleRepository $controleRepository ) { } public function supports(string $type): bool { return 'manager-report' === $type; } public function getRateControls( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, \DateTimeInterface $prevDateStart, \DateTimeInterface $prevDateEnd, Solution $solution ): array { $agents = $this->userService->getAgentsForSolution($solution, $user); $total = $this->getRateControlsByRange($agents, $dateStart, $dateEnd, $solution); $prevTotal = $this->getRateControlsByRange($agents, $prevDateStart, $prevDateEnd, $solution); return [ 'evo' => number_format($this->utils->calculateEvolution($total, $prevTotal), 2), 'total' => $total, ]; } /** * @param User[] $agents */ private function getRateControlsByRange( array $agents, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, ?Solution $solution = null ): float { $total = 0; /** @var User $agent */ foreach ($agents as $agent) { $total += count($this->agentService->getControlesByRangeDate($agent, $dateStart, $dateEnd, $solution)); } return $total; } 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; } 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->controleRepository->averageNoteByManager( manager: $user, options: [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ) ?? 0, 1 ); } 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, ] ); } public function getLessNotedItems( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): mixed { return $this->itemRepository->getLessRatedItemsByAgencies( agenciesId: [], user: $user, options: [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ); } public function getElevatorbreakDownByMonth( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): array { return $this->elevatorBreakdownRepository->getBreakDownByAgenciesByMonth( agenciesId: [], manager: $user, options: [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ); } }
Save File
Cancel