dbo.completar_cuit_socios_aa.nop

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
select top 1007 * from socios_aa order by id 
*/

CREATE OR ALTER PROCEDURE dbo.[completar_cuit_socios_aa.nop]

AS
BEGIN
BEGIN TRY
    SET NOCOUNT ON
 declare @p_dni      varchar(20)
 declare @p_apellido varchar(100)
 declare @p_nombre   varchar(100)
 declare @p_nombre_completo varchar(200)
 declare @p_nombre_persona  varchar(200)
 declare @p_cuit  varchar(20) = null 
 declare @cuits table (cuit varchar(20), nombre varchar(200))
 declare @id int 
 declare @total_procesado int = 0
 declare @p_dni2 varchar(12) 

 /*declare @desde bigint = (select max(id) from socios_aa where isnull(identificacion,'') != '')*/
 DECLARE C_padron CURSOR FOR
  select top 10 dni, 
      apellido, 
      nombre, 
      id 
    from Socios_AA 
   where isnull(identificacion,'') = '' 
    /*and id > @desde
       and [column 1] not in ('CI')*/

    and procesado = 'N' 
    and len(dni) = 8 
    --and len(replace(replace(replace(replace(ltrim(rtrim(dni)),'.',''),'F',''),'M',''),'N','')) = 8 
    --and id=0

  /* order by id */

 OPEN C_Padron
 FETCH NEXT FROM C_Padron INTO @p_dni, @p_apellido, @p_nombre, @id
 WHILE @@fetch_status = 0
 BEGIN
 
  set @total_procesado = @total_procesado + 1
  if (isnull(@p_dni,'') != '' and isnull(@p_apellido, '') != '' and isnull(@p_nombre,'') != '') 
  begin 
   /*if @p_dni like '[FfMm]%' 
    begin
    set @p_dni = REPLACE(@p_dni, 'F','')
    set @p_dni = REPLACE(@p_dni, 'f','')
    set @p_dni = REPLACE(@p_dni, 'M','')
    set @p_dni = REPLACE(@p_dni, 'm','')
    end 
   
    set @p_dni = REPLACE(@p_dni, '.','')
    */

   if (ISNUMERIC(@p_dni) != 1 or @p_dni like '%[A-Za-z]%')
   begin
      update Socios_AA 
         set procesado='P' 
       where id = @id 
    FETCH NEXT FROM C_Padron INTO @p_dni, @p_apellido, @p_nombre, @id
    continue
   end 

   set @p_apellido = ltrim(rtrim(isnull(@p_apellido, '')))
   set @p_nombre = ltrim(rtrim(isnull(@p_nombre, '')))

   --set @p_nombre_completo = ltrim(rtrim(isnull(@p_apellido, ''))) + ' ' + ltrim(rtrim(isnull(@p_nombre,'')))
   set @p_nombre_completo =  @p_apellido  + ' ' +  @p_nombre 
   set @p_cuit = null 

   set @p_dni2 = right('00000000'+@p_dni,8)
   set @p_dni = convert(int,@p_dni)
   if (select count(*) 
      from padrones 
     where (cuerpo_clave =  @p_dni or cuerpo_clave =  @p_dni2) 
       and nombre_persona = @p_nombre_completo
       and habilitado = 1) = 1 
   begin
      select @p_cuit = identificacion, @p_nombre_persona = nombre_persona
     from padrones where (cuerpo_clave =  @p_dni or cuerpo_clave =  @p_dni2) 
      and nombre_persona = @p_nombre_completo
      and habilitado = 1
   end 


   if (@p_cuit is null)
   begin
    delete from @cuits
    insert into @cuits 
    exec [sp_buscar_cuit] null, @p_dni, null 
    if (select COUNT(*) from @cuits) = 1
    begin 
     select @p_cuit = cuit, @p_nombre_persona = nombre from @cuits
    end
   end 
 
   if (@p_cuit is null)
   begin
    delete from @cuits
    insert into @cuits 
    exec [sp_buscar_cuit] @p_nombre_completo, @p_dni, null 
    if (select COUNT(*) from @cuits) = 1
    begin 
     select @p_cuit = cuit, @p_nombre_persona = nombre from @cuits
    end
   end 

   if (@p_cuit is null)
   begin
    declare @palabra_1 table (palabra varchar(30), id int identity(1,1))
    declare @palabra varchar(100) = null 
    insert into @palabra_1
     exec sp_separo_nombres @p_nombre_completo 
    declare @total int = (select MAX(id) from @palabra_1)
    declare @i int = 1 
    while (@i <= @total and @p_cuit is null) 
    begin
     set @palabra = null 
     select @palabra = palabra from @palabra_1 where id = @i
     if (@palabra is not null)
     begin
      set @palabra = '"' + @palabra + '"'
      delete from @cuits
      insert into @cuits 
      exec [sp_buscar_cuit] @p_nombre_completo, @p_dni, @palabra 
      if (select COUNT(*) from @cuits) = 1
      begin 
       select @p_cuit = cuit, @p_nombre_persona = nombre from @cuits
      end
     end 
     set @i = @i + 1 
    end
   end 
   
   set @p_nombre_persona = REPLACE(@p_nombre_persona,'¥','Ñ')
      
   declare @es_cuit bit = 0 
   if (@p_cuit is not null and LEN(@p_cuit) = 11) set @es_cuit = 1  

   if (@es_cuit = 1)
   begin
    declare @iguales char(1) = 'N'
    if (REPLACE(@p_nombre_persona,'Ñ','N') like REPLACE(@p_apellido,'Ñ','N') + '%')
    begin
     set @iguales = 'S'
    end 

    if (@iguales = 'N' and (REPLACE(@p_nombre_persona,'Ñ','N') like REPLACE(@p_apellido,'NI','N') + '%'))
    begin
     set @iguales = 'S'
    end

    if (@iguales = 'N' and @p_apellido like '% %' and -- para apellidos compuestos 
     (REPLACE(@p_nombre_persona,'Ñ','N') like REPLACE((substring(@p_apellido,1,charindex(' ',@p_apellido,1))),'Ñ','N') + '%'))
    begin
     set @iguales = 'P'
    end

    if (@iguales = 'N' and @p_nombre_persona like '%?%')
    begin 
     if (REPLACE(@p_nombre_persona,'?','N') like REPLACE(@p_apellido,'Ñ','N') + '%') set @iguales = 'S'
     if (REPLACE(@p_nombre_persona,'?','A') like @p_apellido + '%') set @iguales = 'S'
     if (REPLACE(@p_nombre_persona,'?','E') like @p_apellido + '%') set @iguales = 'S'
     if (REPLACE(@p_nombre_persona,'?','I') like @p_apellido + '%') set @iguales = 'S'
     if (REPLACE(@p_nombre_persona,'?','O') like @p_apellido + '%') set @iguales = 'S'
     if (REPLACE(@p_nombre_persona,'?','U') like @p_apellido + '%') set @iguales = 'S'
    end
   
    update Socios_AA 
       set identificacion = @p_cuit, 
        nombre_persona = @p_nombre_persona,
        --apellido_ok = iif(REPLACE(nombre_persona,'Ñ','N') like REPLACE(apellido,'Ñ','N') + '%', 'S','N'), 
        apellido_ok = @iguales, 
        procesado = 'P' 
    where id = @id 

    /*
     if (@p_apellido like '% %') -- Si tiene mas de una palabra el apellido, compara la primer palabra 
                                 -- y si apellido_ok habia quedado en 'N', lo pone = 'P'
                                 -- (porque a veces mandan dos apellido, y en padron está solo el primero)
     begin
     update socios_aa set apellido_ok = 'P'
     where id = @id and apellido_ok = 'N' 
     and REPLACE(nombre_persona,'Ñ','N') like REPLACE((substring(apellido,1,charindex(' ',apellido,1))),'Ñ','N') + '%'
     and apellido like '% %'
     end 
     */

   end
   else 
   begin
    update Socios_AA 
       set procesado='P' 
    where id = @id 
   end 
  end 
  FETCH NEXT FROM C_Padron INTO @p_dni, @p_apellido, @p_nombre, @id

 END 
 CLOSE C_Padron
 DEALLOCATE C_Padron
 select @total_procesado as total_procesado
 
END TRY
BEGIN CATCH
 DEALLOCATE C_Padron
 
 UPDATE Socios_AA SET procesado = 'E' where id = @id

 select  @p_dni as dni_error, @id as id_error, 
 @total_procesado as total_procesado,
 ERROR_NUMBER() as error
END CATCH

END

GO

Used By

No items found