src/Controller/DefaultController.php line 186

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  9. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use App\Service\Booking\BookingService;
  12. use App\Service\Housekeeping\HousekeepingManager;
  13. use App\Entity\Booking;
  14. use App\Entity\Referer;
  15. use App\Entity\Hotel;
  16. use App\Entity\Housekeeper;
  17. use App\Entity\Season;
  18. use App\Entity\Room;
  19. use App\Entity\Financial;
  20. use App\Entity\Guest;
  21. use App\Repository\HousekeeperRepository;
  22. class DefaultController extends AbstractController
  23. {
  24.     private $booking_repository;
  25.     
  26.     public function __construct(
  27.         private EntityManagerInterface $entityManager,
  28.         private HousekeepingManager $housekeepingManager,
  29.         private HousekeeperRepository $housekeeperRepository,
  30.         private BookingService $bookingService,
  31.     ) {
  32.         $this->booking_repository $entityManager->getRepository(Booking::class);
  33.         $this->roomRepository $entityManager->getRepository(Room::class);
  34.     }
  35.     
  36.     public function index(): Response
  37.     {
  38.         $user $this->getUser();
  39.         // Check for a valid user and necessary methods
  40.         if (!$user || !method_exists($user'getHotelUsers') || !method_exists($user'getRoomUsers')) {
  41.             // Handle this error case as you see fit, for example:
  42.             throw new \RuntimeException('Invalid user or missing methods.');
  43.         }
  44.         $properties = [
  45.             'admin' => [],
  46.             'hotel' => [],
  47.             'owner' => [],
  48.             'cleaning' => [],
  49.         ];
  50.         foreach ($user->getHotelUsers() as $hotel_user) {
  51.             $this->assignPropertiesByRole($hotel_user$properties'getHotel');
  52.         }
  53.         foreach ($user->getRoomUsers() as $room_user) {
  54.             $this->assignPropertiesByRole($room_user$properties'getRoom');
  55.         }
  56.         return $this->render('default/index.html.twig', [
  57.             'properties' => $properties,
  58.         ]);
  59.     }
  60.     
  61.     public function search(Request $request){
  62.         $term $request->get('term');
  63.         
  64.         $user $this->isGranted('ROLE_ADMIN') ? null $this->getUser();
  65.         
  66.         // Supposons que votre méthode searchByTerm renvoie un tableau de résultats
  67.         $bookings $this->booking_repository->searchByTermAndUser($term$user);
  68.         // Construire la réponse au format JSON
  69.         $response = [];
  70.         foreach ($bookings as $booking) {
  71.             $response[] = [
  72.                 'id' => $booking->getId(), // Supposons que l'ID soit dans la propriété id
  73.                 'date' => $booking->getArrival()->format('D d M Y'), // Formatage de la date selon vos besoins
  74.                 'name' => (string) $booking->getGuest(), // Supposons que le nom soit dans la propriété name
  75.             ];
  76.         }
  77.         return new JsonResponse($response);
  78.     }
  79.     
  80.     public function hotelMenu($route): Response 
  81.     {
  82.         $user $this->getUser();
  83.         
  84.         $hotels $this->entityManager->getRepository(Hotel::class)->findByUser($user);
  85.         
  86.         return $this->render('default/hotel_menu.html.twig', [
  87.             'hotels' => $hotels,
  88.             'route' => $route,
  89.         ]);
  90.     }
  91.     
  92.     public function myBooking(): Response
  93.     {
  94.         $user_id $this->isGranted('ROLE_ADMIN') ? null $this->getUser()->getId();
  95.         
  96.         $arrival_list $this->booking_repository->findByType('arrival''hotel'$user_id);
  97.         $departure_list $this->booking_repository->findByType('departure''hotel'$user_id);
  98.         $in_progress_list $this->booking_repository->findByType('in-progress''hotel'$user_id);
  99.         $next_list $this->booking_repository->findByType('next''hotel'$user_id);
  100.                 
  101.         return $this->render('default/my_booking.html.twig', [ 
  102.             'arrival_list' => $arrival_list,
  103.             'departure_list' => $departure_list,
  104.             'in_progress_list' => $in_progress_list,
  105.             'next_list' => $next_list,
  106.         ]);
  107.     }
  108.     
  109.     public function cleaning(): Response 
  110.     {
  111.         $user_id $this->getUser()->getId();
  112.         $housekeepers $this->housekeeperRepository->findMyHousekeeper($user_id);
  113.         
  114.         return $this->render('default/cleaning.html.twig', [ 
  115.             'housekeepers' => $housekeepers,
  116.         ]);
  117.     }
  118.     
  119.     public function lastBooking(): Response 
  120.     {
  121.         $user_id $this->isGranted('ROLE_ADMIN') ? null $this->getUser()->getId();
  122.         
  123.         $last_bookings $this->booking_repository->findMyLastBookings($user_id);
  124.         
  125.         return $this->render('default/last_booking.html.twig', [
  126.             'last_bookings' => $last_bookings,
  127.         ]);
  128.     }
  129.     
  130.     public function owner(): Response 
  131.     {
  132.         $user_id $this->getUser()->getId();
  133.         
  134.         $holidays $this->booking_repository->findMyHolidays($user_id);
  135.         
  136.         return $this->render('default/my_holiday.html.twig', [
  137.             'holidays' => $holidays,
  138.         ]);
  139.     }
  140.     
  141.     public function fetchDailyAndNextCleanings(): Response
  142.     {
  143.         $dailyCleanings $this->booking_repository->findDailyCleanings($this->getUser());
  144.         $nextCleanings $this->booking_repository->findNextCleanings($this->getUser());
  145.         
  146.         return $this->render('default/daily_and_next_cleanings.html.twig', [
  147.             'dailyCleanings' => $dailyCleanings,
  148.             'nextCleanings' => $nextCleanings,
  149.         ]);
  150.     }
  151.     
  152.     public function termsConditions(): Response
  153.     {
  154.         return $this->render('default/terms_conditions.html.twig');
  155.     }
  156.     
  157.     public function validateTermsConditions(): Response 
  158.     {
  159.         
  160.         return $this->render('default/validate_terms_conditions.html.twig');
  161.     }
  162.     
  163.     public function blockNights(): Response 
  164.     {
  165.         $userIsAvailable $this->getUser()->getHotelUsers()->isEmpty() ? false true;
  166.        
  167.         return $this->render('default/_block-nights.html.twig', [
  168.             'userIsAvailable' => $userIsAvailable,
  169.         ]);
  170.     }
  171.     
  172.     public function blockNightsModal(Request $request): Response 
  173.     {
  174.         
  175.         if($this->isGranted('ROLE_ADMIN')){
  176.             $rooms $this->roomRepository->findAll();
  177.         }else{
  178.             $rooms $this->roomRepository->findByUser($this->getUser());
  179.         }
  180.             
  181.         $form $this->createFormBuilder()
  182.             ->setAction($this->generateUrl('admin_block_nights_modal'))
  183.             ->add('rooms'EntityType::class, [ 'label' => 'Chambre à bloquer''class' => Room::class, 'choice_label' => 'name''choices' => $rooms])
  184.             ->add('arrival'null, [ 'label' => "Date d'arrivée" ])
  185.             ->add('departure'null, [ 'label' => "Date de départ" ])
  186.             ->add('save'SubmitType::class, [ 'label' => 'Bloquer' ])
  187.             ->getForm();
  188.         
  189.         $isCleaningAvailable $this->isCleaningAvailable($rooms);
  190.         if($isCleaningAvailable){
  191.             $form->add('cleaning'ChoiceType::class, [
  192.                 'label' => "Le service de ménage doit-il passer pour ce blocage?",
  193.                 'choices' => [
  194.                     'Oui' => true,
  195.                     'Non' => false,
  196.                 ],
  197.                 'expanded' => true// Affiche les choix sous forme de boutons radio
  198.                 'multiple' => false// Assure qu'un seul choix peut être sélectionné
  199.                 'data' => true,
  200.             ]);
  201.         }
  202.         
  203.         $form->handleRequest($request);
  204.         if ($form->isSubmitted() && $form->isValid()) {
  205.             $form_data $form->getData();
  206.             
  207.             $result $this->bookingService->createBooking($form_data);
  208.             
  209.             if($result){
  210.                 $this->addFlash('success'"Vous avez correctement bloqué les nuits");
  211.             }else{
  212.                 $this->addFlash('error'"Les dates sélectionnées ne sont pas disponible.");
  213.             }
  214.             
  215.             return $this->redirectToRoute('admin_calendar_index');
  216.         }
  217.         
  218.         return $this->render('default/_block-nights-modal.html.twig', [
  219.             'form' => $form->createView(),
  220.             'isCleaningAvailable' => $isCleaningAvailable,
  221.         ]);
  222.     }
  223.     
  224.     public function modify(HousekeepingManager $housekeepingManager){
  225.         $housekeepersOfTheDay $this->housekeeperRepository->findAllDailyHousekeepers();
  226.        
  227.         // On crée un tableau pour stocker les IDs des utilisateurs (User) déjà traités
  228.         $uniqueUsers = [];
  229.         $filteredHousekeepers = [];
  230.         foreach ($housekeepersOfTheDay as $housekeeper) {
  231.             $userId $housekeeper->getCleaning()->getId(); // récupère l'ID de l'utilisateur (User)
  232.             if (!in_array($userId$uniqueUsers)) {
  233.                 $uniqueUsers[] = $userId// Ajoute l'ID utilisateur au tableau des IDs uniques
  234.                 $filteredHousekeepers[] = $housekeeper// Ajoute le résultat filtré à la liste
  235.             }
  236.         }
  237.         
  238.         foreach($filteredHousekeepers as $housekeeper){
  239.             $this->housekeepingManager->sendReminderToHousekeeper($housekeeper);
  240.         }
  241.         return new Response('ok');
  242.     }
  243.     
  244.     private function assignPropertiesByRole($user, array &$propertiesstring $method): void
  245.     {
  246.         $roleMapping = [
  247.             'ROLE_ADMIN' => 'admin',
  248.             'ROLE_HOTEL' => 'hotel',
  249.             'ROLE_OWNER' => 'owner',
  250.             'ROLE_CLEANING' => 'cleaning',
  251.         ];
  252.         foreach ($roleMapping as $role => $key) {
  253.             if (method_exists($user'hasRole') && $user->hasRole($role) && method_exists($user$method)) {
  254.                 $properties[$key][] = $user->$method();
  255.             }
  256.         }
  257.     }
  258.     
  259.     private function getReferer(Booking $booking){
  260.         $referer_list $booking->getHotel()->getReferers();
  261.         foreach($referer_list as $referer){
  262.             if(str_contains($booking->getOrigin(), $referer->getKeyword())){
  263.                 return $referer;
  264.             }
  265.         }
  266.         
  267.         return $this->entityManager->getRepository(Referer::class)->find(10);
  268.     }
  269.     
  270.     private function isCleaningAvailable(array $rooms){
  271.         foreach($rooms as $room){
  272.             if(!$room->getRoomUsers()->isEmpty()){return true;}
  273.         }
  274.         
  275.         return false;
  276.     }
  277. }