dbo.api_edictos_dni_cuit

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

5
Name Data Type Length Description
@p_Cuit varchar 20
@nombre varchar 1000
@nombre_contains varchar 1000
@maxqty int 4
@provincia smallint 2

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec [api_edictos_dni_cuit] '5984260','',''

exec api_edictos_dni_cuit '','juan carlos gomez','',5

exec api_edictos_dni_cuit '','LEONEL TURCHETTO','"leonel*" NEAR "turchetto*"',10,'Ciudad autonoma de buenos aires'

exec api_edictos_dni_cuit '','SORIA MARIA INES','"SORIA*" NEAR "MARIA*" NEAR "INES*"',10,3
exec api_edictos_dni_cuit '','burgol','"burgol*"',10,3

-- No me gusta porque devuelve una SORIANO MARIA INES

exec api_edictos_dni_cuit '','juan alvarez','"juan*" NEAR "alvarez*"',10,'TIERRA DEL FUEGO'
*/


/*

exec api_edictos_dni_cuit '','quiroga estela','"quiroga estela*"',100,2


*/

/*
select prv_nombre as provincia,codigo_afip as id_provincia from provincias where pai_codigo_ifx=54 order by codigo_afip

*/




CREATE OR ALTER PROCEDURE dbo.api_edictos_dni_cuit
@p_Cuit varchar(20),
@nombre varchar(1000),
@nombre_contains varchar(1000),
@maxqty int = 20,
@provincia smallint=null

AS
BEGIN
    SET NOCOUNT ON;
 declare @es_cuit bit = 0
 declare @es_dni  bit = 0
 declare @error   bit = 0
 declare @busqueda_igual int, @busqueda_contains int, @busqueda_no_contains int, @nombre_encontrado varchar(1000), @cuit_buscado varchar(20)

set  @p_cuit = LTRIM(rtrim(@p_cuit))

set @nombre = LTRIM(rtrim(@nombre))
if (isnull(@nombre,'') != '' AND LEN(@nombre) < 3)   
begin
 set @error = 1 
 select 'Debe informar un nombre que contenga almenos 3 caracteres para poder realizar la consulta' as mensaje_error, 
   2 as error, 
   0 as validacion, '' as mensaje_validacion,
   '' as nombre_correcto,
   '' as cuit_buscado
end 


if (LEN(@p_Cuit) = 11) 
begin
 set @es_cuit = 1 
end 
if (LEN(@p_cuit) >= 4 and LEN(@p_Cuit) <= 8) 
begin
 set @es_dni = 1 
 set @p_Cuit = right('00000000'+@p_Cuit,8);
end 

-- Búsqueda por nombre, sin importar la provincia 
if(@es_cuit=0 and @es_dni = 0 and isnull(@provincia,99) = 99)
begin
 
  select top (@maxqty) '' as mensaje_error, 
   0 as error, 
   1 as validacion, 'búsqueda por nombre' as mensaje_validacion,
   nombre_persona as nombre_correcto,
     identificacion as cuit_buscado
   from padrones 
   where habilitado = 1 
    and contains(nombre_persona,@nombre_contains) 
   and identificador in(select identificador from identificadores where country_code = 'AR')
 
end

-- Búsqueda por nombre y provincia 
if(@es_cuit=0 and @es_dni = 0 and isnull(@provincia,99) != 99)
begin
 declare @total int = 0 
   declare @identificacion varchar(20)
 declare @respuesta table (  mensaje_error varchar(500),
        error int ,
        validacion int,
        mensaje_validacion varchar(500),
        nombre_correcto varchar(500),
          cuit_buscado varchar(20))
 declare @domicilios_pcia table (id int, domicilio varchar(250), pai_nombre varchar(250),prv_codigo smallint, prv_nombre varchar(250), loc_nombre varchar(250), codpos varchar(10), tipo_domicilio varchar(10), created datetime)

 declare w_cursor cursor for
 -- Sin Top no anda ... 
 select top (@maxqty*100) identificacion 
   from padrones 
   where habilitado = 1 
    and contains(nombre_persona,@nombre_contains) 
   and identificador in (select identificador from identificadores where country_code = 'AR')
 
 open w_cursor
 Fetch next from w_cursor INTO @identificacion
 WHILE @@fetch_status = 0
 BEGIN
   delete from @domicilios_pcia
   insert into @domicilios_pcia 
    exec sp_obtener_domicilios @identificacion,0,1 /* el segundo es para que traiga todos los domicilios y no solo los de AFIP, el tercero parámetro es que devuelva el código de PRV*/
   if (select count(*) from @domicilios_pcia where isnull(prv_codigo,99) = @provincia) > 0 
   begin
    insert into @respuesta
    select '' as mensaje_error, 
      0 as error, 
      1 as validacion, 'búsqueda por nombre' as mensaje_validacion,
      nombre_persona as nombre_correcto,
        identificacion as cuit_buscado
      from padrones 
      where habilitado = 1 
       and identificacion = @identificacion
      and identificador in(select identificador from identificadores where country_code = 'AR')
    set @total = @total + 1 
   end 
   if (@total = @maxqty)
   begin
    break 
   end 

  Fetch next from w_cursor INTO @identificacion
 END
 CLOSE w_cursor
    DEALLOCATE w_cursor
 
 select * from @respuesta
end

if (@es_cuit = 1) 
begin
 set @busqueda_igual = (select count(distinct nombre_persona) 
                         from padrones 
         where identificacion = @p_Cuit and habilitado = 1 
        and identificador in(select identificador from identificadores where country_code = 'AR'))


 if( @busqueda_igual = 1)
 begin
  select top 1 '' as mensaje_error, 
    0 as error, 
    1 as validacion,'El registro es exactamente igual' as mensaje_validacion,
    nombre_persona as nombre_correcto,
      identificacion as cuit_buscado
    from padrones 
    where identificacion = @p_Cuit 
    and habilitado = 1 
    and identificador in(select identificador from identificadores where country_code = 'AR')
 end
 else 
 begin 
  if(@busqueda_igual = 0)
  begin
   set @busqueda_contains = (select count(distinct nombre_persona) from padrones where identificacion = @p_Cuit and habilitado = 1 and contains(nombre_persona,@nombre_contains) and identificador in(select identificador from identificadores where country_code = 'AR'))
   if(@busqueda_contains = 1)
   begin
    select top 1 '' as mensaje_error, 
        0 as error, 
        1 as validacion, 'El registro coincide pero con una o más variantes en su nombre' as mensaje_validacion,
        nombre_persona as nombre_correcto,
          identificacion as cuit_buscado
        from padrones 
       where identificacion = @p_Cuit 
         and habilitado = 1 
          and contains(nombre_persona,@nombre_contains) 
         and identificador in(select identificador from identificadores where country_code = 'AR')
   end
   else 
   begin 
    set @busqueda_no_contains = (select count(distinct nombre_persona) from padrones 
              where identificacion = @p_Cuit 
             and habilitado = 1  
             and identificador in(select identificador from identificadores where country_code = 'AR'))
    if(@busqueda_no_contains = 0)
    begin
       select 'No se encontraron registros para ese CUIT' as mensaje_error,
               3 as error, 
         0 as validacion, '' as mensaje_validacion, 
         null as nombre_correcto, '' as cuit_buscado
    end
    else
    begin
       select distinct '' as mensaje_error, 
        0 as error, 
        0 as validacion,'Registro encontrado con ese CUIT con otro Nombre' as mensaje_validacion,
        nombre_persona as nombre_correcto,
        identificacion as cuit_buscado
        from padrones 
       where identificacion = @p_Cuit 
         and habilitado = 1 
          and identificador in(select identificador from identificadores where country_code = 'AR')

    end 
   end
  end 
 end  
end

if (@es_dni = 1) 
begin
 set @busqueda_igual = (select count(distinct nombre_persona) from padrones 
                         where cuerpo_clave = @p_Cuit 
           and habilitado = 1 
         and identificador in(select identificador from identificadores where country_code = 'AR'))

 if( @busqueda_igual = 1)
 begin
   select top 1 '' as mensaje_error, 
        0 as error, 
        1 as validacion, 'El registro es exactamente igual' as mensaje_validacion,
        nombre_persona as nombre_correcto,
          identificacion as cuit_buscado
        from padrones 
       where cuerpo_clave = @p_Cuit 
         and habilitado = 1   
          and identificador in(select identificador from identificadores where country_code = 'AR')
 end
 else 
 begin 
  if(@busqueda_igual = 0)
  begin
   begin
    select '' as mensaje_error, 
            3 as error, 
      0 as validacion, 'No se encontraron registros para ese DNI' as mensaje_validacion,
      null as nombre_correcto,
      '' as cuit_buscado
   end
  end 
  else
  begin
    select distinct '' as mensaje_error, 
    0 as error, 
    0 as validacion,'Registros encontrado con ese DNI con otro Nombre' as mensaje_validacion,
    nombre_persona as nombre_correcto,
    identificacion as cuit_buscado
    from padrones 
    where cuerpo_clave = @p_Cuit 
    and habilitado = 1 
     and identificador in(select identificador from identificadores where country_code = 'AR')
  end
 end 
 
 
end


END 

GO

Used By

No items found