src/Controller/Front/AccountController.php line 47

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\DocumentType;
  6. use App\Constant\NotificationType;
  7. use App\Constant\PaymentStatus;
  8. use App\Controller\AppController;
  9. use App\Entity\AccommodationOccupancy;
  10. use App\Entity\Booking;
  11. use App\Entity\BookingDocument;
  12. use App\Entity\BookingRoom;
  13. use App\Entity\Inventory;
  14. use App\Entity\InventoryProxy;
  15. use App\Entity\Page;
  16. use App\Entity\UserDocument;
  17. use App\Form\Front\InventoryProxyType;
  18. use App\Form\Front\UserIdentityType;
  19. use App\Repository\BookingRepository;
  20. use App\Repository\InventoryRepository;
  21. use App\Service\DocuSignService;
  22. use App\Service\FileUploader;
  23. use App\Service\InventoryService;
  24. use App\Service\ProxyService;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  26. use Stripe\Charge;
  27. use Stripe\PaymentIntent;
  28. use Stripe\Stripe;
  29. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  30. use Symfony\Component\HttpFoundation\JsonResponse;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\Routing\Annotation\Route;
  33. use Symfony\Component\Routing\RouterInterface;
  34. /**
  35.  * @Route("/compte", name="front_account_")
  36.  * @Security("is_granted('ROLE_USER')")
  37.  */
  38. class AccountController extends AppController
  39. {
  40.     /**
  41.      * @Route("", name="dashboard", methods={"GET"})
  42.      */
  43.     public function dashboard(Request $request)
  44.     {
  45.         $page = new Page();
  46.         $page->setMetaTitle("Espace Client - Citizens");
  47.         return $this->render('front/account/dashboard.html.twig', [
  48.             'page' => $page,
  49.         ]);
  50.     }
  51.     /**
  52.      * @Route("/documents", name="documents", methods={"GET"})
  53.      */
  54.     public function documents(Request $request)
  55.     {
  56.         $page = new Page();
  57.         $page->setMetaTitle("Documents - Espace Client - Citizens");
  58.         return $this->render('front/account/documents.html.twig', [
  59.             'page' => $page,
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route("/informations", name="informations")
  64.      */
  65.     public function informations(Request $request)
  66.     {
  67.         $user $this->getUser();
  68.         $form $this->createForm(UserIdentityType::class, $user);
  69.         $form->handleRequest($request);
  70.         if ($form->isSubmitted() && $form->isValid()) {
  71.             $this->em->persist($user);
  72.             $this->em->flush();
  73.             return $this->redirectToRoute("front_account_informations");
  74.         }
  75.         $page = new Page();
  76.         $page->setMetaTitle("Informations personnelles - Espace Client - Citizens");
  77.         return $this->render('front/account/informations.html.twig', [
  78.             'page' => $page,
  79.             'form' => $form->createView(),
  80.         ]);
  81.     }
  82.     /**
  83.      * @Route("/loyers", name="rents", methods={"GET"})
  84.      */
  85.     public function rents(Request $requestParameterBagInterface $params)
  86.     {
  87.         $user $this->getUser();
  88.         Stripe::setApiKey($params->get('stripe_secret_key'));
  89.         $payments Charge::all(["customer" => $user->getStripeId()]);
  90.         $page = new Page();
  91.         $page->setMetaTitle("Mes loyers - Espace Client - Citizens");
  92.         return $this->render('front/account/rents.html.twig', [
  93.             'page' => $page,
  94.             'payments' => $payments,
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/etat-des-lieux", name="inventory", methods={"GET"})
  99.      */
  100.     public function inventory(Request $requestInventoryRepository $inventoryRepository)
  101.     {
  102.         $page = new Page();
  103.         $page->setMetaTitle("Mes états des lieux - Espace Client - Citizens");
  104.         $inventories $inventoryRepository->searchInventories(["user" => $this->getUser()], 110, [['event''desc']]);
  105.         return $this->render('front/account/inventory.html.twig', [
  106.             'page' => $page,
  107.             'inventories' => $inventories,
  108.         ]);
  109.     }
  110.     /**
  111.      * @Route("/etat-des-lieux/{inventory}/proxy", name="inventory_proxy", methods={"GET", "POST"})
  112.      */
  113.     public function inventory_proxy(Request $requestInventory $inventoryProxyService $proxyServiceDocuSignService $docuSignService)
  114.     {
  115.         if ($inventory->getUser()->getId() !== $this->getUser()->getId()) return false;
  116.         $proxy = new InventoryProxy();
  117.         $form $this->createForm(InventoryProxyType::class, $proxy);
  118.         $form->handleRequest($request);
  119.         if ($form->isSubmitted() && $form->isValid()) {
  120.             $proxy->setStatus(DocumentStatus::SENT);
  121.             $proxy->setInventory($inventory);
  122.             $this->em->persist($proxy);
  123.             $this->em->flush();
  124.             $documentPath $proxyService->generate($proxy);
  125.             $envelope $docuSignService->createProxyDocumentEnvelope($documentPath$proxy);
  126.             $document = new BookingDocument();
  127.             $document->setName($proxyService->getFileName($proxy));
  128.             $document->setStatus(DocumentStatus::SENT);
  129.             $document->setType(DocumentType::PROXY);
  130.             $document->setDocusignEnvelopeId($envelope->getEnvelopeId());
  131.             $proxy->setDocument($document);
  132.             $this->em->persist($proxy);
  133.             $this->em->flush();
  134.             return $this->redirect($docuSignService->getProxySigningPageUrl(
  135.                 $proxy,
  136.                 $this->generateUrl('front_account_inventory', [], RouterInterface::ABSOLUTE_URL)
  137.             ));
  138.         }
  139.         $page = new Page();
  140.         $page->setMetaTitle("Mes états des lieux - Espace Client - Citizens");
  141.         return $this->render('front/account/inventory_proxy.html.twig', [
  142.             'page' => $page,
  143.             'inventory' => $inventory,
  144.             'form' => $form->createView(),
  145.         ]);
  146.     }
  147.     /**
  148.      * @Route("/sign-proxy/{proxy}", name="sign_proxy", methods={"GET"})
  149.      */
  150.     public function sign_proxy(Request $requestInventoryProxy $proxyDocuSignService $docuSignService)
  151.     {
  152.         if ($proxy->getInventory()->getUser()->getId() !== $this->getUser()->getId()) return false;
  153.         return $this->redirect($docuSignService->getProxySigningPageUrl(
  154.             $proxy,
  155.             $this->generateUrl('front_account_inventory', [], RouterInterface::ABSOLUTE_URL)
  156.         ));
  157.     }
  158.     /**
  159.      * @Route("/reservation/{reference}", name="booking", methods={"GET"})
  160.      */
  161.     public function booking(Request $request$referenceBookingRepository $bookingRepository)
  162.     {
  163.         $user $this->getUser();
  164.         $booking $bookingRepository->findOneBy(["user" => $user"reference" => $reference]);
  165.         if(empty($booking)) throw $this->createNotFoundException('Réservation introuvable');
  166.         $bookingRoom $booking->getRoomByUser($user);
  167.         $page = new Page();
  168.         $page->setMetaTitle("Réservation ".$booking->getReference()." - Espace Client - Citizens");
  169.         return $this->render('front/account/booking.html.twig', [
  170.             'page' => $page,
  171.             'booking' => $booking,
  172.             'bookingRoom' => $bookingRoom,
  173.         ]);
  174.     }
  175.     /**
  176.      * @Route("/{bookingRoom}/payer-caution-premier-loyer", name="first_rent_payment")
  177.      */
  178.     public function first_rent_payment(Request $requestBookingRoom $bookingRoomParameterBagInterface $params)
  179.     {
  180.         if(PaymentStatus::SENT !== $bookingRoom->getFirstRentPayment()->getStatus() || ($bookingRoom->getUser()->getId() !== $this->getUser()->getId())) throw $this->createNotFoundException('Not found');
  181.         Stripe::setApiKey($params->get('stripe_secret_key'));
  182.         $intent PaymentIntent::retrieve($bookingRoom->getFirstRentPayment()->getStripeId());
  183.         if ($intent->status === "succeeded") {
  184.            return $this->redirectToRoute("front_account_first_rent_payment_confirm", ["bookingRoom" => $bookingRoom->getId()]);
  185.         }
  186.         $page = new Page();
  187.         $page->setMetaTitle("Paiement de la caution et de mon premier loyer - Espace Client - Citizens");
  188.         return $this->render('front/account/first-rent-payment.html.twig', [
  189.             'page' => $page,
  190.             'bookingRoom' => $bookingRoom,
  191.             'stripe' => [
  192.                 'intent' => $intent,
  193.             ]
  194.         ]);
  195.     }
  196.     /**
  197.      * @Route("/{bookingRoom}/payer-caution-premier-loyer/confirmation", name="first_rent_payment_confirm")
  198.      */
  199.     public function first_rent_payment_confirm(Request $requestBookingRoom $bookingRoomParameterBagInterface $params)
  200.     {
  201.         if(PaymentStatus::SENT !== $bookingRoom->getFirstRentPayment()->getStatus() || ($bookingRoom->getUser()->getId() !== $this->getUser()->getId())) throw $this->createNotFoundException('Not found');
  202.         Stripe::setApiKey($params->get('stripe_secret_key'));
  203.         $intent PaymentIntent::retrieve($bookingRoom->getFirstRentPayment()->getStripeId());
  204.         if ($intent->status === "succeeded") {
  205.             $bookingRoom->getFirstRentPayment()->setStatus(PaymentStatus::PAID);
  206.             // --------------------------------------------
  207.             // If all rooms first rent are paid, change booking status
  208.             if($bookingRoom->getBooking()->isAllRoomsFirstRentPayed()){
  209.                 $booking $bookingRoom->getBooking();
  210.                 if($booking->getInventory() instanceof Inventory) {
  211.                     $booking->setStatus(BookingStatus::INVENTORY);
  212.                 } else {
  213.                     $booking->setStatus(BookingStatus::WAIT_ADMIN_SIGNATURE);
  214.                 }
  215.                 $this->em->persist($booking);
  216.                 $this->notifier->admin(NotificationType::ADMIN_BOOKING_DEPOSIT_PAID, ["booking" => $booking]);
  217.             }
  218.             $this->em->persist($bookingRoom);
  219.             $this->em->flush();
  220.         }
  221.         $page = new Page();
  222.         $page->setMetaTitle("Paiement de la caution et de mon premier loyer - Espace Client - Citizens");
  223.         return $this->render('front/account/first-rent-payment-confirm.html.twig', [
  224.             'page' => $page,
  225.             'bookingRoom' => $bookingRoom,
  226.         ]);
  227.     }
  228.     /**
  229.      * @Route("/lease-signer-redirect/{id}", name="lease_signer_redirect")
  230.      */
  231.     public function redirectToLeaseSigningPage(BookingRoom $bookingRoomDocuSignService $docuSignService)
  232.     {
  233.         if($bookingRoom->getBooking() && $this->getUser()->getId() === $bookingRoom->getUser()->getId()) {
  234.             return $this->redirect($docuSignService->getDocumentSigningPageUrl(
  235.                 $bookingRoom->getBooking(),
  236.                 $bookingRoom->getUser(),
  237.                 $bookingRoom->getBooking()->getLease()->getDocusignEnvelopeId(),
  238.                 $this->generateUrl('front_account_documents', [], RouterInterface::ABSOLUTE_URL)
  239.             ));
  240.         } else {
  241.             return $this->redirectToRoute('front_account_documents');
  242.         }
  243.     }
  244.     /**
  245.      * @Route("/sepa-signer-redirect/{id}", name="sepa_signer_redirect")
  246.      */
  247.     public function redirectToSepaSigningPage(BookingRoom $bookingRoomDocuSignService $docuSignService)
  248.     {
  249.         if($bookingRoom->getBooking() && $this->getUser()->getId() === $bookingRoom->getUser()->getId()) {
  250.             return $this->redirect($docuSignService->getDocumentSigningPageUrl(
  251.                 $bookingRoom->getBooking(),
  252.                 $bookingRoom->getUser(),
  253.                 $bookingRoom->getSepa()->getDocusignEnvelopeId(),
  254.                 $this->generateUrl('front_account_documents', [], RouterInterface::ABSOLUTE_URL)
  255.             ));
  256.         } else {
  257.             return $this->redirectToRoute('front_account_documents');
  258.         }
  259.     }
  260.     /**
  261.      * @Route("/inventory-signer-redirect/{id}", name="inventory_signer_redirect")
  262.      */
  263.     public function redirectToInventorySigningPage(BookingRoom $bookingRoomDocuSignService $docuSignService)
  264.     {
  265.         if($bookingRoom->getBooking() && $this->getUser()->getId() === $bookingRoom->getUser()->getId()) {
  266.             $inventory $bookingRoom->getBooking()->getInventory();
  267.             return $this->redirect($docuSignService->getInventorySigningPageUrl(
  268.                 $inventory,
  269.                 $this->generateUrl('front_inventory_signature_callback', [
  270.                     "id" => $inventory->getId(),
  271.                     "token" => $inventory->getToken(),
  272.                 ], RouterInterface::ABSOLUTE_URL)
  273.             ));
  274.         } else {
  275.             return $this->redirectToRoute('front_account_documents');
  276.         }
  277.     }
  278.     /**
  279.      * @Route("/leave-inventory-signer-redirect/{id}", name="leave_inventory_signer_redirect")
  280.      */
  281.     public function redirectToLeavingInventorySigningPage(AccommodationOccupancy $occupancyDocuSignService $docuSignService)
  282.     {
  283.         if($occupancy->getLeavingInventory() && $this->getUser()->getId() === $occupancy->getUser()->getId()) {
  284.             $inventory $occupancy->getLeavingInventory();
  285.             return $this->redirect($docuSignService->getInventorySigningPageUrl(
  286.                 $inventory,
  287.                 $this->generateUrl('front_inventory_signature_callback', [
  288.                     "id" => $inventory->getId(),
  289.                     "token" => $inventory->getToken(),
  290.                 ], RouterInterface::ABSOLUTE_URL)
  291.             ));
  292.         } else {
  293.             return $this->redirectToRoute('front_account_documents');
  294.         }
  295.     }
  296.     /**
  297.      * @Route("/document", name="document", methods={"GET"})
  298.      */
  299.     public function document(Request $request)
  300.     {
  301.         $user $this->getUser();
  302.         $documentType $request->query->get('type');
  303.         $documents $user->getDocuments()->filter(function(UserDocument $document) use($documentType){
  304.             return $document->getType() === $documentType;
  305.         });
  306.         $html $this->render("front/_shared/document-upload.html.twig", ["user" => $user"type" => $documentType])->getContent();
  307.         return $this->json([
  308.             "html" => $html,
  309.         ]);
  310.     }
  311.     /**
  312.      * @Route("/document/upload", name="document_upload", methods={"POST"})
  313.      */
  314.     public function upload_document(Request $requestFileUploader $fileUploader)
  315.     {
  316.         $user $this->getUser();
  317.         $attachment null;
  318.         foreach ($request->files as $uploadedFile) {
  319.             if($uploadedFile->isValid() && $fileUploader->validTypes($uploadedFile$request->get("type"))) {
  320.                 $uploadFolder UserDocument::uploadFolder();
  321.                 $fileName $fileUploader->upload($uploadedFile$uploadFoldertrue);
  322.                 $attachment = new UserDocument();
  323.                 $attachment->setPrivate(true);
  324.                 $attachment->setName($fileName);
  325.                 $attachment->setCreatedAt(new \DateTime());
  326.                 $attachment->setUser($user);
  327.                 $attachment->setType($request->get("type"));
  328.                 $attachment->setStatus(DocumentStatus::SENT);
  329.                 $this->em->persist($attachment);
  330.                 $this->notifier->admin(NotificationType::ADMIN_USER_DOCUMENT_SUBMITTED, ["user" => $user"document" => $attachment]);
  331.             }
  332.         }
  333.         $this->em->flush();
  334.         if($attachment instanceof UserDocument){
  335.             $response = new JsonResponse($attachment->toArray());
  336.         }
  337.         else{
  338.             $response = new JsonResponse(['error' => true], 400);
  339.         }
  340.         return $response;
  341.     }
  342.     /**
  343.      * @Route("/{bookingRoom}/frais-etat-des-lieux", name="previous_room_fees_payment")
  344.      */
  345.     public function previous_room_fees_payment(Request $requestBookingRoom $bookingRoomParameterBagInterface $params)
  346.     {
  347.         if(PaymentStatus::SENT !== $bookingRoom->getPreviousRoomPayment()->getStatus() || ($bookingRoom->getUser()->getId() !== $this->getUser()->getId())) throw $this->createNotFoundException('Not found');
  348.         Stripe::setApiKey($params->get('stripe_secret_key'));
  349.         $intent PaymentIntent::retrieve($bookingRoom->getPreviousRoomPayment()->getStripeId());
  350.         if ($intent->status === "succeeded") {
  351.             return $this->redirectToRoute("front_account_previous_room_fees_payment_confirm", ["bookingRoom" => $bookingRoom->getId()]);
  352.         }
  353.         $page = new Page();
  354.         $page->setMetaTitle("Paiement frais état des lieux - Espace Client - Citizens");
  355.         return $this->render('front/account/previous-room-fees-payment.html.twig', [
  356.             'page' => $page,
  357.             'bookingRoom' => $bookingRoom,
  358.             'stripe' => [
  359.                 'intent' => $intent,
  360.             ]
  361.         ]);
  362.     }
  363.     /**
  364.      * @Route("/{bookingRoom}/frais-etat-des-lieux/confirmation", name="previous_room_fees_payment_confirm")
  365.      */
  366.     public function previous_room_fees_payment_confirm(Request $requestBookingRoom $bookingRoomParameterBagInterface $params)
  367.     {
  368.         if(PaymentStatus::SENT !== $bookingRoom->getPreviousRoomPayment()->getStatus() || ($bookingRoom->getUser()->getId() !== $this->getUser()->getId())) throw $this->createNotFoundException('Not found');
  369.         Stripe::setApiKey($params->get('stripe_secret_key'));
  370.         $intent PaymentIntent::retrieve($bookingRoom->getPreviousRoomPayment()->getStripeId());
  371.         if ($intent->status === "succeeded") {
  372.             $bookingRoom->getPreviousRoomPayment()->setStatus(PaymentStatus::PAID);
  373.             $this->em->persist($bookingRoom);
  374.             $this->em->flush();
  375.         }
  376.         $page = new Page();
  377.         $page->setMetaTitle("Paiement frais état des lieux - Espace Client - Citizens");
  378.         return $this->render('front/account/previous-room-fees-payment-confirm.html.twig', [
  379.             'page' => $page,
  380.             'bookingRoom' => $bookingRoom,
  381.         ]);
  382.     }
  383.     /**
  384.      * @Route("/download-document/{type}", name="download_document")
  385.      */
  386.     public function download_document($typeDocuSignService $docuSignService)
  387.     {
  388.         $user $this->getUser();
  389.         $bookingRoom $user->getLastActiveBookingRoom();
  390.         if ($bookingRoom instanceof BookingRoom) {
  391.             $booking $bookingRoom->getBooking();
  392.             if ($booking instanceof Booking) {
  393.                 switch ($type) {
  394.                     case "lease":
  395.                         if ($booking->getLease()) {
  396.                             return $docuSignService->downloadEnvelopeDocument($booking->getLease()->getDocusignEnvelopeId());
  397.                         }
  398.                         break;
  399.                     case "sepa":
  400.                         if ($bookingRoom->getSepa()) {
  401.                             return $docuSignService->downloadEnvelopeDocument($bookingRoom->getSepa()->getDocusignEnvelopeId());
  402.                         }
  403.                         break;
  404.                 }
  405.             }
  406.         }
  407.         return $this->redirectToRoute('front_account_documents');
  408.     }
  409. }