dbo.obtener_card_bancos

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_bancos] '20081138738'
CREATE OR ALTER PROCEDURE dbo.obtener_card_bancos 
@p_Cuit varchar(20)
AS
BEGIN
    SET NOCOUNT ON;
 
 declare @p_5anios date = getdate() - (5*365)
 /*
  05/04/2018:
  Para que se vean en el grafico 2 años completos, se sacó la restriccion de los 2 años del dia de hoy, para deudas regularizadas.
  declare @p_2anios date = getdate() - (2*365)
  */

 declare @p_2anios date = getdate() - (5*365)
 declare @p_3meses date = getdate() - (90)
 
 declare @periodo_maximo date,
         @periodo_maximo_cd  varchar(7), 
         @periodo_maximo_cuit varchar(7),
   @periodo_maximo_cuit_temp varchar(7), 
   @fecha_formateada varchar(10),
   @periodo_comparar varchar(6),
   @periodo_comparar1 varchar(6),
   @periodo_comparar2 varchar(6)

 /*
  Antes lo hacia asi, pero en caso que la deuda no este publicada, aparecia
  select @periodo_maximo_cuit      = max(cendeu.fec_inf)      from cendeu where iden = @p_cuit AND vigente = 1  
  select @periodo_maximo_cuit_temp = max(cendeu_temp.fec_inf) from cendeu_temp where iden = @p_cuit AND vigente = 1 
  if(@periodo_maximo_cuit_temp > @periodo_maximo_cuit
  OR (@periodo_maximo_cuit is null and @periodo_maximo_cuit_temp is not null)
  )
  begin
  set @periodo_maximo_cuit = @periodo_maximo_cuit_temp
  end
  if (@periodo_maximo_cuit is not null) 
  begin
  set @periodo_maximo   = convert(date,@periodo_maximo_cuit+'01')
  set @periodo_comparar = @periodo_maximo_cuit
  end
  */
   
 select @periodo_maximo_cd = replace(convert(char(7), max(fecha_informacion)),'-','') from fuentes_procesadas where visible = '1' and fuente ='cendeu'
 select @periodo_maximo_cuit      = max(cendeu.fec_inf)      from cendeu where iden = @p_cuit AND vigente = 1  and id_fuente = 2
 select @periodo_maximo_cuit_temp = max(cendeu_temp.fec_inf) from cendeu_temp where iden = @p_cuit AND vigente = 1 and id_fuente = 2
 if(@periodo_maximo_cuit_temp > @periodo_maximo_cuit
 OR (@periodo_maximo_cuit is null and @periodo_maximo_cuit_temp is not null)
 )
  begin
  set @periodo_maximo_cuit = @periodo_maximo_cuit_temp
  end
 
  if (@periodo_maximo_cd > @periodo_maximo_cuit or @periodo_maximo_cuit is null) 
  begin
  set @periodo_maximo_cuit = @periodo_maximo_cd
  end 
  
 if (@periodo_maximo_cuit is not null) 
 begin
 set @periodo_maximo   = convert(date,@periodo_maximo_cuit+'01')
  set @periodo_comparar = @periodo_maximo_cuit
 end 
 set @periodo_comparar1 = (select MAX(fec_inf) from cendeu b where b.iden = @p_Cuit and b.vigente = 1 and b.fec_inf <= @periodo_comparar) 
 set @periodo_comparar2 = (select MAX(fec_inf) from cendeu_temp b where b.iden = @p_Cuit and b.vigente = 1 and b.fec_inf <= @periodo_comparar) 

 if (@periodo_comparar1 > @periodo_comparar2 or (@periodo_comparar1 is not null and  @periodo_comparar2 is null)) 
 begin
 set @periodo_comparar = @periodo_comparar1
 end 

 if (@periodo_comparar2 > @periodo_comparar1 or (@periodo_comparar2 is not null and  @periodo_comparar1 is null)) 
 begin
 set @periodo_comparar = @periodo_comparar2
 end 
 set @periodo_maximo   = convert(date,@periodo_comparar+'01')

 
 if (@periodo_maximo is not null AND @periodo_maximo >= dateadd(year,-2,(select max(fecha_informacion) from fuentes_procesadas where visible = '1' and fuente ='cendeu')) ) 
 begin
  select distinct count(*) as cantidad_bancos,
         @periodo_maximo as periodo,
      sum((prestamos+particip+garotor+o_cptos+deuda_total)) as deuda_total 
     from cendeu where iden = @p_Cuit 
      and fec_inf = @periodo_comparar
      and vigente = 1 
    and not exists (select * from cendeu b where b.iden = cendeu.iden and cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int) and b.fec_inf = cendeu.fec_inf and b.vigente = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
     and not exists (select * from cendeu b2 
       where b2.iden = cendeu.iden
       and  cast(b2.codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  b2.fec_inf = cendeu.fec_inf
       and  b2.vigente = cendeu.vigente
       and (isnull(b2.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  b2.id_cendeu < cendeu.id_cendeu)
 and not (
        (cast(fec_inf+'01' as date) < @p_5anios) 
        OR
        (cast(fec_inf+'01' as date) < @p_2anios 
       and 
    --not (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    not exists (select * from cendeu c where c.iden = cendeu.iden and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,c.created) > @p_3meses)))
     OR 
     (cast(fec_inf+'01' as date) < @p_2anios 
      and 
      exists (select * from cendeu_cancela c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
      having count(*) > 0 
   union all
  select distinct count(*) as cantidad_bancos,@periodo_maximo as periodo,sum((prestamos+particip+garotor+o_cptos+deuda_total)) as deuda_total 
        from cendeu_temp 
     where iden = @p_Cuit
      and fec_inf = @periodo_comparar
     and vigente = 1 
    and not exists (select * from cendeu_temp b where b.iden = cendeu_temp.iden and cast(b.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) and b.fec_inf = cendeu_temp.fec_inf and b.vigente = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu_temp.id_cendeu)
         and not exists (select * from cendeu_temp b2 
       where b2.iden = cendeu_temp.iden
       and  cast(b2.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int)
       and  b2.fec_inf = cendeu_temp.fec_inf
       and  b2.vigente = cendeu_temp.vigente
       and (isnull(b2.id_fuente,1)) = (isnull(cendeu_temp.id_fuente,1))
       and  b2.id_cendeu < cendeu_temp.id_cendeu)
 and not (
        (cast(fec_inf+'01' as date) < @p_5anios) 
        OR
        (cast(fec_inf+'01' as date) < @p_2anios 
       and 
    --not (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    not exists (select * from cendeu_temp c where c.iden = cendeu_temp.iden and cast(c.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) and (convert(date,c.created) > @p_3meses)))
     OR 
     (cast(fec_inf+'01' as date) < @p_2anios 
      and 
      exists (select * from cendeu_cancela c 
      where c.iden = cendeu_temp.iden 
        and cast(c.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
      having count(*) > 0 
 end 
END

GO