← Back
Editing: OrganizationReport.php
<?php namespace App\Report; use App\Entity\Agency; 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\Utils; use App\Session\Filter\ValueObject\DateRange; use Symfony\Component\Security\Core\User\UserInterface; class OrganizationReport implements ReportInterface { public function __construct( private readonly Utils $utils, private readonly ControleRepository $controleRepository, private readonly HistoriqueAlerteRepository $historiqueAlerteRepository, private readonly ItemRepository $itemRepository, private readonly ElevatorBreakdownRepository $elevatorBreakdownRepository ) { } public function supports(string $type): bool { return 'organization-report' === $type; } /** * @return array{evo: string, total: int} */ public function getRateControls( UserInterface $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, \DateTimeInterface $prevDateStart, \DateTimeInterface $prevDateEnd, Solution $solution ): array { $total = $this->getRateControlsByRangeDate($user, $dateStart, $dateEnd, $solution); $prevTotal = $this->getRateControlsByRangeDate($user, $prevDateStart, $prevDateEnd, $solution); return [ 'evo' => number_format($this->utils->calculateEvolution($total, $prevTotal), 2), 'total' => $total, ]; } private function getRateControlsByRangeDate( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): float { $organization = $user->getOrganization(); if (null === $organization) { throw new \RuntimeException(sprintf('User %s with organization roles have no organization attached', $user->getEmail())); } $controls = $this->controleRepository->countControlesByAgenciesByMonth( agenciesId: $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->toArray(), options: [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ); return array_reduce($controls, function ($carry, $item) { return $carry + $item['nbControls']; }, 0); } /** * @return array{evo: string, total: int} */ public function getRateAlerts( UserInterface $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 { $result = $this->historiqueAlerteRepository->getByAgenciesByMonth( agenciesId: $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->toArray(), options: [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ); return array_reduce($result, fn ($carry, $item) => $carry + $item['cnt'], 0); } /** * @return array{evo: string, total: int} * * @throws \Exception */ 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 \Exception */ private function getRateAverageNoteByRange( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): float { $notes = $this->controleRepository->averageNoteByAgenciesByMonth( agenciesId: $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->toArray(), options: [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ); $total = 0; if (count($notes) > 0) { $total = array_reduce($notes, function ($carry, $item) { return $carry + $item['note']; }, 0) / count($notes); } return $total; } /** * @return array{evo: string, 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) { $evolution = (($total - $prevTotal) / $prevTotal) * 100; } else { $evolution = 101; } return [ 'evo' => number_format($evolution, 2), 'total' => $total, ]; } private function getRateIncidencebyRange( User $user, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, Solution $solution ): float { /** @var int[] $agencyIds */ $agencyIds = $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->toArray(); $notes = $this->controleRepository->incidenceByAgenciesByMonth( agencies: $agencyIds, 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->getAverageNoteByAgencies( agenciesId: $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->toArray(), options: [ '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->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->toArray(), 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: $user->getOrganization()->getAgencies()->map(fn (Agency $agency) => $agency->getId())->toArray(), options: [ 'dateRange' => new DateRange($dateStart, $dateEnd), 'solution' => $solution, ] ); } }
Save File
Cancel