dbo.obtener_financiero_brief

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
-- [obtener_financiero_brief] '20081138738'
CREATE OR ALTER PROCEDURE dbo.obtener_financiero_brief 
@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_maximo  varchar(7)
  declare @periodo_maximo_cuit varchar(7)
  declare @periodo_maximo_cuit_temp varchar(7)
 
  select @periodo_maximo = (select replace(convert(char(7), max(fecha_informacion)),'-','') from fuentes_procesadas where visible = '1' and fuente ='cendeu')
  select @periodo_maximo_cuit = max(cendeu.fec_inf) from cendeu where iden = @p_cuit AND vigente = 1 and id_fuente = 2
  select @periodo_maximo_cuit_temp = max(cendeu_temp.fec_inf) from cendeu_temp where iden = @p_cuit AND vigente = 1 and id_fuente = 2
 
  if(@periodo_maximo_cuit_temp > @periodo_maximo_cuit or @periodo_maximo_cuit is null)
  begin
  set @periodo_maximo_cuit = @periodo_maximo_cuit_temp
  end
 
  if (@periodo_maximo > @periodo_maximo_cuit or @periodo_maximo_cuit is null) 
  begin
  set @periodo_maximo_cuit = @periodo_maximo
  end 
  */

 declare @periodo_maximo date,
         @periodo_maximo_cd  varchar(7), 
         @periodo_maximo_cuit varchar(7),
   @periodo_maximo_cuit_temp varchar(7), 
   @fecha_formateada varchar(10),
   @periodo_comparar varchar(6),
   @periodo_comparar1 varchar(6),
   @periodo_comparar2 varchar(6)
 
 
 select @periodo_maximo_cd = replace(convert(char(7), max(fecha_informacion)),'-','') from fuentes_procesadas where visible = '1' and fuente ='cendeu'
 select @periodo_maximo_cuit      = max(cendeu.fec_inf)      from cendeu where iden = @p_cuit AND vigente = 1  and id_fuente = 2
 select @periodo_maximo_cuit_temp = max(cendeu_temp.fec_inf) from cendeu_temp where iden = @p_cuit AND vigente = 1 and id_fuente = 2
 if(@periodo_maximo_cuit_temp > @periodo_maximo_cuit
 OR (@periodo_maximo_cuit is null and @periodo_maximo_cuit_temp is not null)
 )
  begin
  set @periodo_maximo_cuit = @periodo_maximo_cuit_temp
  end

 if (@periodo_maximo_cd > @periodo_maximo_cuit or @periodo_maximo_cuit is null) 
  begin
  set @periodo_maximo_cuit = @periodo_maximo_cd
  end 

 if (@periodo_maximo_cuit is not null) 
 begin
 set @periodo_maximo   = convert(date,@periodo_maximo_cuit+'01')
 set @periodo_comparar = @periodo_maximo_cuit
 end 
 set @periodo_comparar1 = (select MAX(fec_inf) from cendeu b where b.iden = @p_Cuit and b.vigente = 1 and b.fec_inf <= @periodo_comparar) 
 set @periodo_comparar2 = (select MAX(fec_inf) from cendeu_temp b where b.iden = @p_Cuit and b.vigente = 1 and b.fec_inf <= @periodo_comparar) 

 if (@periodo_comparar1 > @periodo_comparar2 or (@periodo_comparar1 is not null and  @periodo_comparar2 is null)) 
 begin
 set @periodo_comparar = @periodo_comparar1
 end 

 if (@periodo_comparar2 > @periodo_comparar1 or (@periodo_comparar2 is not null and  @periodo_comparar1 is null)) 
 begin
 set @periodo_comparar = @periodo_comparar2
 end 
 set @periodo_maximo_cuit   = @periodo_comparar
 set @periodo_maximo   = convert(date,@periodo_comparar+'01')
 if (@periodo_maximo is not null AND @periodo_maximo > dateadd(year,-2,(select max(fecha_informacion) from fuentes_procesadas where visible = '1' and fuente ='cendeu')) ) 
 begin
 select distinct 
  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 exists (select * from cendeu b2 
       where b2.iden = c1.iden
       and  cast(b2.codigo_ent as int) = cast(c1.codigo_ent as int)
       and  b2.fec_inf = c1.fec_inf
       and  b2.vigente = c1.vigente
       and (isnull(b2.id_fuente,1)) = (isnull(c1.id_fuente,1))
       and  b2.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'))
         )
   union all
   select distinct
  fec_inf, isnull(nombre_corto,nombre) as nombre, ((prestamos+particip+garotor+o_cptos+c1.deuda_total)) 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 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 exists (select * from cendeu b2 
       where b2.iden = c1.iden
       and  cast(b2.codigo_ent as int) = cast(c1.codigo_ent as int)
       and  b2.fec_inf = c1.fec_inf
       and  b2.vigente = c1.vigente
       and (isnull(b2.id_fuente,1)) = (isnull(c1.id_fuente,1))
       and  b2.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



END

GO

Used By

No items found