src/Controller/Front/BookingController.php line 89

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Constant\BookingMetadata;
  4. use App\Constant\BookingRoomStatus;
  5. use App\Constant\BookingStatus;
  6. use App\Constant\BookingType;
  7. use App\Constant\NotificationType;
  8. use App\Constant\OccupancyStatus;
  9. use App\Constant\PaymentStatus;
  10. use App\Controller\AppController;
  11. use App\Entity\AccommodationOccupancy;
  12. use App\Entity\AccommodationRoom;
  13. use App\Entity\Booking;
  14. use App\Entity\BookingRoom;
  15. use App\Entity\Metadata;
  16. use App\Entity\Page;
  17. use App\Entity\User;
  18. use App\Form\Front\BookingDateType;
  19. use App\Form\Front\BookingFinancialSituationType;
  20. use App\Form\Front\BookingIdentityType;
  21. use App\Repository\UserRepository;
  22. use App\Service\BookingService;
  23. use App\Service\CouponChecker;
  24. use App\Service\InventoryService;
  25. use App\Service\PaymentService;
  26. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  27. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\Routing\Annotation\Route;
  30. /**
  31.  * @Route("/reservation", name="front_booking_")
  32.  * @Security("is_granted('ROLE_USER')")
  33.  */
  34. class BookingController extends AppController
  35. {
  36.     private function checkIfUserOwnBooking (Booking $bookingstring $status null) {
  37.         if($booking->getUser()->getId() !== $this->getUser()->getId()) throw $this->createNotFoundException('Réservation introuvable');
  38.         if(!empty($status)){
  39.             if($status !== $booking->getStatus()) throw $this->createNotFoundException('Réservation introuvable');
  40.         }
  41.     }
  42.     private function handleBookingDateForm ($request$booking) {
  43.         $form $this->createForm(BookingDateType::class, $booking);
  44.         $form->handleRequest($request);
  45.         if ($form->isSubmitted() && $form->isValid()) {
  46.             $bookingRoom $booking->getRooms()->first();
  47.             if(!empty($bookingRoom)) {
  48.                 $date \DateTime::createFromFormat(
  49.                     $form->get('dateFormat')->getViewData(),
  50.                     $form->get('dateFrom')->getViewData()
  51.                 );
  52.                 $bookingRoom->setEntranceDate($date);
  53.             }
  54.             if($booking->isMove()){
  55.                 $metadata = new Metadata(BookingMetadata::INVENTORY_USER$form->get('dateUid')->getViewData());
  56.                 $booking->addMetadata($metadata);
  57.             }
  58.             $this->em->persist($booking);
  59.             $this->em->flush();
  60.             return $this->redirectToRoute("front_booking_identity", ["booking" => $booking->getId()]);
  61.         }
  62.         $page = new Page();
  63.         $page->setIndexed(false);
  64.         $page->setMetaTitle("Réservation de votre chambre - Citizens");
  65.         return $this->render('front/booking/booking_1.html.twig', [
  66.             'booking' => $booking,
  67.             'page' => $page,
  68.             'accommodation' => $booking->getAccommodation(),
  69.             'form' => $form->createView(),
  70.         ]);
  71.     }
  72.     /**
  73.      * @Route("/chambre/{room}", name="index")
  74.      */
  75.     public function index(Request $requestAccommodationRoom $room)
  76.     {
  77.         $occupancy $this->getUser()->getCurrentOccupancy();
  78.         if($occupancy instanceof AccommodationOccupancy){
  79.             if($occupancy->isLeaving() || $occupancy->isSwitching()){
  80.                 // If the user is leaving or switching room, show an alert message
  81.                 return $this->render('front/booking/booking_1_alert.html.twig', [
  82.                     '$occupancy' => $occupancy,
  83.                     'page' => new Page(),
  84.                 ]);
  85.             }
  86.         }
  87.         if(empty($booking)) {
  88.             $booking = new Booking();
  89.             $booking->setUser($this->getUser());
  90.             $booking->generateReference();
  91.             $booking->setAccommodation($room->getAccommodation());
  92.             $booking->setStatus(BookingStatus::DRAFT);
  93.             $booking->setType((!$this->getUser()->isOccupyingRoom()) ? BookingType::CLASSIC BookingType::MOVE);
  94.             $bookingRoom = new BookingRoom();
  95.             $bookingRoom->setUser($this->getUser());
  96.             $bookingRoom->setStatus(BookingRoomStatus::PENDING);
  97.             $bookingRoom->setRoom($room);
  98.             $booking->addRoom($bookingRoom);
  99.         }
  100.         return $this->handleBookingDateForm($request$booking);
  101.     }
  102.     /**
  103.      * @Route("/room/{room}/date-update", name="ajax_date_update", methods={"GET"})
  104.      */
  105.     public function dateUpdate(Request $requestAccommodationRoom $room)
  106.     {
  107.         $date \DateTime::createFromFormat($request->query->get('format''d/m/Y'), $request->query->get('date'));
  108.         $booking = new Booking();
  109.         $bookingRoom = new BookingRoom();
  110.         $bookingRoom->setRoom($room);
  111.         $bookingRoom->setEntranceDate($date);
  112.         $booking->addRoom($bookingRoom);
  113.         $params = [
  114.             "accommodation" => $room->getAccommodation(),
  115.             "room" => $room,
  116.             "booking" => $booking,
  117.         ];
  118.         return $this->json([
  119.             'to_pay_before' => $this->render('front/booking/booking_1_to_pay_before.html.twig'$params)->getContent(),
  120.             'sidebar' => $this->render('front/booking/booking_sidebar.html.twig'$params)->getContent(),
  121.         ]);
  122.     }
  123.     /**
  124.      * @Route("/{booking}/date", name="date")
  125.      */
  126.     public function date(Request $requestBooking $booking)
  127.     {
  128.         $this->checkIfUserOwnBooking($bookingBookingStatus::DRAFT);
  129.         return $this->handleBookingDateForm($request$booking);
  130.     }
  131.     /**
  132.      * @Route("/{booking}/identite", name="identity")
  133.      */
  134.     public function identity(Request $requestBooking $booking)
  135.     {
  136.         $user $this->getUser();
  137.         $this->checkIfUserOwnBooking($bookingBookingStatus::DRAFT);
  138.         $form $this->createForm(BookingIdentityType::class, $user);
  139.         $form->handleRequest($request);
  140.         if ($form->isSubmitted() && $form->isValid()) {
  141.             $this->em->persist($user);
  142.             $this->em->flush();
  143.             return $this->redirectToRoute("front_booking_financial_situation", ["booking" => $booking->getId()]);
  144.         }
  145.         $page = new Page();
  146.         $page->setIndexed(false);
  147.         $page->setMetaTitle("Réservation de votre chambre - Citizens");
  148.         return $this->render('front/booking/booking_2.html.twig', [
  149.             'booking' => $booking,
  150.             'page' => $page,
  151.             'form' => $form->createView(),
  152.         ]);
  153.     }
  154.     /**
  155.      * @Route("/{booking}/situation-financiere", name="financial_situation")
  156.      */
  157.     public function financial_situation(Request $requestBooking $booking)
  158.     {
  159.         $user $this->getUser();
  160.         $this->checkIfUserOwnBooking($bookingBookingStatus::DRAFT);
  161.         $form $this->createForm(BookingFinancialSituationType::class, $user);
  162.         $form->handleRequest($request);
  163.         if ($form->isSubmitted() && $form->isValid()) {
  164.             $this->em->persist($user);
  165.             $this->em->flush();
  166.             return $this->redirectToRoute("front_booking_payment", ["booking" => $booking->getId()]);
  167.         }
  168.         $page = new Page();
  169.         $page->setIndexed(false);
  170.         $page->setMetaTitle("Réservation de votre chambre - Citizens");
  171.         return $this->render('front/booking/booking_3.html.twig', [
  172.             'booking' => $booking,
  173.             'page' => $page,
  174.             'form' => $form->createView(),
  175.         ]);
  176.     }
  177.     /**
  178.      * @Route("/{booking}/coupon-check", name="ajax_coupon_check", methods={"POST"})
  179.      */
  180.     public function couponCheck(Request $requestBooking $bookingParameterBagInterface $paramsCouponChecker $couponCheckerPaymentService $paymentService)
  181.     {
  182.         $user $this->getUser();
  183.         $this->checkIfUserOwnBooking($bookingBookingStatus::DRAFT);
  184.         $intent $paymentService->getPaymentIntent($booking->getPayment());
  185.         $coupon $couponChecker->apply_coupon($request->request->get('code'), $user$booking);
  186.         $paymentService->updateAmount($booking->getPayment(), $booking->getTotalPrice() * 100);
  187.         $this->em->persist($booking);
  188.         $this->em->flush();
  189.         $params = [
  190.             "accommodation" => $booking->getAccommodation(),
  191.             "booking" => $booking,
  192.             'stripe' => [
  193.                 'intent' => $intent,
  194.             ],
  195.         ];
  196.         return $this->json([
  197.             'coupon' => $this->render('front/booking/booking_discount_code.html.twig'$params)->getContent(),
  198.             'payment' => $this->render('front/booking/booking_payment_card.html.twig'$params)->getContent(),
  199.             'sidebar' => $this->render('front/booking/booking_sidebar.html.twig'$params)->getContent(),
  200.         ]);
  201.     }
  202.     /**
  203.      * @Route("/{booking}/paiement", name="payment")
  204.      */
  205.     public function payment(Request $requestBooking $bookingParameterBagInterface $paramsCouponChecker $couponCheckerPaymentService $paymentService)
  206.     {
  207.         $user $this->getUser();
  208.         $this->checkIfUserOwnBooking($bookingBookingStatus::DRAFT);
  209.         //-------------------------------
  210.         // Check coupon code
  211.         if (!empty($booking->getDiscountCode())) {
  212.             $couponChecker->apply_coupon($booking->getDiscountCode(), $user$booking); // Check if coupon is still valid
  213.             if (empty($booking->getDiscountCode())) {
  214.                 $paymentService->updateAmount($booking->getPayment(),  $booking->getTotalPrice() * 100);
  215.             }
  216.         }
  217.         //------------------------------------------------------------
  218.         // Create or Update stripe customer
  219.         if ($user instanceof User) {
  220.             $paymentService->updateCustomer($user);
  221.         }
  222.         //-------------------------------
  223.         // Stripe - Payment Intent
  224.         if(empty($booking->getPayment())) {
  225.             $intent $paymentService->createBookingPayment($booking);
  226.         }else{
  227.             $intent $paymentService->getPaymentIntent($booking->getPayment());
  228.         }
  229.         $this->em->flush();
  230.         $page = new Page();
  231.         $page->setIndexed(false);
  232.         $page->setMetaTitle("Réservation de votre chambre - Citizens");
  233.         return $this->render('front/booking/booking_4.html.twig', [
  234.             'booking' => $booking,
  235.             'page' => $page,
  236.             'stripe' => [
  237.                 'intent' => $intent,
  238.             ],
  239.         ]);
  240.     }
  241.     /**
  242.      * @Route("/{booking}/confirmation", name="confirmation")
  243.      */
  244.     public function confirmation(
  245.         Booking $booking,
  246.         PaymentService $paymentService,
  247.         BookingService $bookingService,
  248.         InventoryService $inventoryService,
  249.         UserRepository $userRepository
  250.     ) {
  251.         $user $this->getUser();
  252.         $this->checkIfUserOwnBooking($booking);
  253.         if (!empty($booking->getPayment()) && BookingStatus::DRAFT === $booking->getStatus()){
  254.             $paymentIntent $paymentService->getPaymentIntent($booking->getPayment());
  255.             if (
  256.                 (!empty($paymentIntent) && $paymentIntent->status === 'succeeded')
  257.             ) {
  258.                 $booking->setStatus(BookingStatus::PENDING);
  259.                 $booking->getPayment()->setStatus(PaymentStatus::PAID);
  260.                 // -----------------------------------------------------
  261.                 // If user is switching room, create the inventories
  262.                 if ($booking->getType() === BookingType::MOVE) {
  263.                     $occupancy $booking->getUser()->getCurrentOccupancy();
  264.                     if ($occupancy instanceof AccommodationOccupancy) {
  265.                         $occupancy->setStatus(OccupancyStatus::SWITCHING);
  266.                         $this->em->persist($occupancy);
  267.                         try {
  268.                             // Get inventory admin user selected
  269.                             $inventoryUser $userRepository->findOneBy(["id" => $booking->getMetadata(BookingMetadata::INVENTORY_USER)]);
  270.                             // ---------------------------------------
  271.                             // Create the inventories (departure and entrance)
  272.                             $date $booking->getRooms()->first()->getEntranceDate();
  273.                             $inventoryService->departure($user->getCurrentOccupancy(), $date$inventoryUser);
  274.                             $entranceDate = clone $date;
  275.                             $entranceDate->modify("+2 hours");
  276.                             $inventoryService->entrance($booking$entranceDate$inventoryUserfalse);
  277.                         } catch (\Exception $e) {
  278.                             dump($e);
  279.                         }
  280.                     }
  281.                 }
  282.                 // -----------------------------------------------------
  283.                 // Persist and notify
  284.                 $this->em->persist($booking);
  285.                 $this->em->flush();
  286.                 $bookingService->updateAvailability($booking);
  287.                 
  288.                 $this->notifier->notify($booking->getUser(), NotificationType::BOOKING_CONFIRM, [
  289.                     "booking" => $booking,
  290.                 ]);
  291.             }
  292.         }
  293.         $page = new Page();
  294.         $page->setIndexed(false);
  295.         $page->setMetaTitle("Confirmation de réservation - Citizens");
  296.         return $this->render('front/booking/booking_confirm.html.twig', [
  297.             'booking' => $booking,
  298.             'page' => $page,
  299.         ]);
  300.     }
  301. }