← Back
Editing: ControleService.php
<?php namespace App\Service; use App\Entity\AgencyPrestataire; use App\Entity\Controle; use App\Entity\HistoriqueAlerte; use App\Entity\Prestataire; use App\Entity\Service; use Doctrine\ORM\EntityManagerInterface; class ControleService { public function __construct( private readonly EntityManagerInterface $em, private readonly UserService $userService ) { } /** * Generate array of numbers of Control by month * Current month included. * * @param array<int> $agentIds */ public function groupControlByMonth( \DateTimeInterface $startDate, \DateTimeInterface $endDate, array $agentIds = [] ): array { $array = []; $agents = $this->userService->getAgentsForCurrentSolution(); $formatter = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::TRADITIONAL); $formatter->setPattern('MMM yy'); $newEndDate = clone $endDate; $newEndDate->modify('last day of this month'); $period = new \DatePeriod($startDate, new \DateInterval('P1M'), $newEndDate); /** @var \DateTime $i */ foreach ($period as $i) { $date = $formatter->format($i); array_key_exists($date, $array) ?: $array[$date] = 0; } foreach ($agents as $agent) { if (0 === count($agentIds) || in_array($agent->getId(), $agentIds)) { $controls = $agent->getControlsWithDates( $startDate, $newEndDate ); foreach ($controls as $control) { if ($control->getDateRealisation()) { $dateRealisation = $formatter->format($control->getDateRealisation()); if (array_key_exists( $dateRealisation, $array ) && true === $control->getValide() ) { ++$array[$dateRealisation]; } } } } } return $array; } public function addAlertToHistory(Controle $controle): void { $adresse = $controle->getAdresse(); /** @var Prestataire $prestataire */ $prestataire = $adresse->getPrestataires()->first(); /** @var AgencyPrestataire $agencyPrestataire */ $agencyPrestataire = $prestataire->getAgencyPrestataires()->first(); $agency = $controle->getAdresse()->getAgency(); $this->createServiceItem($controle, $agencyPrestataire); $alertHistory = new HistoriqueAlerte(); $alertHistory->setControle($controle); $alertHistory->setAgency($agency); $alertHistory->setPrestataire($agencyPrestataire); $this->em->persist($alertHistory); } public function createServiceItem(Controle $control, AgencyPrestataire $agencyPrestataire): void { $service = new Service(); $service->setControle($control); $service->setProviderContact($agencyPrestataire); $service->setHash(uniqid()); $this->em->persist($service); $control->setService($service); } }
Save File
Cancel