src/Controller/Quote/QuoteController.php line 138

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Quote;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  5. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  6. use App\DataProvider\Quote\QuoteCollectionDataProvider;
  7. use App\Handler\Quote\CrudQuoteHandler;
  8. use App\Model\CustomerFile\CustomerFile;
  9. use App\Model\Exception\ProspectNotFoundException;
  10. use App\Model\Exception\QuoteNotFoundException;
  11. use App\Model\Product\Product;
  12. use App\Model\Response\DataPersisterResponse;
  13. use App\Service\ApiWebServiceFilterBuilder\PaginationFilterBuilder;
  14. use App\V4\Model\Quote\Quote;
  15. use App\V4\Service\Pdf\PdfGenerator;
  16. use App\V4\Voters\QuoteVoter;
  17. use Doctrine\Common\Annotations\AnnotationException;
  18. use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface;
  19. use Psr\Cache\InvalidArgumentException;
  20. use ReflectionException;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\HttpFoundation\JsonResponse;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\RequestStack;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  29. use Symfony\Component\Routing\RouterInterface;
  30. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  31. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  32. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  33. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  34. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  35. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  36. use Twig\Error\LoaderError;
  37. use Twig\Error\SyntaxError;
  38. final class QuoteController extends AbstractController
  39. {
  40.     /**
  41.      * @var ItemDataProviderInterface
  42.      */
  43.     private $itemDataProvider;
  44.     /**
  45.      * @var PdfGenerator
  46.      */
  47.     private $pdfGenerator;
  48.     public function __construct(ItemDataProviderInterface $itemDataProviderPdfGenerator $pdfGenerator)
  49.     {
  50.         $this->itemDataProvider $itemDataProvider;
  51.         $this->pdfGenerator $pdfGenerator;
  52.     }
  53.     /**
  54.      * @Route("/api/quote", name="quote_edit_route", methods={"PUT"})
  55.      * @Security("is_granted('ROLE_ADMIN') or (is_granted('QUOTE_CUD'))", message="not_allowed_edit_quote")
  56.      *
  57.      * @throws AnnotationException
  58.      * @throws ClientExceptionInterface
  59.      * @throws DecodingExceptionInterface
  60.      * @throws ExceptionInterface
  61.      * @throws RedirectionExceptionInterface
  62.      * @throws ServerExceptionInterface
  63.      * @throws TransportExceptionInterface
  64.      * @throws ProspectNotFoundException
  65.      * @throws QuoteNotFoundException
  66.      * @throws InvalidArgumentException
  67.      * @throws ReflectionException
  68.      */
  69.     public function editQuote(Request $requestCrudQuoteHandler $crudQuoteHandler)
  70.     {
  71.         $jsonResponse = new DataPersisterResponse($crudQuoteHandler->handlePut(
  72.             $request->get('request') ? $request->get('request') : $request->getContent(),
  73.             $request->files->all()
  74.         ));
  75.         return $jsonResponse;
  76.     }
  77.     /**
  78.      * @Route("/api/quotes", name="quote_get_route", methods={"GET"})
  79.      * @Security("is_granted('ROLE_USER')",
  80.      *      message="not_allowed_get_quote")
  81.      *
  82.      * @param Request                     $request
  83.      * @param QuoteCollectionDataProvider $collectionDataProvider
  84.      *
  85.      * @return JsonResponse
  86.      *
  87.      * @throws ClientExceptionInterface
  88.      * @throws DecodingExceptionInterface
  89.      * @throws RedirectionExceptionInterface
  90.      * @throws ServerExceptionInterface
  91.      * @throws TransportExceptionInterface
  92.      */
  93.     public function listQuote(Request $requestQuoteCollectionDataProvider $collectionDataProvider): JsonResponse
  94.     {
  95.         return new JsonResponse(
  96.             $collectionDataProvider->getRawCollection(
  97.                 $collectionDataProvider->completeSearchBodyFromRequest($request, [])
  98.             ),
  99.             Response::HTTP_OK
  100.         );
  101.     }
  102.     /**
  103.      * @Route("/api/quote/{id}/generatepdf", name="quote_generate_pdf", methods={"POST"})
  104.      *
  105.      * @throws ClientExceptionInterface
  106.      * @throws LoaderError
  107.      * @throws RedirectionExceptionInterface
  108.      * @throws ResourceClassNotSupportedException
  109.      * @throws ServerExceptionInterface
  110.      * @throws SyntaxError
  111.      * @throws TransportExceptionInterface
  112.      */
  113.     public function generateQuotePdf(Request $requeststring $id): JsonResponse
  114.     {
  115.         $this->denyAccessUnlessGranted(QuoteVoter::QUOTE_LINES_EXPORT_PDF);
  116.         $quote $this->itemDataProvider->getItem(Quote::class, $id);
  117.         if (!$quote instanceof Quote) {
  118.             return new JsonResponse(['code' => Response::HTTP_NOT_FOUND'errorMessage' => 'not_found'], Response::HTTP_NOT_FOUND);
  119.         }
  120.         $quoteData json_decode($request->getContent(), true);
  121.         if (empty($quoteData['templateId']) || empty($quoteData['customerId']) || !$id) {
  122.             return new JsonResponse(['code' => Response::HTTP_BAD_REQUEST'errorMessage' => 'generate_quote_pdf_missing_template_or_id'], 400);
  123.         }
  124.         if (!isset($quoteData[PdfGenerator::OPTION_MODE])) {
  125.             $quoteData[PdfGenerator::OPTION_MODE] = PdfGenerator::MODE_REPLACE;
  126.         }
  127.         $base64File $this->pdfGenerator->generatePdfContentByTemplate($quote$quoteData['templateId'], $quoteData);
  128.         $response $this->pdfGenerator->generatePdfWithTemplateForQuote($quote$base64File$quoteData[PdfGenerator::OPTION_MODE], $quoteData['templateId']);
  129.         return new JsonResponse($response);
  130.     }
  131.     /**
  132.      * @Route("/api/quote/{id}/productsfiles", name="quote_products_files", methods={"GET"})
  133.      *
  134.      * @param CollectionDataProviderInterface $collectionDataProvider
  135.      * @param TokenExtractorInterface         $tokenExtractor
  136.      * @param RequestStack                    $requestStack
  137.      * @param RouterInterface                 $router
  138.      * @param Request                         $request
  139.      * @param string                          $id
  140.      *
  141.      * @return JsonResponse
  142.      *
  143.      * @throws ResourceClassNotSupportedException
  144.      */
  145.     public function getQuoteProductsFiles(
  146.         CollectionDataProviderInterface $collectionDataProvider,
  147.         TokenExtractorInterface $tokenExtractor,
  148.         RequestStack $requestStack,
  149.         RouterInterface $router,
  150.         Request $request,
  151.         string $id
  152.     ): JsonResponse {
  153.         $products $collectionDataProvider->getCollection(Product::class, null, [
  154.             'filters' => [
  155.                 PaginationFilterBuilder::ACTIVATE_PAGNIATION_KEY => false,
  156.                 'quoteIds' => [$id],
  157.             ],
  158.         ]);
  159.         $allFiles = [];
  160.         /** @var Product $product */
  161.         foreach ($products as $product) {
  162.             /** @var CustomerFile[] $files */
  163.             $files $collectionDataProvider->getCollection(CustomerFile::class, null, [
  164.                 'filters' => [
  165.                     'productId' => $product->getId(),
  166.                 ],
  167.             ]);
  168.             // Filter logo
  169.             foreach ($files as $key => $file) {
  170.                 if ($file->getId() === $product->getImageId()) {
  171.                     continue;
  172.                 }
  173.                 $token $tokenExtractor->extract($requestStack->getMasterRequest());
  174.                 $allFiles[] = [
  175.                     'name' => $file->getRealFileName(),
  176.                     'type' => $file->getMimeType(),
  177.                     'source' => $router->generate(
  178.                         'files_download_route',
  179.                         ['id' => $file->getId()],
  180.                         UrlGeneratorInterface::ABSOLUTE_URL
  181.                     ),
  182.                 ];
  183.             }
  184.         }
  185.         return new JsonResponse(['files' => $allFiles]);
  186.     }
  187. }