src/Controller/CotizacionesController.php line 44

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  9. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Symfony\Component\HttpFoundation\Cookie;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;
  16. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  17. // use Symfony\Component\Security\Core\Security;
  18. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  19. use Symfony\Contracts\Cache\ItemInterface;
  20. use App\Form\AlertaType;
  21. use App\Entity\Alerta;
  22. use App\Services\SettingsManager;
  23. class CotizacionesController extends AbstractController
  24. {
  25.     
  26. //     private $security;
  27. //     
  28. //     public function __construct(Security $security)
  29. //     {
  30. //         $this->security = $security;
  31. //     }
  32.     
  33.     #[Route(path: [
  34.         'es' => '/cotizaciones',
  35.         'en' => '/en/exchange-rates'
  36.     ], name'cotizaciones')]
  37.     public function cotizaciones(Request $requestPersistenceManagerRegistry $doctrine): Response
  38.     {
  39.         $local=$request->getLocale();
  40.         $em $doctrine->getManager();
  41.         $of $maxDia $ofi $cotizaciones null;
  42.         $divisas $em->getRepository('App\Entity\Divisa')->findBy(["visible" => 1]);
  43.         $oficinas $em->getRepository('App\Entity\Oficina')->findAll();
  44. //         $permiteCompra = 1;
  45.         $rep "";
  46.         if(isset($_COOKIE['cookieoficina']) && $_COOKIE['cookieoficina'] != 0){
  47.             $ofi = (integer)($_COOKIE['cookieoficina']);
  48.             
  49.             if($ofi == 1){
  50.                 $rep $em->getRepository('App\Entity\Oficina')->findOneById($this->getParameter('oficina_por_defecto'));
  51.                 $maxDia $em->getRepository('App\Entity\Cotizaciones')->getMaxDia($this->getParameter('oficina_por_defecto'));
  52.                 $fechaMax = new \DateTime($maxDia);
  53.                 $cotizaciones $em->getRepository('App\Entity\Cotizaciones')->findAllOrdered($fechaMax$this->getParameter('oficina_por_defecto'));
  54.             } else {
  55.                 $rep $em->getRepository('App\Entity\Oficina')->findOneById($_COOKIE['cookieoficina']);
  56.                 $maxDia $em->getRepository('App\Entity\Cotizaciones')->getMaxDia($ofi);
  57.                 $fechaMax = new \DateTime($maxDia);
  58.                 $cotizaciones $em->getRepository('App\Entity\Cotizaciones')->findAllOrdered($fechaMax$ofi);
  59.             }
  60.             
  61.         } else {
  62.             $ofi = (integer)0;
  63. //             $cotizaciones        = $em->getRepository('App\Entity\Cotizaciones')->getLastChanges($local);
  64.             $cotizaciones = (integer)0;
  65.         }
  66. //        dump($cotizaciones);die();
  67.         $contenido $em->getRepository('App\Entity\Pagina')->findOneByClave('Cotizaciones');
  68.         $returnArray = array(
  69. //             'pagina'     => $pagina,
  70.             'contenido'  => $contenido,
  71.             'oficinas' => $oficinas,
  72.             'cotizaciones' => $cotizaciones,
  73.             'diaMax' => $maxDia,
  74.             'ofi_var' => $ofi,
  75.             'of' => $rep,
  76.             //'provincias' => $provincias1,
  77.             //'permiteCompra' => $permiteCompra,
  78.             'divisas' => $divisas
  79.         );
  80.         return $this->render('default/cotizaciones.html.twig'$returnArray);
  81.     }
  82.     
  83.     #[Route(path: [
  84.         'es' => '/cotizaciones/{nombre}',
  85.         'en' => '/en/exchange-rates/{nombre}'
  86.     ], name'divisas')]
  87.     public function divisas(Request $requestPersistenceManagerRegistry $doctrine$nombre): Response
  88.     {
  89.         $local=$request->getLocale();
  90.         $em $doctrine->getManager();
  91.         $divisas $em->getRepository('App\Entity\Divisa')->findAll();
  92.         $divisaT $em->getRepository('App\Entity\DivisaTranslation')->findOneBy(['slug' => $nombre,'locale' => $local]);
  93.         
  94.         
  95.         
  96. //         $divisa = $em->getRepository('App\Entity\Divisa')->findOneBy(['shortName' => $nombre]);
  97.         
  98.         $conversionId $em->getRepository("App\Entity\DivisaConversionTranslation")->findOneBy(['slug' => $nombre'locale' => $local]);
  99.         if($conversionId != null){
  100.             $conversion $em->getRepository("App\Entity\DivisaConversion")->findOneBy(['id' => $conversionId->getTranslatable()]);
  101.             if ($conversion) {
  102.                 $visitas=$conversion->getVisitas();
  103.                 $conversion->setVisitas($visitas+1);
  104.                 $em->persist($conversion);
  105.                 $em->flush();
  106.             }
  107.         } else {
  108.             $conversion null;
  109.         }
  110.         
  111.         if (!$conversion and !$divisaT)
  112.             throw $this->createNotFoundException('404');
  113.             
  114.         if ($divisaT)
  115.             $divisa $divisaT->getTranslatable();
  116.         else
  117.             $divisa null;
  118.         
  119.         $oficinas $em->getRepository('App\Entity\Oficina')->findAll();
  120.         $cotizaciones $em->getRepository('App\Entity\Cotizaciones')->findAll();
  121.         
  122.         if(isset($_COOKIE['cookieoficina']) && $_COOKIE['cookieoficina'] != 0){
  123.             $ofi = (integer)($_COOKIE['cookieoficina']);
  124.             if($ofi == 1){
  125.                 $ofi $this->getParameter('oficina_por_defecto');
  126.             }
  127.             $oficina $em->getRepository('App\Entity\Oficina')->findOneBy(['id' => $ofi]);
  128.         }else{
  129.             $ofi = (integer)0;
  130.             $oficina 0;
  131.         }
  132.         if($divisa != null) {
  133.             $cotizacionesJson=json_encode($em->getRepository('App\Entity\Cotizaciones')->getUltimasCotizacionesOficina($divisa$ofi));
  134.         } elseif($conversion != null && $conversion->getDivisaOrigen()->getId() != 10) {
  135.             $cotizacionesJson=json_encode($em->getRepository('App\Entity\Cotizaciones')->getUltimasCotizacionesOficina($conversion->getDivisaOrigen(), $ofi));
  136.         } elseif($conversion != null && $conversion->getDivisaDestino()->getId() != 10) {
  137.             $cotizacionesJson=json_encode($em->getRepository('App\Entity\Cotizaciones')->getUltimasCotizacionesOficina($conversion->getDivisaDestino(), $ofi));
  138.         }
  139.         
  140.         $returnArray = array(
  141.             'oficinas' => $oficinas,
  142.             'oficina' => $oficina,
  143.             'cotizaciones' => $cotizaciones,
  144.             'cotizacionesJsonG' => $cotizacionesJson,
  145.             'divisas' => $divisas,
  146.             'divisa' => $divisa,
  147.             'ofi_var' => $ofi,
  148.             'conversion' => $conversion
  149.         );
  150.         return $this->render('default/divisas.html.twig'$returnArray);
  151.     }
  152.     
  153.     
  154.     
  155.     #[Route(path: [
  156.         'es' => '/createAlert',
  157.         'en' => '/en/createAlert'
  158.     ], name'create_alert')]
  159.     public function alerta(Request $requestPersistenceManagerRegistry $doctrineSettingsManager $smAuthorizationCheckerInterface $securityContext): Response
  160.     {
  161.         $local=$request->getLocale();
  162.         
  163.         
  164.         $ajax $request->isXmlHttpRequest();
  165.         
  166. //         $settings = $this->get('dinamic_settings.manager');
  167.         // Comprobamos que el usuario esté logueado
  168. //         $securityContext = $this->container->get('security.context');
  169.         if($ajax and !$securityContext->isGranted('IS_AUTHENTICATED_FULLY') || !$securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
  170.             return new JsonResponse(['state' => false'msg' => $sm->getValue('currency.alert.login')]);
  171.         }
  172.         
  173.         
  174.         // Creamos el formulario de alerta
  175.         $entity = new Alerta();
  176.         $form $this->createForm(AlertaType::class, $entity, array(
  177.             'action' => $this->generateUrl('create_alert'),
  178.             'method' => 'POST',
  179.             'settings' => $sm,
  180.         ));
  181.         if ($ajax) {
  182.             $form->handleRequest($request);
  183.             
  184.             if($form->isSubmitted() and $form->isValid()) {
  185.                 dump($form->get('tipo')->getData());
  186.                 
  187.                 $em $doctrine->getManager();
  188.                 $user=$this->getUser();
  189.                 $user->setIdiomaPreferido($local);
  190.                 // Buscamos que no tenga alertas creadas actualmente de esta divisa
  191.                 $currentlyCreated $em->getRepository('App\Entity\Alerta')->findOneBy(['usuario' => $this->getUser(), 'divisa' => $form->get('divisa')->getData()]);
  192.                 if(isset($_COOKIE['cookieoficina']) && $_COOKIE['cookieoficina'] != 0){
  193.                     $ofi = (integer)($_COOKIE['cookieoficina']);
  194.                     if($ofi == 1){
  195.                         $ofi $this->getParameter('oficina_por_defecto');
  196.                     }
  197.                 }else{
  198.                     $ofi = (integer)0;
  199.                 }
  200.                 $oficina $em->getRepository('App\Entity\Oficina')->findOneBy(['id' => $ofi]);
  201.                 if($currentlyCreated) {
  202.                     return new JsonResponse(['state' => false'msg' => $sm->getValue('currency.alert.hasalert')]);
  203.                 }
  204.                 $entity->setActivo(true)
  205.                     ->setOficina($oficina)
  206.                     ->setUsuario($this->getUser());
  207.                 $em->persist($entity);
  208.                 $em->flush();
  209.                 return new JsonResponse(['status' => true'msg' => $sm->getValue('currency.alert.newalert')]);
  210.             }
  211.         
  212.         }
  213.         
  214.          
  215.         $html $this->render('default/alerta.html.twig', [
  216.             'status' => false
  217.             'form' => $form
  218.         ]);
  219.         $returnArray = [
  220.             'html' => $html
  221.         ];
  222.         
  223. //         dump($html); die();
  224.         if ($ajax)
  225.             return new JsonResponse($returnArray200);
  226.             
  227.         return $html;
  228.     }
  229.     
  230.     
  231.     
  232.     
  233.     
  234.      #[Route(path: [
  235.         'es' => '/rates-table',
  236.         'en' => '/en/rates-table'
  237.     ], name'app_rates')]
  238.     public function ratesAction(Request $requestPersistenceManagerRegistry $doctrine): Response
  239.     {
  240.         $locale=$request->getLocale();
  241.         $em $doctrine->getManager();
  242.         
  243.         $fecha = new \DateTime();
  244.         $oficina $_GET['id'];
  245.         $cotizaciones $em->getRepository('App\Entity\Cotizaciones')->getLastChangesOffice($oficina$locale);
  246.         $objeto $em->getRepository('App\Entity\Oficina')->findOneBy(['id' => $oficina]);
  247.         if (!$objeto) {
  248.             throw $this->createNotFoundException('Oficina no encontrada');
  249.         }
  250.         $ofi_nom $objeto->getNombre();
  251.         return $this->render('admin/cotizacion/list_imprimir.html.twig', [
  252.             'entities' => $cotizaciones,
  253.             'oficina' => $ofi_nom
  254.         ]);
  255.     }    
  256.      #[Route(path: [
  257.         'es' => '/rates-screen',
  258.         'en' => '/en/rates-screen'
  259.     ], name'app_rates_screen')]
  260.     public function ratesScreenAction(Request $requestPersistenceManagerRegistry $doctrine): Response
  261.     {
  262.         $locale=$request->getLocale();
  263.         $em $doctrine->getManager();
  264.         
  265.         $fecha = new \DateTime();
  266.         $oficina $_GET['id'];
  267.         $cotizaciones $em->getRepository('App\Entity\Cotizaciones')->getLastChangesOffice($oficina$locale);
  268.         $objeto $em->getRepository('App\Entity\Oficina')->findOneBy(['id' => $oficina]);
  269.         if (!$objeto) {
  270.             throw $this->createNotFoundException('Oficina no encontrada');
  271.         }
  272.         $ofi_nom $objeto->getNombre();
  273.         return $this->render('admin/cotizacion/pantalla_list_imprimir.html.twig', [
  274.             'entities' => $cotizaciones,
  275.             'oficina' => $ofi_nom
  276.         ]);
  277.     }    
  278. }