src/Controller/Comun/NotificacionController.php line 339

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Comun;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use App\Repository\Comun\NotificacionRepository;
  7. use App\Entity\Comun\Notificacion;
  8. use App\Entity\Admin\Empleada;
  9. use App\Repository\Accesos\UsuarioRepository;
  10. use App\Repository\Comun\TipoNotificacionRepository;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use App\Entity\Comun\TipoNotificacion;
  13. use App\Repository\Comun\FichajeDiarioRepository;
  14. use App\Service\FichajeService;
  15. use App\Entity\Comun\FichajeDiario;
  16. use Symfony\Component\Mailer\MailerInterface;
  17. use Symfony\Component\Mime\Email;
  18. use Symfony\Component\Mime\Address;
  19. #[Route('/comun/notificacion')]
  20. class NotificacionController extends AbstractController {
  21.     private $fichajeService;
  22.     public function __construct(FichajeService $fichajeService) {
  23.         $this->fichajeService $fichajeService;
  24.     }
  25.     #[Route('/'name'app_comun_notificacion'methods: ['POST''GET'])]
  26.     public function index(NotificacionRepository $notificacionRepository): Response {
  27.         $empleada $this->get('security.token_storage')->getToken()->getUser()->getEmpleada();
  28.         $notificaciones $notificacionRepository->findBy(['destinatario' => $empleada'leido' => 0], ['fecha' => 'DESC']);
  29.         $notificacionesLeidas $notificacionRepository->findBy(['destinatario' => $empleada'leido' => 1], ['fecha' => 'DESC']);
  30.         return $this->render('comun/notificacion/index.html.twig', [
  31.                     'controller_name' => 'NotificacionController',
  32.                     'notificaciones' => $notificaciones,
  33.                     'notificacionesLeidas' => $notificacionesLeidas,
  34.         ]);
  35.     }
  36.     #[Route('/index'name'app_comun_notificacion_index'methods: ['POST''GET'])]
  37.     public function index2(NotificacionRepository $notificacionRepository): Response {
  38.         $empleada $this->get('security.token_storage')->getToken()->getUser()->getEmpleada();
  39.         $notificaciones $notificacionRepository->findBy(['destinatario' => $empleada'leido' => 0], ['fecha' => 'DESC']);
  40.         $notificacionesLeidas $notificacionRepository->findBy(['destinatario' => $empleada'leido' => 1], ['fecha' => 'DESC']);
  41.         return $this->render('comun/notificacion/index2.html.twig', [
  42.                     'controller_name' => 'NotificacionController',
  43.                     'notificaciones' => $notificaciones,
  44.                     'notificacionesLeidas' => $notificacionesLeidas,
  45.         ]);
  46.     }
  47.     #[Route('/leer/{notificacion}'name'app_comun_notificacion_leer'methods: ['POST''GET'])]
  48.     public function leerNotificacion(NotificacionRepository $notificacionRepositoryNotificacion $notificacion): Response {
  49.         $notificacion->setLeido(true);
  50.         $notificacionRepository->add($notificacion);
  51.         return $this->redirect($this->generateUrl('app_comun_notificacion', []));
  52.     }
  53.     #[Route('/leertodas/'name'app_comun_notificacion_leer_todas'methods: ['POST''GET'])]
  54.     public function leerNotificacionTodas(NotificacionRepository $notificacionRepository): Response {
  55.         $empleada $this->get('security.token_storage')->getToken()->getUser()->getEmpleada();
  56.         $notificaciones $notificacionRepository->findBy(['destinatario' => $empleada'leido' => 0], ['fecha' => 'DESC']);
  57.         foreach ($notificaciones as $notificacion) {
  58.             $notificacion->setLeido(true);
  59.             $notificacionRepository->add($notificacion);
  60.         }
  61.         return $this->redirect($this->generateUrl('app_comun_notificacion', []));
  62.     }
  63.     #[Route('/generar/{empleada}'name'app_comun_notificacion_generar'methods: ['POST''GET'])]
  64.     public function generar(NotificacionRepository $notificacionRepositoryEmpleada $empleada nullUsuarioRepository $usuarioRepository\App\Repository\Comun\TipoNotificacionRepository $tipoNotificacionRepository): Response {
  65.         $administradores $usuarioRepository->getAdministradores();
  66.         $notificacion = new Notificacion();
  67.         $notificacion->setFecha(date_create('now'));
  68.         $notificacion->setEmpleada($empleada);
  69.         $notificacion->setLeido(false);
  70.         $notificacion->setTextoes("Notificacion");
  71.         $notificacion->setTextoeus("Notifikazioa");
  72.         $notificacion->setTipo($tipoNotificacionRepository->find(1));
  73.         $notificacion->setDestinatario($empleada);
  74.         $notificacionRepository->add($notificacion);
  75.         foreach ($administradores as $admin) {
  76.             $noti = new Notificacion();
  77.             $noti->setFecha(date_create('now'));
  78.             $noti->setEmpleada($empleada);
  79.             $noti->setLeido(false);
  80.             $noti->setTextoes("Notificacion");
  81.             $noti->setTextoeus("Notifikazioa");
  82.             $noti->setTipo($tipoNotificacionRepository->find(1));
  83.             $noti->setDestinatario($admin->getEmpleada());
  84.             $notificacionRepository->add($noti);
  85.         }
  86.         return $this->render('comun/notificacion/index.html.twig', [
  87.                     'controller_name' => 'NotificacionController',
  88.         ]);
  89.     }
  90.     #[Route('/mostrar/popup'name'app_comun_notificacion_mostrar_popup'methods: ['POST''GET'])]
  91.     public function mostrarNotificacionesPopUp(NotificacionRepository $notificacionRepository): Response {
  92.         $empleada $this->get('security.token_storage')->getToken()->getUser()->getEmpleada();
  93.         $notificaciones $notificacionRepository->notificacionesNoLeidasCinco($empleada);
  94.         $cantidad count($notificacionRepository->findBy(['destinatario' => $empleada'leido' => 0], ['fecha' => 'DESC']));
  95.         return $this->render('comun/notificacion/popup.html.twig', [
  96.                     'notificaciones' => $notificaciones,
  97.                     'cantidad' => $cantidad,
  98.         ]);
  99.     }
  100.     #[Route('/horas'name'app_comun_notificacion_horas'methods: ['GET''POST'])]
  101.     public function comprobarHorasTrabajadas(): Response {
  102.         $em $this->getDoctrine()->getManager();
  103.         $empleadas $em->getRepository("App:Admin\Empleada")->findAll();
  104.         $globales $em->getRepository("App:Admin\Globales")->findOneby([]);
  105.         $tipoNotificacionRepository $em->getRepository("App:Comun\TipoNotificacion");
  106.         foreach ($empleadas as $empleada) {
  107.             $tiempo $this->fichajeService->calcularHorasPorFecha($empleadadate_create('now'));
  108.             if ($tiempo['horas'] != || $tiempo['minutos'] != || $tiempo['segundos'] != 0) { //solo notifica si has trabajado
  109.                 $tiempoMinutos = ($tiempo['horas'] * 60) + $tiempo['minutos'];
  110.                 $maximo = (intval($globales->getHoraAvisoMax()->format("H")) * 60) + intval($globales->getHoraAvisoMax()->format("i"));
  111.                 $minimo = (intval($globales->getHoraAvisoMin()->format("H")) * 60) + intval($globales->getHoraAvisoMin()->format("i"));
  112.                 if ($tiempoMinutos $minimo) {
  113.                     $horas $this->fichajeService->convertirMinutos($minimo $tiempoMinutos);
  114.                     $textoes $empleada->getNombre() . " ha trabajado " $tiempo['horas'] . "h " $tiempo['minutos'] . "m , " $horas " horas menos de lo establecido";
  115.                     $textoeus $empleada->getNombre() . " " $tiempo['horas'] . "h " $tiempo['minutos'] . "m egin du lan , ezarritakoa baino  " $horas " gutxiago";
  116.                     $tipo $tipoNotificacionRepository->findOneBy(['descripcion' => 'tipoNotificacion.horasNoCumplidas']);
  117.                     $this->generarNotificacion($empleada$empleada->getId(), $tipo$textoeus$textoes);
  118.                 }
  119.                 if ($tiempoMinutos $maximo) {
  120.                     $horas $this->fichajeService->convertirMinutos($tiempoMinutos $maximo);
  121.                     $textoes $empleada->getNombre() . " ha trabajado un total de " $tiempo['horas'] . "h " $tiempo['minutos'] . "m , " $horas " horas mas de lo establecido";
  122.                     $textoeus $empleada->getNombre() . " " $tiempo['horas'] . "h " $tiempo['minutos'] . "m egin du lan, gehienezko kopurua baino " $horas "  gehiago";
  123.                     $tipo $tipoNotificacionRepository->findOneBy(['descripcion' => 'tipoNotificacion.horasSuperadas']);
  124.                     $this->generarNotificacion($empleada$empleada->getId(), $tipo$textoeus$textoes);
  125.                 }
  126.             }
  127.         }
  128.         return new Response();
  129.     }
  130.     #[Route('/cerrarfichaje'name'app_comun_notificacion_cerrar_fichaje'methods: ['GET''POST'])]
  131.     public function cerrarFichaje(): Response {
  132.         $em $this->getDoctrine()->getManager();
  133.         $fichajeDiarioRepository $em->getRepository("App:Comun\FichajeDiario");
  134.         $empleadas $em->getRepository("App:Admin\Empleada")->findAll();
  135.         $globales $em->getRepository("App:Admin\Globales")->findOneby([]);
  136.         $tipoNotificacionRepository $em->getRepository("App:Comun\TipoNotificacion");
  137.         foreach ($empleadas as $empleada) {
  138.             $ultimoFichaje $fichajeDiarioRepository->findOneBy(['empleada' => $empleada], ['fecha' => 'DESC']);
  139.             if (!is_null($ultimoFichaje)) {
  140.                 if ($ultimoFichaje->getEntrada()) {
  141.                     $fecha date_create_from_format("Y-m-d H:i"$ultimoFichaje->getFecha()->format("Y-m-d") . " " $globales->getHoraFin()->format("H:i"));
  142.                     $fichajeDiario = new FichajeDiario();
  143.                     $fichajeDiario->setEntrada(false);
  144.                     $fichajeDiario->setFecha($fecha);
  145.                     $fichajeDiario->setEmpleada($empleada);
  146.                     $em->persist($fichajeDiario);
  147.                     $em->flush();
  148.                     $textoes "Se ha cerrado un fichaje para " $empleada->getNombre() . " con fecha: " $fichajeDiario->getFecha()->format("Y-m-d");
  149.                     $textoeus "Fitxaje bat ixti da " $empleada->getNombre() . "-rentzat " $fichajeDiario->getFecha()->format("d-m-Y") . " datarekin";
  150.                     $tipo $tipoNotificacionRepository->findOneBy(['descripcion' => 'tipoNotificacion.fichajeNoCerrado']);
  151.                     $this->generarNotificacion($empleada$empleada->getId(), $tipo$textoeus$textoes);
  152.                 }
  153.             }
  154.         }
  155.         return new Response();
  156.     }
  157.     public function generarNotificacion(Empleada $empleada$objetoTipoNotificacion $tipo$textoeus$textoes) {
  158.         $em $this->getDoctrine()->getManager();
  159.         $usuarioRepository $em->getRepository("App:Accesos\Usuario");
  160.         $administradores $usuarioRepository->getAdministradores();
  161.         $notificacion = new Notificacion();
  162.         if (!is_null($empleada)) {
  163.             $notificacion->setFecha(date_create('now'));
  164.             $notificacion->setObjeto($objeto);
  165.             $notificacion->setLeido(false);
  166.             $notificacion->setTextoes($textoes);
  167.             $notificacion->setTextoeus($textoeus);
  168.             $notificacion->setTipo($tipo);
  169.             $notificacion->setDestinatario($empleada);
  170.             $em->persist($notificacion);
  171.         }
  172.         foreach ($administradores as $admin) {
  173.             if ($empleada->getId() != $admin->getEmpleada()->getId()) {
  174.                 $noti = new Notificacion();
  175.                 $noti->setFecha(date_create('now'));
  176.                 $noti->setObjeto($objeto);
  177.                 $noti->setLeido(false);
  178.                 $noti->setTextoes($textoes);
  179.                 $noti->setTextoeus($textoeus);
  180.                 $noti->setTipo($tipo);
  181.                 $noti->setDestinatario($admin->getEmpleada());
  182.                 $em->persist($noti);
  183.             }
  184.         }
  185.         $em->flush();
  186.     }
  187.     #[Route('/informe'name'app_comun_notificacion_informe'methods: ['GET''POST'])]
  188.     public function informeNotificaciones(MailerInterface $mailer) {
  189.         $em $this->getDoctrine()->getManager();
  190.         $tipoNotificacionRepository $em->getRepository("App:Comun\TipoNotificacion");
  191.         $notificacionRepository $em->getRepository("App:Comun\Notificacion");
  192.         $usuarioRepository $em->getRepository("App:Accesos\Usuario");
  193.         $administradoras $usuarioRepository->getAdministradores();
  194.         $fecha date_create('now');
  195.         $horasSuperadas $tipoNotificacionRepository->findOneBy(['descripcion' => 'tipoNotificacion.horasSuperadas']);
  196.         $horasNoCumplidas $tipoNotificacionRepository->findOneBy(['descripcion' => 'tipoNotificacion.horasNoCumplidas']);
  197.         $fichajeCerrado $tipoNotificacionRepository->findOneBy(['descripcion' => 'tipoNotificacion.fichajeNoCerrado']);
  198.         foreach ($administradoras as $administradora) {
  199.             $notificacionesHorasSuperadas $notificacionRepository->findBy(['tipo' => $horasSuperadas'fecha' => $fecha'destinatario' => $administradora->getEmpleada()], ['fecha' => 'DESC']);
  200.             $notificacionesHorasNoCumplidas $notificacionRepository->findBy(['tipo' => $horasNoCumplidas'fecha' => $fecha'destinatario' => $administradora->getEmpleada()], ['fecha' => 'DESC']);
  201.             $notificacionesfichajeCerrado $notificacionRepository->findBy(['tipo' => $fichajeCerrado'fecha' => $fecha'destinatario' => $administradora->getEmpleada()], ['fecha' => 'DESC']);
  202.             $notificacionesJustificacion $notificacionRepository->notificacionesJustificacion($administradora->getEmpleada(), $fecha);
  203.             $contador 0;
  204.             $contador += count($notificacionesHorasSuperadas);
  205.             $contador += count($notificacionesHorasNoCumplidas);
  206.             $contador += count($notificacionesfichajeCerrado);
  207.             $contador += count($notificacionesJustificacion);
  208.             
  209.             if ($contador 0){
  210.                 $html $this->renderView('comun/notificacion/notificacionesTipo.html.twig', [
  211.                     'notificacionesHorasSuperadas' => $notificacionesHorasSuperadas,
  212.                     'notificacionesHorasNoCumplidas' => $notificacionesHorasNoCumplidas,
  213.                     'notificacionesFichajeCerrado' => $notificacionesfichajeCerrado,
  214.                     'tipoHorasSuperadas' => $horasSuperadas,
  215.                     'tipoHorasNoCumplidas' => $horasNoCumplidas,
  216.                     'tipoFichajeCerrado' => $fichajeCerrado,
  217.                     'notificacionesJustificacion' => $notificacionesJustificacion,
  218.                     'fecha' => $fecha
  219.                 ]);
  220.                 $email = (new Email())
  221.                         ->from(new Address("daitekecrm@outlook.com"'Daiteke CRM'))
  222.                         ->to($administradora->getEmail())
  223.                         ->priority(Email::PRIORITY_HIGH)
  224.                         ->subject("Informe notificaciones")
  225.                         ->html($html);
  226.                 $mailer->send($email);
  227.             }
  228.         }
  229.         return new Response("");
  230.     }
  231.     #[Route('/justificacion'name'app_comun_notificacion_justificacion'methods: ['GET''POST'])]
  232.     public function notificacionJustificacion() {
  233.         $em $this->getDoctrine()->getManager();
  234.         $tipoNotificacionRepository $em->getRepository("App:Comun\TipoNotificacion");
  235.         $notificacionRepository $em->getRepository("App:Comun\Notificacion");
  236.         $justificacionRepository $em->getRepository("App:Comun\Justificacion");
  237.         $globales $em->getRepository("App:Admin\Globales")->findOneby([]);
  238.         $tipo1 $tipoNotificacionRepository->findOneBy(['descripcion' => 'tipoNotificacion.justificacion1']);
  239.         $tipo2 $tipoNotificacionRepository->findOneBy(['descripcion' => 'tipoNotificacion.justificacion2']);
  240.         $tipo3 $tipoNotificacionRepository->findOneBy(['descripcion' => 'tipoNotificacion.justificacion3']);
  241.         $justificaciones $justificacionRepository->findBy(['presentada' => 0]);
  242.         $hoy date_create('now');
  243.         foreach ($justificaciones as $justificacion) {
  244.             $fecha $justificacion->getFecha();
  245.             if ($hoy $fecha) {
  246.                 $interval date_diff($hoy$fecha);
  247.                 $dias = ($interval->days) + 1;
  248.                 if ($dias <= $globales->getJustificacionAviso1()) {
  249.                     $notificacion $notificacionRepository->findOneBy(['tipo' => $tipo1'objeto' => $justificacion->getId()]);
  250.                     if (is_null($notificacion)) {
  251.                         $textoes "La justificación " $justificacion->getNombre() . " para el proyecto " $justificacion->getProyecto()->getNombre() . " tiene que presentarse en menos de " $globales->getJustificacionAviso1() . " dias (" $justificacion->getFecha()->format("Y-m-d") . ")";
  252.                         $textoeus $justificacion->getNombre() . " justifikazioa " $justificacion->getProyecto()->getNombre() . " proiekturako " $globales->getJustificacionAviso1() . " egun baino lehen aurkeztu behar da (" $justificacion->getFecha()->format("d-m-Y") . ")";
  253.                         $this->generarNotificacion($justificacion->getProyecto()->getResponsable(), $justificacion->getId(), $tipo1$textoeus$textoes);
  254.                     }
  255.                 }
  256.                 if ($dias <= $globales->getJustificacionAviso2()) {
  257.                     $notificacion $notificacionRepository->findOneBy(['tipo' => $tipo2'objeto' => $justificacion->getId()]);
  258.                     if (is_null($notificacion)) {
  259.                         $textoes "La justificación " $justificacion->getNombre() . " para el proyecto " $justificacion->getProyecto()->getNombre() . " tiene que presentarse en menos de " $globales->getJustificacionAviso2() . " dias (" $justificacion->getFecha()->format("Y-m-d") . ")";
  260.                         $textoeus $justificacion->getNombre() . " justifikazioa " $justificacion->getProyecto()->getNombre() . " proiekturako " $globales->getJustificacionAviso2() . " egun baino lehen aurkeztu behar da (" $justificacion->getFecha()->format("d-m-Y") . ")";
  261.                         $this->generarNotificacion($justificacion->getProyecto()->getResponsable(), $justificacion->getId(), $tipo2$textoeus$textoes);
  262.                     }
  263.                 }
  264.                 if ($dias <= $globales->getJustificacionAviso3()) {
  265.                     $notificacion $notificacionRepository->findOneBy(['tipo' => $tipo3'objeto' => $justificacion->getId()]);
  266.                     if (is_null($notificacion)) {
  267.                         $textoes "La justificación " $justificacion->getNombre() . " para el proyecto " $justificacion->getProyecto()->getNombre() . " tiene que presentarse en menos de " $globales->getJustificacionAviso3() . " dias (" $justificacion->getFecha()->format("Y-m-d") . ")";
  268.                         $textoeus $justificacion->getNombre() . " justifikazioa " $justificacion->getProyecto()->getNombre() . " proiekturako " $globales->getJustificacionAviso3() . " egun baino lehen aurkeztu behar da (" $justificacion->getFecha()->format("d-m-Y") . ")";
  269.                         $this->generarNotificacion($justificacion->getProyecto()->getResponsable(), $justificacion->getId(), $tipo3$textoeus$textoes);
  270.                     }
  271.                 }
  272.             }
  273.         }
  274.         return new Response("");
  275.     }
  276.     #[Route('/cron'name'app_comun_notificacion_cron'methods: ['GET''POST'])]
  277.     public function ejecutarCron(MailerInterface $mailer) {
  278.         $this->cerrarFichaje();
  279.         $this->comprobarHorasTrabajadas();
  280.         $this->notificacionJustificacion();
  281.         $this->informeNotificaciones($mailer);
  282.         return new Response("Enviado");
  283.     }
  284.     
  285.     #[Route('/llamada/notificar/{llamada}'name'app_comun_notificacion_llamada'methods: ['GET''POST'])]
  286.     public function generarNotificacionLlamada(\App\Entity\Accion\Llamada $llamada) {
  287.         $em=$this->getDoctrine()->getManager();
  288.         $tipoNotificacionRepository $em->getRepository("App:Comun\TipoNotificacion");
  289.         $empleadaConectada $this->get('security.token_storage')->getToken()->getUser()->getEmpleada();
  290.         $notificacion = new Notificacion();
  291.         
  292.         $notificacion->setDestinatario($llamada->getEmpleada());
  293.         $notificacion->setFecha($llamada->getFecha());
  294.         $notificacion->setLeido(false);
  295.         $notificacion->setTextoes("Has recibido una llamada del cliente "$llamada->getCliente()  ." el "$llamada->getFecha()->format("Y-m-d")." sobre (" .$llamada->getObservaciones().") | notificado por: "$empleadaConectada);
  296.         $notificacion->setTextoeus($llamada->getFecha()->format("d-m-Y")." an "$llamada->getCliente()  ." bezeroaren dei bat jaso duzu (" .$llamada->getObservaciones().")ri buruz | ".$empleadaConectada."rengatik sortuta");
  297.         $tipo $tipoNotificacionRepository->findOneBy(['descripcion'=>'tipoNotificacion.llamada']);
  298.         $notificacion->setTipo($tipo);
  299.         $notificacion->setObjeto($llamada->getId());
  300.         $em->persist($notificacion);
  301.         $em->flush();
  302.         
  303.         return new Response("Llamada notificada");
  304.     }
  305.     
  306.     
  307. }