dbo.LN_capacidad_endeudamiento_Balance

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

5
Name Data Type Length Description
@contraparte int 4
@cuit varchar 20
@monto decimal 13
@leyenda varchar 200
@fecha date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.LN_capacidad_endeudamiento_Balance 
  @contraparte int,
  @cuit varchar(20) = null, 
  @monto decimal(20,2) = null output,
  @leyenda varchar(200) = null output, 
  @fecha date = null output 
AS
BEGIN
  SET NOCOUNT ON; 

  if (@contraparte is not null) 
  begin
   select @cuit = identificacion from padrones 
   where id_padron = (select id_padron from LN_contrapartes where contraparte = @contraparte)  
  end 

  if (@cuit is null)  -- No encontrado
  begin
   set @monto = null
   set @leyenda = null 
   set @fecha = null 
   return 
  end 

  -- Si tiene balance de los ultimos 18 meses
  declare @hay_balance int = (select COUNT(*) from balances_contables where cuit = @cuit and fecha_balance >= dateadd(month, -18, getdate()))
  if (@hay_balance = 0) 
  begin
   set @monto = 0.01
   set @leyenda = null 
   set @fecha = null 
   return 
  end 
  declare @fecha_bal date = (select MAX(fecha_balance) from balances_contables where cuit = @cuit and fecha_balance >= dateadd(month, -18, getdate()))
  declare  @ac_cajban decimal(20,2) = null,
     @ac_inver decimal(20,2) = null,
     @ac_crevta decimal(20,2) = null,
     @ac_otrcre decimal(20,2) = null,
     @ac_biecam decimal(20,2) = null,
     @ac_otract decimal(20,2) = null,
     @ac_bietan decimal(20,2) = null,
     @ac_total decimal(20,2) = null,
     @anc_crevta decimal(20,2) = null,
     @anc_otrcre decimal(20,2) = null,
     @anc_inver decimal(20,2) = null,
     @anc_actint decimal(20,2) = null,
     @anc_otract decimal(20,2) = null,
     @anc_bieuso decimal(20,2) = null,
     @anc_bieusovo decimal(20,2) = null,
     @anc_depacu decimal(20,2) = null,
     @anc_total decimal(20,2) = null,
     @act_total decimal(20,2) = null,
     @pc_ctapag decimal(20,2) = null,
     @pc_presta decimal(20,2) = null,
     @pc_remune decimal(20,2) = null,
     @pc_carfis decimal(20,2) = null,
     @pc_antcli decimal(20,2) = null,
     @pc_otrpas decimal(20,2) = null,
     @pc_total decimal(20,2) = null,
     @pnc_cuepag decimal(20,2) = null,
     @pnc_previ decimal(20,2) = null,
     @pnc_presta decimal(20,2) = null,
     @pnc_otrpas decimal(20,2) = null,
     @pnc_deufis decimal(20,2) = null,
     @pnc_total decimal(20,2) = null,
     @pas_total decimal(20,2) = null,
     @capsoc decimal(20,2) = null,
     @resacu decimal(20,2) = null,
     @res_noasig decimal(20,2) = null,
     @patrimonio decimal(20,2) = null,
     @res_vtanet decimal(20,2) = null,
     @res_compras decimal(20,2) = null,
     @res_cosmer decimal(20,2) = null,
     @amortiza decimal(20,2) = null,
     @res_gascom decimal(20,2) = null,
     @res_gasadm decimal(20,2) = null,
     @res_gasfin decimal(20,2) = null,
     @res_otrosing decimal(20,2) = null,
     @res_otrosegr decimal(20,2) = null,
     @res_resfin decimal(20,2) = null,
     @res_iva decimal(20,2) = null,
     @ganancia decimal(20,2) = null,
     @ebitda decimal(20,2) = null,
     @ajucap decimal(20,2) = null,
     @ind_liqcorr decimal(20,2) = null,
     @ind_liqseca decimal(20,2) = null,
     @ind_endeu decimal(20,2) = null,
     @ind_solvencia decimal(20,2) = null,
     @ind_endeuact decimal(20,2) = null,
     @ind_captra decimal(20,2) = null,
     @ind_cappro decimal(20,2) = null,
     @ind_invbu decimal(20,2) = null,
     @ind_inmact decimal(20,2) = null,
     @ind_inmpat decimal(20,2) = null,
     @ind_marbru decimal(20,2) = null,
     @ind_utilfin decimal(20,2) = null,
     @ind_retpat decimal(20,2) = null,
     @ind_rotvta decimal(20,2) = null,
     @ind_rotvta_dias decimal(20,2) = null,
     @ind_rotact decimal(20,2) = null,
     @ind_rotact_dias decimal(20,2) = null,
     @ind_rentabilidad decimal(20,2) = null,
     @ind_indrot decimal(20,2) = null,
     @ind_indrot_dias decimal(20,2) = null,
     @ind_rotinv decimal(20,2) = null,
     @ind_rotinv_dias decimal(20,2) = null,
     @ind_prest_pn decimal(20,2) = null,
     @ind_prest_vtas decimal(20,2) = null,
     @ind_solvencia_pat decimal(20,2) = null,
     @ind_ebipas decimal(20,2) = null,
     @resacu_ant decimal(20,2) = null,
     @res_noasig_ant decimal(20,2) = null

  select top 1 
     @ac_cajban = ac_cajban,
     @ac_inver = ac_inver,
     @ac_crevta = ac_crevta,
     @ac_otrcre = ac_otrcre,
     @ac_biecam = ac_biecam,
     @ac_otract = ac_otract,
     @ac_bietan = ac_bietan,
     @ac_total = ac_total,
     @anc_crevta = anc_crevta,
     @anc_otrcre = anc_otrcre,
     @anc_inver = anc_inver,
     @anc_actint = anc_actint,
     @anc_otract = anc_otract,
     @anc_bieuso = anc_bieuso,
     @anc_bieusovo = anc_bieusovo,
     @anc_depacu = anc_depacu,
     @anc_total = anc_total,
     @act_total = act_total,
     @pc_ctapag = pc_ctapag,
     @pc_presta = pc_presta,
     @pc_remune = pc_remune,
     @pc_carfis = pc_carfis,
     @pc_antcli = pc_antcli,
     @pc_otrpas = pc_otrpas,
     @pc_total = pc_total,
     @pnc_cuepag = pnc_cuepag,
     @pnc_previ = pnc_previ,
     @pnc_presta = pnc_presta,
     @pnc_otrpas = pnc_otrpas,
     @pnc_deufis = pnc_deufis,
     @pnc_total = pnc_total,
     @pas_total = pas_total,
     @capsoc = capsoc,
     @resacu = resacu,
     @res_noasig = res_noasig,
     @patrimonio = patrimonio,
     @res_vtanet = res_vtanet,
     @res_compras = res_compras,
     @res_cosmer = res_cosmer,
     @amortiza = amortiza,
     @res_gascom = res_gascom,
     @res_gasadm = res_gasadm,
     @res_gasfin = res_gasfin,
     @res_otrosing = res_otrosing,
     @res_otrosegr = res_otrosegr,
     @res_resfin = res_resfin,
     @res_iva = res_iva,
     @ganancia = ganancia,
     @ebitda = ebitda,
     @ajucap = ajucap,
     @ind_liqcorr = ind_liqcorr,
     @ind_liqseca = ind_liqseca,
     @ind_endeu = ind_endeu,
     @ind_solvencia = ind_solvencia,
     @ind_endeuact = ind_endeuact,
     @ind_captra = ind_captra,
     @ind_cappro = ind_cappro,
     @ind_invbu = ind_invbu,
     @ind_inmact = ind_inmact,
     @ind_inmpat = ind_inmpat,
     @ind_marbru = ind_marbru,
     @ind_utilfin = ind_utilfin,
     @ind_retpat = ind_retpat,
     @ind_rotvta = ind_rotvta,
     @ind_rotvta_dias = ind_rotvta_dias,
     @ind_rotact = ind_rotact,
     @ind_rotact_dias = ind_rotact_dias,
     @ind_rentabilidad = ind_rentabilidad,
     @ind_indrot = ind_indrot,
     @ind_indrot_dias = ind_indrot_dias,
     @ind_rotinv = ind_rotinv,
     @ind_rotinv_dias = ind_rotinv_dias,
     @ind_prest_pn = ind_prest_pn,
     @ind_prest_vtas = ind_prest_vtas,
     @ind_solvencia_pat = ind_solvencia_pat,
     @ind_ebipas = ind_ebipas,
     @resacu_ant = resacu_ant,
     @res_noasig_ant = res_noasig_ant
   from balances_contables 
  where cuit = @cuit 
   and fecha_balance >= dateadd(month, -18, getdate())
   and isnull(balances_contables.visible,0) = 1
   and isnull(balances_contables.estado,'') = 'PUBLICADO'
  order by fecha_balance desc 

  IF (@patrimonio < 0)
  begin 
   set @monto   = 0
   set @Leyenda = 'CONTADO - PATRIMINIO NETO NEGATIVO'
   set @fecha   = @fecha_bal
   return 
  end 

  declare @W_Monto decimal(20,2) = 0
  
  declare @Activo_fijo   decimal(20,2) = @anc_bieuso + @anc_actint
  declare @Capital       decimal(20,2) = @ac_total   - @pc_Total
  declare @Activo_reali  decimal(20,2) = @anc_crevta + @anc_otrcre + @anc_inver + @anc_otract

  set @W_Monto  = @patrimonio   * 3 / 100 +
      @activo_fijo  * 1 / 100 +
      @capital      * 2 / 100 +
      @activo_reali * 0.5 / 100

  IF (@W_Monto IS NULL) set @w_monto = 0

  IF (@W_Monto < 0)
   begin 
    set @monto   = 0
    set @leyenda = null 
    set @fecha = @fecha_bal
    return 
   end 
  
  
  declare @W_Monto_Total decimal(20,2)= @W_Monto
  -- PRUEBA DE UTILIDAD:
  -- Si tiene Utilidad Neta "NEGATIVA" e igual o mayor al 5% de las
  -- Ventas Anuales pone "CONTADO" pero
  -- si tiene Utilidad Neta "NEGATIVA" y menor al 5% de las
  -- Ventas Anuales entonces resta un 30%
   
  IF (@ganancia < 0) begin
         IF (@ganancia * (-1) >= @res_vtanet * 0.05) begin
     set @Leyenda = 'CONTADO - UTILIDAD NETA NEGATIVA'
    set @monto   = 0
    set @fecha = @fecha_bal
    return    
   end 
   else 
   begin
    set @W_Monto_Total = @W_Monto_Total - (@W_Monto * 0.30);
   end
  end 

  declare @W_Cumple int = 0

  -- PRUEBAS DE LIQUIDEZ
  -- Si Liquidez Corriente >= 1     ==> sumo 0.5  %
  -- Si Prueba Acida       >= 0,5   ==> sumo 0.25 %
  -- Si Liquidez Inmediata >= 0,10  ==> sumo 0.25 %
  -- Si Premio Liquidez (las 3 anteriores OK) ==> sumo 0.25 %

  -- LIQUIDEZ CORRIENTE = ACTIVO CORRIENTE / PASIVO CORRIENTE
  IF (@pc_total != 0) begin 
   declare @Liquidez decimal(20,2) = @ac_total / @pc_total
   IF (@Liquidez >= 1) begin
    set @w_monto_total = @w_monto_total + (@Patrimonio * 0.5 / 100)
    set @W_Cumple = @W_Cumple + 1
   end 
  end

  -- LIQUIDEZ ACIDA
  -- (DISPONIBILIDADES + IVERSIONES CORRIENTES + CREDITOS CORRIENTES) / PASIVO CORRIENTE
  IF (@pc_total != 0) begin 
   declare @Liquidezacida decimal(20,2) = (@ac_cajban + @ac_inver + @ac_crevta + @ac_otrcre) / @pc_total
   IF (@Liquidezacida >= 0.5) begin
    set @w_monto_total = @w_monto_total + (@Patrimonio * 0.25 / 100)
    set @W_Cumple = @W_Cumple + 1
   end 
  end
    
  -- LIQUIDEZ INMEDIATA
  -- CAJA + BANCOS / PASIVO CTE.
  IF (@pc_total != 0) begin
   declare @Liquidezinm decimal(20,2) =  @ac_cajban / @pc_total
   IF (@Liquidezacida >= 0.10) begin
    set @w_monto_total = @w_monto_total + (@Patrimonio * 0.25 / 100)
    set @W_Cumple = @W_Cumple + 1
   end 
  end

  if (@W_Cumple = 3) begin 
   set @w_monto_total = @w_monto_total + (@Patrimonio * 0.25 / 100)
  end

  -- PRUEBA DE ENDEUDAMIENTO
  -- Si Endeudamiento Total =< 1           ==> sumo 0.5 %
  -- Si Endeudamiento a Corto Plazo =< 1  ==> sumo 0.5 %
  -- Premio Endeudamiento                  ==> sumo 0.25 %
  
  set @W_Cumple = 0

  -- ENDEUDAMIENTO TOTAL: PASIVO TOTAL / PATRIMONIO NETO
  IF (@patrimonio != 0) begin
   declare @Endeudamiento decimal(20,2) = @pas_total / @Patrimonio
     IF (@Endeudamiento <= 1) begin
    set @w_monto_total = @w_monto_total + (@Patrimonio * 0.5 / 100)
    set @W_Cumple = @W_Cumple + 1
   end
  end 
  -- ENDEUDAMIENTO A CORTO PLAZO: PASIVO CORRIENTO / PATRIMONIO NETO
  IF (@patrimonio != 0) begin
   declare @Endeudamiento_corto decimal(20,2) = @pc_total / @Patrimonio
   IF (@Endeudamiento_corto <= 1) begin
    set @w_monto_total = @w_monto_total + (@Patrimonio * 0.5 / 100)
    set @W_Cumple = @W_Cumple + 1
   end
  end 
   
  if (@W_Cumple = 2) begin 
   set @w_monto_total = @w_monto_total + (@Patrimonio * 0.25 / 100)
  end

  -- PRUEBA DE SOLVENCIA
  -- Si Solvencia >= 1 ==>  sumo 0.5 %
  IF (@pas_total != 0) begin
   declare @Solvencia decimal(20,2) = @act_total / @pas_total
   IF (@Solvencia >= 1) begin
    set @w_monto_total = @w_monto_total + (@Patrimonio * 0.5 / 100)
   end
  end 

  -- PRUEBA DE CAPITAL DE TRABAJO
  -- Si Cap. Trab. Ejer. - Cap. Trab Ejer. ant. (Debe haber aumento > 10%)
  -- ==> Si cumple sumo 0.5 %
  declare @I int = 0
  declare @W_1_capital decimal(20,2) = NULL
  declare @W_2_capital decimal(20,2) = NULL
  declare @fecha_balance date
  declare @w_1_bal_fecha date = NULL
  declare @w_2_bal_fecha date = NULL
  declare @W_Capital decimal(20,2)

  DECLARE C_bal CURSOR FOR
               select fecha_balance, ac_total, pc_total
          from balances_contables 
     where cuit = @cuit 
       and isnull(balances_contables.visible,0) = 1
       and isnull(balances_contables.estado,'') = 'PUBLICADO'
     order by fecha_balance desc 

  OPEN C_bal
  FETCH NEXT FROM C_bal INTO @fecha_balance, @ac_total, @pc_total
  WHILE @@fetch_status = 0
  BEGIN                 
         set @W_Capital  = @ac_total - @pc_total
   set @I = @I + 1
   IF @I = 1 begin
       set @W_1_capital = @W_Capital
       set @W_1_bal_fecha = @fecha_balance
   END 
   IF @I = 2 begin 
       IF (MONTH(@fecha_balance) = MONTH(@W_1_bal_fecha) AND
           YEAR(@fecha_balance)  = YEAR(@W_1_bal_fecha) - 1) begin
          set @W_2_capital   = @W_capital
          set @W_2_bal_fecha = @fecha_balance
       break 
       end  
   end 
   FETCH NEXT FROM C_bal INTO @fecha_balance, @ac_total, @pc_total
  END
  CLOSE C_bal
  DEALLOCATE C_bal 
  declare @W_Diferencia_1 decimal(20,2) = NULL
  IF @W_1_capital != 0 AND
     @W_2_capital != 0 begin 
     set @W_Diferencia_1 = @W_1_capital - @W_2_capital
     IF @W_Diferencia_1 > 0 begin 
        set @W_Diferencia_1 = (100 * @W_Diferencia_1) / ABS(@W_2_capital);
     END 
     IF @W_Diferencia_1 > 10 begin
        set @W_Monto_Total = @W_Monto_Total + (@W_Monto * 0.5 / 100);
     END 
  END 

  set @monto = @W_Monto_Total
  set @leyenda = null 
        
END

GO