src/Controller/ECommerce/App/InvoiceController.php line 111

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ECommerce\App;
  3. use App\Controller\ControllerTrait;
  4. use App\Entity\ECommerce\Cart;
  5. use App\Entity\ECommerce\Invoice;
  6. use App\Repository\ECommerce\CartRepository;
  7. use App\Service\ECommerce\InvoicePdfManager;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17. * @Route("/{_locale}/invoice", requirements={"_locale": "\w{2}"})
  18. */
  19. class InvoiceController extends AbstractController
  20. {
  21. const INDEX_ROUTE = 'mdl_public_invoice_index';
  22. use ControllerTrait;
  23. /**
  24. * @Route("/index", name="mdl_public_invoice_index")
  25. *
  26. * @Security("is_granted('ROLE_USER')")
  27. *
  28. *
  29. */
  30. public function indexAction(Request $request, PaginatorInterface $paginator): Response
  31. {
  32. return $this->render('ECommerce/App/Invoice/index.html.twig', [
  33. 'invoices' => $paginator->paginate(
  34. $this->getDoctrine()->getRepository(Invoice::class)->findQuery($this->getUser()),
  35. (int) $request->get('page', 1),
  36. (int) $request->get('itemsPerPage', 10)
  37. ),
  38. ]);
  39. }
  40. /**
  41. * @Route("/print_last", name="mdl_public_invoice_print_last")
  42. *
  43. * @Security("is_granted('ROLE_USER')")
  44. */
  45. public function printLastAction(): RedirectResponse
  46. {
  47. $invoiceId = $this->getDoctrine()->getRepository(Invoice::class)->findLastIdByUser($this->getUser());
  48. if (!$invoiceId) {
  49. return $this->redirectToIndex();
  50. }
  51. return $this->redirectToRoute('mdl_public_invoice_print', [
  52. 'id' => $invoiceId,
  53. ]);
  54. }
  55. /**
  56. * @Route("/{id}/print", name="mdl_public_invoice_print", requirements={"id"="\S+"})
  57. *
  58. * @ParamConverter("invoice", class="App\Entity\ECommerce\Invoice", options={"repository_method" = "findOneById"})
  59. *
  60. * @Security("is_granted('INVOICE_PRINT', invoice)")
  61. *
  62. *
  63. *
  64. * @throws \Twig\Error\LoaderError
  65. * @throws \Twig\Error\RuntimeError
  66. * @throws \Twig\Error\SyntaxError
  67. */
  68. public function printAction(Invoice $invoice, InvoicePdfManager $invoicePdfManager): Response
  69. {
  70. return new Response(
  71. $invoicePdfManager->getContent($invoice),
  72. 200,
  73. [
  74. 'Content-Type' => 'application/pdf',
  75. ]
  76. );
  77. }
  78. /**
  79. * @Route("/{id}", name="mdl_public_invoice_show", requirements={"id"="\S+"})
  80. *
  81. * @ParamConverter("invoice", class="App\Entity\ECommerce\Invoice", options={"repository_method" = "findOneById"})
  82. *
  83. * @Security("is_granted('INVOICE_VIEW', invoice)")
  84. *
  85. *
  86. */
  87. public function showAction(Invoice $invoice): Response
  88. {
  89. return $this->render('ECommerce/App/Invoice/show.html.twig', [
  90. 'invoice' => $invoice,
  91. ]);
  92. }
  93. /**
  94. * @Route("/unpaid", name="mdl_public_invoice_unpaid")
  95. *
  96. *
  97. *
  98. * @throws \Doctrine\ORM\NonUniqueResultException
  99. */
  100. public function unPaidAction(Request $request, CartRepository $repository): \Symfony\Component\HttpFoundation\Response
  101. {
  102. $user = $this->getUser();
  103. if (!$user) {
  104. return new Response();
  105. }
  106. $cart = $repository->findOneUnPayed($this->getUser());
  107. if ('/_fragment' === $request->getPathInfo()) {
  108. return $this->render('ECommerce/App/Invoice/unpaid.ajax.twig', [
  109. 'cart' => $cart
  110. ]);
  111. }
  112. return $this->render('ECommerce/App/Invoice/unpaid.html.twig', [
  113. 'cart' => $cart
  114. ]);
  115. }
  116. }