/**
  * Se encarga de validar que el valor de los grados se encuentre dentro del rango
  */
 public function isValid($doctrine, $entidad, $invId, $productor)
 {
     $msg = array();
     if ($this->getLocalDetLitros() && $invId) {
         if ($this->getLocalDetLitros() + 0 <= 0) {
             $msg[] = '- La cantidad en litros ingresados "' . $this->getLocalDetLitros() . '" debe ser mayor a 0';
         } else {
             $solLocalDao = new SolLocalDao($doctrine);
             $solLocalDetDao = new SolLocalDetDao($doctrine);
             $inventarioDao = new InventarioDao($doctrine);
             $litrosInventario = $solLocalDetDao->getLitrosInventarioXCuota($entidad->getEntId(), $this->getCuota()->getCuoId());
             $litrosSolicitudesPendientes = $solLocalDao->getLitrosSolicitudXCuota($entidad->getEntId(), $this->getCuota()->getCuoId());
             $disponible = $this->getCuota()->getCuoLitros() - $litrosInventario - $litrosSolicitudesPendientes;
             if ($this->getLocalDetLitros() > $disponible) {
                 $msg[] = '- La cantidad en litros ingresados "' . $this->getLocalDetLitros() . '" es mayor al saldo disponible de la cuota "' . $disponible . '"';
             }
             if ($productor == false) {
                 $inventario = $inventarioDao->getInventario($invId);
                 $litrosDisponiblesProveedor = $inventario->getInvLitros() - $inventario->getInvReservado();
                 if ($this->getLocalDetLitros() > $litrosDisponiblesProveedor) {
                     $msg[] = '- No se puede ingresar la solicitud debido a que las existencias del proveedor no pueden cubrir la cantidad a solicitar';
                 }
             }
         }
     } else {
         if (!$this->getLocalDetLitros()) {
             $msg[] = '- El campo "Cantidad" se encuentra vacio';
         }
         if (!$invId && $productor == false) {
             $msg[] = '- Debe seleccionar un proveedor';
         }
     }
     return $msg;
 }
 /**
  * Actualiza el estado/transicion de la solicitud de local
  * 
  * pendiente revisar a que pagina se redirecciona de forma dinamica
  * pendiente extraer la nueva transicion a la que se actualizara la solicitud
  * pendiente validar si la solicitud no ha sido previamente cambiada de estado y se quiere volver a cambiar. (submit + back + submit again)
  * 
  * @param \Symfony\Component\HttpFoundation\Request $request
  * @param type $localDetId
  * @param type $traId
  * @return type
  */
 public function cambiarEstadoAction(Request $request, $localDetId, $traId)
 {
     $auditUser = $this->container->get('security.context')->getToken()->getUser();
     $errorList = '';
     $proseguir = false;
     $solLocalDao = new SolLocalDao($this->getDoctrine());
     $solLocalDetDao = new SolLocalDetDao($this->getDoctrine());
     $transicionDao = new TransicionDao($this->getDoctrine());
     $inventarioDetDao = new InventarioDetDao($this->getDoctrine());
     //Buscamos el encabezado para realizar la transicion
     $solLocalDet = new SolLocalDet();
     $solLocalDet = $solLocalDao->getSolLocalDet($localDetId);
     $invsDetTemp = $solLocalDet->getInventariosDet();
     foreach ($invsDetTemp as $tmp) {
         if ($solLocalDet->getSolLocal()->getEntidad()->getEntId() != $tmp->getInventario()->getEntidad()->getEntId()) {
             $inventarioProv = $tmp->getInventario();
         }
     }
     $roles = $auditUser->getRols();
     $nextTransiciones = $transicionDao->getTransicionesSiguientes($solLocalDet->getSolLocal()->getTransicion()->getTraId());
     //Validacion para asegurarse que el usuario que esta visualizando la solicitud tiene autorizacion para evaluarla y pasarla a la siguiente etapa
     foreach ($roles as $rol) {
         $transicionesRol = $rol->getTransiciones();
         foreach ($transicionesRol as $transicionRol) {
             foreach ($nextTransiciones as $reg) {
                 if ($reg->getFlujo()->getFluId() == Flujo::$LOCAL && $transicionRol->getTraId() == $reg->getTraId() && $traId == $reg->getTraId()) {
                     //Validacion de empresa que ingresa la solicitud
                     $entidad = $solLocalDet->getSolLocal()->getEntidad();
                     $year = new \DateTime();
                     $listadoDNMDao = new ListadoDNMDao($this->getDoctrine());
                     $autorizadoDNM = $listadoDNMDao->estaAutorizado($year->format('Y') + 0, $entidad->getEntNrc(), $entidad->getEntNit());
                     //Validacion de la empresa seleccionada como proveedor
                     $autorizadoDNMProv = true;
                     $inventarioDetTmp = $solLocalDet->getInventariosDet();
                     $inventarioDetTmp = $inventarioDetTmp[0];
                     $provEntidad = $inventarioDetTmp->getInventario()->getEntidad();
                     if ($provEntidad == null) {
                         $provEntidad = true;
                     } else {
                         $autorizadoDNM = $listadoDNMDao->estaAutorizado($year->format('Y') + 0, $provEntidad->getEntNrc(), $provEntidad->getEntNit());
                         $provEntidad = $autorizadoDNM && $provEntidad->getEntHabilitado();
                     }
                     if ($autorizadoDNM == true && $entidad->getEntHabilitado() == true && $provEntidad == true) {
                         if ($solLocalDet->getSolLocal()->getEntidad()->getEntId() == $auditUser->getEntidad()->getEntId() && $rol->getRolTipo() == User::$COMPRADOR || $inventarioProv->getEntidad()->getEntId() == $auditUser->getEntidad()->getEntId() && $rol->getRolTipo() == User::$VENDEDOR) {
                             $proseguir = true;
                         }
                         if ($proseguir == false) {
                             $errorList = $errorList . '#### AUDITORIA: Su usuario no tiene permisos para cambiar el estado de esta solicitud ####';
                         } else {
                             if ($reg->getTraComentario()) {
                                 $solLocalComentario = $request->get('solLocalComentario');
                                 if ($solLocalComentario == null || $solLocalComentario == '') {
                                     $errorList = $errorList . '- Es necesario detallar un comentario para pasar a la siguiente etapa';
                                 } else {
                                     $solLocalDet->getSolLocal()->setSolLocalComentario($solLocalComentario);
                                 }
                             }
                             if ($reg->getTraLitrosLibera() || $reg->getTraLiberaTotal()) {
                                 $localDetLitrosLib = $solLocalDet->getLocalDetLitrosLib();
                                 $localDetLitros = $solLocalDet->getLocalDetLitros();
                                 $litrosLib = $request->get('localDetLitrosLib');
                                 if ($reg->getTraLiberaTotal()) {
                                     $solLocalDet->setLocalDetLitrosLib($localDetLitros);
                                     $inventarioDet = $this->agregarInventario($solLocalDet->getCuota(), $localDetLitros - $localDetLitrosLib);
                                     $inventarioDet->setSolLocalDet($solLocalDet);
                                     $solLocalDet->addInventarioDet($inventarioDet);
                                     $inventarioDetProv = $this->agregarInventarioProveedor($solLocalDet, $inventarioProv->getInvId(), $localDetLitros - $localDetLitrosLib, $solLocalDet->getCuota()->getCuoGrado(), false, false);
                                     $inventarioDetProv->setSolLocalDet($solLocalDet);
                                     $inventarioDetProv->getSolLocalDet()->addInventarioDet($inventarioDet);
                                 } else {
                                     if ($reg->getTraLitrosLibera()) {
                                         try {
                                             $litrosLib = (double) $litrosLib;
                                             $localDetLitrosLib = (double) $localDetLitrosLib;
                                             $localDetLitros = (double) $localDetLitros;
                                             if ($litrosLib == null || $litrosLib == '') {
                                                 $errorList = $errorList . '- Debe ingresar los litros a liberar';
                                             } else {
                                                 if ($localDetLitros - $localDetLitrosLib - $litrosLib <= 0) {
                                                     $errorList = $errorList . '- La cantidad de litros liberados debe ser menor a la cantidad pendiente por liberar ' . ($localDetLitros - $localDetLitrosLib);
                                                 } else {
                                                     if ($litrosLib <= 0) {
                                                         $errorList = $errorList . '- Debe ingresar una cantidad mayor a 0';
                                                     } else {
                                                         $solLocalDet->setLocalDetLitrosLib($localDetLitrosLib + $litrosLib);
                                                         $inventarioDet = $this->agregarInventario($solLocalDet->getCuota(), $litrosLib);
                                                         $inventarioDet->setSolLocalDet($solLocalDet);
                                                         $solLocalDet->addInventarioDet($inventarioDet);
                                                         $inventarioDetProv = $this->agregarInventarioProveedor($solLocalDet, $inventarioProv->getInvId(), $litrosLib, $solLocalDet->getCuota()->getCuoGrado(), false, true);
                                                         $inventarioDetProv->setSolLocalDet($solLocalDet);
                                                         $inventarioDetProv->getSolLocalDet()->addInventarioDet($inventarioDet);
                                                     }
                                                 }
                                             }
                                         } catch (Exception $e) {
                                             $errorList = $errorList . '- Debe ingresar un número valido';
                                         }
                                     }
                                 }
                             }
                         }
                     } else {
                         $errorList = ' ';
                     }
                     if ($errorList == '') {
                         if ($reg->getEtpFin()->getEtpId() == Etapa::$FINALIZADA_OBS && ($reg->getEstado()->getEstId() == Estado::$CANCELADO || $reg->getEstado()->getEstId() == Estado::$RECHAZADO)) {
                             /*NOTA: Solo se busca el registro de inventario que esten en R (reserva) para eliminarse
                              * Los demás se asumen que si ya entraron a inventario no hay reversa 
                              */
                             $inventarioDetTmp = $inventarioDetDao->findInventarioDet($inventarioProv->getInvId(), $localDetId, 'R');
                             $inventarioDetTmp = $this->eliminarInventarioDetProveedorAction($inventarioDetTmp);
                         }
                         $solLocalDet->getSolLocal()->setTransicion($reg);
                         $solLocalDet->getSolLocal()->setAuditUserUpd($auditUser->getUsername());
                         $solLocalDet->getSolLocal()->setAuditDateUpd(new \DateTime());
                         $solLocalDetDao->editSolLocalDet($solLocalDet);
                         $this->generarEmailEtapaNotificacion($solLocalDet, $reg, $inventarioProv->getEntidad()->getEntId());
                         $this->get('session')->setFlash('notice', '#### El registro paso a etapa "' . $reg->getEtpFin()->getEtpNombre() . '" con estado "' . $reg->getEstado()->getEstNombre() . '" ####');
                         return $this->redirect($this->generateUrl('MinSalSCAProcesosBundle_mantSolLocalVerSolicitudes'));
                     }
                 }
             }
         }
     }
     if ($errorList == '') {
         $this->get('session')->setFlash('notice', '#### AUDITORIA: Su usuario no tiene permisos para cambiar el estado en esta etapa "' . $solLocalDet->getSolLocal()->getTransicion()->getEtpFin()->getEtpNombre() . '" ####');
     } else {
         $this->get('session')->setFlash('notice', $errorList);
     }
     return $this->redirect($this->generateUrl('MinSalSCAProcesosBundle_mantCargarSolLocal', array('localDetId' => $localDetId)));
 }