dbo.ws_obtener_cuadro_bcra

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.ws_obtener_cuadro_bcra
@p_Cuit varchar(20)
AS


BEGIN
    SET NOCOUNT ON;
 declare @dtStart date, @dtEnd date, @dtEnd_cuit 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

 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, -24,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

    declare @codigo_ent varchar(5)
 declare @nombre_corto varchar(100)
 declare @nombre varchar(200)
 declare @situacion varchar(2)
 declare @situacion_char varchar(50)
 declare @primer_mes varchar(6)
 declare @ultimo_mes varchar(6)
 declare @monto_maximo numeric(12,1)
 declare @ultimo_monto numeric(12,1)
 declare @mes_1 varchar(7)
 declare @mes_2 varchar(7)
 declare @monto_1 char(13)
 declare @monto_2 char(13)
 declare @respuesta table (cod int, nombre_corto varchar(100), nombre varchar(100), mes_1 char(7), mes_2 char(7), sit int, situacion char(26), max_monto char(15), ult_monto char(15))

DECLARE bn CURSOR

FOR

 select 
     convert(int,codigo_ent) as codigo_ent, 
  nombre_corto,
  nombre,
  --convert(int,situacion) as situacion,
  substring(convert(char(2),convert(int,cendeu.situacion)),1,1) as situacion, 
  min(fec_inf), 
  max(fec_inf),
  max((prestamos+particip+garotor+o_cptos+deuda_total)) as maximo_monto
 
 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)

 group by 
  convert(int,codigo_ent),
  nombre_corto, 
  nombre,
  convert(int,situacion)

 union all 
 select 
     convert(int,codigo_ent) as codigo_ent, 
  nombre_corto,
  nombre,
  --convert(int,situacion) as situacion,
  substring(convert(char(2),convert(int,cendeu.situacion)),1,1) as situacion, 
  min(fec_inf), 
  max(fec_inf),
  max((prestamos+particip+garotor+o_cptos+deuda_total)) as maximo_monto
 
 from cendeu_temp 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)

 group by 
  convert(int,codigo_ent),
  nombre_corto, 
  nombre,
  convert(int,situacion)

 order by 2, 6 asc

 OPEN bn 

  -- Lectura de la primera fila del cursor
 Fetch next from bn INTO @codigo_ent, @nombre_corto, @nombre, @situacion, @primer_mes, @ultimo_mes, @monto_maximo
 
 WHILE (@@FETCH_STATUS <> -1 )
 BEGIN
  set @ultimo_monto = (SELECT top 1 (prestamos+particip+garotor+o_cptos+deuda_total)
       from cendeu
         where iden = @p_Cuit 
        and fec_inf = @ultimo_mes
        and convert(int,codigo_ent) = convert(int,@codigo_ent)
        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))
   if (@ultimo_monto is null) 
   begin
    set @ultimo_monto = (SELECT top 1 (prestamos+particip+garotor+o_cptos+deuda_total)
       from cendeu_temp cendeu
         where iden = @p_Cuit 
        and fec_inf = @ultimo_mes
        and convert(int,codigo_ent) = convert(int,@codigo_ent)
        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))

   end 

   if (@nombre_corto=null) 
      begin
      set @nombre_corto = @nombre
   end

   set @situacion_char = (select case convert(int,@situacion) 
          when '1' then 'SITUACION NORMAL' 
          when '2' then 'CON RIESGO POTENCIAL' 
          when '3' then 'CON PROBLEMAS' 
          when '4' then 'ALTO RIESGO DE INSOLVENCIA' 
          when '5' then 'IRRECUPERABLE' 
          when '6' then 'IRRECUPERABLE DISP.TECNICA' 
          when '9' then 'DADO DE BAJA CONTABLEMENTE' 
          when '0' then 'STANDARD' 
        end)

   set @mes_1 = SUBSTRING(@primer_mes,5,2) + '/' + SUBSTRING(@primer_mes,1,4) 
   set @mes_2 = SUBSTRING(@ultimo_mes,5,2) + '/' + SUBSTRING(@ultimo_mes,1,4) 
   set @monto_1 = RIGHT('             ' + Ltrim(Rtrim(@monto_maximo)),13)
   set @monto_2 = RIGHT('             ' + Ltrim(Rtrim(@ultimo_monto)),13)
   
     
   insert into @respuesta values(convert(int,@codigo_ent),@nombre_corto,@nombre, @mes_1, @mes_2, convert(int,@situacion), @situacion_char, @monto_1, @monto_2)
    
   Fetch next from bn INTO  @codigo_ent, @nombre_corto, @nombre, @situacion, @primer_mes, @ultimo_mes, @monto_maximo
 END
 -- Cierra el cursor
 CLOSE bn
 -- Libera los recursos del cursor
 DEALLOCATE bn 
 select * from @respuesta 
END 

GO

Used By

1