dbo.obtener_financiero_brief_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

CREATE OR ALTER PROCEDURE dbo.obtener_financiero_brief_test
@p_Cuit varchar(20)
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 @periodo_maximo_cuit varchar(7)
 declare @total int
 set @periodo_maximo_cuit = '201701'--max(cendeu.fec_inf) from cendeu where iden = @p_cuit AND vigente = 1



 set @total = (select(prestamos+particip+garotor+o_cptos+c1.deuda_total) as total from cendeu_temp as c1 
  where iden = @p_Cuit
 and c1.fec_inf = @periodo_maximo_cuit and vigente = 1 and not exists (select * from cendeu_temp b where b.iden = c1.iden and cast(b.codigo_ent as int) = cast(c1.codigo_ent as int) and b.fec_inf = c1.fec_inf and b.vigente = 1 and b.id_fuente = 2 and b.id_cendeu != c1.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 = c1.iden and cast(c.codigo_ent as int) = cast(c1.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 = c1.iden 
        and cast(c.codigo_ent as int) = cast(c1.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'))
         ))
 if(@total is not null)
  begin
   select 
    fec_inf, isnull(nombre_corto,nombre) as nombre,0 as total
   from cendeu_temp as c1 inner join EntidadesBancarias on 
   c1.codigo_ent = EntidadesBancarias.id_banco
   where iden = @p_Cuit
   and c1.fec_inf = @periodo_maximo_cuit and vigente = 1 and not exists (select * from cendeu_temp b where b.iden = c1.iden and cast(b.codigo_ent as int) = cast(c1.codigo_ent as int) and b.fec_inf = c1.fec_inf and b.vigente = 1 and b.id_fuente = 2 and b.id_cendeu != c1.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 = c1.iden and cast(c.codigo_ent as int) = cast(c1.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 = c1.iden 
          and cast(c.codigo_ent as int) = cast(c1.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 c1.fec_inf,total desc
  end
 else
  begin
  
   select 
    fec_inf, isnull(nombre_corto,nombre) as nombre, ((prestamos+particip+garotor+o_cptos+c1.deuda_total)) as total
   from cendeu as c1 inner join EntidadesBancarias on 
   c1.codigo_ent = EntidadesBancarias.id_banco
   where iden = @p_Cuit
   and c1.fec_inf = @periodo_maximo_cuit and vigente = 1 and not exists (select * from cendeu b where b.iden = c1.iden and cast(b.codigo_ent as int) = cast(c1.codigo_ent as int) and b.fec_inf = c1.fec_inf and b.vigente = 1 and b.id_fuente = 2 and b.id_cendeu != c1.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 = c1.iden and cast(c.codigo_ent as int) = cast(c1.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 = c1.iden 
          and cast(c.codigo_ent as int) = cast(c1.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 c1.fec_inf,total desc
  end

END

GO

Used By

No items found