src/Controller/MainController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Comercial;
  4. use App\Entity\Factura;
  5. use App\Repository\BancoRepository;
  6. use App\Repository\ContratoRepository;
  7. use App\Repository\FacturaGenericaRepository;
  8. use App\Repository\FacturaRepository;
  9. use App\Repository\SuministroRepository;
  10. use App\Repository\TipoImpuestoRepository;
  11. use App\Repository\TipoIvaRepository;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Dompdf\Dompdf;
  14. use Monolog\Handler\StreamHandler;
  15. use Monolog\Logger;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  21. use Symfony\Component\Mailer\MailerInterface;
  22. use Symfony\Component\Mime\Email;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
  25. use Symfony\Contracts\Cache\ItemInterface;
  26. class MainController extends AbstractController
  27. {
  28.     /**
  29.      * @Route("/", name="homepage")
  30.      */
  31.     public function index(ContratoRepository $contratoRepositoryFacturaRepository $facturaRepositoryFacturaGenericaRepository $facturaGenericaRepository): Response
  32.     {
  33.         if( !$this->isGranted("ROLE_COMERCIAL") && !$this->isGranted("ROLE_FACTURACION") ){
  34.             try {
  35.                 session_destroy();
  36.             }catch (\Exception $e){}
  37.             $this->createAccessDeniedException();
  38.             $this->redirectToRoute("login");
  39.         }
  40.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  41.         $cliente=$this->getUser()->getCliente();
  42.         $facturasContrato=[];
  43.         if($cliente==null){
  44.             $contratos=[];
  45.         } else{
  46.             $contratos=$contratoRepository->findByCliente($cliente);
  47.         }
  48.         foreach ($contratos as $contrato){
  49.             $facturas=$facturaRepository->findBy(['contrato'=>$contrato]);
  50.             $genericas=$facturaGenericaRepository->findBy(['contrato'=>$contrato]);
  51.             $facturasFinal=array_merge($facturas,$genericas);
  52.             usort$facturasFinal, function ($a,$b){
  53.                 $aClass=$a->getClassName();
  54.                 $bClass=$b->getClassName();
  55.                 if($aClass==$bClass){
  56.                     switch ($aClass){
  57.                         case 'Factura':
  58.                             return date_diff($a->getFechaLectura(),$b->getFechaLectura())->days;
  59.                             break;
  60.                         case 'FacturaGenerica':
  61.                             return date_diff($a->getFechaEmision(),$b->getFechaEmision())->days;
  62.                             break;
  63.                     }
  64.                 }else{
  65.                     switch ($aClass){
  66.                         case 'Factura':
  67.                             return date_diff($a->getFechaLectura(),$b->getFechaEmision())->days;
  68.                             break;
  69.                         case 'FacturaGenerica':
  70.                             return date_diff($a->getFechaEmision(),$b->getFechaLectura())->days;
  71.                             break;
  72.                     }
  73.                 }
  74.                 return date_diff($a->getFechaEmision(),$b->getFechaEmision());
  75.             } );
  76.             $facturasContrato[$contrato->getId()]=array_reverse($facturasFinal);
  77.         }
  78.         if( $this->isGranted('ROLE_ADMIN') ){
  79.             $totalContratos=count$contratoRepository->findBy([ 'oculto'=>false ]) );
  80.             $totalActivos=count$contratoRepository->findBy(['estado'=>1,'oculto'=>false]) );
  81.             $totalBaja=count$contratoRepository->findBy(['estado'=>2,'oculto'=>false]) );
  82.             $totalPendienteActivacion=count$contratoRepository->findBy(['estado'=>3,'oculto'=>false]) );
  83.             $totalPendienteFirma=count$contratoRepository->findBy(['estado'=>6,'oculto'=>false]) );
  84.             $totalBorrador=count$contratoRepository->findBy(['estado'=>8,'oculto'=>false]) );
  85.         }
  86.         return $this->render('main/index.html.twig',[
  87.             'contratos'=>$contratos,
  88.             'facturasContrato'=>$facturasContrato,
  89.             'totalContratos'=>$totalContratos??null,
  90.             'totalActivos'=>$totalActivos??null,
  91.             'totalBaja'=>$totalBaja??null,
  92.             'totalPendienteActivacion'=>$totalPendienteActivacion??null,
  93.             'totalPendienteFirma'=>$totalPendienteFirma??null,
  94.             'totalBorrador'=>$totalBorrador??null
  95.         ]);
  96.     }
  97.     /*
  98.      * @Route("/mailing", name="mailing")
  99.      */
  100. /*    public function mailing( MailerInterface $mailer)
  101.     {
  102.         $this->denyAccessUnlessGranted('ROLE_SUPERADMIN');
  103.         $logger = new Logger('factura_logger');
  104.         $logger->pushHandler(new StreamHandler(__DIR__.'/../../logs/email.log', Logger::DEBUG));
  105.         try {
  106.             if ( ($_SERVER['APP_ENV']??'finkaluz')=='finkaluz' ){
  107.                 $body=$this->renderView('email/finkaluz.mailing.html.twig');
  108.                 $from='finkaluz@finkaluz.com';
  109.                 $subject='PROMOCIÓN FINKALUZ';
  110.                 $tos=explode( ',',file_get_contents(__DIR__ . '/../../temp/asdf') );
  111.                 foreach ($tos as $to){
  112.                     $message = (new Email())
  113.                         ->subject($subject)
  114.                         ->from($from)
  115.                         ->to($to)
  116.                         ->html($body)
  117.                         ->attachFromPath(__DIR__ . '/../../temp/Dossier.pdf');
  118.                         //->attachFromPath(__DIR__ . '/../../temp/' . ($factura->getNumeroFactura() ?? 'factura_sin_número') . '.pdf');
  119.                     $mailer->send($message);
  120.                 }
  121.             }
  122.             $status='success';
  123.         }catch (\Exception $exception){
  124.             $status='error';
  125.             $logger->info($exception->getMessage());
  126.         } catch (TransportExceptionInterface $e) {
  127.             $status='error';
  128.             $logger->info($e->getMessage());
  129.         }
  130.         return $this->render('test/echo.html.twig',[
  131.             'echo'=>$status,
  132.         ]);
  133.     }*/
  134.     /*
  135.      * @Route("/env", name="env")
  136.      */
  137.     /*public function env(): Response
  138.     {
  139.         $this->denyAccessUnlessGranted('ROLE_SUPERADMIN');
  140.         return $this->render('main/test.html.twig',[
  141.             'echo'=>var_export($_ENV,1)
  142.         ]);
  143.     }*/
  144.     /*
  145.      * @Route("/test", name="test")
  146.     */
  147.     /*public function test(ContratoRepository $contratoRepository)
  148.     {
  149.         $this->denyAccessUnlessGranted('ROLE_SUPERADMIN');
  150.         $cache=new FilesystemAdapter();
  151.         $numeroContratos = $cache->getItem('numero_contratos');
  152.         $numeroContratos->set( count($contratoRepository->findAll()) );
  153.         $cache->save($numeroContratos);
  154.         $numeroContratos=$cache->getItem('numero_contratos')->get();
  155.         return $this->render('main/test.html.twig',[
  156.             'echo'=>''
  157.         ]);
  158.     }*/
  159.     /*
  160.      * @Route("/test/{id}", name="test")
  161.     public function test(Factura $factura, Request $request, FacturaRepository $facturaRepository, BancoRepository $bancoRepository)
  162.     {
  163.     }
  164.     /*
  165.      * @Route("/update_producto_proveedor", name="update_producto_proveedor")
  166.     public function updateProductoProveedor(ContratoRepository $contratoRepository, ProductoProveedorRepository $productoProveedorRepository)
  167.     {
  168.         $contratos=$contratoRepository->findAll();
  169.         //$contratos=$contratoRepository->findBy(['id'=>647]);
  170.         $status='success';
  171.         try{
  172.             foreach ($contratos as $contrato){
  173.                 $productoProveedorOld=$contrato->getProductoProveedor();
  174.                 if($productoProveedorOld->getNombre()!='PERSONALIZADO'){
  175.                     $producto=$contrato->getProducto();
  176.                     $productoProveedorNew=new ProductoProveedor();
  177.                     $proveedor=$productoProveedorOld->getProveedor();
  178.                     $productoProveedorNew->setNombre('PERSONALIZADO');
  179.                     $productoProveedorNew->setProveedor($proveedor);
  180.                     $productoProveedorNew->setTarifa($producto->getTarifa());
  181.                     $productoProveedorNew->setFacturaElectronica(true);
  182.                     $productoProveedorNew->setPersonalizado(true);
  183.                     $productoProveedorNew->setDuracion( date_interval_create_from_date_string('1 year') );
  184.                     $contrato->setProveedor($proveedor);
  185.                     $this->getDoctrine()->getManager()->persist($productoProveedorNew);
  186.                     $contrato->setProductoProveedor($productoProveedorNew);
  187.                 }
  188.             }
  189.         }catch (\Exception $exception){
  190.             $status=$exception->getMessage();
  191.         }
  192.         $this->getDoctrine()->getManager()->flush();
  193.         return $this->render('main/test.html.twig',[
  194.             'echo'=>$status
  195.         ]);
  196.     }
  197. */
  198.     /*
  199.      * @Route("/test/{id}", name="test")
  200.     public function test(Factura $factura, Request $request, FacturaRepository $facturaRepository, BancoRepository $bancoRepository)
  201.     {
  202.         return $this->render('main/test.html.twig',[
  203.             'echo'=>$status
  204.         ]);
  205.     }
  206. */
  207.     /*
  208.      * @Route("/buttons", name="buttons")
  209.      *
  210.     public function buttons()
  211.     {
  212.         return $this->render('main/buttons.html.twig');
  213.     }
  214.     /*
  215.      * @Route("/cards", name="cards")
  216.      *
  217.     public function cards()
  218.     {
  219.         return $this->render('main/cards.html.twig');
  220.     }
  221.     /**
  222.      * @Route("/color", name="color")
  223.      *
  224.     public function color()
  225.     {
  226.         return $this->render('main/color.html.twig');
  227.     }
  228.     /**
  229.      * @Route("/border", name="border")
  230.      *
  231.     public function border()
  232.     {
  233.         return $this->render('main/border.html.twig');
  234.     }
  235.     /**
  236.      * @Route("/animation", name="animation")
  237.      *
  238.     public function animation()
  239.     {
  240.         return $this->render('main/animation.html.twig');
  241.     }
  242.     /**
  243.      * @Route("/other", name="other")
  244.      *
  245.     public function other()
  246.     {
  247.         return $this->render('main/other.html.twig');
  248.     }
  249.      */
  250. }