Linux SRV-16 6.18.44-paas #1 SMP PREEMPT_DYNAMIC Thu Aug 13 10:08:12 UTC 2026 x86_64
/
srv
/
data
/
web
/
vhosts
/
716cf56cb2.url-de-test.ws
/
htdocs
/
src
/
Service
/
/srv/data/web/vhosts/716cf56cb2.url-de-test.ws/htdocs/src/Service/AvailableLogements.php
<?php namespace App\Service; use DateTimeImmutable; use App\Repository\BookingRepository; use App\Repository\LogementRepository; class AvailableLogements { protected $logementRepository; protected $bookingRepository; public function __construct(LogementRepository $logementRepository, BookingRepository $bookingRepository) { $this->logementRepository = $logementRepository; $this->bookingRepository = $bookingRepository; } public function consecutiveAvailableDays($data) { $logements = $this->logementRepository->findLogementWithConditions($data); if (count($logements) === 0) { return false; } $bookings = $this->bookingRepository->findAll(); // Dates limites de recherche $dateDebutRecherche = $data['start']->modify('-15 days'); $dateFinRecherche = $data['start']->modify('+15 days'); // Tableau pour stocker les disponibilités consécutives $availableDaysByLogement = []; foreach ($logements as $logement) { $bookings = $logement->getBookings(); $currentIntervalStart = null; $currentIntervalEnd = null; $longestIntervalStart = null; $longestIntervalEnd = null; // Parcourez chaque jour dans l'intervalle $currentDate = $dateDebutRecherche; while ($currentDate <= $dateFinRecherche) { if ($currentDate >= new DateTimeImmutable()) { // Vérifiez si le jour est occupé par un booking $isOccupied = false; foreach ($bookings as $booking) { if ($currentDate >= $booking->getCheckin() && $currentDate <= $booking->getCheckout()) { // Le jour est occupé $isOccupied = true; $currentIntervalStart = null; $currentIntervalEnd = null; break; // Sortez de la boucle dès qu'un booking est trouvé } } // Si le jour n'est pas occupé, ajoutez-le au tableau if (!$isOccupied) { if ($currentIntervalStart === null) { $currentIntervalStart = $currentDate; } $currentIntervalEnd = $currentDate; // Mettez à jour l'intervalle consécutif le plus long $currentIntervalLength = $currentIntervalStart->diff($currentIntervalEnd)->days; $longestIntervalLength = $longestIntervalStart ? $longestIntervalStart->diff($longestIntervalEnd)->days : 0; if ($currentIntervalLength > $longestIntervalLength) { $longestIntervalStart = $currentIntervalStart; $longestIntervalEnd = $currentIntervalEnd; } } } //passez au jour suivant $currentDate = $currentDate->modify('+1 day'); } // Ajoutez les jours non occupés pour ce logement au tableau principal $availableDaysByLogement[] = [ 'logement' => $logement, 'longestIntervalStart' => $longestIntervalStart, 'longestIntervalEnd' => $longestIntervalEnd, ]; } // Initialisez les variables pour suivre l'intervalle le plus long global $globalLongestIntervalStart = null; $globalLongestIntervalEnd = null; $globalLongestInterval = 0; $newDates = []; foreach ($availableDaysByLogement as $availableInterval) { $start = $availableInterval['longestIntervalStart']; $end = $availableInterval['longestIntervalEnd']; $currentLongestInterval = $start->diff($end)->days; if ($currentLongestInterval > $globalLongestInterval) { $globalLongestInterval = $currentLongestInterval; $globalLongestIntervalStart = $start; $globalLongestIntervalEnd = $end; } } $newDates['start'] = $globalLongestIntervalStart; $newDates['end'] = $globalLongestIntervalEnd; return $newDates; } }