<?php 
namespace Noahtech\Sistemas\InterjamaBundle\Controller\Atencion; 
 
use Noahtech\Sistemas\InterjamaBundle\Utils\Codes; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\Annotation\Route; 
use Symfony\Component\HttpFoundation\Request; 
use Noahtech\Sistemas\InterjamaBundle\Controller\BaseController; 
use Noahtech\Sistemas\InterjamaBundle\Handler\McTurnoHandler; 
use Noahtech\Sistemas\InterjamaBundle\Handler\McChoferHandler; 
use Noahtech\Sistemas\InterjamaBundle\Handler\McClienteHandler; 
use Noahtech\Sistemas\InterjamaBundle\Handler\McVehiculoHandler; 
use Noahtech\Sistemas\InterjamaBundle\Handler\McPrecargaHandler; 
use Noahtech\Sistemas\InterjamaBundle\Handler\McTiposTramiteHandler; 
use Noahtech\Sistemas\InterjamaBundle\Handler\McCajaHandler; 
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 
use Noahtech\Sistemas\InterjamaBundle\Utils\Constants; 
use Noahtech\Sistemas\InterjamaBundle\Handler\McTurnoProximoHandler; 
 
class TurnoController extends BaseController { 
 
    private function validarAtencion () { 
        $usuario = $this->getUser(); 
        if ($usuario) { 
            $atencion = false; 
            foreach ($usuario->getRoles() as $rol) { 
                if ($rol == 'ROLE_ATENCION'){ 
                    $atencion = true; 
                } 
            } 
            if (!$atencion){ 
                throw new AccessDeniedException("No tiene permiso para acceder a esta pagina."); 
            } 
        } else { 
            throw new AccessDeniedException("No tiene permiso para acceder a esta pagina."); 
        } 
    } 
     
    /** 
     * @Route("/atencion/turnos", name="atencion_turnos_listado") 
     * @return type 
     */ 
    public function turnoListadoAction() { 
        $this->validarAtencion(); 
        $usuario = $this->getUser();                
        $this->setTitle("Listado de turnos | InterJama"); 
        $this->addBreadCrumb("Inicio - Atención", false, "atencion_home"); 
        $this->addBreadCrumb("Listado de turnos", true);         
        $this->data['data'] = null; 
        return $this->render( 
                        '@NoahtechSistemasInterjama/atencion/turnos/listado.html.twig', $this->data 
        ); 
    } 
 
    /** 
     * consulta de turnos 
     * 
     * @Route("/atencion/ajax/turnos/search", name="atencion_ajax_turnos_listado", methods={"POST"}, condition="request.isXmlHttpRequest()") 
     */ 
    public function searchAction(Request $request) { 
        $this->validarAtencion(); 
        $searchParam = $request->request->all();         
        $usuario = $this->getUser();                
        $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 McTurnoHandler $handler */ 
            $handler = $this->get(McTurnoHandler::class); 
            $lp = $handler->search($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("/atencion/turno/nuevo", name="atencion_turno_nuevo") 
     * @return type 
     */ 
    public function turnoNuevoAction() { 
        $this->validarAtencion(); 
        $this->setTitle("Nuevo Turno"); 
        $this->addBreadCrumb("Inicio - Atención", false, "atencion_home");         
        $this->addBreadCrumb("Nuevo Turno", true);         
        $this->data['data'] = null; 
        return $this->render( 
                        '@NoahtechSistemasInterjama/atencion/turnos/nuevo.html.twig', $this->data 
        ); 
    } 
 
    /** 
     * Guarda un turno 
     * 
     * @Route("/atencion/ajax/turnos/nuevo", name="atencion_turnos_save", methods={"POST"}, condition="request.isXmlHttpRequest()") 
     */ 
    public function postTurnoSaveAction(Request $request) { 
        $this->validarAtencion(); 
        $handler = $this->get(McTurnoHandler::class); 
        $turno = $handler->getTurnoFromRequest($request); 
        $result = $handler->save($turno); 
        $this->response->setData($result); 
        $this->response->setCode(Codes::OK); 
        $serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json'); 
        return new Response($serializedEntity); 
    } 
 
    /** 
     * @Route("/atencion/turnero", name="atencion_turnero") 
     * @return type 
     */ 
    public function turneroAction(Request $request) { 
        $this->validarAtencion(); 
        $this->setTitle(""); 
        $this->data['data'] = null; 
        return $this->render( 
                        '@NoahtechSistemasInterjama/atencion/turnos/turnero.html.twig', $this->data 
        ); 
    } 
 
    /** 
     * Guardar el turno actual 
     * 
     * @Route("/atencion/ajax/turno/proximo", name="atencion_turno_proximo_save", methods={"POST"}, condition="request.isXmlHttpRequest()") 
     */ 
    public function postTurnoProximoSaveAction(Request $request) {         
        $this->validarAtencion();         
        $handler = $this->get(McTurnoProximoHandler::class); 
        $id = 1; 
        $turnoProximo = $handler->getTurnoProximoFromRequest($request, $id);       
        $result = $handler->save($turnoProximo); 
        $this->response->setData($result); 
        $this->response->setCode(Codes::OK); 
        $serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json'); 
        return new Response($serializedEntity); 
    } 
 
    /** 
     * Devuelve el turno actual 
     * 
     * @Route("/atencion/ajax/turno/actual/{id}", name="atencion_ajax_turno_actual_get_by_id", methods={"GET"}, condition="request.isXmlHttpRequest()") 
     */ 
    public function getTurnoActualByIdAction($id) { 
        $this->validarAtencion(); 
        try { 
            $handler = $this->get(McTurnoProximoHandler::class); 
            $turnoActual = $handler->getById($id); 
            $this->response->setData($turnoActual); 
            $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 choferes 
     * 
     * @Route("/atencion/ajax/search/choferes/Bytermino", name="atencion_ajax_search_choferes_by_termino", methods={"GET"}, condition="request.isXmlHttpRequest()") 
     */ 
    public function searchChoferesByTerminoAction(Request $request) { 
        $this->validarAtencion(); 
        $searchParam = $request->query->get('q'); 
        try { 
            $handler = $this->get(McChoferHandler::class); 
            $choferes = $handler->searchChoferesByTermino($searchParam); 
            $this->response->setData($choferes); 
            $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 todos los clientes 
     * 
     * @Route("/atencion/ajax/clientes/all", name="atencion_ajax_clientes_all", methods={"GET"}, condition="request.isXmlHttpRequest()") 
     */ 
    public function searchClientesAction() { 
        $this->validarAtencion(); 
        $userCurrent = $this->getUser(); 
        try { 
            $handler = $this->get(McClienteHandler::class); 
            $choferes = $handler->getClientes(); 
            $this->response->setData($choferes); 
            $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("/atencion/turno/{turnoId}/precarga/nueva", name="atencion_turno_precarga_nueva") 
     * @return type 
     */ 
    public function precargaNuevoAction($turnoId) { 
        $this->validarAtencion(); 
        $this->setTitle("Nuevo ingreso"); 
        $this->addBreadCrumb("Inicio - Atención", false, "atencion_home"); 
        $this->addBreadCrumb("Turnos", false, "atencion_turnos_listado");         
        $this->addBreadCrumb("Nuevo trámite", true); 
        $turno = $this->get(McTurnoHandler::class)->getById($turnoId); 
        $clientes = $this->get(McClienteHandler::class)->getClientes(); 
        $tiposTramite = $this->get(McTiposTramiteHandler::class)->getTiposTramite(); 
        $caja = $this->get(McCajaHandler::class)->getCajaAbierta('operador'); 
        $this->data['turno'] = $turno; 
        $this->data['clientes'] = $clientes; 
        $this->data['tiposTramite'] = $tiposTramite; 
        $this->data['caja'] = $caja; 
        return $this->render( 
                        '@NoahtechSistemasInterjama/atencion/precargas/nueva.html.twig', $this->data 
        ); 
    } 
 
    /** 
     * Devuelve los vehiculos de un cliente 
     * 
     * @Route("/atencion/ajax/cliente/{clienteId}/search/vehiculos/Bytermino", name="atencion_ajax_search_vehiculos_by_termino", methods={"GET"}, condition="request.isXmlHttpRequest()") 
     */ 
    public function searchVehiculoByTerminoAction(Request $request, $clienteId) { 
        $this->validarAtencion(); 
        $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); 
    } 
 
    /** 
     * Guarda una precarga 
     * 
     * @Route("/atencion/ajax/precargas", name="atencion_ajax_precargas_save", methods={"POST"}, condition="request.isXmlHttpRequest()") 
     */ 
    public function postPrecargaSaveAction(Request $request) { 
        $this->validarAtencion(); 
        $archivos = (isset($_FILES)) ? $_FILES : null; 
        try { 
            $handler = $this->get(McPrecargaHandler::class); 
            $usuario = $this->getUser(); 
            $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); 
    } 
 
    /** 
     * Guarda un vehículo 
     * 
     * @Route("/atencion/ajax/vehiculos", name="atencion_ajax_vehiculo_save", methods={"POST"}, condition="request.isXmlHttpRequest()") 
     */ 
    public function vehiculoSaveAction(Request $request) { 
        $this->validarAtencion(); 
        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);        
    } 
}