src/Controller/Front/AccountController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Constant\BookingStatus;
  4. use App\Constant\DocumentStatus;
  5. use App\Constant\NotificationType;
  6. use App\Constant\PaymentStatus;
  7. use App\Controller\AppController;
  8. use App\Entity\AccommodationOccupancy;
  9. use App\Entity\BookingRoom;
  10. use App\Entity\Inventory;
  11. use App\Entity\Page;
  12. use App\Entity\UserDocument;
  13. use App\Form\Front\UserIdentityType;
  14. use App\Repository\BookingRepository;
  15. use App\Service\DocuSignService;
  16. use App\Service\FileUploader;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  18. use Stripe\Charge;
  19. use Stripe\PaymentIntent;
  20. use Stripe\Stripe;
  21. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  22. use Symfony\Component\HttpFoundation\JsonResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Component\Routing\RouterInterface;
  26. /**
  27.  * @Route("/compte", name="front_account_")
  28.  * @Security("is_granted('ROLE_USER')")
  29.  */
  30. class AccountController extends AppController
  31. {
  32.     /**
  33.      * @Route("", name="dashboard", methods={"GET"})
  34.      */
  35.     public function dashboard(Request $request)
  36.     {
  37.         $page = new Page();
  38.         $page->setMetaTitle("Espace Client - Citizens");
  39.         return $this->render('front/account/dashboard.html.twig', [
  40.             'page' => $page,
  41.         ]);
  42.     }
  43.     /**
  44.      * @Route("/documents", name="documents", methods={"GET"})
  45.      */
  46.     public function documents(Request $request)
  47.     {
  48.         $page = new Page();
  49.         $page->setMetaTitle("Documents - Espace Client - Citizens");
  50.         return $this->render('front/account/documents.html.twig', [
  51.             'page' => $page,
  52.         ]);
  53.     }
  54.     /**
  55.      * @Route("/informations", name="informations")
  56.      */
  57.     public function informations(Request $request)
  58.     {
  59.         $user $this->getUser();
  60.         $form $this->createForm(UserIdentityType::class, $user);
  61.         $form->handleRequest($request);
  62.         if ($form->isSubmitted() && $form->isValid()) {
  63.             $this->em->persist($user);
  64.             $this->em->flush();
  65.             return $this->redirectToRoute("front_account_informations");
  66.         }
  67.         $page = new Page();
  68.         $page->setMetaTitle("Informations personnelles - Espace Client - Citizens");
  69.         return $this->render('front/account/informations.html.twig', [
  70.             'page' => $page,
  71.             'form' => $form->createView(),
  72.         ]);
  73.     }
  74.     /**
  75.      * @Route("/loyers", name="rents", methods={"GET"})
  76.      */
  77.     public function rents(Request $requestParameterBagInterface $params)
  78.     {
  79.         $user $this->getUser();
  80.         Stripe::setApiKey($params->get('stripe_secret_key'));
  81.         $payments Charge::all(["customer" => $user->getStripeId()]);
  82.         $page = new Page();
  83.         $page->setMetaTitle("Mes loyers - Espace Client - Citizens");
  84.         return $this->render('front/account/rents.html.twig', [
  85.             'page' => $page,
  86.             'payments' => $payments,
  87.         ]);
  88.     }
  89.     /**
  90.      * @Route("/etat-des-lieux", name="inventory", methods={"GET"})
  91.      */
  92.     public function inventory(Request $request)
  93.     {
  94.         $page = new Page();
  95.         $page->setMetaTitle("Mon état des lieux - Espace Client - Citizens");
  96.         return $this->render('front/account/inventory.html.twig', [
  97.             'page' => $page,
  98.         ]);
  99.     }
  100.     /**
  101.      * @Route("/reservation/{reference}", name="booking", methods={"GET"})
  102.      */
  103.     public function booking(Request $request$referenceBookingRepository $bookingRepository)
  104.     {
  105.         $user $this->getUser();
  106.         $booking $bookingRepository->findOneBy(["user" => $user"reference" => $reference]);
  107.         if(empty($booking)) throw $this->createNotFoundException('Réservation introuvable');
  108.         $bookingRoom $booking->getRoomByUser($user);
  109.         $page = new Page();
  110.         $page->setMetaTitle("Réservation ".$booking->getReference()." - Espace Client - Citizens");
  111.         return $this->render('front/account/booking.html.twig', [
  112.             'page' => $page,
  113.             'booking' => $booking,
  114.             'bookingRoom' => $bookingRoom,
  115.         ]);
  116.     }
  117.     /**
  118.      * @Route("/{bookingRoom}/payer-caution-premier-loyer", name="first_rent_payment")
  119.      */
  120.     public function first_rent_payment(Request $requestBookingRoom $bookingRoomParameterBagInterface $params)
  121.     {
  122.         if(PaymentStatus::SENT !== $bookingRoom->getFirstRentPayment()->getStatus() || ($bookingRoom->getUser()->getId() !== $this->getUser()->getId())) throw $this->createNotFoundException('Not found');
  123.         Stripe::setApiKey($params->get('stripe_secret_key'));
  124.         $intent PaymentIntent::retrieve($bookingRoom->getFirstRentPayment()->getStripeId());
  125.         if ($intent->status === "succeeded") {
  126.            return $this->redirectToRoute("front_account_first_rent_payment_confirm", ["bookingRoom" => $bookingRoom->getId()]);
  127.         }
  128.         $page = new Page();
  129.         $page->setMetaTitle("Paiement de la caution et de mon premier loyer - Espace Client - Citizens");
  130.         return $this->render('front/account/first-rent-payment.html.twig', [
  131.             'page' => $page,
  132.             'bookingRoom' => $bookingRoom,
  133.             'stripe' => [
  134.                 'intent' => $intent,
  135.             ]
  136.         ]);
  137.     }
  138.     /**
  139.      * @Route("/{bookingRoom}/payer-caution-premier-loyer/confirmation", name="first_rent_payment_confirm")
  140.      */
  141.     public function first_rent_payment_confirm(Request $requestBookingRoom $bookingRoomParameterBagInterface $params)
  142.     {
  143.         if(PaymentStatus::SENT !== $bookingRoom->getFirstRentPayment()->getStatus() || ($bookingRoom->getUser()->getId() !== $this->getUser()->getId())) throw $this->createNotFoundException('Not found');
  144.         Stripe::setApiKey($params->get('stripe_secret_key'));
  145.         $intent PaymentIntent::retrieve($bookingRoom->getFirstRentPayment()->getStripeId());
  146.         if ($intent->status === "succeeded") {
  147.             $bookingRoom->getFirstRentPayment()->setStatus(PaymentStatus::PAID);
  148.             // --------------------------------------------
  149.             // If all rooms first rent are paid, change booking status
  150.             if($bookingRoom->getBooking()->isAllRoomsFirstRentPayed()){
  151.                 $booking $bookingRoom->getBooking();
  152.                 if($booking->getInventory() instanceof Inventory) {
  153.                     $booking->setStatus(BookingStatus::INVENTORY);
  154.                 } else {
  155.                     $booking->setStatus(BookingStatus::INVENTORY_DATE);
  156.                 }
  157.                 $this->em->persist($booking);
  158.                 $this->notifier->admin(NotificationType::ADMIN_BOOKING_DEPOSIT_PAID, ["booking" => $booking]);
  159.             }
  160.             $this->em->persist($bookingRoom);
  161.             $this->em->flush();
  162.         }
  163.         $page = new Page();
  164.         $page->setMetaTitle("Paiement de la caution et de mon premier loyer - Espace Client - Citizens");
  165.         return $this->render('front/account/first-rent-payment-confirm.html.twig', [
  166.             'page' => $page,
  167.             'bookingRoom' => $bookingRoom,
  168.         ]);
  169.     }
  170.     /**
  171.      * @Route("/lease-signer-redirect/{id}", name="lease_signer_redirect")
  172.      */
  173.     public function redirectToLeaseSigningPage(BookingRoom $bookingRoomDocuSignService $docuSignService)
  174.     {
  175.         if($bookingRoom->getBooking() && $this->getUser()->getId() === $bookingRoom->getUser()->getId()) {
  176.             return $this->redirect($docuSignService->getDocumentSigningPageUrl(
  177.                 $bookingRoom->getBooking(),
  178.                 $bookingRoom->getUser(),
  179.                 $bookingRoom->getBooking()->getLease()->getDocusignEnvelopeId(),
  180.                 $this->generateUrl('front_account_documents', [], RouterInterface::ABSOLUTE_URL)
  181.             ));
  182.         } else {
  183.             return $this->redirectToRoute('front_account_documents');
  184.         }
  185.     }
  186.     /**
  187.      * @Route("/sepa-signer-redirect/{id}", name="sepa_signer_redirect")
  188.      */
  189.     public function redirectToSepaSigningPage(BookingRoom $bookingRoomDocuSignService $docuSignService)
  190.     {
  191.         if($bookingRoom->getBooking() && $this->getUser()->getId() === $bookingRoom->getUser()->getId()) {
  192.             return $this->redirect($docuSignService->getDocumentSigningPageUrl(
  193.                 $bookingRoom->getBooking(),
  194.                 $bookingRoom->getUser(),
  195.                 $bookingRoom->getSepa()->getDocusignEnvelopeId(),
  196.                 $this->generateUrl('front_account_documents', [], RouterInterface::ABSOLUTE_URL)
  197.             ));
  198.         } else {
  199.             return $this->redirectToRoute('front_account_documents');
  200.         }
  201.     }
  202.     /**
  203.      * @Route("/inventory-signer-redirect/{id}", name="inventory_signer_redirect")
  204.      */
  205.     public function redirectToInventorySigningPage(BookingRoom $bookingRoomDocuSignService $docuSignService)
  206.     {
  207.         if($bookingRoom->getBooking() && $this->getUser()->getId() === $bookingRoom->getUser()->getId()) {
  208.             $inventory $bookingRoom->getBooking()->getInventory();
  209.             return $this->redirect($docuSignService->getInventorySigningPageUrl(
  210.                 $inventory,
  211.                 $this->generateUrl('front_inventory_signature_callback', [
  212.                     "id" => $inventory->getId(),
  213.                     "token" => $inventory->getToken(),
  214.                 ], RouterInterface::ABSOLUTE_URL)
  215.             ));
  216.         } else {
  217.             return $this->redirectToRoute('front_account_documents');
  218.         }
  219.     }
  220.     /**
  221.      * @Route("/leave-inventory-signer-redirect/{id}", name="leave_inventory_signer_redirect")
  222.      */
  223.     public function redirectToLeavingInventorySigningPage(AccommodationOccupancy $occupancyDocuSignService $docuSignService)
  224.     {
  225.         if($occupancy->getLeavingInventory() && $this->getUser()->getId() === $occupancy->getUser()->getId()) {
  226.             $inventory $occupancy->getLeavingInventory();
  227.             return $this->redirect($docuSignService->getInventorySigningPageUrl(
  228.                 $inventory,
  229.                 $this->generateUrl('front_inventory_signature_callback', [
  230.                     "id" => $inventory->getId(),
  231.                     "token" => $inventory->getToken(),
  232.                 ], RouterInterface::ABSOLUTE_URL)
  233.             ));
  234.         } else {
  235.             return $this->redirectToRoute('front_account_documents');
  236.         }
  237.     }
  238.     /**
  239.      * @Route("/document", name="document", methods={"GET"})
  240.      */
  241.     public function document(Request $request)
  242.     {
  243.         $user $this->getUser();
  244.         $documentType $request->query->get('type');
  245.         $documents $user->getDocuments()->filter(function(UserDocument $document) use($documentType){
  246.             return $document->getType() === $documentType;
  247.         });
  248.         $html $this->render("front/_shared/document-upload.html.twig", ["user" => $user"type" => $documentType])->getContent();
  249.         return $this->json([
  250.             "html" => $html,
  251.         ]);
  252.     }
  253.     /**
  254.      * @Route("/document/upload", name="document_upload", methods={"POST"})
  255.      */
  256.     public function upload_document(Request $requestFileUploader $fileUploader)
  257.     {
  258.         $user $this->getUser();
  259.         $attachment null;
  260.         foreach ($request->files as $uploadedFile) {
  261.             if($uploadedFile->isValid() && $fileUploader->validTypes($uploadedFile$request->get("type"))) {
  262.                 $uploadFolder UserDocument::uploadFolder();
  263.                 $fileName $fileUploader->upload($uploadedFile$uploadFoldertrue);
  264.                 $attachment = new UserDocument();
  265.                 $attachment->setPrivate(true);
  266.                 $attachment->setName($fileName);
  267.                 $attachment->setCreatedAt(new \DateTime());
  268.                 $attachment->setUser($user);
  269.                 $attachment->setType($request->get("type"));
  270.                 $attachment->setStatus(DocumentStatus::SENT);
  271.                 $this->em->persist($attachment);
  272.                 $this->notifier->admin(NotificationType::ADMIN_USER_DOCUMENT_SUBMITTED, ["user" => $user"document" => $attachment]);
  273.             }
  274.         }
  275.         $this->em->flush();
  276.         if($attachment instanceof UserDocument){
  277.             $response = new JsonResponse($attachment->toArray());
  278.         }
  279.         else{
  280.             $response = new JsonResponse(['error' => true], 400);
  281.         }
  282.         return $response;
  283.     }
  284.     /**
  285.      * @Route("/{bookingRoom}/frais-etat-des-lieux", name="previous_room_fees_payment")
  286.      */
  287.     public function previous_room_fees_payment(Request $requestBookingRoom $bookingRoomParameterBagInterface $params)
  288.     {
  289.         if(PaymentStatus::SENT !== $bookingRoom->getPreviousRoomPayment()->getStatus() || ($bookingRoom->getUser()->getId() !== $this->getUser()->getId())) throw $this->createNotFoundException('Not found');
  290.         Stripe::setApiKey($params->get('stripe_secret_key'));
  291.         $intent PaymentIntent::retrieve($bookingRoom->getPreviousRoomPayment()->getStripeId());
  292.         if ($intent->status === "succeeded") {
  293.             return $this->redirectToRoute("front_account_previous_room_fees_payment_confirm", ["bookingRoom" => $bookingRoom->getId()]);
  294.         }
  295.         $page = new Page();
  296.         $page->setMetaTitle("Paiement frais état des lieux - Espace Client - Citizens");
  297.         return $this->render('front/account/previous-room-fees-payment.html.twig', [
  298.             'page' => $page,
  299.             'bookingRoom' => $bookingRoom,
  300.             'stripe' => [
  301.                 'intent' => $intent,
  302.             ]
  303.         ]);
  304.     }
  305.     /**
  306.      * @Route("/{bookingRoom}/frais-etat-des-lieux/confirmation", name="previous_room_fees_payment_confirm")
  307.      */
  308.     public function previous_room_fees_payment_confirm(Request $requestBookingRoom $bookingRoomParameterBagInterface $params)
  309.     {
  310.         dump("test");
  311.         if(PaymentStatus::SENT !== $bookingRoom->getPreviousRoomPayment()->getStatus() || ($bookingRoom->getUser()->getId() !== $this->getUser()->getId())) throw $this->createNotFoundException('Not found');
  312.         Stripe::setApiKey($params->get('stripe_secret_key'));
  313.         $intent PaymentIntent::retrieve($bookingRoom->getPreviousRoomPayment()->getStripeId());
  314.         if ($intent->status === "succeeded") {
  315.             $bookingRoom->getPreviousRoomPayment()->setStatus(PaymentStatus::PAID);
  316.             $this->em->persist($bookingRoom);
  317.             $this->em->flush();
  318.         }
  319.         $page = new Page();
  320.         $page->setMetaTitle("Paiement frais état des lieux - Espace Client - Citizens");
  321.         return $this->render('front/account/previous-room-fees-payment-confirm.html.twig', [
  322.             'page' => $page,
  323.             'bookingRoom' => $bookingRoom,
  324.         ]);
  325.     }
  326. }