src/Controller/Front/AppointmentController.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use JsonException;
  4. use Exception;
  5. use Throwable;
  6. use App\Adapter\AuthAdapter;
  7. use App\Controller\PageController;
  8. use App\Form\AnonymousClientType;
  9. use App\Model\Appointment;
  10. use App\Model\Client;
  11. use App\Model\Vehicle;
  12. use App\Repository\MenuRepository;
  13. use App\Service\AppointmentService;
  14. use App\Service\SlotService;
  15. use App\Service\ValidatorService;
  16. use Doctrine\Persistence\ManagerRegistry;
  17. use Psr\Log\LoggerInterface;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  19. use Symfony\Component\Form\FormFactoryInterface;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\RedirectResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  26. use Symfony\Component\Serializer\SerializerInterface;
  27. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  28. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  29. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  30. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  31. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  32. /**
  33.  * @Route("/appointment", priority="1", options={"expose"=true}, name="appointment_")
  34.  */
  35. class AppointmentController extends PageController
  36. {
  37.     private LoggerInterface $logger;
  38.     private AppointmentService $appointmentService;
  39.     public function __construct(ManagerRegistry $doctrineMenuRepository $menuRepositoryLoggerInterface $oatLoggerAppointmentService $appointmentService)
  40.     {
  41.         parent::__construct($doctrine$menuRepository);
  42.         $this->logger $oatLogger;
  43.         $this->appointmentService $appointmentService;
  44.     }
  45.     /**
  46.      * @Route("", name="index")
  47.      */
  48.     public function index(Request $request): Response
  49.     {
  50.         $this->getPageByType('create-appointment');
  51.         $this->_initDatas($request);
  52.         $session $request->getSession();
  53.         if (!$session->has('gon')) {
  54.             $session->set('gon'false);
  55.         }
  56.         // init session
  57.         if (!$session->get('appointment')) {
  58.             $session->set('appointment', new Appointment());
  59.         }
  60.         $reinit 0;
  61.         if ($session->has('reinit')) {
  62.             $reinit 1;
  63.             $session->remove('reinit');
  64.         }
  65.         $logout 0;
  66.         if ($session->has('logout')) {
  67.             $logout 1;
  68.             $session->remove('logout');
  69.         }
  70.         $needRegister false;
  71.         if($this->isGranted('ROLE_CLIENT')) {
  72.             /** @var Client $user */
  73.             $user $this->getUser();
  74.             $needRegister $user->isInformationConfirmed();
  75.         }
  76.         return $this->render('front/appointment/index.html.twig'$this->getDatas([
  77.             'loggedUser' => $this->isGranted('ROLE_CLIENT'),
  78.             'needRegister' => $needRegister,
  79.             'reinit' => $reinit,
  80.             'logout' => $logout,
  81.         ]));
  82.     }
  83.     /**
  84.      * @Route("/connexion", name="auth")
  85.      *
  86.      * @return Response
  87.      */
  88.     public function connexion(AuthenticationUtils $authenticationUtilsRequest $requestAuthAdapter $adapter): Response
  89.     {
  90.         return $this->forward('App\Controller\Front\SecurityController::auth', [$authenticationUtils$request$adapter'rdv' => AppointmentService::APPOINTMENT_SLUG]);
  91.     }
  92.     /**
  93.      * @Route("/profil", name="profil")
  94.      *
  95.      * @return RedirectResponse|Response
  96.      *
  97.      * @throws JsonException
  98.      */
  99.     public function profil(Request $requestValidatorService $validatorFormFactoryInterface $formFactory): Response
  100.     {
  101.         $this->_initDatas($request'anonymous-profil');
  102.         $client = new Client();
  103.         $form $formFactory->createNamed('client'AnonymousClientType::class, $client);
  104.         $form->handleRequest($request);
  105.         if ($form->isSubmitted()) {
  106.             if ($form->isValid()) {
  107.                 $datas $request->request->get('client')['login'];
  108.                 $client->setEmail($datas['email']);
  109.                 $this->addFlash('success''inscription1.confirmation');
  110.                 /** @var Appointment $appointment */
  111.                 $session $request->getSession();
  112.                 $appointment $session->get('appointment');
  113.                 $appointment->setClient($client);
  114.                 $session->set('gon'true);
  115.                 return $this->redirectToRoute('appointment_index');
  116.             }
  117.         }
  118.         $errors $validator->getErrorsForm($form);
  119.         return $this->render(
  120.             'front/appointment/profil.html.twig',
  121.             $this->getDatas([
  122.                 'clientForm' => $form->createView(),
  123.                 'errors' => $errors,
  124.             ])
  125.         );
  126.     }
  127.     /**
  128.      * @Route("/garages", name="garages", methods={"GET", "POST"})
  129.      */
  130.     public function garages(Request $requestSerializerInterface $serializer): JsonResponse
  131.     {
  132.         try {
  133.             $session $request->getSession();
  134.             $garages $this->appointmentService->getGarages();
  135.             $session->set('garages'$garages);
  136.             /** @var Appointment $appointment */
  137.             $appointment $session->get('appointment');
  138.             $currentStep $request->query->get('currentStep'1);
  139.             $maxStep $request->query->get('maxStep'1);
  140.             $session->set('maxStep'$maxStep);
  141.             $current = ($appointment) ? $appointment->getGarage() : null;
  142.             if ($request->isMethod('POST')) {
  143.                 /** @var Appointment $appointment */
  144.                 $appointment $session->get('appointment');
  145.                 $garageId $request->request->get('garageId');
  146.                 $garage $this->appointmentService->getSelectedGarage($garages$garageId);
  147.                 if (!$garage) {
  148.                     throw new Exception('location.not.found');
  149.                 }
  150.                 $appointment->setGarage($garage);
  151.                 // reinit all futur step
  152.                 $this->appointmentService->removeNextStep($appointment'garage');
  153.                 $session->remove('vehicles');
  154.                 return $this->json([
  155.                     'success' => true,
  156.                     'data' => $garage,
  157.                 ]);
  158.             }
  159.             return $this->json([
  160.                 'success' => true,
  161.                 'data' => [
  162.                     'html' => $this->renderView('front/appointment/garages.html.twig', [
  163.                         'maxStep' => $maxStep,
  164.                         'currentStep' => $currentStep,
  165.                         'garages' => $garages,
  166.                         'current' => $current,
  167.                         'appointment' => $appointment
  168.                     ]),
  169.                     'garages' => $garages,
  170.                 ],
  171.             ]);
  172.         } catch (Throwable $exception) {
  173.             $this->logger->error('GET GARAGE ERROR : '.$exception->getMessage());
  174.             return $this->json([
  175.                 'error' => true,
  176.             ], Response::HTTP_INTERNAL_SERVER_ERROR);
  177.         }
  178.     }
  179.     /**
  180.      * @Route("/vehicle/{id}/save", name="vehicle_save", methods={"POST"})
  181.      *
  182.      * @ParamConverter("selectedVehicle", class="App\Model\Vehicle")
  183.      *
  184.      * @return void
  185.      */
  186.     public function vehicle(Request $requestVehicle $selectedVehicle): JsonResponse
  187.     {
  188.         $session $request->getSession();
  189.         /** @var Appointment $appointment */
  190.         $appointment $session->get('appointment');
  191.         $appointment->setVehicle($selectedVehicle);
  192.         // reinit all futur step
  193.         $this->appointmentService->removeNextStep($appointment'vehicle');
  194.         return $this->json([
  195.             'success' => true,
  196.             'vehicle' => $selectedVehicle,
  197.         ]);
  198.     }
  199.     /**
  200.      * @Route("/service", name="service", methods={"POST", "GET"})
  201.      */
  202.     public function service(Request $request): JsonResponse
  203.     {
  204.         $currentStep $request->query->get('currentStep'3);
  205.         $maxStep $request->query->get('maxStep'3);
  206.         $session $request->getSession();
  207.         $session->set('maxStep'$maxStep);
  208.         /** @var Appointment $appointment */
  209.         $appointment $session->get('appointment');
  210.         $data = [
  211.             'brand' => $appointment->getVehicle()->getBrand(),
  212.             'model' => $appointment->getVehicle()->getModel(),
  213.             'submodel' => $appointment->getVehicle()->getSubModel(),
  214.             'language' => $request->getLocale(),
  215.         ];
  216.         $services $this->appointmentService->getService($data);
  217.         /** @var array $ids */
  218.         $ids $request->request->get('serviceIds', []);
  219.         if ($request->isMethod('POST')) {
  220.             $selectedServices = [];
  221.             foreach ($services as $service) {
  222.                 if (in_array($service['id'], $idstrue)) {
  223.                     $selectedServices[] = $service;
  224.                 }
  225.             }
  226.             $appointment->setServices($selectedServices);
  227.             // reinit all futur step
  228.             $this->appointmentService->removeNextStep($appointment'service');
  229.             return $this->json([
  230.                 'success' => true,
  231.                 'services' => $ids,
  232.             ]);
  233.         }
  234.         if ($rdvServices $appointment->getServices()) {
  235.             foreach ($rdvServices as $service) {
  236.                 $ids[] = $service['id'];
  237.             }
  238.         }
  239.         return $this->json([
  240.             'success' => true,
  241.             'data' => [
  242.                 'html' => $this->renderView('front/appointment/service.html.twig', [
  243.                     'maxStep' => $maxStep,
  244.                     'currentStep' => $currentStep,
  245.                     'services' => $services,
  246.                     'current' => $appointment->getServices(),
  247.                     'ids' => $ids,
  248.                     'appointment' => $appointment
  249.                 ]),
  250.             ],
  251.         ]);
  252.     }
  253.     /**
  254.      * @Route("/availability", name="availability")
  255.      */
  256.     public function availability(Request $requestSlotService $slotService): JsonResponse
  257.     {
  258.         $maxStep $request->query->get('maxStep'4);
  259.         $currentStep $request->query->get('currentStep'4);
  260.         $day $request->get('day');
  261.         $time $request->get('time');
  262.         $timeSlot = [
  263.             'day' => $day,
  264.             'time' => $time,
  265.         ];
  266.         /** @var Appointment $appointment */
  267.         $session $request->getSession();
  268.         $session->set('maxStep'$maxStep);
  269.         $appointment $session->get('appointment');
  270.         if ($request->isMethod('POST')) { // save in Appointment
  271.             $receptionTime $request->get('receptionTime');
  272.             $deliveryTime $request->get('deliveryTime');
  273.             $receptionSlot = [
  274.                 'reception' => $receptionTime,
  275.                 'delivery' => $deliveryTime,
  276.             ];
  277.             $slotService->saveSlots($appointment$timeSlot$receptionSlot);
  278.             return $this->json([
  279.                 'success' => true,
  280.             ]);
  281.         }
  282.         $responseDatas = [
  283.             'maxStep' => $maxStep,
  284.             'currentStep' => $currentStep,
  285.         ];
  286.         $responseDatas $slotService->handleRequest($request$timeSlot$responseDatas);
  287.         return $this->json([
  288.             'success' => true,
  289.             'data' => [
  290.                 'html' => $this->renderView('front/appointment/availability.html.twig'$responseDatas),
  291.                 'id' => $responseDatas['garageID'],
  292.             ],
  293.         ]);
  294.     }
  295.     /**
  296.      * @Route("/resume", name="resume")
  297.      */
  298.     public function resume(Request $request): JsonResponse
  299.     {
  300.         $maxStep $request->query->get('maxStep'5);
  301.         $currentStep $request->query->get('currentStep'5);
  302.         $session $request->getSession();
  303.         $session->set('maxStep'$maxStep);
  304.         /** @var Appointment $appointment */
  305.         $appointment $session->get('appointment');
  306.         $this->appointmentService->getServiceHeaderInfo($appointment);
  307.         return $this->json([
  308.             'success' => true,
  309.             'data' => [
  310.                 'html' => $this->renderView('front/appointment/resume.html.twig', [
  311.                     'maxStep' => $maxStep,
  312.                     'currentStep' => $currentStep,
  313.                     'appointment' => $appointment
  314.                 ]),
  315.             ],
  316.         ]);
  317.     }
  318.     /**
  319.      * @Route("/payment", name="payment")
  320.      *
  321.      * @return void
  322.      */
  323.     public function payment(Request $request): JsonResponse
  324.     {
  325.         $request->getSession()->set('maxStep'6);
  326.         if ($request->isMethod('POST')) {
  327.             /** @var Appointment $appointment */
  328.             $appointment $request->getSession()->get('appointment');
  329.             $appointment->setPaidOnline((bool) $request->request->get('payment'));
  330.             return $this->json([
  331.                 'success' => true,
  332.             ]);
  333.         }
  334.         return $this->json([
  335.             'data' => [
  336.                 'html' => $this->renderView('front/appointment/payment.html.twig', []),
  337.             ],
  338.         ]);
  339.     }
  340.     /**
  341.      * @Route("/confirmation", name="confirmation", methods={"POST"})
  342.      *
  343.      * @throws JsonException
  344.      * @throws Throwable
  345.      * @throws ClientExceptionInterface
  346.      * @throws DecodingExceptionInterface
  347.      * @throws RedirectionExceptionInterface
  348.      * @throws ServerExceptionInterface
  349.      * @throws TransportExceptionInterface
  350.      */
  351.     public function confirmation(Request $requestAppointmentService $service): JsonResponse
  352.     {
  353.         /** @var Appointment $appointment */
  354.         $appointment $request->getSession()->get('appointment');
  355.         $session $request->getSession();
  356.         $user $this->getUser();
  357.         $isUserConnected $user && !$session->get('gon');
  358.         $this->appointmentService->sendAppointment($appointment$isUserConnected);
  359.         $service->removeAllSession($session);
  360.         return $this->json([
  361.             'data' => [
  362.                 'html' => $this->renderView('front/appointment/confirm_payment.html.twig', [
  363.                     'appointment' => $appointment,
  364.                 ]),
  365.             ],
  366.         ]);
  367.     }
  368. }