dbo.obtener_situaciones_bancos_test2

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

CREATE OR ALTER PROCEDURE dbo.obtener_situaciones_bancos_test2
@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_minimo date, @periodo date
    declare @periodo_maximo date,@periodo_maximo_cuit date,@periodo_maximo_cuit2 date,@fecha_formateada varchar(10),@periodo_comparar varchar(6);
    declare @respuesta table (periodo date, situacion int, total decimal(20))
 declare @respuesta2 table (periodo date, sit1 decimal(20), sit2 decimal(20), sit3 decimal(20), sit4 decimal(20), sit5 decimal(20), sit6 decimal(20))

 select @periodo_maximo = (select max(fecha_informacion) from fuentes_procesadas where visible = '1' and fuente ='cendeu')

 select @periodo_maximo_cuit = convert(date,isnull(max(cendeu.fec_inf),'200112')+'01') from cendeu where iden = @p_cuit AND vigente = 1 and id_fuente = 2
 select @periodo_maximo_cuit2 = convert(date,isnull(max(cendeu.fec_inf),'200112')+'01') from cendeu_temp cendeu where iden = @p_cuit AND vigente = 1 and id_fuente = 2 
 if @periodo_maximo_cuit2 > @periodo_maximo_cuit
 begin
 set @periodo_maximo_cuit = @periodo_maximo_cuit2
 end 

 if (@periodo_maximo_cuit > @periodo_maximo) 
  begin 
     set @periodo_maximo = @periodo_maximo_cuit
  end 
 set @periodo_minimo = cast(DATEADD(MM,-23,@periodo_maximo) as date)
 
 insert into @respuesta 
 
 select cast(fec_inf+'01' as date) as periodo, 
        substring(convert(char(2),convert(int,situacion)),1,1) as situacion,  
     --convert(int,situacion) as situacion, 
        sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  
 from cendeu 
 where iden = @p_Cuit 
   and vigente= 1   
   and cast(fec_inf+'01' as date) between @periodo_minimo and @periodo_maximo
   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 c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       --and  id_cendeu < cendeu.id_cendeu)
       and  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 exists (select * from cendeu z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.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'))
       )
 group by cast(fec_inf+'01' as date), convert(int,situacion)

 union 

 select cast(fec_inf+'01' as date) as periodo, 
        substring(convert(char(2),convert(int,situacion)),1,1) as situacion, 
     --convert(int,situacion) as situacion, 
        sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000)  
 from cendeu_temp cendeu
 where iden = @p_Cuit 
   and vigente= 1   
   and cast(fec_inf+'01' as date) between @periodo_minimo and @periodo_maximo
   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_temp c1
                   where iden = cendeu.iden
       and  cast(codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  fec_inf = cendeu.fec_inf
       and  vigente = cendeu.vigente
       and (isnull(c1.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       --and  id_cendeu < cendeu.id_cendeu)
       and  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 exists (select * from cendeu_temp z where z.iden = cendeu.iden and cast(z.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,z.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'))
       )
 group by cast(fec_inf+'01' as date), convert(int,situacion)

 order by 1,2

 set @periodo = @periodo_minimo
 while (@periodo <= @periodo_maximo) 
 begin 

 insert into @respuesta2
   select @periodo as periodo,
   (select sum(total)
    from @respuesta 
    where periodo = @periodo
      and situacion = 1) as sit1,
    (select sum(total) 
    from @respuesta 
    where periodo = @periodo
      and situacion = 2) as sit2,   
    (select sum(total) 
    from @respuesta 
    where periodo = @periodo
      and situacion = 3) as sit3,
    (select sum(total) 
    from @respuesta 
    where periodo = @periodo
      and situacion = 4) as sit4,
   (select sum(total)
    from @respuesta 
    where periodo = @periodo
      and situacion = 5) as sit5,
    (select sum(total) 
    from @respuesta 
    where periodo = @periodo
    and situacion = 6) as sit6

     
      

   set @periodo = cast(DATEADD(MM,+1,@periodo) as date)
 end    


 select * from @respuesta2 order by 1
END

GO

Used By

No items found