dbo.obtener_status_bancario_old3

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 12

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.obtener_status_bancario_old3
@p_cuit varchar(12)
AS
BEGIN
    SET NOCOUNT ON;
 declare @p_5anios date = getdate() - (5*365)
 declare @p_2anios date = getdate() - (2*365)
 declare @p_3meses date = getdate() - (90) 

 declare @cheques_rechazados int,@situaciones int,@ultima_posicion int,@max int,@fec_inf varchar(20),@situaciones_temp int,@max_temp int
            
 select @max_temp = isnull(max(subquery.maximo),0) from (select fec_inf,sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as maximo 
   from cendeu_temp as cendeu 
   where iden = @p_cuit
   and vigente =1 
   and convert(date,fec_inf+'01') > getdate() - 770 
   and not exists (select * from cendeu_temp 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 = 1 
        and c.id_fuente = 2 
        and c.id_cendeu != cendeu.id_cendeu)
   and not exists (select * from cendeu_temp 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 = cendeu.vigente
        and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
        and  b.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 d 
        where d.iden = cendeu.iden 
         and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
         and d.vigente = '1'))
      )
   group by fec_inf ) subquery
  select @max = max(subquery.maximo) from (select fec_inf,sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000) as maximo 
   from cendeu 
   where iden = @p_cuit
   and vigente =1 
   and convert(date,fec_inf+'01') > getdate() - 770 
   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 = 1 
        and c.id_fuente = 2 
        and c.id_cendeu != cendeu.id_cendeu)
   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 = cendeu.vigente
        and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
        and  b.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 d 
        where d.iden = cendeu.iden 
         and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
         and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
         and d.vigente = '1'))
      )
   group by fec_inf ) subquery
   /*
        select @fec_inf = (select top 1 fec_inf
    from cendeu 
    where iden = @p_cuit 
    and vigente =1 
    and convert(date,fec_inf+'01') > getdate() - 770 
    and @max = (select max(subquery.maximo) from (select  fec_inf,sum(prestamos+particip+garotor+o_cptos+deuda_total) as maximo 
    from cendeu 
    where iden = @p_cuit  
    and vigente =1 
    and convert(date,fec_inf+'01') > getdate() - 770 
    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 = 1 
       and c.id_fuente = 2 
       and c.id_cendeu != cendeu.id_cendeu)
    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 = cendeu.vigente
     and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
     and  b.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 d 
    where d.iden = cendeu.iden 
      and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
      and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
      and d.vigente = '1'))
       )
    group by fec_inf) subquery))*/

         


 --select @fec_inf = convert(varchar,convert(date,fec_inf+'01'),103)  from cendeu where iden = @p_cuit and cendeu.vigente = 1 and convert(date,fec_inf+'01') > getdate() - 770
 select @cheques_rechazados = count(*) from ChequesBCRA where cuit_firmante = @p_cuit AND fecha_rechazo > getdate() - 770 and visible = 1

 select @situaciones = count(situacion) from cendeu where iden = @p_cuit and situacion > 01 and vigente = 1 and convert(date,fec_inf+'01') > getdate() - 770
           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 = 1 
                  and c.id_fuente = 2 
                  and c.id_cendeu != cendeu.id_cendeu)
           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 = cendeu.vigente
                and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
                and  b.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 d 
                where d.iden = cendeu.iden 
                  and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
                  and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
                  and d.vigente = '1'))
                )
   select @situaciones_temp = count(situacion) from cendeu_temp as cendeu where iden = @p_cuit and situacion > 01 and vigente = 1 and convert(date,fec_inf+'01') > getdate() - 770
           and not exists (select * from cendeu_temp 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 = 1 
                  and c.id_fuente = 2 
                  and c.id_cendeu != cendeu.id_cendeu)
           and not exists (select * from cendeu_temp 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 = cendeu.vigente
                and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
                and  b.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 d 
                where d.iden = cendeu.iden 
                  and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
                  and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
                  and d.vigente = '1'))
                )
 set @situaciones = isnull(@situaciones,0) + isnull(@situaciones_temp,0)
 set @max = iif(@max >= @max_temp,@max,@max_temp)
/*
select @ultima_posicion = isnull(sum(prestamos+particip+garotor+o_cptos+deuda_total),0)  from cendeu where iden = @p_cuit and cendeu.vigente = 1 and fec_inf = (select max(fec_inf) from cendeu where iden = @p_cuit and cendeu.vigente = 1)
*/

 
 select @ultima_posicion = isnull(sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000),0)  
   from cendeu_temp cendeu
    where iden = @p_cuit 
    and cendeu.vigente = 1
    and fec_inf = (select max(fec_inf) from cendeu_temp where iden = @p_cuit and cendeu_temp.vigente = 1 and convert(date,fec_inf+'01') > getdate() - 770
               and not exists (select * from cendeu_temp 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 = 1 
                  and c.id_fuente = 2 
                  and c.id_cendeu != cendeu.id_cendeu)
           and not exists (select * from cendeu_temp 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 = cendeu.vigente
                and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
                and  b.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 d 
                where d.iden = cendeu.iden 
                  and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
                  and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
                  and d.vigente = '1'))
    )) 
 
 if(@ultima_posicion = 0)
 begin
  select @ultima_posicion = isnull(sum((prestamos+particip+garotor+o_cptos+deuda_total)*1000),0)  
    from cendeu 
     where iden = @p_cuit 
     and cendeu.vigente = 1
     and fec_inf = (select max(fec_inf) from cendeu where iden = @p_cuit and cendeu.vigente = 1 and convert(date,fec_inf+'01') > getdate() - 770
               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 = 1 
                  and c.id_fuente = 2 
                  and c.id_cendeu != cendeu.id_cendeu)
           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 = cendeu.vigente
                and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
                and  b.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 d 
                where d.iden = cendeu.iden 
                  and cast(d.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
                  and cast(fec_inf+'01' as date) between convert(date,d.fec_inf_desde) and convert(date,d.fec_inf_hasta)
                  and d.vigente = '1'))
                )) 
 end

 
 select top 1 @cheques_rechazados as cheques_rechazados,@situaciones as situaciones,@ultima_posicion as ultima_posicion, @max as maxima_posicion/*,@fec_inf as fec_inf*/

END

GO

Used By

No items found