src/Controller/CotizacionesController.php line 159
- <?php
- namespace App\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Routing\Annotation\Route;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\HttpFoundation\ResponseHeaderBag;
- use Symfony\Component\HttpFoundation\BinaryFileResponse;
- use Symfony\Contracts\Translation\TranslatorInterface;
- use Doctrine\ORM\EntityManagerInterface;
- use Knp\Component\Pager\PaginatorInterface;
- use Symfony\Component\HttpFoundation\Cookie;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;
- use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
- // use Symfony\Component\Security\Core\Security;
- use Symfony\Component\Cache\Adapter\FilesystemAdapter;
- use Symfony\Contracts\Cache\ItemInterface;
- use App\Form\AlertaType;
- use App\Entity\Alerta;
- use App\Services\SettingsManager;
- class CotizacionesController extends AbstractController
- {
- // private $security;
- //
- // public function __construct(Security $security)
- // {
- // $this->security = $security;
- // }
- #[Route(path: [
- 'es' => '/cotizaciones',
- 'en' => '/en/exchange-rates'
- ], name: 'cotizaciones')]
- public function cotizaciones(Request $request, PersistenceManagerRegistry $doctrine): Response
- {
- $local=$request->getLocale();
- $em = $doctrine->getManager();
- $of = $maxDia = $ofi = $cotizaciones = null;
- $divisas = $em->getRepository('App\Entity\Divisa')->findBy(["visible" => 1]);
- $oficinas = $em->getRepository('App\Entity\Oficina')->findAll();
- // $permiteCompra = 1;
- $rep = "";
- if(isset($_COOKIE['cookieoficina']) && $_COOKIE['cookieoficina'] != 0){
- $ofi = (integer)($_COOKIE['cookieoficina']);
- if($ofi == 1){
- $rep = $em->getRepository('App\Entity\Oficina')->findOneById($this->getParameter('oficina_por_defecto'));
- $maxDia = $em->getRepository('App\Entity\Cotizaciones')->getMaxDia($this->getParameter('oficina_por_defecto'));
- $fechaMax = new \DateTime($maxDia);
- $cotizaciones = $em->getRepository('App\Entity\Cotizaciones')->findAllOrdered($fechaMax, $this->getParameter('oficina_por_defecto'));
- } else {
- $rep = $em->getRepository('App\Entity\Oficina')->findOneById($_COOKIE['cookieoficina']);
- $maxDia = $em->getRepository('App\Entity\Cotizaciones')->getMaxDia($ofi);
- $fechaMax = new \DateTime($maxDia);
- $cotizaciones = $em->getRepository('App\Entity\Cotizaciones')->findAllOrdered($fechaMax, $ofi);
- }
- } else {
- $ofi = (integer)0;
- // $cotizaciones = $em->getRepository('App\Entity\Cotizaciones')->getLastChanges($local);
- $cotizaciones = (integer)0;
- }
- // dump($cotizaciones);die();
- $contenido = $em->getRepository('App\Entity\Pagina')->findOneByClave('Cotizaciones');
- $returnArray = array(
- // 'pagina' => $pagina,
- 'contenido' => $contenido,
- 'oficinas' => $oficinas,
- 'cotizaciones' => $cotizaciones,
- 'diaMax' => $maxDia,
- 'ofi_var' => $ofi,
- 'of' => $rep,
- //'provincias' => $provincias1,
- //'permiteCompra' => $permiteCompra,
- 'divisas' => $divisas
- );
- return $this->render('default/cotizaciones.html.twig', $returnArray);
- }
- #[Route(path: [
- 'es' => '/cotizaciones/{nombre}',
- 'en' => '/en/exchange-rates/{nombre}'
- ], name: 'divisas')]
- public function divisas(Request $request, PersistenceManagerRegistry $doctrine, $nombre): Response
- {
- $local=$request->getLocale();
- $em = $doctrine->getManager();
- $divisas = $em->getRepository('App\Entity\Divisa')->findAll();
- $divisaT = $em->getRepository('App\Entity\DivisaTranslation')->findOneBy(['slug' => $nombre,'locale' => $local]);
- // $divisa = $em->getRepository('App\Entity\Divisa')->findOneBy(['shortName' => $nombre]);
- $conversionId = $em->getRepository("App\Entity\DivisaConversionTranslation")->findOneBy(['slug' => $nombre, 'locale' => $local]);
- if($conversionId != null){
- $conversion = $em->getRepository("App\Entity\DivisaConversion")->findOneBy(['id' => $conversionId->getTranslatable()]);
- } else {
- $conversion = null;
- }
- if (!$conversion and !$divisaT)
- throw $this->createNotFoundException('404');
- if ($divisaT)
- $divisa = $divisaT->getTranslatable();
- else
- $divisa = null;
- $oficinas = $em->getRepository('App\Entity\Oficina')->findAll();
- $cotizaciones = $em->getRepository('App\Entity\Cotizaciones')->findAll();
- if(isset($_COOKIE['cookieoficina']) && $_COOKIE['cookieoficina'] != 0){
- $ofi = (integer)($_COOKIE['cookieoficina']);
- if($ofi == 1){
- $ofi = $this->getParameter('oficina_por_defecto');
- }
- $oficina = $em->getRepository('App\Entity\Oficina')->findOneBy(['id' => $ofi]);
- }else{
- $ofi = (integer)0;
- $oficina = 0;
- }
- if($divisa != null) {
- $cotizacionesJson=json_encode($em->getRepository('App\Entity\Cotizaciones')->getUltimasCotizacionesOficina($divisa, $ofi));
- } elseif($conversion != null && $conversion->getDivisaOrigen()->getId() != 10) {
- $cotizacionesJson=json_encode($em->getRepository('App\Entity\Cotizaciones')->getUltimasCotizacionesOficina($conversion->getDivisaOrigen(), $ofi));
- } elseif($conversion != null && $conversion->getDivisaDestino()->getId() != 10) {
- $cotizacionesJson=json_encode($em->getRepository('App\Entity\Cotizaciones')->getUltimasCotizacionesOficina($conversion->getDivisaDestino(), $ofi));
- }
- $returnArray = array(
- 'oficinas' => $oficinas,
- 'oficina' => $oficina,
- 'cotizaciones' => $cotizaciones,
- 'cotizacionesJsonG' => $cotizacionesJson,
- 'divisas' => $divisas,
- 'divisa' => $divisa,
- 'ofi_var' => $ofi,
- 'conversion' => $conversion
- );
- return $this->render('default/divisas.html.twig', $returnArray);
- }
- #[Route(path: [
- 'es' => '/createAlert',
- 'en' => '/en/createAlert'
- ], name: 'create_alert')]
- public function alerta(Request $request, PersistenceManagerRegistry $doctrine, SettingsManager $sm, AuthorizationCheckerInterface $securityContext): Response
- {
- $local=$request->getLocale();
- $ajax = $request->isXmlHttpRequest();
- // $settings = $this->get('dinamic_settings.manager');
- // Comprobamos que el usuario esté logueado
- // $securityContext = $this->container->get('security.context');
- if($ajax and !$securityContext->isGranted('IS_AUTHENTICATED_FULLY') || !$securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
- return new JsonResponse(['state' => false, 'msg' => $sm->getValue('currency.alert.login')]);
- }
- // Creamos el formulario de alerta
- $entity = new Alerta();
- $form = $this->createForm(AlertaType::class, $entity, array(
- 'action' => $this->generateUrl('create_alert'),
- 'method' => 'POST',
- 'settings' => $sm,
- ));
- if ($ajax) {
- $form->handleRequest($request);
- if($form->isSubmitted() and $form->isValid()) {
- dump($form->get('tipo')->getData());
- $em = $doctrine->getManager();
- $user=$this->getUser();
- $user->setIdiomaPreferido($local);
- // Buscamos que no tenga alertas creadas actualmente de esta divisa
- $currentlyCreated = $em->getRepository('App\Entity\Alerta')->findOneBy(['usuario' => $this->getUser(), 'divisa' => $form->get('divisa')->getData()]);
- if(isset($_COOKIE['cookieoficina']) && $_COOKIE['cookieoficina'] != 0){
- $ofi = (integer)($_COOKIE['cookieoficina']);
- if($ofi == 1){
- $ofi = $this->getParameter('oficina_por_defecto');
- }
- }else{
- $ofi = (integer)0;
- }
- $oficina = $em->getRepository('App\Entity\Oficina')->findOneBy(['id' => $ofi]);
- if($currentlyCreated) {
- return new JsonResponse(['state' => false, 'msg' => $sm->getValue('currency.alert.hasalert')]);
- }
- $entity->setActivo(true)
- ->setOficina($oficina)
- ->setUsuario($this->getUser());
- $em->persist($entity);
- $em->flush();
- return new JsonResponse(['status' => true, 'msg' => $sm->getValue('currency.alert.newalert')]);
- }
- }
- $html = $this->render('default/alerta.html.twig', [
- 'status' => false,
- 'form' => $form
- ]);
- $returnArray = [
- 'html' => $html
- ];
- // dump($html); die();
- if ($ajax)
- return new JsonResponse($returnArray, 200);
- return $html;
- }
- #[Route(path: [
- 'es' => '/rates-table',
- 'en' => '/en/rates-table'
- ], name: 'app_rates')]
- public function ratesAction(Request $request, PersistenceManagerRegistry $doctrine): Response
- {
- $locale=$request->getLocale();
- $em = $doctrine->getManager();
- $fecha = new \DateTime();
- $oficina = $_GET['id'];
- $cotizaciones = $em->getRepository('App\Entity\Cotizaciones')->getLastChangesOffice($oficina, $locale);
- $objeto = $em->getRepository('App\Entity\Oficina')->findOneBy(['id' => $oficina]);
- if (!$objeto) {
- throw $this->createNotFoundException('Oficina no encontrada');
- }
- $ofi_nom = $objeto->getNombre();
- return $this->render('admin/cotizacion/list_imprimir.html.twig', [
- 'entities' => $cotizaciones,
- 'oficina' => $ofi_nom
- ]);
- }
- #[Route(path: [
- 'es' => '/rates-screen',
- 'en' => '/en/rates-screen'
- ], name: 'app_rates_screen')]
- public function ratesScreenAction(Request $request, PersistenceManagerRegistry $doctrine): Response
- {
- $locale=$request->getLocale();
- $em = $doctrine->getManager();
- $fecha = new \DateTime();
- $oficina = $_GET['id'];
- $cotizaciones = $em->getRepository('App\Entity\Cotizaciones')->getLastChangesOffice($oficina, $locale);
- $objeto = $em->getRepository('App\Entity\Oficina')->findOneBy(['id' => $oficina]);
- if (!$objeto) {
- throw $this->createNotFoundException('Oficina no encontrada');
- }
- $ofi_nom = $objeto->getNombre();
- return $this->render('admin/cotizacion/pantalla_list_imprimir.html.twig', [
- 'entities' => $cotizaciones,
- 'oficina' => $ofi_nom
- ]);
- }
- }