dbo.obtener_bancos_test

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_bancos_test '30687288145'
*/


CREATE OR ALTER PROCEDURE dbo.obtener_bancos_test
@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 @dtStart date, @dtEnd date, @dtEnd_cuit date, @dtEnd_cuit2 date, @periodo_maximo varchar(7),@fec_periodo_minimo varchar(11),@periodo_minimo varchar(7);
 
 select @dtEnd = (select max(fecha_informacion) from fuentes_procesadas where visible = '1' and fuente = 'cendeu')
 
 select @dtEnd_cuit  = convert(date,isnull(max(cendeu.fec_inf),'200112')+'01') from cendeu where iden = @p_cuit  and vigente = 1 and id_fuente = 2
 select @dtEnd_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 @dtEnd_cuit2 > @dtEnd_cuit
 begin
 set @dtEnd_cuit = @dtEnd_cuit2
 end 


 if (@dtEnd_cuit > @dtEnd) 
  begin 
     set @dtEnd = @dtEnd_cuit
  end 
 set @dtStart = dateadd(month,-23, @dtEnd)
 set @periodo_maximo = replace(Convert(varchar(7),@dtEnd),'-','')
 set @fec_periodo_minimo = @dtStart
 set @periodo_minimo = replace(Convert(varchar(7),@dtStart),'-','')
 set @dtStart = dateadd(month,-23, @dtEnd)
 if(@periodo_maximo is not null)
  begin
  set @fec_periodo_minimo = dateadd(MM, -23,convert(date,@periodo_maximo+'01'))
  set @periodo_minimo = convert(varchar(4),DATEPART(yyyy,@fec_periodo_minimo))+right('0'+convert(varchar(2),DATEPART(mm,@fec_periodo_minimo)),2)
  end
  select distinct 
 fec_inf,
 isnull(nombre_corto,nombre) as nombre,
 convert(int,situacion) as situacion,
 ((prestamos+particip+garotor+o_cptos+deuda_total)) as total

 from cendeu inner join EntidadesBancarias on
 convert(int,cendeu.codigo_ent) = EntidadesBancarias.id_banco
  where iden = @p_Cuit and cendeu.fec_inf between @periodo_minimo and @periodo_maximo 
  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 c 
        where c.iden = cendeu.iden
       and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and c.fec_inf = cendeu.fec_inf
       and c.vigente = cendeu.vigente
          and (isnull(c.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       --and c.id_cendeu < cendeu.id_cendeu)
       and c.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_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'))
         )
      union 
  select distinct 
 fec_inf,
 isnull(nombre_corto,nombre) as nombre,
 convert(int,situacion) as situacion,
 ((prestamos+particip+garotor+o_cptos+deuda_total)) as total

 from cendeu_temp inner join EntidadesBancarias on
 convert(int,cendeu_temp.codigo_ent) = EntidadesBancarias.id_banco
  where iden = @p_Cuit and cendeu_temp.fec_inf between @periodo_minimo and @periodo_maximo 
  and vigente = 1  
  and not exists (select * from cendeu 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 c 
        where c.iden = cendeu_temp.iden
       and cast(c.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int)
       and c.fec_inf = cendeu_temp.fec_inf
       and c.vigente = cendeu_temp.vigente
          and (isnull(c.id_fuente,1)) = (isnull(cendeu_temp.id_fuente,1))
       --and c.id_cendeu < cendeu_temp.id_cendeu)
       and c.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 z where z.iden = cendeu_temp.iden and cast(z.codigo_ent as int) = cast(cendeu_temp.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_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'))
         )
order by cendeu.fec_inf asc

END

GO