diff --git a/src/CoreBundle/Command/ReinscriptionCheckCommand.php b/src/CoreBundle/Command/ReinscriptionCheckCommand.php index 52f4b5f00dc..d5fab996521 100644 --- a/src/CoreBundle/Command/ReinscriptionCheckCommand.php +++ b/src/CoreBundle/Command/ReinscriptionCheckCommand.php @@ -73,45 +73,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int )); } - // Check if the session is marked as the last repetition - if ($session->getLastRepetition()) { - if ($debug) { - $output->writeln('The session is marked as the last repetition. Skipping...'); - } - continue; - } - - // Find a valid child session - $validChildSession = $this->sessionRepository->findValidChildSession($session); + $validSession = $this->findValidSessionInHierarchy($session); - if ($validChildSession) { - $this->enrollUserInSession($user, $validChildSession); + if ($validSession) { + $this->enrollUserInSession($user, $validSession); if ($debug) { $output->writeln(sprintf( - 'User %d re-enrolled into the valid child session %d.', + 'User %d re-enrolled into session %d.', $user->getId(), - $validChildSession->getId() - )); - } - continue; - } - - // If no valid child session exists, check the parent session - $validParentSession = $this->sessionRepository->findValidParentSession($session); - - if ($validParentSession) { - $this->enrollUserInSession($user, $validParentSession); - if ($debug) { - $output->writeln(sprintf( - 'User %d re-enrolled into the valid parent session %d.', - $user->getId(), - $validParentSession->getId() + $validSession->getId() )); } } else { if ($debug) { $output->writeln(sprintf( - 'No valid child or parent session found for user %d.', + 'No valid session found for user %d.', $user->getId() )); } @@ -164,4 +140,35 @@ private function findUserSubscriptionInSession($user, $session) 'session' => $session, ]); } + + private function findValidSessionInHierarchy(Session $session): ?Session + { + $childSessions = $this->sessionRepository->findChildSessions($session); + + /* @var Session $child */ + foreach ($childSessions as $child) { + $validUntil = (clone $child->getAccessEndDate())->modify("-{$child->getDaysToReinscription()} days"); + if (new \DateTime() <= $validUntil) { + return $child; + } + + $validChild = $this->findValidSessionInHierarchy($child); + if ($validChild) { + return $validChild; + } + } + + /* @var Session $parentSession */ + $parentSession = $this->sessionRepository->findParentSession($session); + + if ($parentSession) { + $validUntil = (clone $parentSession->getAccessEndDate())->modify("-{$parentSession->getDaysToReinscription()} days"); + if (new \DateTime() <= $validUntil) { + return $parentSession; + } + } + + return null; + } + } diff --git a/src/CoreBundle/Command/SessionRepetitionCommand.php b/src/CoreBundle/Command/SessionRepetitionCommand.php index 0280698cb7a..27b1d015fa9 100644 --- a/src/CoreBundle/Command/SessionRepetitionCommand.php +++ b/src/CoreBundle/Command/SessionRepetitionCommand.php @@ -81,9 +81,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int private function duplicateSession(Session $session, bool $debug, OutputInterface $output): Session { // Calculate new session dates based on the duration of the original session - $duration = $session->getAccessEndDate()->diff($session->getAccessStartDate()); + $duration = $session->getAccessEndDate()->diff($session->getAccessStartDate())->days; $newStartDate = (clone $session->getAccessEndDate())->modify('+1 day'); - $newEndDate = (clone $newStartDate)->add($duration); + $newEndDate = (clone $newStartDate)->modify("+{$duration} days"); if ($debug) { $output->writeln(sprintf( @@ -105,7 +105,7 @@ private function duplicateSession(Session $session, bool $debug, OutputInterface ->setCoachAccessStartDate($newStartDate) ->setCoachAccessEndDate($newEndDate) ->setVisibility($session->getVisibility()) - ->setDuration($session->getDuration()) + ->setDuration($duration) ->setDescription($session->getDescription() ?? '') ->setShowDescription($session->getShowDescription() ?? false) ->setCategory($session->getCategory())