src/Noahtech/Sistemas/InterjamaBundle/Controller/CajaController.php line 255

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