<?php
namespace Noahtech\Sistemas\InterjamaBundle\Controller;
use Noahtech\Sistemas\InterjamaBundle\Handler\McUsuarioHandler;
use Noahtech\Sistemas\InterjamaBundle\Handler\McClienteHandler;
use Noahtech\Sistemas\InterjamaBundle\Handler\McChoferHandler;
use Noahtech\Sistemas\InterjamaBundle\Handler\McPrecargaHandler;
use Noahtech\Sistemas\InterjamaBundle\Handler\McVehiculoHandler;
use Noahtech\Sistemas\InterjamaBundle\Handler\McCategoriaHandler;
use Noahtech\Sistemas\InterjamaBundle\Handler\McContactoHandler;
use Noahtech\Sistemas\InterjamaBundle\Utils\Codes;
use Exception;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\HttpException;
class PublicUserController extends BaseController {
private function validarChofer ($tipo, $numero, $id=null) {
$chofer = $this->get(McChoferHandler::class)->getChoferDuplicate($tipo, $numero, $id);
if (!is_null($chofer)) {
throw new HttpException(409, "Ya existe un chofer con el tipo y número de documento ingresados.");
}
}
private function validarSiEsChofer($choferId) {
$chofer = $this->get(McChoferHandler::class)->getChoferById($choferId);
if (is_null($chofer)) {
throw new HttpException(409, "No tiene permisos suficientes para ingresar a esta pagina.");
}
}
private function validarPrecarga($id, $choferId) {
$precarga = $this->get(McPrecargaHandler::class)->getPrecargaByChofer($id, $choferId);
if (count($precarga) == 0) {
throw new AccessDeniedException("No tiene permiso para acceder a esta pagina.");
}
}
private function validarPrecargasSinFinalizar($choferId) {
$precarcasSinFinalizar = $this->get(McprecargaHandler::class)->getPrecargasByChoferSinFinalizar($choferId);
if (count($precarcasSinFinalizar) !== 0) {
throw new AccessDeniedException("Tiene registrada una precarga sin finalizar.");
}
}
/**
* @Route("user/public/generar_clave/{token}", name="public_nueva_clave")
* @return type
*/
public function generarClaveAction($token) {
$this->setTitle("Generación de contraseña");
$band = true;
$user = $this->get(McUsuarioHandler::class)->getDataPublicUser($token, $band);
$this->data["data"] = $user;
$this->response->setData($this->data);
return $this->render(
'@NoahtechSistemasInterjama/public/usuarios/generar_clave.html.twig', $this->data
);
}
/**
* @Route("user/public/recuperar_clave", name="public_recuperar_clave")
* @return type
*/
public function recuperarClaveAction() {
$this->setTitle("Recuperar contraseña");
$this->data["data"] = null;
return $this->render(
'@NoahtechSistemasInterjama/public/usuarios/recuperar_clave.html.twig', $this->data
);
}
/**
* @Route("user/public/recuperar_clave_update/{token}", name="public_recuperar_clave_update")
* @return type
*/
public function recuperarClaveUpdateAction($token) {
$this->setTitle("Recuperación de contraseña");
$band = false;
$user = $this->get(McUsuarioHandler::class)->getDataPublicUser($token, $band);
$this->data["data"] = $user;
$this->response->setData($this->data);
return $this->render(
'@NoahtechSistemasInterjama/public/usuarios/generar_clave.html.twig', $this->data
);
}
/**
* Modifica el password de un usuario
*
* @Route("user/public/ajax/usuarios/clave", name="public_ajax_usuario_clave_update", methods={"PUT"}, condition="request.isXmlHttpRequest()")
*/
public function putClaveUpdateAction(Request $request) {
$data = $request->request->all();
//En este caso no hay usuario logueado, porque esta acción se realiza sin ingreso al sistema.
try {
$currentPersona = $this->get(McUsuarioHandler::class)->getById($data["id"]);
$currentUser = $this->get(McUsuarioHandler::class)->getUsuarioByPersona($currentPersona["persona"]["id"]);
$result = $this->container->get(McUsuarioHandler::class)->updateClave($data, $currentUser);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode($e->getCode());
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* @Route("user/public/ajax/usuario/recuperar_clave_email", name="public_ajax_usuario_recuperar_clave_email", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function recuperarClaveEmailAction(Request $request) {
$cuil = $request->request->get('cuil');
try {
$result = $this->container->get(McUsuarioHandler::class)->recuperarClaveEmail($request, $cuil);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode($e->getStatusCode());
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* @Route("/", name="public_inicio")
* @return type
*/
public function inicioAction() {
$this->setTitle("Interjama | Home");
$this->data['title'] = "Interjama | Home";
$this->data['data'] = null;
return $this->render('@NoahtechSistemasInterjama/public/home/inicio.html.twig', $this->data);
}
/**
* @Route("public/chofer", name="public_chofer_inicio")
* @return type
*/
public function publicChoferAction() {
$this->setTitle("Interjama | Chofer");
$clientes = $this->get(McClienteHandler::class)->getClientes();
$this->data["data"] = $clientes;
return $this->render(
'@NoahtechSistemasInterjama/public/chofer/inicio.html.twig', $this->data
);
}
/**
* @Route("public/ajax/chofer/search", name="public_ajax_chofer_search", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function searchChoferAction(Request $request) {
$data = $request->request->all();
try {
$result = $this->container->get(McChoferHandler::class)->serachChofer($data);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* Guarda un chofer
*
* @Route("public/ajax/chofer", name="public_chofer_save", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function postSaveAction(Request $request) {
$this->validarChofer($request->request->get('tipo'), $request->request->get('numero'));
try {
$handler = $this->get(McChoferHandler::class);
$chofer = $handler->getChoferFromRequest($request);
$result = $handler->save($chofer);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* Modifica un chofer
*
* @Route("public/ajax/chofer/{id}", name="public_chofer_update", methods={"PUT"}, condition="request.isXmlHttpRequest()")
*/
public function putSaveAction(Request $request, $id) {
$this->validarChofer($request->request->get('tipo'), $request->request->get('numero'), (int)$id);
try {
$handler = $this->get(McChoferHandler::class);
$chofer = $handler->getChoferFromRequest($request, (int)$id);
$result = $handler->update($chofer);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/** SECCION PRECARGAS */
/**
* @Route("public/chofer/{choferId}/precargas", name="public_chofer_precargas_listado")
* @return type
*/
public function precargaListadoAction($choferId) {
$this->validarSiEsChofer((int)$choferId);
$this->setTitle("Bandeja de entrada | InterJama");
$clientes = $this->get(McClienteHandler::class)->getClientes();
$precarcasSinFinalizar = $this->get(McprecargaHandler::class)->getPrecargasByChoferSinFinalizar($choferId);
$this->data['data'] = null;
$this->data['chofer'] = $choferId;
$this->data['precargasSinFinalizar'] = COUNT($precarcasSinFinalizar) == 0;
return $this->render( '@NoahtechSistemasInterjama/public/chofer/precargas/listado.html.twig', $this->data );
}
/**
* consulta de precarga
*
* @Route("public/ajax/precargas/search", name="public_ajax_precargas_listado", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function searchAction(Request $request) {
$searchParam = $request->request->all();
$currentPage = $request->query->get('page');
$sortField = $request->query->get('sort');
$sortDirection = $request->query->get('direction');
$currentPage = null == $currentPage ? 1 : $currentPage;
$offset = ($currentPage - 1) * 100;
$limit = 100;
try {
/** @var McPrecargaHandler $handler */
$handler = $this->get(McPrecargaHandler::class);
$lp = $handler->searchPrecargasByChofer($offset, $limit, $sortField, $sortDirection, $searchParam);
$lp->setCurrentPage($currentPage);
$lp->setPageSize(100);
$this->response->setData($lp);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* @Route("public/chofer/{choferId}/precarga/nueva", name="public_chofer_precarga_nueva")
* @return type
*/
public function precargaNuevoAction($choferId) {
$this->validarSiEsChofer((int)$choferId);
$this->setTitle("Nueva Entrada");
$chofer = $this->get(McChoferHandler::class)->getById((int)$choferId);
$this->data['data'] = null;
$this->data['chofer'] = $chofer;
return $this->render(
'@NoahtechSistemasInterjama/public/chofer/precargas/nueva.html.twig', $this->data
);
}
/**
* Guarda una precarga
*
* @Route("public/chofer/{choferId}/ajax/precargas", name="public_chofer_ajax_precargas_save", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function postPrecargaSaveAction(Request $request, $choferId) {
$this->validarSiEsChofer((int)$choferId);
$this->validarPrecargasSinFinalizar((int)$choferId);
$archivos = (isset($_FILES)) ? $_FILES : null;
try {
$handler = $this->get(McPrecargaHandler::class);
$usuario = null;
$precarga = $handler->getPrecargaFromRequest($request, $usuario);
$result = $handler->save($precarga, $archivos, $request);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* @Route("public/chofer/{choferId}/precargas/{id}", name="public_chofer_precargas_modificacion")
* @return type
*/
public function precargaModificacionAction($choferId, $id) {
$this->validarSiEsChofer((int)$choferId);
$this->validarPrecarga($id, (int)$choferId);
$this->setTitle("Modificar Entrada");
$chofer = $this->get(McChoferHandler::class)->getById((int)$choferId);
$this->data['data'] = $id;
$this->data['chofer'] = $chofer;
return $this->render(
'@NoahtechSistemasInterjama/public/chofer/precargas/modificacion.html.twig', $this->data
);
}
/**
* Devuelve una precarga
*
* @Route("public/chofer/{choferId}/ajax/precargas/{id}", name="public_chofer_ajax_precarga_get_by_id", methods={"GET"}, condition="request.isXmlHttpRequest()")
*/
public function getPrecargaByIdAction($choferId, $id) {
$this->validarSiEsChofer((int)$choferId);
try {
$handler = $this->get(McPrecargaHandler::class);
$precarga = $handler->getById($id);
$this->response->setData($precarga);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* Modifica una precarga
*
* @Route("public/chofer/{choferId}/ajax/precargas/{id}", name="public_chofer_ajax_precarga_update", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function putUpdateAction(Request $request, $id, $choferId) {
$this->validarSiEsChofer((int)$choferId);
$archivos = (isset($_FILES)) ? $_FILES : null;
try {
$handler = $this->get(McPrecargaHandler::class);
$usuario = null;
$precarga = $handler->getPrecargaFromRequest($request, $usuario, $id);
$result = $handler->update($precarga, $archivos, $request, $id);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* Devuelve los vehiculos de un cliente
*
* @Route("public/ajax/cliente/{clienteId}/vehiculos/search/By/Termino", name="public_ajax_cliente_search_vehiculo_by_termino", methods={"GET"}, condition="request.isXmlHttpRequest()")
*/
public function searchVehiculoByTerminoAction(Request $request, $clienteId) {
$searchParam = $request->query->get('q');
try {
$handler = $this->get(McVehiculoHandler::class);
$vehiculos = $handler->searchVehiculosByTermino($searchParam, (int)$clienteId);
$this->response->setData($vehiculos);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* Devuelve todas las categorias de vehiculos
*
* @Route("public/ajax/categorias/all", name="public_ajax_get_all_categorias", methods={"GET"}, condition="request.isXmlHttpRequest()")
*/
public function getAllCategoriasAction(Request $request) {
try {
$handler = $this->get(McCategoriaHandler::class);
$categorias = $handler->getCategorias();
$this->response->setData($categorias);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* consulta de un vehículo por dominio principal
*
* @Route("public/ajax/vehiculo/dominio/{dominio}/exists", name="public_ajax_vehiculo_dominio_exists", methods={"GET"}, condition="request.isXmlHttpRequest()")
*/
public function dominioExistsAction($dominio) {
try {
$handler = $this->get(McVehiculoHandler::class);
$vehiculo = $handler->getVehiculoByDominio($dominio);
$this->response->setData($vehiculo);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* Guarda un vehículo
*
* @Route("public/ajax/vehiculos", name="public_ajax_vehiculo_save", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function vehiculoSaveAction(Request $request) {
try {
$handler = $this->get(McVehiculoHandler::class);
$vehiculo = $handler->getVehiculoFromRequest($request);
$result = $handler->save($vehiculo);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* Guarda un contacto
*
* @Route("public/ajax/contacto", name="public_ajax_contacto_save", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function contactoSaveAction(Request $request) {
try {
$handler = $this->get(McContactoHandler::class);
$contacto = $handler->getContactoFromRequest($request);
$result = $handler->save($contacto);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
}