src/Noahtech/Sistemas/InterjamaBundle/Controller/Atencion/CajaController.php line 122

Open in your IDE?
  1. <?php
  2. namespace Noahtech\Sistemas\InterjamaBundle\Controller\Atencion;
  3. use Noahtech\Sistemas\InterjamaBundle\Utils\Codes;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Noahtech\Sistemas\InterjamaBundle\Controller\BaseController;
  8. use Noahtech\Sistemas\InterjamaBundle\Handler\McCajaHandler;
  9. use Noahtech\Sistemas\InterjamaBundle\Handler\McMonedaHandler;
  10. use Noahtech\Sistemas\InterjamaBundle\Handler\McMovimientoHandler;
  11. use Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler;
  12. use Noahtech\Sistemas\InterjamaBundle\Handler\McChoferHandler;
  13. use Noahtech\Sistemas\InterjamaBundle\Handler\McClienteHandler;
  14. use Noahtech\Sistemas\InterjamaBundle\Handler\McDivisaHandler;
  15. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  16. use Noahtech\Sistemas\InterjamaBundle\Utils\Constants;
  17. class CajaController extends BaseController {
  18.     private function validarAtencion () {
  19.         $usuario $this->getUser();
  20.         if ($usuario) {
  21.             $atencion false;
  22.             foreach ($usuario->getRoles() as $rol) {
  23.                 if ($rol == 'ROLE_ATENCION'){
  24.                     $atencion true;
  25.                 }
  26.             }
  27.             if (!$atencion){
  28.                 throw new AccessDeniedException("No tiene permiso para acceder a esta pagina.");
  29.             }
  30.         } else {
  31.             throw new AccessDeniedException("No tiene permiso para acceder a esta pagina.");
  32.         }
  33.     }
  34.     private function validarCajaCerrada($cajaId) {        
  35.         $caja $this->get(McCajaHandler::class)->getCajaById($cajaId);
  36.         if ($caja) {
  37.             if ($caja->getCerrado()) {
  38.                 throw new AccessDeniedException("No tiene permiso para acceder a esta pagina.");
  39.             }
  40.         } else {
  41.             throw new AccessDeniedException("No tiene permiso para acceder a esta pagina."); 
  42.         }        
  43.     }
  44.     
  45.     private function validarDescargaCaja($cajaId) {
  46.         $caja $this->get(McCajaHandler::class)->getCajaById($cajaId);
  47.         if ($caja) {
  48.             if (!$caja->getCerrado()) {
  49.                 throw new AccessDeniedException("No tiene permiso para acceder a esta pagina.");
  50.             }
  51.         } else {
  52.             throw new AccessDeniedException("No tiene permiso para acceder a esta pagina."); 
  53.         }
  54.     }
  55.     private function validarChofer ($tipo$numero$id=null) {
  56.         $chofer $this->get(McChoferHandler::class)->getChoferDuplicate($tipo$numero$id);
  57.         if (!is_null($chofer)) {
  58.             throw new HttpException(409"Ya existe un chofer con el tipo y número de documento ingresados.");
  59.         }
  60.     }
  61.     private function validarMovimiento($cajaId$movimientoId) {
  62.         $movimiento $this->get(McMovimientoHandler::class)->getMovimientoById($movimientoId);
  63.         if ($movimiento) {
  64.             if ($movimiento->getCaja()->getId() != (int)$cajaId) {
  65.                 throw new AccessDeniedException("No tiene permiso para acceder a esta pagina.");
  66.             }
  67.         } else {
  68.             throw new AccessDeniedException("No tiene permiso para acceder a esta pagina."); 
  69.         }
  70.     }
  71.     /**
  72.      * @Route("/atencion/cajas", name="atencion_cajas_listado")
  73.      * @return type
  74.      */
  75.     public function cajasListadoAction() {
  76.         $this->validarAtencion();
  77.         $usuario $this->getUser();               
  78.         $this->setTitle("Listado de cajas | InterJama");
  79.         $this->addBreadCrumb("Inicio - Atencion"false"atencion_home");
  80.         $this->addBreadCrumb("Listado de cajas"true);
  81.         $this->data['data'] = null;
  82.         return $this->render(
  83.                         '@NoahtechSistemasInterjama/atencion/cajas/listado.html.twig'$this->data
  84.         );
  85.     }
  86.     /**
  87.      * consulta de cajas
  88.      *
  89.      * @Route("/atencion/ajax/cajas/search", name="atencion_ajax_cajas_listado", methods={"POST"}, condition="request.isXmlHttpRequest()")
  90.      */
  91.     public function searchAction(Request $request) {
  92.         $this->validarAtencion();
  93.         $searchParam $request->request->all();        
  94.         $usuario $this->getUser();
  95.         $searchParam['tipo'] = 'operador';           
  96.         $currentPage $request->query->get('page');
  97.         $sortField $request->query->get('sort');
  98.         $sortDirection $request->query->get('direction');
  99.         $currentPage null == $currentPage $currentPage;
  100.         $offset = ($currentPage 1) * 100;
  101.         $limit 100;
  102.         try {
  103.             /** @var McCajaHandler $handler */
  104.             $handler $this->get(McCajaHandler::class);
  105.             $lp $handler->search($offset$limit$sortField$sortDirection$searchParam);
  106.             $lp->setCurrentPage($currentPage);
  107.             $lp->setPageSize(100);
  108.             $this->response->setData($lp);
  109.             $this->response->setCode(Codes::OK);
  110.         } catch (Exception $e) {
  111.             $this->response->setCode(Codes::ERROR);
  112.             $this->response->setMessage($e->getMessage());
  113.         }
  114.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  115.         return new Response($serializedEntity);
  116.     }
  117.     /**
  118.      * @Route("/atencion/cajas/nueva", name="atencion_cajas_nueva")
  119.      * @return type
  120.      */
  121.     public function cajaNuevoAction() {
  122.         $this->validarAtencion();
  123.         $this->setTitle("Nueva Caja");
  124.         $this->addBreadCrumb("Inicio - Atención "false"atencion_home");
  125.         $this->addBreadCrumb("Listado de cajas"false"atencion_cajas_listado");
  126.         $this->addBreadCrumb("Nueva caja"true);     
  127.         $monedas $this->get(McMonedaHandler::class)->getAllMonedas();
  128.         $this->data['data'] = null;
  129.         $this->data['monedas'] = $monedas;       
  130.         return $this->render(
  131.                         '@NoahtechSistemasInterjama/atencion/cajas/nueva.html.twig'$this->data
  132.         );
  133.     }
  134.     /**
  135.      * Guarda una caja de operador
  136.      *
  137.      * @Route("/atencion/ajax/cajas", name="atencion_caja_save", methods={"POST"}, condition="request.isXmlHttpRequest()")
  138.      */
  139.     public function postSaveAction(Request $request) {
  140.         $this->validarAtencion();
  141.         $operadorOpen $this->getUser();
  142.         try {
  143.             $handler $this->get(McCajaHandler::class);            
  144.             $caja $handler->getCajaFromRequest($request$operadorOpen'operador');
  145.             $result $handler->save($request$caja);
  146.             $this->response->setData($result);
  147.             $this->response->setCode(Codes::OK);            
  148.         } catch (Exception $e) {
  149.             $this->response->setCode(Codes::ERROR);
  150.             $this->response->setMessage($e->getMessage());
  151.         }
  152.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  153.         return new Response($serializedEntity);       
  154.     }
  155.     /**
  156.      * @Route("/atencion/cajas/{id}", name="atencion_cajas_modificacion")
  157.      * @return type
  158.      */
  159.     public function cajaModificacionAction($id) {
  160.         $this->validarAtencion();
  161.         $this->validarCajaCerrada($id);
  162.         $this->setTitle("Modificar caja");
  163.         $this->addBreadCrumb("Inicio - Atención "false"atencion_home");
  164.         $this->addBreadCrumb("Listado de cajas"false"atencion_cajas_listado");
  165.         $this->addBreadCrumb("Modificar caja"true);        
  166.         $monedas $this->get(McMonedaHandler::class)->getAllMonedas();
  167.         $this->data['data'] = $id;
  168.         $this->data['monedas'] = $monedas;
  169.         return $this->render(
  170.                         '@NoahtechSistemasInterjama/atencion/cajas/modificacion.html.twig'$this->data
  171.         );
  172.     }
  173.     /**
  174.      * Devuelve un caja
  175.      *
  176.      * @Route("/atencion/ajax/cajas/{id}", name="atencion_ajax_caja_get_by_id", methods={"GET"}, condition="request.isXmlHttpRequest()")
  177.      */
  178.     public function getcajaByIdAction($id) {
  179.         $this->validarAtencion();
  180.         try {
  181.             $handler $this->get(McCajaHandler::class);
  182.             $caja $handler->getById($id);
  183.             $this->response->setData($caja);
  184.             $this->response->setCode(Codes::OK);
  185.         } catch (Exception $e) {
  186.             $this->response->setCode(Codes::ERROR);
  187.             $this->response->setMessage($e->getMessage());
  188.         }
  189.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  190.         return new Response($serializedEntity);
  191.     }
  192.     /**
  193.      * Modifica una caja
  194.      *
  195.      * @Route("/atencion/ajax/cajas/{id}", name="atencion_ajax_caja_update", methods={"PUT"}, condition="request.isXmlHttpRequest()")
  196.      */
  197.     public function putUpdateAction(Request $request$id) {
  198.         $this->validarAtencion();
  199.         $this->validarCajaCerrada($id);
  200.         $data $request->request->all();
  201.         $operadorOpen $this->getUser();
  202.         try {
  203.             $handler $this->get(McCajaHandler::class);        
  204.             $caja $handler->getcajaFromRequest($request$operadorOpen'operador'$id);        
  205.             $result $handler->update($request$caja$id);
  206.             $this->response->setData($result);
  207.             $this->response->setCode(Codes::OK);
  208.         } catch (Exception $e) {
  209.             $this->response->setCode(Codes::ERROR);
  210.             $this->response->setMessage($e->getMessage());
  211.         }
  212.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  213.         return new Response($serializedEntity);
  214.     }
  215.     /**
  216.      * Modifica el estado de una caja
  217.      *
  218.      * @Route("/atencion/ajax/cajas/{id}/estado", name="atencion_ajax_caja_update_esatado", methods={"PUT"}, condition="request.isXmlHttpRequest()")
  219.      */
  220.     public function putUpdateEstadoAction(Request $request$id) {
  221.         $this->validarAtencion();
  222.         $data $request->request->all();
  223.         $operadorClose $this->getUser();
  224.         try {
  225.             $handler $this->get(McCajaHandler::class);        
  226.             $caja $handler->getCajaEstadoFromRequest($request$operadorClose'operador'$id);        
  227.             $result $handler->update($request$caja$id);
  228.             $this->response->setData($result);
  229.             $this->response->setCode(Codes::OK);
  230.         } catch (Exception $e) {
  231.             $this->response->setCode(Codes::ERROR);
  232.             $this->response->setMessage($e->getMessage());
  233.         }
  234.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  235.         return new Response($serializedEntity);
  236.     }
  237.     /**
  238.      * 
  239.      * Descarga recibo de caja cerrada
  240.      * 
  241.      * @Route("/atencion/ajax/cajas/{cajaId}/descarga", name="atencion_ajax_caja_recibo_descarga")
  242.      * 
  243.      */
  244.     public function descargaReciboCajaAction($cajaId) {
  245.         try { 
  246.            $this->validarAtencion();
  247.            $this->validarDescargaCaja($cajaId);
  248.            return $this->get(McCajaHandler::class)->descargaReciboCajaPdf($cajaId);
  249.         } catch (Exception $e) {
  250.            return $e->getMessage();
  251.         }
  252.    }
  253.    /** Sección moviemientos */
  254.    /**
  255.      * @Route("/atencion/caja/{cajaId}/movimientos", name="atencion_caja_movimientos_listado")
  256.      * @return type
  257.      */
  258.     public function movimientosListadoAction($cajaId) {
  259.         $this->validarAtencion();
  260.         $usuario $this->getUser();
  261.         $this->validarCajaCerrada($cajaId);
  262.         $this->setTitle("Listado de movimientos | InterJama");
  263.         $this->addBreadCrumb("Inicio - Atención"false"atencion_home");
  264.         $this->addBreadCrumb("Listado de cajas"false"atencion_cajas_listado");
  265.         $this->addBreadCrumb("Listado de movimientos"true);
  266.         $this->data['data'] = null;
  267.         $this->data['caja'] = $cajaId;
  268.         return $this->render(
  269.                         '@NoahtechSistemasInterjama/atencion/cajas/movimientos/listado.html.twig'$this->data
  270.         );
  271.     }
  272.     /**
  273.      * consulta de cajas
  274.      *
  275.      * @Route("/atencion/ajax/caja/{cajaId}/movimientos/search", name="atencion_ajax_caja_movimientos_listado", methods={"POST"}, condition="request.isXmlHttpRequest()")
  276.      */
  277.     public function searchMovimientosAction(Request $request$cajaId) {
  278.         $this->validarAtencion();
  279.         $searchParam $request->request->all();
  280.         $searchParam['caja'] = $cajaId;        
  281.         $usuario $this->getUser();             
  282.         $currentPage $request->query->get('page');
  283.         $sortField $request->query->get('sort');
  284.         $sortDirection $request->query->get('direction');
  285.         $currentPage null == $currentPage $currentPage;
  286.         $offset = ($currentPage 1) * 100;
  287.         $limit 100;
  288.         try {
  289.             /** @var McMovimientoHandler $handler */
  290.             $handler $this->get(McMovimientoHandler::class);
  291.             $lp $handler->search($offset$limit$sortField$sortDirection$searchParam);
  292.             $lp->setCurrentPage($currentPage);
  293.             $lp->setPageSize(100);
  294.             $this->response->setData($lp);
  295.             $this->response->setCode(Codes::OK);
  296.         } catch (Exception $e) {
  297.             $this->response->setCode(Codes::ERROR);
  298.             $this->response->setMessage($e->getMessage());
  299.         }
  300.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  301.         return new Response($serializedEntity);
  302.     }
  303.     /**
  304.      * @Route("/atencion/caja/{cajaId}/movimientos/nuevo", name="atencion_caja_movimientos_nuevo")
  305.      * @return type
  306.      */
  307.     public function movimientoNuevoAction($cajaId) {
  308.         $this->validarAtencion();
  309.         $this->validarCajaCerrada($cajaId);
  310.         $this->setTitle("Nuevo Movimiento");
  311.         $this->addBreadCrumb("Inicio - Atención"false"atencion_home");
  312.         $this->addBreadCrumb("Listado de cajas"false"atencion_cajas_listado");
  313.         $this->addBreadCrumb("Listado de movimientos"false"atencion_caja_movimientos_listado", array('cajaId' =>$cajaId));
  314.         $this->addBreadCrumb("Nuevo Movimiento"true);
  315.         $monedas $this->get(McCajaMonedaHandler::class)->getAllMonedasByCaja($cajaId);
  316.         $divisas $this->get(McDivisaHandler::class)->getAllDivisas();
  317.         $this->data['data'] = null;
  318.         $this->data['caja'] = $cajaId;
  319.         $this->data['monedas'] = $monedas;
  320.         $this->data['divisas'] = $divisas;
  321.         return $this->render(
  322.                         '@NoahtechSistemasInterjama/atencion/cajas/movimientos/nuevo.html.twig'$this->data
  323.         );
  324.     }
  325.     /**
  326.      * Guarda un movimiento de una caja
  327.      *
  328.      * @Route("/atencion/ajax/caja/{cajaId}/movimientos", name="atencion_caja_movimiento_save", methods={"POST"}, condition="request.isXmlHttpRequest()")
  329.      */
  330.     public function postMovimientoSaveAction(Request $request$cajaId) {
  331.         $this->validarAtencion();
  332.         $usuario $this->getUser();
  333.         $this->validarCajaCerrada($cajaId);
  334.         try {
  335.             $handler $this->get(McMovimientoHandler::class);
  336.             $result $handler->getMovimientoFromRequest($request$usuario$cajaId);
  337.             $this->response->setData($result);
  338.             $this->response->setCode(Codes::OK);            
  339.         } catch (Exception $e) {
  340.             $this->response->setCode(Codes::ERROR);
  341.             $this->response->setMessage($e->getMessage());
  342.         }
  343.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  344.         return new Response($serializedEntity);       
  345.     }
  346.     /**
  347.      * Devuelve un chofer segun el termino de busqueda
  348.      *
  349.      * @Route("/atencion/ajax/choferes/search/By/Termino", name="atencion_ajax_chofer_search_by_termino", methods={"GET"}, condition="request.isXmlHttpRequest()")
  350.      */
  351.     public function searchChoferByTerminoAction(Request $request) {
  352.         $this->validarAtencion();
  353.         $userCurrent $this->getUser();
  354.         $searchParam $request->query->get('q');
  355.         try {
  356.             $handler $this->get(McChoferHandler::class);
  357.             $choferes $handler->searchChoferesByTermino($searchParam);
  358.             $this->response->setData($choferes);
  359.             $this->response->setCode(Codes::OK);
  360.         } catch (Exception $e) {
  361.             $this->response->setCode(Codes::ERROR);
  362.             $this->response->setMessage($e->getMessage());
  363.         }
  364.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  365.         return new Response($serializedEntity);
  366.     }
  367.     /**
  368.      * Devuelve todos los clientes
  369.      *
  370.      * @Route("/atencion/ajax/clientes/all", name="atencion_ajax_clientes_all", methods={"GET"}, condition="request.isXmlHttpRequest()")
  371.      */
  372.     public function searchClientesAction() {
  373.         $this->validarAtencion();
  374.         $userCurrent $this->getUser();
  375.         try {
  376.             $handler $this->get(McClienteHandler::class);
  377.             $choferes $handler->getClientes();
  378.             $this->response->setData($choferes);
  379.             $this->response->setCode(Codes::OK);
  380.         } catch (Exception $e) {
  381.             $this->response->setCode(Codes::ERROR);
  382.             $this->response->setMessage($e->getMessage());
  383.         }
  384.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  385.         return new Response($serializedEntity);
  386.     }
  387.     /**
  388.      * Guarda un chofer
  389.      *
  390.      * @Route("/atencion/ajax/chofer", name="atencion_ajax_chofer_save", methods={"POST"}, condition="request.isXmlHttpRequest()")
  391.      */
  392.     public function registrarChoferAction(Request $request) {
  393.         $this->validarAtencion();
  394.         $this->validarChofer($request->request->get('tipo'), $request->request->get('numero'));
  395.         try {
  396.             $handler $this->get(McChoferHandler::class);            
  397.             $chofer $handler->getChoferFromRequest($request);
  398.             $result $handler->save($chofer);
  399.             $this->response->setData($result);
  400.             $this->response->setCode(Codes::OK);            
  401.         } catch (Exception $e) {
  402.             $this->response->setCode(Codes::ERROR);
  403.             $this->response->setMessage($e->getMessage());
  404.         }
  405.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  406.         return new Response($serializedEntity);       
  407.     }
  408.     /**
  409.      * @Route("/atencion/caja/{cajaId}/movimientos/{movimientoId}", name="atencion_caja_movimientos_modificacion")
  410.      * @return type
  411.      */
  412.     public function movimientoModificacionAction($cajaId$movimientoId) {
  413.         $this->validarAtencion();
  414.         $this->validarCajaCerrada($cajaId);
  415.         $this->validarMovimiento($cajaId$movimientoId);
  416.         $this->setTitle("Modificar movimiento");
  417.         $this->addBreadCrumb("Inicio - Atención"false"atencion_home");
  418.         $this->addBreadCrumb("Listado de cajas"false"atencion_cajas_listado");
  419.         $this->addBreadCrumb("Listado de movimientos"false"atencion_caja_movimientos_listado", array('cajaId' =>$cajaId));
  420.         $this->addBreadCrumb("Modificar movimiento"true);        
  421.         $monedas $this->get(McCajaMonedaHandler::class)->getAllMonedasByCaja($cajaId);
  422.         $this->data['data'] = $movimientoId;
  423.         $this->data['caja'] = $cajaId;
  424.         $this->data['monedas'] = $monedas;
  425.         return $this->render(
  426.                         '@NoahtechSistemasInterjama/atencion/cajas/movimientos/modificacion.html.twig'$this->data
  427.         );
  428.     }
  429.     /**
  430.      * Devuelve un movimiento
  431.      *
  432.      * @Route("/atencion/ajax/caja/{cajaId}/movimientos/{id}", name="atencion_ajax_caja_movimiento_get_by_id", methods={"GET"}, condition="request.isXmlHttpRequest()")
  433.      */
  434.     public function getMovimientoByIdAction($cajaId$id) {
  435.         $this->validarAtencion();
  436.         $this->validarCajaCerrada($cajaId);
  437.         $this->validarMovimiento($cajaId$id);
  438.         try {
  439.             $handler $this->get(McMovimientoHandler::class);
  440.             $movimiento $handler->getById($id);
  441.             $this->response->setData($movimiento);
  442.             $this->response->setCode(Codes::OK);
  443.         } catch (Exception $e) {
  444.             $this->response->setCode(Codes::ERROR);
  445.             $this->response->setMessage($e->getMessage());
  446.         }
  447.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  448.         return new Response($serializedEntity);
  449.     }
  450.     /**
  451.      * Edita un movimiento de una caja
  452.      *
  453.      * @Route("/atencion/ajax/caja/{cajaId}/movimientos/{movimientoId}", name="atencion_caja_movimiento_update", methods={"PUT"}, condition="request.isXmlHttpRequest()")
  454.      */
  455.     public function putMovimientoSaveAction(Request $request$cajaId$movimientoId) {
  456.         $this->validarAtencion();
  457.         $usuario $this->getUser();
  458.         $this->validarCajaCerrada($cajaId);
  459.         $this->validarMovimiento($cajaId$movimientoId);
  460.         try {
  461.             $handler $this->get(McMovimientoHandler::class);
  462.             $movimiento $handler->getMovimientoFromRequest($request$usuario$cajaId$movimientoId);
  463.             $result $handler->update($movimiento$movimientoId);
  464.             $this->response->setData($result);
  465.             $this->response->setCode(Codes::OK);            
  466.         } catch (Exception $e) {
  467.             $this->response->setCode(Codes::ERROR);
  468.             $this->response->setMessage($e->getMessage());
  469.         }
  470.         $serializedEntity $this->container->get('serializer')->serialize($this->response'json');
  471.         return new Response($serializedEntity);       
  472.     }
  473.    
  474. }