src/Controller/PrecatorioController.php line 43
<?php
namespace App\Controller;
use App\Entity\Precatorio;
use App\Form\PrecatorioType;
use App\Repository\PrecatorioRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
#[Route('/precatorio')]
class PrecatorioController extends AbstractController
{
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/', name: 'app_precatorio_index', methods: ['GET'])]
public function index(PrecatorioRepository $precatorioRepository, Request $request): Response
{
$filter['tipo'] = $request->get('tipo') ?: '';
$filter['descricao'] = $request->get('descricao') ?: '';
$filter['pagina'] = $request->get('pagina') ?: 1;
$filter['anterior'] = $this->verifica('anterior', (1 - $filter['pagina']));
$filter['primeira'] = 1;
$filter['total_registros'] = $precatorioRepository->filterGridTotal($filter);
$filter['page'] = $request->get('page') ?: 10;
$filter['ultima'] = $this->verifica('ultima', $filter['total_registros'],$filter['page']);
$filter['proxima'] = $this->verifica('proxima', (1 + $filter['pagina']), $filter['ultima']);
$filter['dataInicial'] = '';
$filter['dataFinal'] = '';
return $this->render('precatorio/index.html.twig', [
'precatorios' => $precatorioRepository->filterGrid($filter),
'filtro' => $filter
]);
}
#[Route('/precatorios/ano', name: 'app_precatorio_publico_ano', methods: ['GET'])]
public function precatorio_ano(PrecatorioRepository $precatorioRepository, Request $request): Response {
$lista = $precatorioRepository->getAnos();
return $this->render('precatorio/ano.html.twig', [
'itens' => $lista,
]);
}
#[Route('/precatorios', name: 'app_precatorio_publico', methods: ['GET'])]
public function precatorios(PrecatorioRepository $precatorioRepository, Request $request): Response {
$filter['ano'] = $request->get('ano')?: date('Y');
$filter['tipo'] = $request->get('tipo') ?: '';
$filter['descricao'] = $request->get('descricao') ?: '';
$filter['pagina'] = $request->get('pagina') ?: 1;
$filter['anterior'] = $this->verifica('anterior', (1 - $filter['pagina']));
$filter['primeira'] = 1;
$filter['total_registros'] = $precatorioRepository->filterGridTotal($filter);
$filter['page'] = $request->get('page') ?: 10;
$filter['ultima'] = $this->verifica('ultima', $filter['total_registros'],$filter['page']);
$filter['proxima'] = $this->verifica('proxima', (1 + $filter['pagina']), $filter['ultima']);
$filter['dataInicial'] = '';
$filter['dataFinal'] = '';
if ($request->get('impressao')) {
$filter['page'] = 'all';
if ($request->get('pdf')) {
$html = $this->renderView('precatorio/impressao_pdf.html.twig', [
'filtro' => $filter,
'lista' => $precatorioRepository->filterGrid($filter),
]);
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'landscape');
$dompdf->set_option("isPhpEnabled", true);
$options = $dompdf->getOptions();
$options->setDefaultFont('Courier');
$dompdf->setOptions($options);
$dompdf->loadHtml($html);
$dompdf->render();
return new Response(
$dompdf->stream('resume', ["Attachment" => false]),
Response::HTTP_OK,
['Content-Type' => 'application/pdf']
);
}elseif($request->get('excel')){
$lista =$precatorioRepository->filterGrid($filter);
$titulos = array("Ano","MES","AGENCIA","CONTA","ENTRADA","SAIDA","SALDO INICIAL", "SALDO FINAL","FINALIDADE");
$rows[] = implode(';', $titulos);
foreach ($lista as $value) {
$data = array($value->getAno(), $value->getMes() , $value->getAgencia(), $value->getConta(),$value->getEntrada(),$value->getSaida(),$value->getSaldoInicial() , $value->getSaldoFinal(),$value->getDescricao());
$rows[] = implode(';', $data);
}
$content = implode("\n", $rows);
$response = new Response($content);
$response->headers->set('Content-Type', 'text/csv');
return $response;
}else{
return $this->render('precatorio/impressao.html.twig', [
'filtro' => $filter,
'lista' => $precatorioRepository->filterGrid($filter),
]);
}
}
return $this->render('precatorio/precatorios.html.twig', [
'filtro' => $filter,
'lista' => $precatorioRepository->filterGrid($filter),
]);
}
private function verifica($str, $pagina, $page = null) {
if ($str == "anterior") {
if ($pagina < 1) {
return 1;
}
}
if ($str == "proxima") {
if ($pagina > $page) {
return $page;
}
return $pagina;
}
if ($str == "ultima") {
if($page == "all"){
$page = $pagina;
}
$valor = intdiv($pagina, $page);
if (($pagina % $page) > 0) {
return ++$valor;
} else {
return $valor;
}
}
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/new', name: 'app_precatorio_new', methods: ['GET', 'POST'])]
public function new(Request $request, PrecatorioRepository $precatorioRepository): Response
{
$precatorio = new Precatorio();
$form = $this->createForm(PrecatorioType::class, $precatorio);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$precatorioRepository->save($precatorio, true);
return $this->redirectToRoute('app_precatorio_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('precatorio/new.html.twig', [
'precatorio' => $precatorio,
'form' => $form,
]);
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/{id}', name: 'app_precatorio_show', methods: ['GET'])]
public function show(Precatorio $precatorio): Response
{
return $this->render('precatorio/show.html.twig', [
'precatorio' => $precatorio,
]);
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/{id}/edit', name: 'app_precatorio_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Precatorio $precatorio, PrecatorioRepository $precatorioRepository): Response
{
$form = $this->createForm(PrecatorioType::class, $precatorio);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$precatorioRepository->save($precatorio, true);
return $this->redirectToRoute('app_precatorio_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('precatorio/edit.html.twig', [
'precatorio' => $precatorio,
'form' => $form,
]);
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/delete/{id}', name: 'app_precatorio_delete', methods: ['GET'])]
public function delete(Precatorio $entity, PrecatorioRepository $entityRepository ): Response {
if ($entity) {
$entityRepository->remove($entity, true);
$this->addFlash('success', "REGISTRO EXCLUIDO COM SUCESSO.");
}
return $this->redirectToRoute('app_precatorio_index', [], Response::HTTP_SEE_OTHER);
}
}