dbo.LN_alertas_cendeu_cliente

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

4
Name Data Type Length Description
@p_Cuit varchar 20
@p_Cliente int 4
@periodo varchar 6
@situacion int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
DECLARE  @periodo VARCHAR(6);
declare @situacion int; 
EXEC [LN_alertas_cendeu_cliente] '30692845605', '5546348', @periodo output, @situacion output
select @periodo as periodo, @situacion as situacion

select dias from ln_dias_alertas where tabla = 'cendeu' and cliente = 5546348
*/


/*****************************************************************************
En la tabla [LN_dias_alertas] buscamos los dias de 'cendeu' que hay que tomar
lo pasamos a meses para que se tomen meses completos
******************************************************************************/
   
/* Agregado 05/05/2020 Cuenta activa -> Solo toma los bancos para los que tenga información en últimos tres periodos 
Si no tiene información en los últimos 3 períodos, se considera que la cuenta no está activa */
 


CREATE OR ALTER PROCEDURE dbo.LN_alertas_cendeu_cliente 
@p_Cuit varchar(20),
@p_Cliente int, 
@periodo varchar(6) = null output,
@situacion int = null output
AS

BEGIN
    SET NOCOUNT ON;
 declare @p_dias  int = null
 declare @p_meses int = null

 set @p_dias = (select dias from ln_dias_alertas where tabla = 'cendeu' and cliente = @p_Cliente) 
 if (@p_dias is null) set @p_dias = 180 -- 360 antes por default se mostraba ultimo año, se bajo a 180 días (6 meses) a pedido de Juan Facundo 29/8/2019.
    set @p_meses = @p_dias / 30
 
 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), @periodo_minimo varchar(7),
   @cuenta_activa_date date, @cuenta_activa varchar(7)
 
 select @dtEnd = (select max(fecha_informacion) from fuentes_procesadas where visible = '1' and fuente = 'cendeu')
 
 select @dtEnd_cuit2  = convert(date,isnull(max(cendeu.fec_inf),'200112')+'01') from cendeu where iden = @p_cuit  and vigente = 1 and id_fuente = 2
 select @dtEnd_cuit = 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) set @dtEnd_cuit = @dtEnd_cuit2
 
 if (@dtEnd_cuit > @dtEnd) set @dtEnd = @dtEnd_cuit
 
 set @dtStart = dateadd(month,((@p_meses-1)*(-1)), @dtEnd) -- Si quiere 6 meses, se resta 5 meses al ultimo mes, para que tome los 6 ultimos.
 set @periodo_maximo = replace(Convert(varchar(7),@dtEnd),'-','')
 set @periodo_minimo = replace(Convert(varchar(7),@dtStart),'-','')

 set @cuenta_activa_date = dateadd(month,-2, @dtEnd)
 set @cuenta_activa = replace(Convert(varchar(7),@cuenta_activa_date),'-','')

 --select @dtStart,@dtEnd,@cuenta_activa
  DECLARE p_CENDEU CURSOR FOR
  
   select distinct 
  fec_inf,
  convert(int,situacion) as situacion
  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  
   /* Agregado 05/05/2020 Cuenta activa -> Tenga registros ultimos tres periodos */ 
   and 
   (
   exists (select * from cendeu ba 
                where ba.iden = cendeu.iden 
                and cast(ba.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and ba.fec_inf >= @cuenta_activa 
       and ba.vigente = 1)
   or
   exists (select * from cendeu_temp bat
                where bat.iden = cendeu.iden 
                and cast(bat.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
       and bat.fec_inf >= @cuenta_activa 
       and bat.vigente = 1)
   )
   /* Fin agregado Cuenta Activa */ 
   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 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,
  convert(int,situacion) as situacion
  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  

   /* Agregado 05/05/2020 Cuenta activa -> Tenga registros ultimos tres periodos */ 
   and 
   (
   exists (select * from cendeu ba 
                where ba.iden = cendeu_temp.iden 
                and cast(ba.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) 
       and ba.fec_inf >= @cuenta_activa 
       and ba.vigente = 1)
   or
   exists (select * from cendeu_temp bat
                where bat.iden = cendeu_temp.iden 
                and cast(bat.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) 
       and bat.fec_inf >= @cuenta_activa 
       and bat.vigente = 1)
   )
   /* Fin agregado Cuenta Activa */ 

   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 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 2 desc, 1 desc

 OPEN p_CENDEU
 FETCH NEXT FROM p_CENDEU INTO @periodo, @situacion
 CLOSE p_CENDEU
 DEALLOCATE p_CENDEU

END

GO