<?php
namespace App\Controller\ECommerce\App;
use App\Controller\ControllerTrait;
use App\Entity\ECommerce\Cart;
use App\Entity\ECommerce\Invoice;
use App\Service\ECommerce\InvoicePdfManager;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/{_locale}/invoice", requirements={"_locale": "\w{2}"})
*/
class InvoiceController extends AbstractController
{
const INDEX_ROUTE = 'mdl_public_invoice_index';
use ControllerTrait;
/**
* @Route("/index", name="mdl_public_invoice_index")
*
* @Security("is_granted('ROLE_USER')")
*
* @param Request $request
* @param PaginatorInterface $paginator
*
* @return Response
*/
public function indexAction(Request $request, PaginatorInterface $paginator): Response
{
return $this->render('ECommerce/App/Invoice/index.html.twig', [
'invoices' => $paginator->paginate(
$this->getDoctrine()->getRepository(Invoice::class)->findQuery($this->getUser()),
(int) $request->get('page', 1),
(int) $request->get('itemsPerPage', 10)
),
]);
}
/**
* @Route("/print_last", name="mdl_public_invoice_print_last")
*
* @Security("is_granted('ROLE_USER')")
*
* @return RedirectResponse
*/
public function printLastAction(): RedirectResponse
{
$invoiceId = $this->getDoctrine()->getRepository(Invoice::class)->findLastIdByUser($this->getUser());
if (!$invoiceId) {
return $this->redirectToIndex();
}
return $this->redirectToRoute('mdl_public_invoice_print', array(
'id' => $invoiceId,
));
}
/**
* @Route("/{id}/print", name="mdl_public_invoice_print", requirements={"id"="\S+"})
*
* @ParamConverter("invoice", class="App\Entity\ECommerce\Invoice", options={"repository_method" = "findOneById"})
*
* @Security("is_granted('INVOICE_PRINT', invoice)")
*
* @param Invoice $invoice
* @param InvoicePdfManager $invoicePdfManager
*
* @return Response
*
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function printAction(Invoice $invoice, InvoicePdfManager $invoicePdfManager): Response
{
return new Response(
$invoicePdfManager->getContent($invoice),
200,
array(
'Content-Type' => 'application/pdf',
)
);
}
/**
* @Route("/{id}", name="mdl_public_invoice_show", requirements={"id"="\S+"})
*
* @ParamConverter("invoice", class="App\Entity\ECommerce\Invoice", options={"repository_method" = "findOneById"})
*
* @Security("is_granted('INVOICE_VIEW', invoice)")
*
* @param Invoice $invoice
*
* @return Response
*/
public function showAction(Invoice $invoice): Response
{
return $this->render('ECommerce/App/Invoice/show.html.twig', [
'invoice' => $invoice,
]);
}
/**
* @Route("/unpaid", name="mdl_public_invoice_unpaid")
*
* @param Request $request
*
* @return Response
*
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function unPaidAction(Request $request)
{
$user = $this->getUser();
if (!$user) {
return new Response();
}
$cart = $this->getDoctrine()->getRepository(Cart::class)->findOneUnPayed($this->getUser());
if ('/_fragment' === $request->getPathInfo()) {
return $this->render('ECommerce/App/Invoice/unpaid.ajax.twig', array(
'cart' => $cart
));
}
return $this->render('ECommerce/App/Invoice/unpaid.html.twig', [
'cart' => $cart
]);
}
}