src/Controller/PrecatorioController.php line 43

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Precatorio;
  4. use App\Form\PrecatorioType;
  5. use App\Repository\PrecatorioRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  11. #[Route('/precatorio')]
  12. class PrecatorioController extends AbstractController
  13. {
  14.     /**
  15.      * @IsGranted("ROLE_ADMIN")
  16.      */    
  17.     #[Route('/'name'app_precatorio_index'methods: ['GET'])]
  18.     public function index(PrecatorioRepository $precatorioRepositoryRequest $request): Response
  19.     {
  20.         $filter['tipo'] = $request->get('tipo') ?: '';
  21.         $filter['descricao'] = $request->get('descricao') ?: '';
  22.         $filter['pagina'] = $request->get('pagina') ?: 1;
  23.         $filter['anterior'] = $this->verifica('anterior', ($filter['pagina']));
  24.         $filter['primeira'] = 1;
  25.         $filter['total_registros'] = $precatorioRepository->filterGridTotal($filter);
  26.         $filter['page'] = $request->get('page') ?: 10;
  27.         $filter['ultima'] = $this->verifica('ultima'$filter['total_registros'],$filter['page']);        
  28.         $filter['proxima'] = $this->verifica('proxima', ($filter['pagina']), $filter['ultima']);        
  29.         $filter['dataInicial'] = '';
  30.         $filter['dataFinal'] = '';           
  31.         return $this->render('precatorio/index.html.twig', [
  32.             'precatorios' => $precatorioRepository->filterGrid($filter),
  33.             'filtro' => $filter
  34.         ]);
  35.     }
  36.     
  37.     
  38.     #[Route('/precatorios/ano'name'app_precatorio_publico_ano'methods: ['GET'])]
  39.     
  40.     public function precatorio_ano(PrecatorioRepository $precatorioRepositoryRequest $request): Response {
  41.         $lista $precatorioRepository->getAnos();
  42.         return $this->render('precatorio/ano.html.twig', [
  43.                             'itens' => $lista,
  44.         ]);        
  45.     }
  46.     
  47.     #[Route('/precatorios'name'app_precatorio_publico'methods: ['GET'])]
  48.     public function precatorios(PrecatorioRepository $precatorioRepositoryRequest $request): Response {        
  49.         $filter['ano'] = $request->get('ano')?: date('Y');        
  50.         $filter['tipo'] = $request->get('tipo') ?: '';
  51.         $filter['descricao'] = $request->get('descricao') ?: '';
  52.         $filter['pagina'] = $request->get('pagina') ?: 1;
  53.         $filter['anterior'] = $this->verifica('anterior', ($filter['pagina']));
  54.         $filter['primeira'] = 1;
  55.         $filter['total_registros'] = $precatorioRepository->filterGridTotal($filter);
  56.         $filter['page'] = $request->get('page') ?: 10;
  57.         $filter['ultima'] = $this->verifica('ultima'$filter['total_registros'],$filter['page']);        
  58.         $filter['proxima'] = $this->verifica('proxima', ($filter['pagina']), $filter['ultima']);        
  59.         $filter['dataInicial'] = '';
  60.         $filter['dataFinal'] = '';  
  61.         
  62.         if ($request->get('impressao')) {
  63.             $filter['page'] = 'all';
  64.             if ($request->get('pdf')) {
  65.                 $html $this->renderView('precatorio/impressao_pdf.html.twig', [
  66.                     'filtro' => $filter,
  67.                     'lista' => $precatorioRepository->filterGrid($filter),
  68.                 ]);
  69.                 $dompdf = new Dompdf();
  70.                 $dompdf->setPaper('A4''landscape');
  71.                 $dompdf->set_option("isPhpEnabled"true);
  72.                 $options $dompdf->getOptions();
  73.                 $options->setDefaultFont('Courier');
  74.                 $dompdf->setOptions($options);
  75.                 $dompdf->loadHtml($html);
  76.                 $dompdf->render();
  77.                 return new Response(
  78.                         $dompdf->stream('resume', ["Attachment" => false]),
  79.                         Response::HTTP_OK,
  80.                         ['Content-Type' => 'application/pdf']
  81.                         );
  82.             }elseif($request->get('excel')){ 
  83.             $lista =$precatorioRepository->filterGrid($filter); 
  84.             $titulos = array("Ano","MES","AGENCIA","CONTA","ENTRADA","SAIDA","SALDO INICIAL""SALDO FINAL","FINALIDADE");
  85.                 $rows[] = implode(';'$titulos);
  86.                 
  87.                 foreach ($lista as $value) {
  88.                     $data = array($value->getAno(), $value->getMes() , $value->getAgencia(), $value->getConta(),$value->getEntrada(),$value->getSaida(),$value->getSaldoInicial() , $value->getSaldoFinal(),$value->getDescricao());
  89.                     $rows[] = implode(';'$data);
  90.                 }    
  91.                 
  92.                 $content implode("\n"$rows);
  93.                 $response = new Response($content);
  94.                 $response->headers->set('Content-Type''text/csv');
  95.                 return $response;
  96.             }else{
  97.                 return $this->render('precatorio/impressao.html.twig', [
  98.                             'filtro' => $filter,
  99.                             'lista' => $precatorioRepository->filterGrid($filter),
  100.                 ]);
  101.             }
  102.         }
  103.         
  104.         return $this->render('precatorio/precatorios.html.twig', [
  105.                     'filtro' => $filter,
  106.                     'lista' => $precatorioRepository->filterGrid($filter),
  107.         ]);
  108.     }    
  109.     private function verifica($str$pagina$page null) {
  110.         if ($str == "anterior") {
  111.             if ($pagina 1) {
  112.                 return 1;
  113.             }
  114.         }
  115.         if ($str == "proxima") {
  116.             if ($pagina $page) {
  117.                 return $page;
  118.             }
  119.             return $pagina;
  120.         }
  121.         if ($str == "ultima") {
  122.             if($page == "all"){
  123.                 $page $pagina;
  124.             }
  125.             $valor intdiv($pagina$page);
  126.             if (($pagina $page) > 0) {
  127.                 return ++$valor;
  128.             } else {
  129.                 return $valor;
  130.             }
  131.         }
  132.     }
  133.     
  134.     /**
  135.      * @IsGranted("ROLE_ADMIN")
  136.      */       
  137.     
  138.     #[Route('/new'name'app_precatorio_new'methods: ['GET''POST'])]
  139.     public function new(Request $requestPrecatorioRepository $precatorioRepository): Response
  140.     {
  141.         $precatorio = new Precatorio();
  142.         $form $this->createForm(PrecatorioType::class, $precatorio);
  143.         $form->handleRequest($request);
  144.         if ($form->isSubmitted() && $form->isValid()) {
  145.             $precatorioRepository->save($precatoriotrue);
  146.             return $this->redirectToRoute('app_precatorio_index', [], Response::HTTP_SEE_OTHER);
  147.         }
  148.         return $this->renderForm('precatorio/new.html.twig', [
  149.             'precatorio' => $precatorio,
  150.             'form' => $form,
  151.         ]);
  152.     }
  153.     
  154.     /**
  155.      * @IsGranted("ROLE_ADMIN")
  156.      */       
  157.     #[Route('/{id}'name'app_precatorio_show'methods: ['GET'])]
  158.     public function show(Precatorio $precatorio): Response
  159.     {
  160.         return $this->render('precatorio/show.html.twig', [
  161.             'precatorio' => $precatorio,
  162.         ]);
  163.     }
  164.     
  165.     /**
  166.      * @IsGranted("ROLE_ADMIN")
  167.      */       
  168.     #[Route('/{id}/edit'name'app_precatorio_edit'methods: ['GET''POST'])]
  169.     public function edit(Request $requestPrecatorio $precatorioPrecatorioRepository $precatorioRepository): Response
  170.     {
  171.         $form $this->createForm(PrecatorioType::class, $precatorio);
  172.         $form->handleRequest($request);
  173.         if ($form->isSubmitted() && $form->isValid()) {
  174.             $precatorioRepository->save($precatoriotrue);
  175.             return $this->redirectToRoute('app_precatorio_index', [], Response::HTTP_SEE_OTHER);
  176.         }
  177.         return $this->renderForm('precatorio/edit.html.twig', [
  178.             'precatorio' => $precatorio,
  179.             'form' => $form,
  180.         ]);
  181.     }
  182.     /**
  183.      * @IsGranted("ROLE_ADMIN")
  184.      */       
  185.     
  186.     #[Route('/delete/{id}'name'app_precatorio_delete'methods: ['GET'])]
  187.     public function delete(Precatorio $entity,  PrecatorioRepository $entityRepository ): Response {
  188.         if ($entity) {
  189.             $entityRepository->remove($entitytrue);
  190.             $this->addFlash('success'"REGISTRO EXCLUIDO COM SUCESSO.");
  191.         }
  192.         return $this->redirectToRoute('app_precatorio_index', [], Response::HTTP_SEE_OTHER);
  193.     }      
  194.       
  195. }