src/Controller/PreparacionController.php line 457

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Preparacion;
  4. use App\Entity\Agenda;
  5. use App\Entity\HistoriaClinica;
  6. use App\Entity\LogAgenda;
  7. use App\Entity\Unidad;
  8. use App\Form\PreparacionType;
  9. use App\Form\BuscaPreparacionTypeNew;
  10. use App\Form\BuscaPreparacionTypeNewUnidad;
  11. use App\Repository\PreparacionRepository;
  12. use App\Repository\PacienteRepository;
  13. use App\Repository\AgendaRepository;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use Symfony\Component\Security\Core\Security;
  20. use App\Form\BuscaPreparacionType;
  21. use App\Repository\HistoriaClinicaRepository;
  22. /**
  23.  * @Route("/preparacion")
  24.  */
  25. class PreparacionController extends AbstractController
  26. {
  27.     /**
  28.      * @var Security
  29.      */
  30.     private $security;
  31.     public function __construct(Security $security)
  32.     {
  33.        date_default_timezone_set("America/Guayaquil");
  34.        $this->security $security;
  35.     }
  36.     /**
  37.      * @Route("/", name="preparacion_index", methods={"GET","POST"})
  38.      */
  39.     public function index(
  40.         PreparacionRepository $preparacionRepository,
  41.         AgendaRepository $agendaRepository,
  42.         PaginatorInterface $paginator,
  43.         Request $request
  44.     ): Response
  45.     {
  46.         date_default_timezone_set("America/Guayaquil");
  47.         $agenda = new Agenda();
  48.         //$form = $this->createForm(BuscaPreparacionTypeNew::class, $agenda);
  49.         $form $this->createForm(BuscaPreparacionTypeNewUnidad::class, $agenda);
  50.         
  51.         $form->handleRequest($request);
  52.         // Datos del formulario
  53.         $unidad      $form->get('unidad')->getData();
  54.         $fecha       $form->get('inicio')->getData();
  55.         $fecha_hasta $form->get('hasta')->getData();
  56.         $paciente    $form->get('nombre')->getData();
  57.         $cedula      $form->get('cedula')->getData();
  58.         // ===============================
  59.         // Manejo de fechas por sesión
  60.         // ===============================
  61.         $session $request->getSession();
  62.         
  63.         // ===============================
  64.         // Guardar filtros al hacer submit
  65.         // ===============================
  66.         if ($form->isSubmitted() && $form->isValid()) {
  67.             $session->set('fecha'$fecha);
  68.             $session->set('fecha_hasta'$fecha_hasta);
  69.            
  70.         }else{
  71.             
  72.             if ($fecha === null) {
  73.                 $fecha $session->get('fecha') ?? new \DateTime('now');
  74.                 $form->get('inicio')->setData($fecha);
  75.             }
  76.             if ($fecha_hasta === null) {
  77.                 $fecha_hasta $session->get('fecha_hasta') ?? new \DateTime('now');
  78.                 $form->get('hasta')->setData($fecha_hasta);
  79.             }
  80.             if ($unidad === null && $request->query->get('unidad')) {
  81.                 $unidad $this->getDoctrine()
  82.                     ->getRepository(Unidad::class)
  83.                     ->find($request->query->get('unidad'));
  84.                 if ($unidad) {
  85.                     $form->get('unidad')->setData($unidad);
  86.                 }
  87.             }
  88.         }
  89.         // ===============================
  90.         // QUERY (SIN redirect)
  91.         // ===============================
  92.         $query $agendaRepository->findByAdmisionadas_paciente_new_desc_2Unidad(
  93.             date_format($fecha'Y-m-d'),
  94.             date_format($fecha_hasta'Y-m-d'),
  95.             $paciente,
  96.             $cedula,
  97.             $unidad
  98.         );
  99.         
  100.         // ===============================
  101.         // PAGINACIÓN
  102.         // 👉 si el form se envía → vuelve a página 1
  103.         // ===============================
  104.         $currentPage 1;
  105.         if (!$form->isSubmitted()) {
  106.             $currentPage $request->query->getInt('page'1);
  107.         }
  108.         $agendas $paginator->paginate(
  109.             $query,
  110.             $currentPage,
  111.             100,
  112.             [
  113.                 'wrap-queries' => true,
  114.                 'params' => [
  115.                     'unidad' => $unidad $unidad->getId() : null,
  116.                 ]
  117.             ]
  118.         );
  119.         return $this->render('preparacion/index.html.twig', [
  120.             'agendas'      => $agendas,
  121.             'form'         => $form->createView(),
  122.             'fecha'        => $fecha,
  123.             'fecha_hasta'  => $fecha_hasta,
  124.             'page'         => $currentPage -1,
  125.             'unidad'       => $unidad
  126.         ]);
  127.     }
  128.     /**
  129.      * @Route("/search", name="preparacion_index_search", methods={"GET","POST"})
  130.      */
  131.     public function index_search(PreparacionRepository $preparacionRepositoryAgendaRepository $agendaRepositoryPaginatorInterface $paginatorRequest $request): Response
  132.     {
  133.         date_default_timezone_set("America/Guayaquil");
  134.         $agenda = new Agenda();
  135.         $form $this->createForm(BuscaPreparacionTypeNew::class,$agenda);
  136.         $form->handleRequest($request);
  137.         $fecha $form->get('inicio')->getData();
  138.         $fecha_hasta $form->get('hasta')->getData();
  139.         $paciente $form->get('nombre')->getData();//dd($paciente);
  140.         $cedula $form->get('cedula')->getData();
  141.         if($paciente == null){
  142.             $paciente $request->query->get('nombre');
  143.             if($paciente != null){
  144.                 $form->get('nombre')->setData($paciente);
  145.             }
  146.         }
  147.         if($cedula== null){
  148.             $cedula$request->query->get('cedula');
  149.             if($cedula!= null){
  150.                 $form->get('cedula')->setData($cedula);
  151.             }
  152.         }    
  153.         
  154.         $page 0;
  155.         if($request->query->get('page') != null ){
  156.             $page $request->query->get('page');
  157.             $page = ($page 1)*100;
  158.         } 
  159.         if($fecha==null){
  160.             $session   $request->getSession();
  161.             $fecha     $session->get('fecha');
  162.             //dd($fecha);
  163.             if($fecha==null){
  164.                 $fecha = new \DateTime("now");
  165.                      
  166.             }
  167.             $form->get('inicio')->setData($fecha);
  168.   
  169.         } 
  170.         
  171.         if($fecha_hasta==null){
  172.             $session   $request->getSession();
  173.             $fecha_hasta     $session->get('fecha_hasta');
  174.             //dd($fecha);
  175.             if($fecha_hasta==null){
  176.                 $fecha = new \DateTime("now");
  177.                      
  178.             }
  179.             $form->get('hasta')->setData($fecha_hasta);
  180.   
  181.         } 
  182.         if ($form->isSubmitted() && $form->isValid()) {
  183.             $session $request->getSession();
  184.             $session->set('fecha',$fecha);
  185.             $session->set('fecha_hasta',$fecha_hasta);
  186.             
  187.             return $this->redirectToRoute('preparacion_index');
  188.         }  
  189.         //$query = $agendaRepository->findByAdmisionadas_paciente_new(date_format($fecha,'Y-m-d'),date_format($fecha_hasta,'Y-m-d'),$paciente,$cedula);
  190.         $query $agendaRepository->findByAdmisionadas_paciente_new_desc_2(date_format($fecha,'Y-m-d'),date_format($fecha_hasta,'Y-m-d'),$paciente,$cedula);
  191.         //dd($query->getResult());
  192.         $agendas $paginator->paginate(
  193.             $query,
  194.             $request->query->getInt('page',1),
  195.             100
  196.         );
  197.         
  198.         return $this->render('preparacion/index.html.twig', [ 'agendas' => $agendas'form' => $form->createView(), 'page' => $page'fecha' => $fecha'fecha_hasta' => $fecha_hasta]);
  199.     }
  200.     /**
  201.      * @Route("/new", name="preparacion_new_old", methods={"GET","POST"})
  202.      */
  203.     public function new(Request $request): Response
  204.     {
  205.         $preparacion = new Preparacion();
  206.         $form $this->createForm(PreparacionType::class, $preparacion);
  207.         $form->handleRequest($request);
  208.         if ($form->isSubmitted() && $form->isValid()) {
  209.             $entityManager $this->getDoctrine()->getManager();
  210.             $entityManager->persist($preparacion);
  211.             $entityManager->flush();
  212.             return $this->redirectToRoute('preparacion_index');
  213.         }
  214.         return $this->render('preparacion/new.html.twig', [
  215.             'preparacion' => $preparacion,
  216.             'form' => $form->createView(),
  217.         ]);
  218.     }
  219.     /**
  220.      * @Route("/preparacion/new/{agenda_id}", name="preparacion_new", methods={"GET"})
  221.      */
  222.     public function preparacion_new(AgendaRepository $agendaRepository$agenda_idRequest $request ): Response
  223.     {
  224.         $ip     $request->getClientIp();
  225.         $user   $this->security->getUser();
  226.         $agenda $agendaRepository->find($agenda_id);
  227.         $preparacion $agenda->getPreparacion();
  228.         if($preparacion!=null){
  229.             return $this->json(['estado' => 'error''preparacion' => 'Ya existe Preparacion']);    
  230.         }
  231.         $preparacion = new Preparacion();
  232.         
  233.         $entityManager $this->getDoctrine()->getManager();
  234.         $preparacion->setEstado('1');
  235.         $preparacion->setIpCrea($ip);
  236.         $preparacion->setIpModifica($ip);
  237.         $preparacion->setUserCrea($user->getId());
  238.         $preparacion->setUserModifica($user->getId());
  239.         $preparacion->setFrecuenciaRespiratoria(0);
  240.         $preparacion->setSaturacionDeOxigeno(0);
  241.         $preparacion->setAgenda($agenda);
  242.         $entityManager->persist($preparacion);
  243.         $entityManager->flush();
  244.         return $this->json(['estado' => 'ok''preparacion' => $preparacion->getId()]);
  245.     }
  246.     /**
  247.      * @Route("/preparacion/new/ecografia/{agenda_id}", name="preparacion_new_ecografia", methods={"GET"})
  248.      */
  249.     public function preparacion_new_ecografia(AgendaRepository $agendaRepository$agenda_idRequest $request ): Response
  250.     {
  251.         $ip     $request->getClientIp();
  252.         $user   $this->security->getUser();
  253.         $agenda $agendaRepository->find($agenda_id);
  254.         $preparacion $agenda->getPreparacion();
  255.         if($preparacion!=null){
  256.             return $this->json(['estado' => 'error''preparacion' => 'Ya existe Preparacion']);    
  257.         }
  258.         $preparacion = new Preparacion();
  259.         
  260.         $entityManager $this->getDoctrine()->getManager();
  261.         $preparacion->setEstado('2');
  262.         $preparacion->setIpCrea($ip);
  263.         $preparacion->setIpModifica($ip);
  264.         $preparacion->setEstatura(1);
  265.         $preparacion->setPeso(1);
  266.         $preparacion->setUserCrea($user->getId());
  267.         $preparacion->setUserModifica($user->getId());
  268.         $preparacion->setFrecuenciaRespiratoria('0');
  269.         $preparacion->setSaturacionDeOxigeno('0');
  270.         $preparacion->setAgenda($agenda);
  271.         $entityManager->persist($preparacion);
  272.         $entityManager->flush();
  273.         return $this->json(['estado' => 'ok''preparacion' => $preparacion->getId()]);
  274.     }
  275.     /**
  276.      * @Route("/{id}", name="preparacion_show", methods={"GET"})
  277.      */
  278.     public function show(Preparacion $preparacion): Response
  279.     {
  280.         return $this->render('preparacion/show.html.twig', [
  281.             'preparacion' => $preparacion,
  282.         ]);
  283.     }
  284.     /**
  285.      * @Route("/{id}/edit", name="preparacion_edit", methods={"GET","POST"})
  286.      */
  287.     public function edit(Request $requestPreparacion $preparacionPacienteRepository $pacienteRepository): Response
  288.     {
  289.         $ip       $request->getClientIp();
  290.         $user     $this->security->getUser();
  291.         $form     $this->createForm(PreparacionType::class, $preparacion);
  292.         $form->handleRequest($request);
  293.         $idpaciente $preparacion->getAgenda()->getPaciente()->getId();
  294.         $paciente  $pacienteRepository->find($idpaciente);
  295.         //dd($paciente->getGrupoSanguineo());
  296.         
  297.         if ($form->isSubmitted() && $form->isValid()) {
  298.             $preparacion->setIpCrea($ip);
  299.             $preparacion->setIpModifica($ip);
  300.             $preparacion->setUserCrea($user->getId());
  301.             $preparacion->setUserModifica($user->getId());
  302.             $preparacion->setEstado('2');
  303.             $paciente->setGrupoSanguineo($preparacion->getGrupoSanquineo());
  304.             $preparacion->getAgenda()->setValidacion($form->get('validacion')->getData());
  305.             $this->getDoctrine()->getManager()->persist($paciente);
  306.             $this->getDoctrine()->getManager()->flush();
  307.             //return $this->redirectToRoute('preparacion_index');
  308.             return $this->redirectToRoute('preparacion_index', [
  309.                 'unidad' => $preparacion->getAgenda()->getSala()->getUnidad() ? $preparacion->getAgenda()->getSala()->getUnidad()->getId() : null,
  310.             ]);
  311.         }else{
  312.             $form->get('validacion')->setData($preparacion->getAgenda()->getValidacion());
  313.         }
  314.         if($paciente->getGrupoSanguineo()!=null){
  315.             //$form->get('grupo_sanquineo')->setData($paciente->getGrupoSanguineo());
  316.         }
  317.         return $this->render('preparacion/edit.html.twig', [
  318.             'preparacion' => $preparacion,
  319.             'form' => $form->createView(),
  320.         ]);
  321.     }
  322.     /**
  323.      * @Route("/{id}", name="preparacion_delete", methods={"DELETE"})
  324.      */
  325.     public function delete(Request $requestPreparacion $preparacion): Response
  326.     {
  327.         if ($this->isCsrfTokenValid('delete'.$preparacion->getId(), $request->request->get('_token'))) {
  328.             $entityManager $this->getDoctrine()->getManager();
  329.             $entityManager->remove($preparacion);
  330.             $entityManager->flush();
  331.         }
  332.         return $this->redirectToRoute('preparacion_index');
  333.     }
  334.     
  335.     /**
  336.      * @Route("/enfermeria/coordinacion", name="preparacion_enfermeria_coordinacion", methods={"GET","POST"})
  337.      */
  338.     public function enfermeria_coordinacion(HistoriaClinicaRepository $historiaClinicaRepositoryAgendaRepository $agendaRepositoryPaginatorInterface $paginatorRequest $request): Response
  339.     {
  340.         $user   $this->security->getUser();
  341.         date_default_timezone_set("America/Guayaquil");
  342.         $agenda = new Agenda();
  343.         
  344.         $form $this->createForm(BuscaPreparacionTypeNew::class,$agenda);
  345.         $form->handleRequest($request);
  346.         $fecha $form->get('inicio')->getData();
  347.         $hasta $form->get('hasta')->getData();
  348.         $paciente $form->get('nombre')->getData();
  349.         $cedula $form->get('cedula')->getData();
  350.         $session $request->getSession();
  351.         $session->set('paciente',null);
  352.         
  353.         if($fecha==null){
  354.             $fecha = new \DateTime("now");
  355.             $form->get('inicio')->setData($fecha);  
  356.         } 
  357.         
  358.         if($hasta==null){
  359.             $hasta = new \DateTime("now");
  360.             $form->get('hasta')->setData($hasta);  
  361.         } 
  362.         
  363.         $query $agendaRepository->findByAdmisionadas_paciente_new_enfermeria(date_format($fecha,'Y-m-d'),date_format($hasta,'Y-m-d'),$paciente,$cedula);
  364.         
  365.         $agendas $paginator->paginate(
  366.             $query,
  367.             $request->query->getInt('page',1),
  368.             100
  369.         );
  370.         return $this->render('preparacion/enfermeria_coo.html.twig', [ 'agendas' => $agendas'form' => $form->createView(), 'paciente' => $paciente ]);
  371.     }
  372.     /**
  373.      * @Route("/terapia/index", name="preparacion_terapia_index", methods={"GET","POST"})
  374.      */
  375.     public function terapia_index(
  376.         PreparacionRepository $preparacionRepository,
  377.         AgendaRepository $agendaRepository,
  378.         PaginatorInterface $paginator,
  379.         Request $request
  380.     ): Response
  381.     {
  382.         date_default_timezone_set("America/Guayaquil");
  383.         $agenda = new Agenda();
  384.         //$form = $this->createForm(BuscaPreparacionTypeNew::class, $agenda);
  385.         $form $this->createForm(BuscaPreparacionTypeNewUnidad::class, $agenda);
  386.         $form->handleRequest($request);
  387.         // Datos del formulario
  388.         $unidad       $form->get('unidad')->getData();
  389.         $fecha       $form->get('inicio')->getData();
  390.         $fecha_hasta $form->get('hasta')->getData();
  391.         $paciente    $form->get('nombre')->getData();
  392.         $cedula      $form->get('cedula')->getData();
  393.         // ===============================
  394.         // Manejo de fechas por sesión
  395.         // ===============================
  396.         $session $request->getSession();
  397.         if ($fecha === null) {
  398.             $fecha $session->get('fecha') ?? new \DateTime('now');
  399.             $form->get('inicio')->setData($fecha);
  400.         }
  401.         if ($fecha_hasta === null) {
  402.             $fecha_hasta $session->get('fecha_hasta') ?? new \DateTime('now');
  403.             $form->get('hasta')->setData($fecha_hasta);
  404.         }
  405.         // ===============================
  406.         // Guardar filtros al hacer submit
  407.         // ===============================
  408.         if ($form->isSubmitted() && $form->isValid()) {
  409.             $session->set('fecha'$fecha);
  410.             $session->set('fecha_hasta'$fecha_hasta);
  411.         }
  412.         // ===============================
  413.         // QUERY (SIN redirect)
  414.         // ===============================
  415.         $query $agendaRepository->findByAdmisionadas_paciente_new_desc_Terapia(
  416.             date_format($fecha'Y-m-d'),
  417.             date_format($fecha_hasta'Y-m-d'),
  418.             $paciente,
  419.             $cedula,
  420.             $unidad
  421.         );
  422.         // ===============================
  423.         // PAGINACIÓN
  424.         // 👉 si el form se envía → vuelve a página 1
  425.         // ===============================
  426.         $currentPage 1;
  427.         if (!$form->isSubmitted()) {
  428.             $currentPage $request->query->getInt('page'1);
  429.         }
  430.         $agendas $paginator->paginate(
  431.             $query,
  432.             $currentPage,
  433.             100
  434.         );
  435.         return $this->render('preparacion/index_terapia.html.twig', [
  436.             'agendas'      => $agendas,
  437.             'form'         => $form->createView(),
  438.             'fecha'        => $fecha,
  439.             'fecha_hasta'  => $fecha_hasta,
  440.             'page'         => $currentPage -1,
  441.         ]);
  442.     }
  443.     /**
  444.      * @Route("/terapia/lista/{id}", name="preparacion_terapia_lista", methods={"GET"})
  445.      */
  446.     public function preparacion_terapia_lista(Agenda $agendaRequest $request ): Response
  447.     {
  448.         $ip     $request->getClientIp();
  449.         $user   $this->security->getUser();
  450.         $entityManager $this->getDoctrine()->getManager();
  451.         $preparacion $agenda->getPreparacion();
  452.         if($preparacion!=null){
  453.                 
  454.         }else{
  455.             
  456.             $preparacion = new Preparacion();
  457.             $preparacion->setEstado('2');
  458.             $preparacion->setIpCrea($ip);
  459.             $preparacion->setIpModifica($ip);
  460.             $preparacion->setEstatura(1);
  461.             $preparacion->setPeso(1);
  462.             $preparacion->setUserCrea($user->getId());
  463.             $preparacion->setUserModifica($user->getId());
  464.             $preparacion->setFrecuenciaRespiratoria('0');
  465.             $preparacion->setSaturacionDeOxigeno('0');
  466.             $preparacion->setAgenda($agenda);
  467.             $entityManager->persist($preparacion);
  468.         }
  469.         
  470.         $historiaclinica $agenda->getHistoriaCLinica();
  471.         if($historiaclinica!=null){
  472.                
  473.         }else{
  474.             $historiaclinica = new HistoriaClinica();
  475.             
  476.             $historiaclinica->setEstado('2');
  477.             $historiaclinica->setPaciente($agenda->getPaciente());
  478.             $historiaclinica->setIpCrea($ip);
  479.             $historiaclinica->setIpModifica($ip);
  480.             $historiaclinica->setUserCrea($user->getId());
  481.             $historiaclinica->setUserModifica($user->getId());
  482.             $historiaclinica->setAgenda($agenda);
  483.             $entityManager->persist($historiaclinica);
  484.         }
  485.         
  486.         $agenda->setDoctor($user);
  487.         $sala         $agenda->getSala();
  488.         $logagenda   = new LogAgenda();
  489.         $logagenda->setSala($sala);
  490.         $logagenda->setIpCrea($ip);
  491.         $logagenda->setIpModifica($ip);
  492.         $logagenda->setEstadoAgenda(3);
  493.         $logagenda->setUserCrea($user->getId());
  494.         $logagenda->setUserModifica($user->getId());
  495.         $inicio          $agenda->getInicio();
  496.         $fin             $agenda->getFin();
  497.         $seguro          $agenda->getSeguro();
  498.         $especialidad    $agenda->getEspecialidad();
  499.         $doctor          $user;
  500.         $observacion     'Terapia Atendida por Enfermería';
  501.         $descripcion     'Terapia Atendida por Enfermería';
  502.         $logagenda->setInicio($inicio);
  503.         $logagenda->setFin($fin);
  504.         $logagenda->setObservacion($observacion);
  505.         $logagenda->setAgenda($agenda);
  506.         $logagenda->setSeguro($seguro);
  507.         $logagenda->setEspecialidad($especialidad);
  508.         $logagenda->setDoctor($doctor);
  509.         $logagenda->setDescripcion($descripcion);
  510.         $entityManager->persist($logagenda);
  511.         $entityManager->flush();
  512.         return $this->json(['estado' => 'ok''preparacion' => $preparacion->getId()]);
  513.     }
  514. }