<?php
namespace App\Controller\Marketplace;
use App\Entity\Company;
use App\Entity\MarketplaceReservation;
use App\Entity\UserFavorite;
use App\Form\Marketplace\CompanyMarketplaceType;
use App\Manager\ConfigurationManager;
use App\Repository\MarketplaceReservationRepository;
use App\Service\ToolsService;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Knp\Snappy\Pdf;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class UserController extends AbstractController
{
/**
* @var ConfigurationManager
*/
private $configurationManager;
public function __construct(ConfigurationManager $configurationManager)
{
$this->configurationManager = $configurationManager;
}
/**
* @IsGranted("ROLE_USER")
* @Route("/mon-compte", name="app_user")
*/
public function index(Request $request, EntityManagerInterface $em): Response
{
$company = $em->getRepository(Company::class)->find($this->getUser()->getCompany()->getId()) ?? null;
try {
// $reservations = $em->getRepository(MarketplaceReservation::class)->findBy([
// 'user' => $this->getUser()->getId(),
// ],['createdAt' => "DESC"]);
$reservations = $em->getRepository(MarketplaceReservation::class)->getMarketplaceReservationsForClient($this->getUser()->getId());
} catch (\Throwable $th) {
throw $th;
}
try {
$demandes = $em->getRepository(MarketplaceReservation::class)->findBy([
'user' => $this->getUser()->getId(),
'paymentAmount' => null,
// 'status' => MarketplaceReservation::STATUS_PENDING,
], ['createdAt' => "DESC"]);
} catch (\Throwable $th) {
//throw $th;
}
try {
$demandes = array_merge($demandes, $em->getRepository(MarketplaceReservation::class)->findBy([
'user' => $this->getUser()->getId(),
'paymentAmount' => 0,
// 'status' => MarketplaceReservation::STATUS_PENDING,
], ['createdAt' => "DESC"]));
} catch (\Throwable $th) {
//throw $th;
}
try {
$favorites = $em->getRepository(UserFavorite::class)->findBy([ // Récupération des favoris
'user' => $this->getUser()->getId(),
], ['createdAt' => "DESC"]);
} catch (\Throwable $th) {
//throw $th;
}
$form = $this->createForm(CompanyMarketplaceType::class, $company);
$form->handleRequest($request);
if ($form->isSubmitted() ) {
$em->persist($company);
$em->flush();
$this->addFlash('success', 'Informations modifiées');
}
return $this->render('marketplace/user/index.html.twig', [
'reservations' => $reservations ?? [],
'demandes' => $demandes ?? [],
'form' => $form->createView(),
'nav' => $request->query->getInt('nav', 1),
'favorites' => $favorites ?? [],
]);
}
/**
* @IsGranted("ROLE_USER")
* @Route("/mon-compte/facture/{reservationId}", name="reservation_invoice")
*/
public function reservationInvoice(Request $request, MarketplaceReservationRepository $marketplaceResRepo, Pdf $knpSnappyPdf, ToolsService $tools, int $reservationId)
{
$reservation = $marketplaceResRepo->find($reservationId);
$ulteam = $this->configurationManager->getInvoiceSellerData();
$name = "Facture pour l'atelier du " . $reservation->getDateStart()->format('d-m-Y');
if (!empty($reservation)) {
$html = $this->renderView('marketplace/components/pdf_invoice.html.twig',
[
'name' => $name,
'reservation' => $reservation,
'ulteam' => $ulteam,
]);
$footerHtml = $this->renderView('marketplace/components/invoice_footer.html.twig');
$knpSnappyPdf->setOption('footer-html', $footerHtml);
$knpSnappyPdf->setOption('enable-local-file-access', true);
return new PdfResponse($knpSnappyPdf->getOutputFromHtml($html), "$name.pdf");
}
}
}