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

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