src/EventSubscriber/ExerciceKpiSuscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Adherent\Magasin;
  4. use App\Entity\Kpi\KpiParameters;
  5. use App\Entity\KpiP;
  6. use App\Repository\KpiParametersRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Security\Core\Security;
  13. class ExerciceKpiSuscriber implements EventSubscriberInterface
  14. {
  15.     public function __construct(
  16.         private Security               $security,
  17.         private EntityManagerInterface $entityManager
  18.     )
  19.     {
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             KernelEvents::RESPONSE => 'onKernelResponse',
  25.         ];
  26.     }
  27.     public function onKernelResponse(ResponseEvent $event): void
  28.     {
  29.         $request $event->getRequest();
  30.         if (!str_contains($request->getPathInfo(), '/kpi')) {
  31.             return;
  32.         }
  33.         $user $this->security->getUser();
  34.         if (!$user) {
  35.             return;
  36.         }
  37.         $magasin $user->getMagasin();
  38.         $today = new \DateTimeImmutable();
  39.         // récupérer tous les exercices EN_COURS + TERMINE
  40.         $exercices $this->entityManager->getRepository(KpiParameters::class)->findBy(['magasin' => $magasin]);
  41.         foreach ($exercices as $exercice) {
  42.             // Vérifier cohérence de l'exercice EN_COURS
  43.             if ($exercice->getStatus() === 'EN_COURS') {
  44.                 if ($today $exercice->getStartExercice() || $today $exercice->getEndExercice()) {
  45.                     // L'exercice n'est plus valide => le basculer en TERMINE
  46.                     $exercice->setStatus('TERMINE');
  47.                     $this->gererRotationExercices($exercice);
  48.                 }
  49.             }
  50.         }
  51.     }
  52.     private function gererRotationExercices(KpiParameters $exerciceTermine): void
  53.     {
  54.         $magasin $exerciceTermine->getMagasin();
  55.         $exercices $this->entityManager->getRepository(KpiParameters::class)->findBy(
  56.             ['magasin' => $magasin],
  57.             ['startExercice' => 'DESC']
  58.         );
  59.         // Supprimer N-2 s’il existe
  60.         if (count($exercices) >= 2) {
  61.             $ancien $exercices[count($exercices) - 1];
  62.             $this->entityManager->remove($ancien);
  63.         }
  64.         // Créer le nouvel exercice N+1
  65.         $nouvelExercice = new KpiParameters();
  66.         $nouvelExercice->setMagasin($magasin);
  67.         $nouvelExercice->setStartExercice((clone $exerciceTermine->getEndExercice())->modify('+1 day'));
  68.         $nouvelExercice->setEndExercice((clone $nouvelExercice->getStartExercice())->modify('+1 year -1 day'));
  69.         $nouvelExercice->setStatus('EN_COURS');
  70.         $nouvelExercice->setCreatedAt(new \DateTimeImmutable());
  71.         $this->entityManager->persist($nouvelExercice);
  72.         $this->entityManager->flush();
  73.     }
  74.     /*   public function onKernelRequest(RequestEvent $event): void
  75.        {
  76.            $request = $event->getRequest();
  77.            if (!str_contains($request->getPathInfo(), '/kpi/')) {
  78.                return;
  79.            }
  80.            $user = $this->security->getUser();
  81.            if (!$user) {
  82.                return;
  83.            }
  84.            $magasin = $user->getMagasin();
  85.            $exercices = $this->entityManager->getRepository(KpiParameters::class)->findOneBy(['magasin' => $magasin]);
  86.            if (!$kpi) {
  87.                $exercice = new KpiParameters();
  88.                $exercice->setMagasin($magasin);
  89.                $exercice->setStartExercice((new \DateTime('first day of January this year')));
  90.                $exercice->setEndExercice((new \DateTime('last day of December this year')));
  91.                $exercice->setDefaultConfig(true);
  92.                $exercice->setCreatedAt(new \DateTimeImmutable('now'));
  93.                $this->entityManager->persist($exercice);
  94.                $this->entityManager->flush();
  95.                return;
  96.            }
  97.            $now = new \DateTimeImmutable("now");
  98.            $start = $kpi->getStartExercice();
  99.            $end = $kpi->getEndExercice();
  100.            if ($now > $end) {
  101.                // Crée de nouveaux objets DateTimeImmutable avec +1 an
  102.                $newStart = (new \DateTime($start->format('Y-m-d')))->modify('+1 year');
  103.                $newEnd = (new \DateTime($end->format('Y-m-d')))->modify('+1 year');
  104.                $kpi->setStartExercice($newStart);
  105.                $kpi->setEndExercice($newEnd);
  106.                $kpi->setUpdatedAt($now);
  107.                $this->entityManager->persist($kpi);
  108.                $this->entityManager->flush();
  109.            }
  110.        }*/
  111. }