dbo.LN_limite_credito_

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

31
Name Data Type Length Description
@accion varchar 50
@p_contraparte int 4
@p_credito_otorgado decimal 13
@p_recomendacion text -1
@p_user_id int 4
@p_activo_caja_bancos decimal 13
@p_activo_inversiones decimal 13
@p_activo_corriente decimal 13
@p_activo_total decimal 13
@p_pasivo_corriente decimal 13
@p_prestamos_corrientes decimal 13
@p_prestamos_no_corrientes decimal 13
@p_pasivo_total decimal 13
@p_patrimonio decimal 13
@p_patrimonio_integrado decimal 13
@p_resultados_acumulados decimal 13
@p_capital_trabajo decimal 13
@p_rel_capital_trabajo_pasivo decimal 13
@p_rel_capital_trabajo_ventas decimal 13
@p_rel_compras_ventas decimal 13
@p_ventas decimal 13
@p_compras decimal 13
@p_ganancia decimal 13
@p_rentabilidad_final decimal 13
@p_resultado_operativo decimal 13
@p_rentabilidad_operativa decimal 13
@p_ebitda decimal 13
@p_otros_ingresos decimal 13
@p_otros_egresos decimal 13
@p_rel_ebitda_ventas decimal 13
@p_fondos decimal 13

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>

/*

score_Balance
tendencia (5 valores)
escenarios futuros
tendencia (5 valores)
situacion de socios (texto corto)
tendencia (5 valores)



*/

-- Description: <Description,,>
-- =============================================
CREATE OR ALTER PROCEDURE dbo.LN_limite_credito_ 
 -- Add the parameters for the stored procedure here

 @accion         varchar(50) = null,
 @p_contraparte       int = null,

 @p_credito_otorgado      decimal(20,2) = null,
 @p_recomendacion      text = NULL,    
 
 @p_user_id        int = null,

 @p_activo_caja_bancos decimal(20, 2) = NULL,
 @p_activo_inversiones decimal(20, 2) = NULL,
 @p_activo_corriente decimal(20, 2) = NULL,
 @p_activo_total decimal(20, 2) = NULL,
 @p_pasivo_corriente decimal(20, 2) = NULL,
 @p_prestamos_corrientes decimal(20, 2) = NULL,
 @p_prestamos_no_corrientes decimal(20, 2) = NULL,
 @p_pasivo_total decimal(20, 2) = NULL,
 @p_patrimonio decimal(20, 2) = NULL,
 @p_patrimonio_integrado decimal(20, 2) = NULL,
 @p_resultados_acumulados decimal(20, 2) = NULL,
 @p_capital_trabajo decimal(20, 2) = NULL,
 @p_rel_capital_trabajo_pasivo decimal(20, 2) = NULL,
 @p_rel_capital_trabajo_ventas decimal(20, 2) = NULL,
 @p_rel_compras_ventas decimal(20, 2) = NULL,
 @p_ventas decimal(20, 2) = NULL,
 @p_compras decimal(20, 2) = NULL,
 @p_ganancia decimal(20, 2) = NULL,
 @p_rentabilidad_final decimal(20, 2) = NULL,
 @p_resultado_operativo decimal(20, 2) = NULL,
 @p_rentabilidad_operativa decimal(20, 2) = NULL,
 @p_ebitda decimal(20, 2) = NULL,
 @p_otros_ingresos decimal(20, 2) = NULL,
 @p_otros_egresos decimal(20, 2) = NULL,
 @p_rel_ebitda_ventas decimal(20, 2) = NULL,
 @p_fondos decimal(20, 2) = NULL

 
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here

 DECLARE @gestion       int = null
 DECLARE @cuit        varchar(30) = null


 if (@p_contraparte is null)
  return
 else begin

  select @gestion = LN_gestiones.gestion,
    @cuit = padrones.identificacion 
  from LN_gestiones 
  inner join LN_contrapartes on LN_gestiones.contraparte = LN_contrapartes.contraparte
  inner join padrones on LN_contrapartes.id_padron = padrones.id_padron
  where LN_gestiones.contraparte = @p_contraparte
  and LN_gestiones.desde <= GETDATE()
  and LN_gestiones.hasta >= GETDATE()

  if (@gestion is null)
   return

 end

 
 
 if (@accion = 'search') begin

  select *, 
  iif(ultimo_aprobado > 0, format(ultimo_aprobado, '$ ### ### ### ### ###'), NULL) as monto_ultimo_aprobado,
  convert(varchar,ultimo_aprobado_fecha,103) as fecha_ultimo_aprobado,
  iif(ultimo_aprobado_usd > 0, format(ultimo_aprobado_usd, '### ### ### ### ###'), NULL) as monto_ultimo_aprobado_usd,
  convert(varchar,ultimo_aprobado_usd_fecha,103) as fecha_ultimo_aprobado_usd
  from LN_limite_credito
  where contraparte = @p_contraparte
  and gestion = @gestion

 end


 if (@accion = 'actualizar_credito_otorgado') begin

  declare @ultimo decimal(20,2) = 0


  if (@p_credito_otorgado > 0) begin

   declare @nota tinyint = 0

   if not exists (select LN_limite_credito.contraparte, LN_limite_credito.gestion
       from LN_limite_credito
       where LN_limite_credito.contraparte = @p_contraparte
       and LN_limite_credito.gestion = @gestion) begin
   
    INSERT INTO LN_limite_credito
     (contraparte, gestion, ultimo_aprobado, ultimo_aprobado_fecha)
    VALUES
     (@p_contraparte, @gestion, @p_credito_otorgado, GETDATE())


    SET @nota = 1

   end else begin

    select @ultimo = ISNULL(ultimo_aprobado, 0)
    from LN_limite_credito
    where LN_limite_credito.contraparte = @p_contraparte
    and LN_limite_credito.gestion = @gestion

    if (@ultimo <> @p_credito_otorgado) begin

     UPDATE LN_limite_credito SET
      ultimo_aprobado = @p_credito_otorgado,
      ultimo_aprobado_fecha = getdate()
     where LN_limite_credito.contraparte = @p_contraparte
     and LN_limite_credito.gestion = @gestion

     SET @nota = 1

    end

   end

   
   if (@nota = 1) begin

    declare @texto_nota varchar(max) = 'Se actualizó el monto de asistencia otorgado a ' + format(@p_credito_otorgado, '$ ### ### ### ###')

    EXEC LN_limite_credito_notas_ 'ingresar_nota', @p_contraparte, @texto_nota, @p_user_id, 1

   end

  end

 end


 /* idem acción anterior pero para los campos en USD (se utiiza el mismo parámetro que para pesos: @p_credito_otorgado) */
 if (@accion = 'actualizar_credito_otorgado_usd') begin

  declare @ultimo_usd decimal(20,2) = 0


  if (@p_credito_otorgado > 0) begin

   declare @nota_usd tinyint = 0

   if not exists (select LN_limite_credito.contraparte, LN_limite_credito.gestion
       from LN_limite_credito
       where LN_limite_credito.contraparte = @p_contraparte
       and LN_limite_credito.gestion = @gestion) begin
   
    INSERT INTO LN_limite_credito
     (contraparte, gestion, ultimo_aprobado_usd, ultimo_aprobado_usd_fecha)
    VALUES
     (@p_contraparte, @gestion, @p_credito_otorgado, GETDATE())


    SET @nota_usd = 1

   end else begin

    select @ultimo_usd = ISNULL(ultimo_aprobado_usd, 0)
    from LN_limite_credito
    where LN_limite_credito.contraparte = @p_contraparte
    and LN_limite_credito.gestion = @gestion

    if (@ultimo_usd <> @p_credito_otorgado) begin

     UPDATE LN_limite_credito SET
      ultimo_aprobado_usd = @p_credito_otorgado,
      ultimo_aprobado_usd_fecha = getdate()
     where LN_limite_credito.contraparte = @p_contraparte
     and LN_limite_credito.gestion = @gestion

     SET @nota_usd = 1

    end

   end

   
   if (@nota_usd = 1) begin

    declare @texto_nota_usd varchar(max) = 'Se actualizó el monto de asistencia otorgado a USD ' + format(@p_credito_otorgado, '### ### ### ###')

    EXEC LN_limite_credito_notas_ 'ingresar_nota', @p_contraparte, @texto_nota_usd, @p_user_id, 1

   end

  end

 end
 /**/




 if (@accion = 'actualizar_post_balance') begin

  if not exists (select LN_limite_credito.contraparte, LN_limite_credito.gestion
      from LN_limite_credito
      where LN_limite_credito.contraparte = @p_contraparte
      and LN_limite_credito.gestion = @gestion)


   INSERT INTO LN_limite_credito
    (contraparte, gestion,
    post_activo_caja_bancos,
    post_activo_inversiones,
    post_activo_corriente,
    post_activo_total,
    post_pasivo_corriente,
    post_prestamos_corrientes,
    post_prestamos_no_corrientes,
    post_pasivo_total,
    post_patrimonio,
    post_patrimonio_integrado,
    post_resultados_acumulados,
    post_capital_trabajo,
    post_rel_capital_trabajo_pasivo,
    post_rel_capital_trabajo_ventas,
    post_rel_compras_ventas,
    post_ventas,
    post_compras,
    post_ganancia,
    post_rentabilidad_final,
    post_resultado_operativo,
    post_rentabilidad_operativa,
    post_ebitda,
    post_otros_ingresos,
    post_otros_egresos,
    post_rel_ebitda_ventas,
    post_fondos)
   
   VALUES

    (@p_contraparte, @gestion,
    @p_activo_caja_bancos,
    @p_activo_inversiones,
    @p_activo_corriente,
    @p_activo_total,
    @p_pasivo_corriente,
    @p_prestamos_corrientes,
    @p_prestamos_no_corrientes,
    @p_pasivo_total,
    @p_patrimonio,
    @p_patrimonio_integrado,
    @p_resultados_acumulados,
    @p_capital_trabajo,
    @p_rel_capital_trabajo_pasivo,
    @p_rel_capital_trabajo_ventas,
    @p_rel_compras_ventas,
    @p_ventas,
    @p_compras,
    @p_ganancia,
    @p_rentabilidad_final,
    @p_resultado_operativo,
    @p_rentabilidad_operativa,
    @p_ebitda,
    @p_otros_ingresos,
    @p_otros_egresos,
    @p_rel_ebitda_ventas,
    @p_fondos)



  else

   UPDATE LN_limite_credito SET

    post_activo_caja_bancos = @p_activo_caja_bancos,
    post_activo_inversiones = @p_activo_inversiones,
    post_activo_corriente = @p_activo_corriente,
    post_activo_total = @p_activo_total,
    post_pasivo_corriente = @p_pasivo_corriente,
    post_prestamos_corrientes = @p_prestamos_corrientes,
    post_prestamos_no_corrientes = @p_prestamos_no_corrientes,
    post_pasivo_total = @p_pasivo_total,
    post_patrimonio = @p_patrimonio,
    post_patrimonio_integrado = @p_patrimonio_integrado,
    post_resultados_acumulados = @p_resultados_acumulados,
    post_capital_trabajo = @p_capital_trabajo,
    post_rel_capital_trabajo_pasivo = @p_rel_capital_trabajo_pasivo,
    post_rel_capital_trabajo_ventas = @p_rel_capital_trabajo_ventas,
    post_rel_compras_ventas = @p_rel_compras_ventas,
    post_ventas = @p_ventas,
    post_compras = @p_compras,
    post_ganancia = @p_ganancia,
    post_rentabilidad_final = @p_rentabilidad_final,
    post_resultado_operativo = @p_resultado_operativo,
    post_rentabilidad_operativa = @p_rentabilidad_operativa,
    post_ebitda = @p_ebitda,
    post_otros_ingresos = @p_otros_ingresos,
    post_otros_egresos = @p_otros_egresos,
    post_rel_ebitda_ventas = @p_rel_ebitda_ventas,
    post_fondos = @p_fondos

   WHERE contraparte = @p_contraparte
   AND gestion = @gestion

 end

 if (@accion = 'actualizar_recomendacion') begin

  if not exists (select LN_limite_credito.contraparte, LN_limite_credito.gestion
      from LN_limite_credito
      where LN_limite_credito.contraparte = @p_contraparte
      and LN_limite_credito.gestion = @gestion)

   INSERT INTO LN_limite_credito
    (contraparte, gestion, recomendacion)
   VALUES
    (@p_contraparte, @gestion, @p_recomendacion)

  else
  
   UPDATE LN_limite_credito SET recomendacion = @p_recomendacion
   where LN_limite_credito.contraparte = @p_contraparte
   and LN_limite_credito.gestion = @gestion
     

 end


 if (@accion = 'cendeu_trimestral') begin


  declare @p_cuit varchar(30) = @cuit

  declare @p_5anios date = getdate() - (5*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),@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 and id_fuente = 2
  select @dtEnd_cuit2 = 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 begin
   set @dtEnd_cuit = @dtEnd_cuit2
  end 


  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,-2, @dtEnd)
  
  if(@periodo_maximo is not null) begin
   set @fec_periodo_minimo = dateadd(MM, -2,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


  SELECT trimestral.nombre, trimestral.situacion, MAX(trimestral.total) as total
  FROM (

   select distinct 
   fec_inf,codigo_ent,
   isnull(nombre_corto,nombre) as nombre,
   convert(int,situacion) as situacion,
   ((prestamos+particip+garotor+o_cptos+deuda_total)) as total

   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)
    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,codigo_ent,
   isnull(nombre_corto,nombre) as nombre,
   convert(int,situacion) as situacion,
   ((prestamos+particip+garotor+o_cptos+deuda_total)) as total

   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  
    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'))
        )

   ) trimestral

  GROUP BY nombre, situacion
  ORDER BY total desc, nombre

 end

 

 if (@accion = 'activar') begin

  if not exists (select LN_limite_credito.contraparte, LN_limite_credito.gestion
      from LN_limite_credito
      where LN_limite_credito.contraparte = @p_contraparte
      and LN_limite_credito.gestion = @gestion) 

   INSERT INTO LN_limite_credito
    (contraparte, gestion, visible)
   VALUES
    (@p_contraparte, @gestion, 1)

  else

   UPDATE LN_limite_credito SET LN_limite_credito.visible = 1 
   where LN_limite_credito.contraparte = @p_contraparte
   and LN_limite_credito.gestion = @gestion
  
 end

 if (@accion = 'desactivar') begin

  if not exists (select LN_limite_credito.contraparte, LN_limite_credito.gestion
      from LN_limite_credito
      where LN_limite_credito.contraparte = @p_contraparte
      and LN_limite_credito.gestion = @gestion) 

   INSERT INTO LN_limite_credito
    (contraparte, gestion, visible)
   VALUES
    (@p_contraparte, @gestion, 0)

  else

   UPDATE LN_limite_credito SET LN_limite_credito.visible = 0
   where LN_limite_credito.contraparte = @p_contraparte
   and LN_limite_credito.gestion = @gestion

 end

END
GO

Used By

No items found