<?php
namespace App\Controller\Frontend;
use App\Controller\BaseController;
use App\Entity\Contact;
use App\Entity\EntityStatus;
use App\Entity\Etablissement;
use App\Entity\Order;
use App\Entity\UserQuestion;
use App\Entity\UserResponse;
use App\Entity\User;
use App\Form\Common\ContactCompleteType;
use App\Form\Common\EtablissementType;
use App\Form\Frontend\EntrepriseType;
use App\Repository\CotisationRepository;
use App\Repository\OrderRepository;
use App\Repository\ParameterRepository;
use App\Repository\UserQuestionRepository;
use App\Repository\UserResponseRepository;
use App\Service\IsMemberService;
use App\Service\MailerService;
use App\Service\OrderPdfExport;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class MySpaceController extends BaseController
{
#[Route('/my-space/profile/show', name: 'customer_show_profile', methods: ['GET'])]
public function showProfile(
CotisationRepository $cotisationRepository,
IsMemberService $isMemberService
): Response
{
$entreprise = $this->getUser()->getContact()->getFirm();
$isMemberService->checkIsMember($entreprise);
return $this->render('frontend/my_space/show_profile.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-profile',
'entreprise' => $entreprise,
]);
}
#[Route('/my-space/profile/edit', name: 'customer_edit_profile', methods: ['GET', 'POST'])]
public function editProfile(
Request $request,
EntityManagerInterface $em,
IsMemberService $isMemberService
): Response
{
$entreprise = $this->getUser()->getContact()->getFirm();
$isMemberService->checkIsMember($entreprise);
$form = $this->createForm(EntrepriseType::class, $entreprise);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->flush();
$this->addFlash(
'success',
'Enregistrement effectué avec succès'
);
return $this->redirectToRoute('customer_show_profile', ['id' => $entreprise->getId()]);
}
return $this->renderForm('frontend/my_space/edit_profile.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-profile',
'entreprise' => $entreprise,
'form' => $form
]);
}
#[Route('/my-space/profile/contact/{id}/show', name: 'customer_show_contact', methods: ['GET'])]
public function showContact(Contact $contact): Response
{
return $this->renderForm('frontend/my_space/show_contact.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-profile',
'contact' => $contact
]);
}
#[Route('/my-space/profile/contact/add', name: 'customer_add_contact', methods: ['GET', 'POST'])]
public function addContact(
Request $request,
EntityManagerInterface $em
): Response
{
$contact = new Contact();
$contact
->setFirm($this->getUser()->getContact()->getFirm())
->setStatus(Contact::STATUS_ENABLED)
;
$form = $this->createForm(ContactCompleteType::class, $contact, [
'create_from_dashboard' => false
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact
->setFirm($this->getUser()->getContact()->getFirm())
->setStatus(Contact::STATUS_ENABLED)
;
$em->persist($contact);
$em->flush();
$this->addFlash(
'success',
'Enregistrement effectué avec succès'
);
return $this->redirectToRoute('customer_show_profile', [
'id' => $contact->getFirm()->getId()
]);
}
return $this->renderForm('frontend/my_space/edit_contact.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-profile',
'form' => $form,
'contact' => $contact
]);
}
#[Route('/my-space/profile/contact/{id}/edit', name: 'customer_edit_contact', methods: ['GET', 'POST'])]
public function editContact(
Contact $contact,
Request $request,
EntityManagerInterface $em
)
{
$form = $this->createForm(ContactCompleteType::class, $contact, [
'create_from_dashboard' => false
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->flush();
$this->addFlash(
'success',
'Enregistrement effectué avec succès'
);
return $this->redirectToRoute('customer_show_profile');
}
return $this->renderForm('frontend/my_space/edit_contact.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-profile',
'form' => $form,
'contact' => $contact
]);
}
#[Route('/my-space/profile/contact/{id}/delete', name: 'customer_archive_contact', methods: ['DELETE', 'POST'])]
public function archiveContact(Contact $contact, Request $request, EntityManagerInterface $em): JsonResponse
{
$data = json_decode($request->getContent(), true);
if ($this->isCsrfTokenValid('contact_'.$contact->getId(), $data['_token'])) {
$contact->setStatus(EntityStatus::STATUS_ARCHIVED);
if ($contact->getUser() instanceof User) {
$contact->getUser()->setStatus(EntityStatus::STATUS_ARCHIVED);
}
$em->persist($contact);
$em->flush();
if ($contact->getUser() instanceof User) {
$em->remove($contact->getUser());
}
$em->flush();
return new JsonResponse([
'status' => 'ok',
'message' => 'Suppression effectuée avec succès'
]);
}
return new JsonResponse([
'status' => 'nok',
'message' => 'Erreur lors de la suppression. Merci de contacter un administrateur.'
]);
}
#[Route('/my-space/orders', name: 'customer_list_order', methods: ['GET'])]
public function order(OrderRepository $orderRepository, PaginatorInterface $paginator, Request $request): Response
{
$query = $orderRepository->findCustomerOrderQuery($this->getUser());
$orders = $paginator->paginate(
$query,
$request->query->getInt('page', 1),
self::ITEM_PER_PAGE
);
return $this->render('frontend/my_space/list_order.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-order',
'orders' => $orders
]);
}
#[Route('/my-space/orders/{id}', name: 'customer_show_order', methods: ['GET'])]
public function showOrder(Order $order, ParameterRepository $parameterRepository): Response
{
$rib = $parameterRepository->findOneBy([
'subtype' => 'banque',
'tag' => 'sale.bank.rib',
]);
return $this->render('frontend/my_space/show_order.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-order',
'order' => $order,
'rib' => $rib,
]);
}
#[Route('/my-space/orders/{id}/download-order', name: 'customer_download_order', methods: ['GET'])]
public function downloadBill(Order $order, OrderPdfExport $orderPdfExport): Response
{
$pdfPath = $orderPdfExport->exportOrder($order);
return $this->file($pdfPath, basename($pdfPath));
}
#[Route('/my-space/orders/{id}/download-invoice', name: 'customer_download_invoice', methods: ['GET'])]
public function downloadInvoice(Order $order, OrderPdfExport $orderPdfExport): Response
{
$pdfPath = $orderPdfExport->exportInvoice($order);
return $this->file($pdfPath, basename($pdfPath));
}
#[Route('/my-space/establishments/{id}/show', name: 'customer_show_establishment', methods: ['GET'])]
public function showEtablissement(Etablissement $etablissement): Response
{
return $this->render('frontend/my_space/show_etablissement.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-profile',
'etablissement' => $etablissement
]);
}
#[Route('/my-space/establishments/{id}/add', name: 'customer_add_establishment', methods: ['GET', 'POST'])]
public function addEtablissement(Request $request, EntityManagerInterface $em): Response
{
$entreprise = $this->getUser()->getContact()->getFirm();
$etablissement = new Etablissement();
$etablissement->setEntreprise($entreprise);
$form = $this->createForm(EtablissementType::class, $etablissement);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$etablissement->setEntreprise($entreprise);
$em->persist($etablissement);
$em->flush();
$this->addFlash(
'success',
'Enregistrement effectué avec succès'
);
return $this->redirectToRoute('customer_show_profile');
}
return $this->renderForm('frontend/my_space/edit_etablissement.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-profile',
'entreprise' => $entreprise,
'form' => $form
]);
}
#[Route('/my-space/establishments/{id}/edit', name: 'customer_edit_establishment', methods: ['GET', 'POST'])]
public function editEtablissement(Etablissement $etablissement, Request $request, EntityManagerInterface $em): Response
{
$form = $this->createForm(EtablissementType::class, $etablissement);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$etablissement->setEntreprise($this->getUser()->getContact()->getFirm());
$em->persist($etablissement);
$em->flush();
$this->addFlash(
'success',
'Enregistrement effectué avec succès'
);
return $this->redirectToRoute('customer_show_profile');
}
return $this->renderForm('frontend/my_space/edit_etablissement.html.twig', [
'controller_name' => 'MySpaceController',
'menu' => 'my-space',
'submenu' => 'my-profile',
'etablissement' => $etablissement,
'form' => $form
]);
}
#[Route('/my-space/establishments/{id}/delete', name: 'customer_delete_establishment', methods: ['DELETE', 'POST'])]
public function delete(Etablissement $etablissement, Request $request, EntityManagerInterface $em): JsonResponse
{
$data = json_decode($request->getContent(), true);
if ($this->isCsrfTokenValid('etablissement_'.$etablissement->getId(), $data['_token'])) {
$em->remove($etablissement);
$em->flush();
return new JsonResponse([
'status' => 'ok',
'message' => 'Suppression effectuée avec succès'
]);
}
return new JsonResponse([
'status' => 'nok',
'message' => 'Erreur lors de la suppression. Merci de contacter un administrateur.'
]);
}
#[Route('/questions', name: 'customer_list_question', methods: ['GET'])]
public function listQuestion(UserQuestionRepository $repository, PaginatorInterface $paginator, Request $request): Response
{
$query = $repository->findAllQuery($this->getUser());
$questions = $paginator->paginate(
$query,
$request->query->getInt('page', 1),
self::ITEM_PER_PAGE
);
return $this->render('frontend/my_space/list_question.html.twig', [
'controller_name' => 'UserQuestionController',
'menu' => 'my-space',
'submenu' => 'my-question',
'questions' => $questions
]);
}
#[Route('/questions/{id}/show', name: 'customer_show_question', methods: ['GET'])]
public function showQuestion(UserQuestion $question, UserQuestionRepository $repository): Response
{
$question->setIsNew(false);
/** @var UserResponse $response */
foreach ($question->getResponses() as $response) {
$response->setIsNew(false);
}
$repository->save($question, true);
return $this->render('frontend/my_space/show_question.html.twig', [
'controller_name' => 'UserQuestionController',
'menu' => 'my-space',
'submenu' => 'my-question',
'question' => $question
]);
}
#[Route('/questions/{id}/send-message', name: 'customer_send_message_question', methods: ['POST'])]
public function sendQuestionMessage(
UserQuestion $question,
Request $request,
UserResponseRepository $repository,
MailerService $mailerService
): JsonResponse
{
$datas = json_decode($request->getContent(), true);
$response = new UserResponse();
$response
->setAuthor($this->getUser())
->setMessage($datas['message'])
->setQuestion($question)
->setIsNew(true)
;
$repository->save($response, true);
$mailerService->sendQuestionNotification($question, $this->getUser());
return new JsonResponse([
'status' => 'ok',
'message' => $response->getMessage(),
'time' => $response->getCreatedAt()->format('H:i')
]);
}
}