Commit 5e645f475a5bf9fb2d8b3f8b89f8a07b93a31856

Authored by Eduardo Santos
Committed by Eduardo Santos
1 parent 47190e25
Exists in master and in 1 other branch 3.1

Corrige exclusão de locais

src/Cacic/CommonBundle/Controller/LocalController.php
... ... @@ -136,7 +136,7 @@ class LocalController extends Controller
136 136 /**
137 137 * Primeiramente, remove as configurações aplicadas ao Local
138 138 */
139   - $this->getDoctrine()->getRepository('CacicCommonBundle:ConfiguracaoLocal')->removerConfiguracoesDoLocal( $local );
  139 + $this->getDoctrine()->getManager()->getRepository('CacicCommonBundle:Local')->excluirLocal( $local );
140 140  
141 141 $em->remove( $local ); // Tenta excluir o registro da base de dados
142 142 $em->flush();
... ... @@ -147,6 +147,7 @@ class LocalController extends Controller
147 147 }
148 148 catch ( \Doctrine\DBAL\DBALException $e )
149 149 {
  150 + $this->get('logger')->error("Erro na exclusão do local\n".$e->getMessage());
150 151 $out = array('status' => 'error', 'code' => false);
151 152 if ( preg_match('#SQLSTATE\[(\d+)\]#', $e->getMessage(), $tmp) )
152 153 $out['code'] = $tmp[1];
... ...
src/Cacic/CommonBundle/Entity/LocalRepository.php
... ... @@ -43,5 +43,73 @@ class LocalRepository extends EntityRepository
43 43  
44 44 return $this->getEntityManager()->createQuery( $_dql )->getArrayResult();
45 45 }
  46 +
  47 + /**
  48 + * Retorna o último local cadastrado
  49 + *
  50 + * @return mixed
  51 + */
  52 +
  53 + public function getMaxLocal($local) {
  54 + $_dql = "SELECT l
  55 + FROM CacicCommonBundle:Local l
  56 + WHERE l.idLocal <> :local
  57 + ORDER BY l.idLocal desc
  58 + ";
  59 +
  60 + return $this->getEntityManager()
  61 + ->createQuery( $_dql )
  62 + ->setMaxResults(1)
  63 + ->setParameter('local', $local)
  64 + ->getSingleResult();
  65 + }
  66 +
  67 + /**
  68 + * Move os usuários de local
  69 + *
  70 + * @param $local
  71 + */
  72 +
  73 + public function moveUsuarios($local) {
  74 + $em = $this->getEntityManager();
  75 + $usuarios = $local->getUsuarios();
  76 + $maxLocal = $em->getRepository('CacicCommonBundle:Local')->getMaxLocal($local->getIdLocal());
  77 + // Coloca cada usuário em no último local cadastrado
  78 + foreach ($usuarios as $modificar) {
  79 + $modificar->setIdLocal($maxLocal);
  80 + $em->persist($modificar);
  81 + }
  82 + $em->flush();
  83 + }
  84 +
  85 + /**
  86 + * Excluir local
  87 + *
  88 + * @param $local
  89 + */
  90 +
  91 + public function excluirLocal($local) {
  92 + $em = $this->getEntityManager();
  93 + $em->getRepository('CacicCommonBundle:ConfiguracaoLocal')->removerConfiguracoesDoLocal( $local );
  94 + $em->getRepository('CacicCommonBundle:Local')->moveUsuarios( $local );
  95 +
  96 + $config = $em->getRepository('CacicCommonBundle:PatrimonioConfigInterface')->findBy(array('local' => $local->getIdLocal()));
  97 + foreach ($config as $excluir) {
  98 + $em->remove($excluir);
  99 + }
  100 +
  101 + $config = $em->getRepository('CacicCommonBundle:UnidOrganizacionalNivel2')->findBy(array('idLocal' => $local->getIdLocal()));
  102 + foreach ($config as $excluir) {
  103 + $em->remove($excluir);
  104 + }
  105 +
  106 + $config = $em->getRepository('CacicCommonBundle:Rede')->findBy(array('idLocal' => $local->getIdLocal()));
  107 + foreach ($config as $excluir) {
  108 + $excluir->setIdLocal(null);
  109 + $em->persist($excluir);
  110 + }
  111 +
  112 + $em->flush();
  113 + }
46 114  
47 115 }
48 116 \ No newline at end of file
... ...