dbo.obtener_card_fiscales_new

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
/*

 [obtener_card_fiscales] '30501677643'
 obtener_sircreb '30501677643'

*/

CREATE OR ALTER PROCEDURE dbo.obtener_card_fiscales_new 
@p_Cuit varchar(20)
AS

BEGIN
  SET NOCOUNT ON;
  declare @fecha_inicio_actividad date
  declare @cod_clae varchar(6)
  declare @clae     varchar(500) 
  declare @principal int 

  declare @clae1    varchar(500) = null
  declare @clae2    varchar(500) = null
  declare @clae3    varchar(500) = null

  declare @tot   int 

  select @fecha_inicio_actividad = convert(varchar(50),min(fecha_inscripcion))
  from padron_impuestos
  where id_padron = (select id_padron from padrones where identificacion = @p_Cuit and habilitado = 1) 
  and fecha_inscripcion != '1901-01-01'

  select top 1 
   @cod_clae = right('000000' + rtrim(replace(clae.cod_clae,'"','')),6),   
   @clae = clae.descripcion, 
   @principal = clae.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
   order by principal desc
   -- Primero trae las cargadas desde afip, y luego si hay más anteriores, con otro codigo.
   DECLARE clae2 CURSOR
   FOR
     select distinct
      right('000000' + rtrim(replace(clae.cod_clae,'"','')),6) as cod_clae,   
      clae.descripcion, 
      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, 
        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) -- son las que se cargaron actualizadas de afip
           */


     order by principal desc
   OPEN clae2 
   set @tot = 0
   FETCH next from clae2 INTO @cod_clae, @clae, @principal
   WHILE (@@FETCH_STATUS <> -1 )
    BEGIN
     set @tot = @tot + 1
     if (@tot = 1) set @clae1 = @clae
     if (@tot = 2) set @clae2 = @clae
     if (@tot = 3) set @clae3 = @clae
     if (@tot > 3) break 
     FETCH next from clae2 INTO @cod_clae, @clae, @principal
    END 
   CLOSE clae2
   DEALLOCATE clae2 

  select @p_cuit as cuit, 
   @fecha_inicio_actividad as inicio_actividad,
   --@cod_clae + ' ' + 
   @clae1 as codigo_actividad_afip,
   @clae2 as codigo_actividad_afip2,
   @clae3 as codigo_actividad_afip3,
   null as estado


   /*
    select datos_sociedades.cuit,
    fecha_cons as inicio_actividad,
    sociedades_tipo.descripcion as codigo_actividad_afip,
    iif(inscripciones.visible is not null,'Activo','Observado') as estado 
    from datos_sociedades 
    inner join sociedades_tipo on sociedades_tipo.cod_sociedad = datos_sociedades.soc_codigo 
    left join inscripciones on inscripciones.cuit = @p_Cuit
   
    where datos_sociedades.cuit =@p_Cuit
    */

END

GO

Used By

No items found