<?phpnamespace App\EventSubscriber;use App\Entity\Adherent\Magasin;use App\Entity\Kpi\KpiParameters;use App\Entity\KpiP;use App\Repository\KpiParametersRepository;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\HttpKernel\Event\ResponseEvent;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Component\Security\Core\Security;class ExerciceKpiSuscriber implements EventSubscriberInterface{ public function __construct( private Security $security, private EntityManagerInterface $entityManager ) { } public static function getSubscribedEvents(): array { return [ KernelEvents::RESPONSE => 'onKernelResponse', ]; } public function onKernelResponse(ResponseEvent $event): void { $request = $event->getRequest(); if (!str_contains($request->getPathInfo(), '/kpi')) { return; } $user = $this->security->getUser(); if (!$user) { return; } $magasin = $user->getMagasin(); $today = new \DateTimeImmutable(); // récupérer tous les exercices EN_COURS + TERMINE $exercices = $this->entityManager->getRepository(KpiParameters::class)->findBy(['magasin' => $magasin]); foreach ($exercices as $exercice) { // Vérifier cohérence de l'exercice EN_COURS if ($exercice->getStatus() === 'EN_COURS') { if ($today < $exercice->getStartExercice() || $today > $exercice->getEndExercice()) { // L'exercice n'est plus valide => le basculer en TERMINE $exercice->setStatus('TERMINE'); $this->gererRotationExercices($exercice); } } } } private function gererRotationExercices(KpiParameters $exerciceTermine): void { $magasin = $exerciceTermine->getMagasin(); $exercices = $this->entityManager->getRepository(KpiParameters::class)->findBy( ['magasin' => $magasin], ['startExercice' => 'DESC'] ); // Supprimer N-2 s’il existe if (count($exercices) >= 2) { $ancien = $exercices[count($exercices) - 1]; $this->entityManager->remove($ancien); } // Créer le nouvel exercice N+1 $nouvelExercice = new KpiParameters(); $nouvelExercice->setMagasin($magasin); $nouvelExercice->setStartExercice((clone $exerciceTermine->getEndExercice())->modify('+1 day')); $nouvelExercice->setEndExercice((clone $nouvelExercice->getStartExercice())->modify('+1 year -1 day')); $nouvelExercice->setStatus('EN_COURS'); $nouvelExercice->setCreatedAt(new \DateTimeImmutable()); $this->entityManager->persist($nouvelExercice); $this->entityManager->flush(); } /* public function onKernelRequest(RequestEvent $event): void { $request = $event->getRequest(); if (!str_contains($request->getPathInfo(), '/kpi/')) { return; } $user = $this->security->getUser(); if (!$user) { return; } $magasin = $user->getMagasin(); $exercices = $this->entityManager->getRepository(KpiParameters::class)->findOneBy(['magasin' => $magasin]); if (!$kpi) { $exercice = new KpiParameters(); $exercice->setMagasin($magasin); $exercice->setStartExercice((new \DateTime('first day of January this year'))); $exercice->setEndExercice((new \DateTime('last day of December this year'))); $exercice->setDefaultConfig(true); $exercice->setCreatedAt(new \DateTimeImmutable('now')); $this->entityManager->persist($exercice); $this->entityManager->flush(); return; } $now = new \DateTimeImmutable("now"); $start = $kpi->getStartExercice(); $end = $kpi->getEndExercice(); if ($now > $end) { // Crée de nouveaux objets DateTimeImmutable avec +1 an $newStart = (new \DateTime($start->format('Y-m-d')))->modify('+1 year'); $newEnd = (new \DateTime($end->format('Y-m-d')))->modify('+1 year'); $kpi->setStartExercice($newStart); $kpi->setEndExercice($newEnd); $kpi->setUpdatedAt($now); $this->entityManager->persist($kpi); $this->entityManager->flush(); } }*/}