dbo.obtener_clae_2025

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@p_cuit varchar 20

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec obtener_clae '30502793175'
exec [obtener_clae_2025] '30502793175'
*/

CREATE OR ALTER PROCEDURE dbo.obtener_clae_2025

@p_cuit varchar(20)


AS
BEGIN
    SET NOCOUNT ON;

 /*
 
   select distinct
         clae.cod_clae, 
         clae.descripcion, 
         convert(varchar(10),clae.fecha_inicio_actividad,103) as fecha_inicio_actividad,
         principal
    from clae
   where (clae.cuit = @p_cuit) and (visible = 1) order by principal desc
 
   */

  -- Clae 
  -- traemos los nuevos que se compleataron desde afip que son los que tienen completo el campo formulario
  -- y despues todos los demas, que tengamos con otro codigo de Act.

  declare @cant int = 0
  set @cant = (select count(*) from clae
     where clae.cuit = @p_cuit
       and clae.visible = 1
       and clae.descripcion is not null
       and clae.formulario is not null) 

  declare @fecha date = null 
  set @fecha = (select max(convert(date,modified)) 
        from clae
        where clae.cuit = @p_cuit
          and clae.visible = 0 
          and clae.descripcion is not null
          and clae.formulario is not null
          and modified >= '2024-09-01')
  if (@cant = 1 and @fecha is not null)
  begin
    select distinct
         right('000000' + rtrim(replace(clae.cod_clae,'"','')),6) as cod_clae,   
         clae.descripcion, 
         convert(varchar(10),clae.fecha_inicio_actividad,103) as fecha_inicio_actividad,
         principal
         from clae
        where clae.cuit = @p_cuit
          and clae.visible = 1
          and clae.descripcion is not null
          and clae.formulario is not null
    union 

    select distinct
         right('000000' + rtrim(replace(clae.cod_clae,'"','')),6) as cod_clae,   
         clae.descripcion, 
         convert(varchar(10),clae.fecha_inicio_actividad,103) as fecha_inicio_actividad,
         principal
         from clae
        where clae.cuit = @p_cuit
          and clae.visible = 0 
          and convert(date,modified) = @fecha 
          and clae.descripcion is not null
          and clae.formulario is not null
          
       order by principal desc


  end 
  else 
  begin
  select distinct
      right('000000' + rtrim(replace(clae.cod_clae,'"','')),6) as cod_clae,   
      clae.descripcion, 
      convert(varchar(10),clae.fecha_inicio_actividad,103) as fecha_inicio_actividad,
      principal
      from clae
     where clae.cuit = @p_cuit
       and clae.visible = 1
       and clae.descripcion is not null
       and clae.formulario is not null -- son las que se cargaron actualizadas de afip
         /*
             union 
         
           select distinct
          right('000000' + rtrim(replace(clae.cod_clae,'"','')),6) as cod_clae,   
          clae.descripcion,
                      convert(varchar(10),clae.fecha_inicio_actividad,103) as fecha_inicio_actividad,
           principal
            from clae
           where clae.cuit = @p_cuit
             and clae.visible = 1
             and clae.descripcion is not null
             and clae.formulario is null 
             and not exists (select * from clae clae2 where clae2.cuit = clae.cuit and clae2.visible = 1 and clae2.formulario is not null) -- que no sean iguales a las que se cargaron actualizadas de afip
             */


     order by principal desc
  end 




    /*
    
     declare @p_id_padron int; 
     set @p_id_padron = (select id_padron from padrones where identificacion = @p_cuit)
    
      select distinct padron_actividades.codigo_actividad as cod_clae, actividades.nombre_actividad as descripcion, null as fecha_inicio_actividad
       from padron_actividades
      inner join actividades 
         on actividades.codigo_actividad = padron_actividades.codigo_actividad
      where id_padron = @p_id_padron*/

END 

GO

Depends On

1

Used By

No items found