<?php
namespace App\Controller;
use App\Entity\Paciente;
use App\Form\BuscarPacienteType;
use App\Form\PacienteType;
use App\Form\PacienteTypeEdit;
use App\Repository\PacienteRepository;
use App\Repository\PreparacionRepository;
use App\Repository\AgendaRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* @Route("/paciente")
*/
class PacienteController extends AbstractController
{
/**
* @Route("/", name="paciente_index", methods={"GET","POST"})
*/
public function index(PacienteRepository $pacienteRepository, Request $request, PaginatorInterface $paginator, SessionInterface $session): Response
{
$form = $this->createForm(BuscarPacienteType::class);
$form->handleRequest($request);
$cedula = null;$apellidos = null;$nombres = null;
if ($form->isSubmitted() && $form->isValid()) {
$cedula = $form->get('cedula')->getData();
$apellidos = $form->get('apellidos')->getData();
$nombres = $form->get('nombres')->getData();
}
// Obtén la URL actual
$currentUrl = $request->getRequestUri();
// Guarda la URL actual en la sesión
$session->set('previous_url', $currentUrl);
$q_pacientes = $pacienteRepository->BuscarPaciente($cedula,$apellidos,$nombres);
//dd($q_pacientes);
$pacientes = $paginator->paginate(
$q_pacientes, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
50 /*limit per page*/
);
//dd($pacientes);
return $this->render('paciente/index.html.twig', [
'pacientes' => $pacientes,
'form' => $form->createView(),
]);
}
/**
* @Route("/new", name="paciente_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$paciente = new Paciente();
$form = $this->createForm(PacienteType::class, $paciente);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($paciente);
$paciente->setApellido1(strtoupper($paciente->getApellido1()));
$paciente->setApellido2(strtoupper($paciente->getApellido2()));
$paciente->setNombre1(strtoupper($paciente->getNombre1()));
$paciente->setNombre2(strtoupper($paciente->getNombre2()));
$entityManager->flush();
return $this->redirectToRoute('paciente_index');
}
return $this->render('paciente/new.html.twig', [
'paciente' => $paciente,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="paciente_show", methods={"GET"})
*/
public function show(Paciente $paciente): Response
{
return $this->render('paciente/show.html.twig', [
'paciente' => $paciente,
]);
}
/**
* @Route("/filiacion/{id}", name="paciente_show", methods={"GET"})
*/
public function show_filiacion(Paciente $paciente, PreparacionRepository $preparacionRepository)
{
$preparaciones = $preparacionRepository->buscaPreparacionxPaciente($paciente->getId());//dd($preparaciones);
return $this->render('paciente/show_filiacion.html.twig', [
'paciente' => $paciente,
'preparaciones' => $preparaciones->getResult()
]);
}
/**
* @Route("/{id}/edit", name="paciente_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Paciente $paciente, SessionInterface $session, AgendaRepository $agendaRepository): Response
{
$form = $this->createForm(PacienteTypeEdit::class, $paciente);
$form->handleRequest($request);
$mensaje = "";
$previousUrl = $session->get('previous_url');//dd($previousUrl);
if ($form->isSubmitted() && $form->isValid()) {
$paciente->setApellido1(strtoupper($paciente->getApellido1()));
$paciente->setApellido2(strtoupper($paciente->getApellido2()));
$paciente->setNombre1(strtoupper($paciente->getNombre1()));
$paciente->setNombre2(strtoupper($paciente->getNombre2()));
$this->getDoctrine()->getManager()->flush();
$mensaje = "PACIENTE ACTUALIZADO";
//return $this->redirectToRoute('paciente_index');
}else{
$pais = $paciente->getPais();
if($pais == null){
$pais = 'ECUADOR';
}
$form->get('pais')->setData($pais);
}
$agendas = $agendaRepository->findByPaciente($paciente->getId())->getResult();
return $this->render('paciente/edit.html.twig', [
'paciente' => $paciente,
'form' => $form->createView(),
'mensaje' => $mensaje,
'previousUrl' => $previousUrl,
'agendas' => $agendas,
]);
}
/**
* @Route("/{id}/buscar/cedula", name="paciente_buscar_cedula", methods={"GET","POST"})
*/
public function busca_paciente($id, PacienteRepository $pacienteRepository, Request $request): Response
{
$dbpaciente = $pacienteRepository->findOneByCedula($id);
//dd($dbpaciente->getId());
if(!is_null($dbpaciente)){
$session = $request->getSession();
$session->set('id_paciente',$dbpaciente->getId());
return $this->json([
// Query is not required as of version 1.2.5
"estado" => "ok",
"paciente" => $dbpaciente->getId(),
"nombres" => $dbpaciente->getApellido1().' '.$dbpaciente->getApellido2().' '.$dbpaciente->getNombre1().' '.$dbpaciente->getNombre2(),
]);
}
return $this->json([
// Query is not required as of version 1.2.5
"estado"=> "no",
"paciente"=> "",
]);
}
/**
* @Route("/{id}/buscar/cedula2", name="paciente_buscar_cedula2", methods={"GET","POST"})
*/
public function busca_paciente2 ($id, PacienteRepository $pacienteRepository, Request $request): Response
{
$dbpaciente = $pacienteRepository->findOneByCedula($id);
//dd($dbpaciente->getId());
if(!is_null($dbpaciente)){
$session = $request->getSession();
$session->set('id_paciente',$dbpaciente->getId());
return $this->json([
// Query is not required as of version 1.2.5
"estado" => "ok",
"paciente" => $dbpaciente->getId(),
"nombres" => $dbpaciente->getApellido1().' '.$dbpaciente->getApellido2().' '.$dbpaciente->getNombre1().' '.$dbpaciente->getNombre2(),
]);
}
return $this->json([
// Query is not required as of version 1.2.5
"estado"=> "no",
"paciente"=> "",
]);
}
/**
* @Route("/{id}", name="paciente_delete", methods={"DELETE"})
*/
public function delete(Request $request, Paciente $paciente): Response
{
if ($this->isCsrfTokenValid('delete'.$paciente->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($paciente);
$entityManager->flush();
}
return $this->redirectToRoute('paciente_index');
}
/**
* @Route("/cedula/buscar/cedula/autocomplete", name="paciente_buscar_cedula_autocomplete", methods={"GET"})
*/
public function buscar_paciente_autocomplete_cedula(Request $request,PacienteRepository $pacienteRepository): Response
{
$query = $request->query->get('query');
$pacientes = $pacienteRepository->BuscarPacientesxCedula($query);
$arr = [];$i=0;
//dd($items);
foreach ($pacientes as $value) {
//dd($value->gettarifario());
$arr[$i]=[ "value"=> $value['cedula'], "data"=> $value['id'] ];
$i++;
}
//dd($arr);
return $this->json([
// Query is not required as of version 1.2.5
"query"=> "Unit",
"suggestions"=> $arr,
]);
}
/**
* @Route("/cedula/buscar/nombres/autocomplete", name="paciente_buscar_nombres_autocomplete", methods={"GET"})
*/
public function buscar_paciente_autocomplete_nombres(Request $request,PacienteRepository $pacienteRepository): Response
{
$query = $request->query->get('query');
$pacientes = $pacienteRepository->BuscarPacientesxNombres($query);
$arr = [];$i=0;
//dd($items);
foreach ($pacientes as $value) {
//dd($value->gettarifario());
$arr[$i]=[ "value"=> $value['nombres'], "data"=> $value['id'], "cedula"=> $value['cedula'] ];
$i++;
}
//dd($arr);
return $this->json([
// Query is not required as of version 1.2.5
"query"=> "Unit",
"suggestions"=> $arr,
]);
}
/**
* @Route("/agenda/new", name="paciente_agenda_new", methods={"GET"})
*/
public function paciente_agenda_new(Request $request): Response
{
$paciente = new Paciente();
$form = $this->createForm(PacienteType::class, $paciente);
$form->handleRequest($request);
/*if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($paciente);
$paciente->setApellido1(strtoupper($paciente->getApellido1()));
$paciente->setApellido2(strtoupper($paciente->getApellido2()));
$paciente->setNombre1(strtoupper($paciente->getNombre1()));
$paciente->setNombre2(strtoupper($paciente->getNombre2()));
$entityManager->flush();
return ['estado' => 'save', 'cedula' => $paciente->getCedula()];
}*/
return $this->render('paciente/modal_new.html.twig', [
'paciente' => $paciente,
'form' => $form->createView(),
]);
}
/**
* @Route("/agenda/new/storage", name="paciente_agenda_storage", methods={"POST"})
*/
public function paciente_agenda_storage(Request $request, PacienteRepository $pacienteRepository): Response
{
$paciente = new Paciente();
$form = $this->createForm(PacienteType::class, $paciente);
$fecha_nacimiento = $form->get('fecha_nacimiento')->getData();
//dd($fecha_nacimiento);
$form->handleRequest($request);
$dbpaciente = $pacienteRepository->findOneByCedula($paciente->getCedula());
if(!is_null($dbpaciente)){
$session = $request->getSession();
$session->set('id_paciente',$dbpaciente->getId());
return $this->json([
// Query is not required as of version 1.2.5
"estado" => "existe",
"cedula" => $dbpaciente->getCedula(),
"nombres" => $paciente->getApellido1().' '.$paciente->getApellido2().' '.$paciente->getNombre1().' '.$paciente->getNombre2()
]);
}
/*if($existe['estado']=='ok'){
return $this->json(['estado' => 'existe', 'cedula' => $paciente->getCedula()]);
}*/
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($paciente);
$paciente->setApellido1(strtoupper($paciente->getApellido1()));
$paciente->setApellido2(strtoupper($paciente->getApellido2()));
$paciente->setNombre1(strtoupper($paciente->getNombre1()));
$paciente->setNombre2(strtoupper($paciente->getNombre2()));
$entityManager->flush();
$paciente = $pacienteRepository->findOneByCedula($paciente->getCedula());
$session = $request->getSession();
$session->set('id_paciente',$paciente->getId());
return $this->json([
'estado' => 'ok',
'cedula' => $paciente->getCedula(),
'nombres' => $paciente->getApellido1().' '.$paciente->getApellido2().' '.$paciente->getNombre1().' '.$paciente->getNombre2()
]);
}
return $this->json([
'estado' => 'no',
'cedula' => $paciente->getCedula(),
'nombres' => $paciente->getApellido1().' '.$paciente->getApellido2().' '.$paciente->getNombre1().' '.$paciente->getNombre2()
]);
}
/**
* @Route("/buscar/antecedentes/personales/familiares/qx/{id}", name="paciente_antecedente_ajax", methods={"GET"})
*/
public function obtener_antecedente(Paciente $paciente): Response
{
//dd($paciente);
$antecedentes_personales = $paciente->getMasAntecedentesPersonales();
$antecedentes_quirurgicos = $paciente->getAntecedentesQuirurgicos();
$antecedentes_familiares = $paciente->getAntecedentesFamiliares();
$alcohol = $paciente->getAlcohol();
$tabaco = $paciente->getTabaco();
$drogas = $paciente->getDrogas();
$alergias = $paciente->getAlergias();
$medicacion = $paciente->getMedicacion();
$ago = $paciente->getAgo();
$fum = $paciente->getFum();
$gesta = $paciente->getGesta();
$abo = $paciente->getAbo();
$para = $paciente->getpara();
$cesa = $paciente->getCesa();
if($antecedentes_personales==null){ $antecedentes_personales = ''; }
if($antecedentes_quirurgicos==null){ $antecedentes_quirurgicos = ''; }
if($antecedentes_familiares==null){ $antecedentes_familiares = ''; }
if($alcohol==null){ $alcohol = ''; }
if($tabaco==null){ $tabaco = ''; }
if($drogas==null){ $drogas = ''; }
if($alergias==null){ $alergias = ''; }
if($medicacion==null){ $medicacion = ''; }
if($ago==null){ $ago = ''; }
if($fum==null){ $fum = ''; }
if($gesta==null){ $gesta = ''; }
if($abo==null){ $abo = ''; }
if($para==null){ $para = ''; }
if($cesa==null){ $cesa = ''; }
return $this->json([
"id" => $paciente->getId(),
"cedula" => $paciente->getCedula(),
"antecedentes_personales" => $antecedentes_personales,
"antecedentes_quirurgicos" => $antecedentes_quirurgicos,
"antecedentes_familiares" => $antecedentes_familiares,
"alcohol" => $alcohol,
"tabaco" => $tabaco,
"drogas" => $drogas,
"alergias" => $alergias,
"medicacion" => $medicacion,
"ago" => $ago,
"fum" => $fum,
"gesta" => $gesta,
"abo" => $abo,
"para" => $para,
"cesa" => $cesa,
]);
}
/**
* @Route("/topaz/firmar/{id}", name="paciente_topaz_firmar", methods={"GET","POST"})
*/
public function topaz_firmar(Request $request, Paciente $paciente): Response
{
$base64Image = $request->request->get('sigRawData');
if($base64Image=='Base64 String: '){
return $this->json(['result' => 'error']);
}
$base = base64_decode($base64Image);
file_put_contents($this->getParameter('brochures_directory').'/firma_'.$paciente->getCedula().'.png',$base);
return $this->json(['result' => 'ok']);
}
/**
* @Route("/guardar/{id}", name="paciente_hc_guardar", methods={"POST"})
*/
public function hc_guardar(Paciente $paciente, Request $request): Response
{
$brochureFile = $request->files->get('res_lab');
if ($brochureFile) {
$originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename ='hc_'.$paciente->getId().date("YmdHis");
//$newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
$newFilename = $safeFilename.'.'.$brochureFile->guessExtension();
// Move the file to the directory where brochures are stored
try {
$brochureFile->move(
$this->getParameter('brochures_directory'),
$newFilename
);
$paciente->setCopiaCedula($newFilename);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($paciente);
$entityManager->flush();
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
return $this->json(['estado' => 'ok']);
}
return $this->json(['estado' => 'no']);
}
/**
* @Route("/eliminar/{id}", name="paciente_hc_eliminar", methods={"GET"})
*/
public function hc_eliminar(Paciente $paciente, Request $request): Response
{
/*$filename = './uploads/laboratorio_externo/le_'.$agenda->getId().'.pdf';
$file_exists = file_exists($filename);//dd($file_exists);
if($file_exists){
unlink($filename);
}*/
$paciente->setCopiaCedula(null);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($paciente);
$entityManager->flush();
return $this->json(['estado' => 'ok']);
}
}