public function ImportarRegistro($Row)
 {
     $resultado = new ResultadoLote();
     //$resultado->Registros[] = $Row;
     if ($Row['INDIVIDUO_TIPO'] != 'PE' && $Row['INDIVIDUO_TIPO'] != 'PJ' && $Row['INDIVIDUO_TIPO'] != 'EN' && $Row['INDIVIDUO_TIPO'] != 'OT') {
         $resultado->RegistrosIgnorados++;
         return $resultado;
     }
     if ($Row['NOMBRE_IND'] != 'NRO.DE CUENTA CEMENTERIO' || $Row['NOMBRE_IND'] != 'NN' || SUBSTR($Row['NOMBRE_IND'], 0, 3) != '???') {
         $resultado->RegistrosIgnorados++;
         return $resultado;
     }
     if ((int) $Row['TRIBUTARIA_ID'] > 0 || (int) $Row['TRIBUTARIA_ID'] <= 9999) {
         $resultado->RegistrosIgnorados++;
         return $resultado;
     }
     $Documento = StringHelper::ObtenerDocumento($Row['TRIBUTARIA_ID']);
     $Apellido = StringHelper::Desoraclizar($Row['APELLIDOS']);
     $Nombre = StringHelper::Desoraclizar($Row['NOMBRES']);
     $RazonSocial = StringHelper::Desoraclizar($Row['RAZON_SOCIAL']);
     if (!$Nombre && !$Apellido && !$RazonSocial) {
         $resultado->RegistrosIgnorados++;
         return $resultado;
     }
     // echo ($Nombre .'/'. $Apellido .'/'. $RazonSocial);
     // echo "\n";
     $Row['TG06100_ID'] = (int) $Row['TG06100_ID'];
     $entity = $this->em->getRepository('YacareBaseBundle:Persona')->findOneBy(array('Tg06100Id' => $Row['TG06100_ID']));
     $Cuilt = '';
     if ($Documento[0] == 'CUIL' || $Documento[0] == 'CUIT') {
         $Cuilt = str_replace('-', '', $Documento[1]);
     }
     if ($Documento[0] == 'CUIL') {
         // Intento obtener el DNI del CUIL
         $Partes = explode('-', $Documento[1]);
         if (count($Partes) == 3) {
             $Documento[0] = 'DNI';
             $Documento[1] = (int) $Partes[1];
         }
     }
     if ($entity == null && $Cuilt) {
         $entity = $this->em->getRepository('YacareBaseBundle:Persona')->findOneBy(array('Cuilt' => $Cuilt));
     }
     if ($entity == null) {
         $entity = $this->em->getRepository('YacareBaseBundle:Persona')->findOneBy(array('DocumentoNumero' => $Documento[1]));
     }
     if ($entity == null) {
         $entity = new \Yacare\BaseBundle\Entity\Persona();
         if ($Row['TIPO_SOCIEDAD']) {
             switch ($Row['TIPO_SOCIEDAD']) {
                 case 'SA':
                     $entity->setTipoSociedad(1);
                     break;
                 case 'SRL':
                     $entity->setTipoSociedad(8);
                     break;
                 case 'SC':
                     $entity->setTipoSociedad(2);
                     break;
                 case 'SCA':
                     $entity->setTipoSociedad(4);
                     break;
                 case 'SH':
                     $entity->setTipoSociedad(3);
                     break;
                 case 'SD':
                     if (strpos($RazonSocial, 'S.A.') !== false || strpos($RazonSocial, ' SA') !== false) {
                         $entity->setTipoSociedad(1);
                     } elseif (strpos($RazonSocial, 'S.R.L.') !== false || strpos($RazonSocial, ' SRL') !== false) {
                         $entity->setTipoSociedad(8);
                     } elseif (strpos($RazonSocial, 'S/H') !== false || strpos($RazonSocial, ' S.DE H.') !== false || strpos($RazonSocial, ' S. DE H.') !== false || strpos($RazonSocial, ' S.H') !== false) {
                         $entity->setTipoSociedad(3);
                     }
                     break;
             }
         } elseif ($Row['INDIVIDUO_TIPO'] != 'EN') {
             $entity->setTipoSociedad(11);
         }
         if ($Documento[0] == 'CUIL' && (substr($Documento[1], 0, 3) == '30-' || substr($Documento[1], 0, 3) == '33-' || substr($Documento[1], 0, 3) == '34-')) {
             $Documento[0] = 'CUIT';
         }
         if (!$entity->getCuilt() && $Cuilt) {
             $entity->setCuilt($Cuilt);
         }
         if (!$entity->getDocumentoNumero()) {
             $entity->setDocumentoNumero($Documento[1]);
         }
         if (!$entity->getDocumentoTipo()) {
             $entity->setDocumentoTipo(1);
         }
         if (!$entity->getNombre()) {
             $entity->setNombre($Nombre);
         }
         if (!$entity->getApellido()) {
             $entity->setApellido($Apellido);
         }
         if (!$entity->getRazonSocial()) {
             $entity->setRazonSocial($RazonSocial);
         }
         if (!$entity->getDomicilioCodigoPostal()) {
             $entity->setDomicilioCodigoPostal('9420');
         }
         $CodigoCalle = $this->ArreglarCodigoCalle($Row['CODIGO_CALLE']);
         if ($CodigoCalle && !$entity->getDomicilioCalle()) {
             $Calle = $this->em->find('YacareCatastroBundle:Calle', $CodigoCalle);
             if ($Calle) {
                 $entity->setDomicilioCalle($Calle);
             }
         }
         if (!$entity->getDomicilioCalleNombre()) {
             $entity->setDomicilioCalleNombre(StringHelper::Desoraclizar($Row['CALLE']));
         }
         if (!$entity->getDomicilioNumero()) {
             $entity->setDomicilioNumero($Row['NUMERO']);
         }
         if (!$entity->getDomicilioPiso()) {
             $entity->setDomicilioPiso($Row['PISO']);
         }
         if (!$entity->getDomicilioPuerta()) {
             $entity->setDomicilioPuerta($Row['DEPTO']);
         }
         // Si no está en el grupo Contribuyentes, lo agrego
         // if ($entity->getGrupos()->contains($this->GrupoContribuyentes) == false) {
         // $entity->getGrupos()->add($this->GrupoContribuyentes);
         // }
         if ($Row['SEXO'] == 'F') {
             $entity->setGenero(2);
         } elseif ($Row['SEXO'] == 'M') {
             $entity->setGenero(1);
         }
         $entity->setTg06100Id($Row['TG06100_ID']);
         $resultado->RegistrosNuevos++;
     } else {
         $resultado->RegistrosActualizados++;
     }
     $entity->getNombreVisible();
     $this->em->persist($entity);
     $this->em->flush();
     return $resultado;
 }
 /**
  * @Route("personas/")
  * @Template("YacareMunirgBundle:Importar:importar.html.twig")
  */
 public function importarPersonasAction(Request $request, $desde = 0)
 {
     $desde = (int) $request->query->get('desde');
     $cant = 500;
     mb_internal_encoding('UTF-8');
     set_time_limit(600);
     ini_set('display_errors', 1);
     ini_set('memory_limit', '1024M');
     $TipoDocs = array('DNI' => 1, 'CF' => 1, 'LE' => 2, 'LC' => 3, 'CI' => 4, 'PAS' => 5, 'CUIL' => 98, 'CUIT' => 99);
     $Dbmunirg = $this->ConectarOracle();
     $em = $this->getDoctrine()->getManager();
     $em->getConnection()->beginTransaction();
     $GrupoContribuyentes = $em->getReference('YacareBaseBundle:PersonaGrupo', 3);
     $importar_importados = 0;
     $importar_actualizados = 0;
     $importar_procesados = 0;
     $log = array();
     $sql = "\n            SELECT * FROM (\n                SELECT a.*, ROWNUM rnum FROM (\n            SELECT\n                a.IND_LEYENDA,\n                a.IND_IDENTIFICACION,\n                a.NOMBRE,\n                a.TG06100_ID,\n                a.ALTA_FECHA,\n                a.TELEFONOS,\n                a.E_MAIL,\n                a.BAJA_FECHA,\n                a.BAJA_MOTIVO,\n                a.INDIVIDUO_TIPO,\n                a.TG06300_TG06300_ID,\n                a.TR02100_TR02100_ID,\n                a.TRIBUTARIA_ID,\n                p.APELLIDOS Q_APELLIDOS,\n                p.NOMBRES Q_NOMBRES,\n                p.SEXO Q_SEXO,\n                p.NACIMIENTO_FECHA Q_NACIMIENTO_FECHA,\n                p.NACIMIENTO_LUGAR Q_NACIMIENTO_LUGAR,\n                p.NACIONALIDAD Q_NACIONALIDAD,\n                j.RAZON_SOCIAL J_RAZON_SOCIAL,\n                j.TIPO_SOCIEDAD J_TIPO_SOCIEDAD,\n                j.NOMBRE_FANTASIA J_NOMBRE_FANTASIA,\n                j.IDENTIFICACION_TRIBUTARIA J_IDENTIFICACION_TRIBUTARIA,\n                d.PAIS,\n                d.PROVINCIA,\n                d.CODIGO_POSTAL,\n                d.LOCALIDAD,\n                d.CODIGO_CALLE,\n                d.CALLE,\n                d.NUMERO,\n                d.NUMERO_EXTENSION,\n                d.PISO,\n                d.DEPARTAMENTO,\n                d.LOCAL,\n                d.DOMICILIO_EXTENSION,\n                doc.DOCUMENTO_TIPO,\n                doc.DOCUMENTO_NRO\n            FROM TG06100X a\n                LEFT JOIN TG06110 p ON a.TG06100_ID = p.TG06100_TG06100_ID\n                LEFT JOIN TG06120 j ON a.TG06100_ID = j.TG06100_TG06100_ID\n                LEFT JOIN TG06300 d ON a.TG06300_TG06300_ID = d.TG06300_ID\n                LEFT JOIN TG06111 doc ON a.TG06100_ID = doc.TG06110_TG06100_TG06100_ID\n                JOIN TR02100 imp ON a.TG06100_ID = imp.TIT_TG06100_ID\n            WHERE a.BAJA_MOTIVO IS NULL\n                AND a.NOMBRE<>'NN'\n                AND imp.IMPONIBLE_TIPO='IND' AND imp.DEFINITIVO='D'\n                AND d.LOCALIDAD='RIO GRANDE' \n                AND a.NOMBRE NOT LIKE '?%'\n            ORDER BY a.TG06100_ID\n            ) a \n                WHERE ROWNUM <=" . ($desde + $cant) . ")\n            WHERE rnum >" . $desde . "\n            ";
     foreach ($Dbmunirg->query($sql) as $Row) {
         $Documento = StringHelper::ObtenerDocumento($Row['IND_IDENTIFICACION']);
         $Apellido = StringHelper::Desoraclizar($Row['Q_APELLIDOS']);
         $Nombre = StringHelper::Desoraclizar($Row['Q_NOMBRES']);
         $RazonSocial = StringHelper::Desoraclizar($Row['J_RAZON_SOCIAL']);
         $PersJur = false;
         if ($Documento[0] == 'CUIL' && (substr($Documento[1], 0, 3) == '30-' || substr($Documento[1], 0, 3) == '33-')) {
             $Documento[0] = 'CUIT';
             $PersJur = true;
         }
         if ($Row['DOCUMENTO_TIPO'] == 'DU') {
             $Row['DOCUMENTO_TIPO'] = 'DNI';
         }
         $Cuilt = '';
         if ($Documento[0] == 'CUIL' || $Documento[0] == 'CUIT') {
             $Cuilt = str_replace('-', '', $Documento[1]);
             if ($Row['DOCUMENTO_TIPO'] && $Row['DOCUMENTO_NRO']) {
                 $Documento[0] = $Row['DOCUMENTO_TIPO'];
                 $Documento[1] = $Row['DOCUMENTO_NRO'];
             }
         } else {
             if ($Row['DOCUMENTO_TIPO'] == 'CUIL' || $Row['DOCUMENTO_TIPO'] == 'CUIT') {
                 $Cuilt = str_replace('-', '', $Row['DOCUMENTO_NRO']);
             }
         }
         if ($Documento[0] == 'CUIL') {
             $Partes = explode('-', $Documento[1]);
             if (count($Partes) == 3) {
                 $Documento[0] = 'DNI';
                 $Documento[1] = (int) $Partes[1];
             }
         }
         if (!$Documento[1]) {
             // No tengo documento, utilizo el campo TRIBUTARIA_ID
             $Documento[0] = 'DNI';
             $Partes = explode('-', $Documento[1]);
             if (count($Partes) == 3) {
                 $Documento[1] = (int) $Partes[1];
             } else {
                 $Documento[1] = trim($Row['TRIBUTARIA_ID']);
             }
         }
         if (!$Nombre && !$Apellido) {
             $Apellido = StringHelper::Desoraclizar($Row['NOMBRE']);
         }
         if (!$Nombre && $Apellido && strpos($Apellido, '.') === false) {
             $a = explode(' ', $Apellido, 2)[0];
             $b = trim(substr($Apellido, strlen($a)));
             $Nombre = $b;
             $Apellido = $a;
         }
         if ($RazonSocial) {
             $NombreVisible = $RazonSocial;
         } else {
             if ($Nombre) {
                 $NombreVisible = $Apellido . ', ' . $Nombre;
             } else {
                 $NombreVisible = $Apellido;
             }
         }
         $Row['TG06100_ID'] = (int) $Row['TG06100_ID'];
         $CodigoCalle = $this->ArreglarCodigoCalle($Row['CODIGO_CALLE']);
         if (!$Cuilt) {
             $Cuilt = str_replace(array(' ', '-', '.'), '', $Row['J_IDENTIFICACION_TRIBUTARIA']);
         }
         $entity = $em->getRepository('YacareBaseBundle:Persona')->findOneBy(array('Tg06100Id' => $Row['TG06100_ID']));
         if ($entity == null && $Cuilt) {
             $entity = $em->getRepository('YacareBaseBundle:Persona')->findOneBy(array('Cuilt' => $Cuilt));
         }
         if ($entity == null) {
             $entity = $em->getRepository('YacareBaseBundle:Persona')->findOneBy(array('DocumentoNumero' => $Documento[1]));
         }
         if ($entity == null) {
             $entity = new \Yacare\BaseBundle\Entity\Persona();
             $entity->setTg06100Id($Row['TG06100_ID']);
             $entity->setDomicilioCodigoPostal('9420');
             if ($CodigoCalle) {
                 $entity->setDomicilioCalle($em->getReference('YacareCatastroBundle:Calle', $CodigoCalle));
             }
             $entity->setDomicilioCalleNombre(StringHelper::Desoraclizar($Row['CALLE']));
             $entity->setDomicilioNumero($Row['NUMERO']);
             $entity->setDomicilioPiso($Row['PISO']);
             $entity->setDomicilioPuerta($Row['DEPARTAMENTO']);
             // Si no está en el grupo Contribuyentes, lo agrego
             if ($entity->getGrupos()->contains($GrupoContribuyentes) == false) {
                 $entity->getGrupos()->add($GrupoContribuyentes);
             }
             if ($Row['Q_SEXO'] == 'F') {
                 $entity->setGenero(2);
             } else {
                 if ($Row['Q_SEXO'] == 'M') {
                     $entity->setGenero(1);
                 }
             }
             $em->persist($entity);
             $importar_importados++;
         } else {
             $entity->setTg06100Id($Row['TG06100_ID']);
             // $entity->setRazonSocial($RazonSocial);
             $importar_actualizados++;
         }
         $entity->setNombre($Nombre);
         $entity->setApellido($Apellido);
         $entity->setRazonSocial($RazonSocial);
         $entity->setPersonaJuridica($PersJur);
         $entity->setDocumentoNumero($Documento[1]);
         if (!$entity->getCuilt() && $Cuilt) {
             $entity->setCuilt($Cuilt);
         }
         // Campos que se actualizan siempre
         $entity->setDocumentoTipo($TipoDocs[$Documento[0]]);
         $log[] = $Cuilt . ' / ' . $Documento[0] . ' ' . $Documento[1] . ': ' . $NombreVisible . "\r\n";
         $importar_procesados++;
         $em->flush();
         if ($importar_procesados % 100 == 0) {
             ob_flush();
             flush();
             $em->getConnection()->commit();
             $em->getConnection()->beginTransaction();
         }
     }
     ob_flush();
     flush();
     $em->getConnection()->commit();
     return array('importar_importados' => $importar_importados, 'importar_actualizados' => $importar_actualizados, 'importar_procesados' => $importar_procesados, 'redir_desde' => $importar_procesados == $cant ? $desde + $cant : 0, 'log' => $log);
 }