dbo.LN_score_judicial_dic2019

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@contraparte int 4
@score_judicial decimal 9

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

/*
-- =============================================
-- Author: <María Inés Soria>
-- Create date: <19/03/2019>
-- Description: Cálculo de Score de alertas
-- =============================================
DECLARE @score_judicial decimal(10,2)
EXEC [dbo].[LN_score_judicial_dic2019] 156, @score_judicial output
select @score_judicial as score_judicial

select * from ln_contrapartes where id_padron = (select id_padron from padrones where identificacion = '30500007539')
*/

CREATE OR ALTER PROCEDURE dbo.LN_score_judicial_dic2019 
  @contraparte int,
  @score_judicial decimal(10,2) = null output
AS
BEGIN
 SET NOCOUNT ON;
 declare @detalle varchar(max) = null
 declare @cuit varchar(20) = (select identificacion from padrones where id_padron = (select id_padron from ln_contrapartes where contraparte = @contraparte))
 declare @cliente int = (select cliente from ln_contrapartes where contraparte = @contraparte)

 declare @puntaje_juicios decimal(10,2) = 100
 declare @puntaje_concursos_quiebras decimal(10,2) = 100
 
 declare @dias        int = null 
 declare @levantados  char(1) = null 
 declare @cant        int = null 
 declare @puntos      decimal(5,2) = null 
 declare @tabla       varchar(20) = null 

-- Es personalizado  
if (select count(*) from ln_dias_alertas where tabla like 'judicial%' and cliente = @Cliente) > 0 
begin

 declare @score_cyq int = 100
 declare @score_pq  int = 100
 declare @score_ojc int = 100

 -- 1) Concursos y Quiebras
 select @dias       = dias, 
     @levantados = levantados, 
     @cant   = isnull(cantidad,1), 
     @puntos = puntos,
     @tabla  = tabla
   from ln_dias_alertas where tabla = 'judicial_CYQ' and cliente = @Cliente
 if (@tabla = 'judicial_CYQ') 
 begin
   declare @cantidad_cyq int = (SELECT COUNT(*) 
         FROM juicios 
         inner join tipos_causas on
              tipos_causas.id_tipo_causa = juicios.id_tipo_causa
         left join tiempo_causas on
             tiempo_causas.cau_codigo   = juicios.id_tipo_causa
         WHERE cuit_demandado = @cuit
         and juicios.visible = 1 and juicios.fecha_causa > GETDATE()-(isnull(tiempo,5)*365)
         and juicios.fecha_causa >= GETDATE() - isnull(@dias,5*365) 
         and tipos_causas.grupo = 'CYQ')
  if (@cantidad_cyq >= @cant) set @score_cyq = @puntos
 end    

 -- 2) Pedidos de quiebras
 select @dias       = dias, 
     @levantados = levantados, 
     @cant   = isnull(cantidad,1), 
     @puntos = puntos,
     @tabla  = tabla
   from ln_dias_alertas where tabla = 'judicial_PQ' and cliente = @Cliente
 if (@tabla = 'judicial_PQ') 
   begin
   declare @cantidad_pq int = (SELECT COUNT(*) 
         FROM juicios 
         inner join tipos_causas on
              tipos_causas.id_tipo_causa = juicios.id_tipo_causa
         left join tiempo_causas on
             tiempo_causas.cau_codigo   = juicios.id_tipo_causa
         WHERE cuit_demandado = @cuit
         and juicios.visible = 1 and juicios.fecha_causa > GETDATE()-(isnull(tiempo,5)*365)
         and juicios.fecha_causa >= GETDATE() - isnull(@dias,5*365) 
         and tipos_causas.grupo = 'PQ')
  if (@cantidad_pq >= @cant) set @score_pq = @puntos
 end 
 -- 3) Otros juicios 
 select @dias       = dias, 
     @levantados = levantados, 
     @cant   = isnull(cantidad,1), 
     @puntos = puntos,
     @tabla  = tabla
   from ln_dias_alertas where tabla = 'judicial_OJC' and cliente = @Cliente
 if (@tabla = 'judicial_OJC') 
 begin
   declare @cantidad_ojc int = (SELECT COUNT(*) 
         FROM juicios 
         inner join tipos_causas on
              tipos_causas.id_tipo_causa = juicios.id_tipo_causa
         left join tiempo_causas on
             tiempo_causas.cau_codigo   = juicios.id_tipo_causa
         WHERE cuit_demandado = @cuit
         and juicios.visible = 1 and juicios.fecha_causa > GETDATE()-(isnull(tiempo,5)*365)
         and juicios.fecha_causa >= GETDATE() - isnull(@dias,5*365) 
         and tipos_causas.grupo = 'OJC')
   if (@cantidad_ojc >= @cant) set @score_ojc = @puntos
 end 

 if (@score_cyq = -100 or @score_pq = -100) 
 begin 
  set @puntaje_concursos_quiebras = -100
  if (@detalle is null) 
   set @detalle = 'ASPECTOS CONCURSALES / QUIEBRAS'
   else 
   set @detalle = @detalle + '; ASPECTOS CONCURSALES / QUIEBRAS'
 end 
 if (@score_ojc = -100) 
 begin 
  set @puntaje_juicios = -100
  if (@detalle is null) 
   set @detalle = 'ASPECTOS JUDICIALES'
   else 
   set @detalle = @detalle + '; ASPECTOS JUDICIALES'
 end 
end 
else 
begin 

-- si no tiene pesonalizado, toma el score general

 /*
  101 ASPECTOS CONCURSALES
  1 QUIEBRAS: QUE NO ESTEN: QUIEBRA REVOCADA/ANULADA  QUIEBRA DESISTIDA  QUIEBRA ANULADA RECHAZO
  2 PROPIA QUIEBRA SOLICITADA - INHABILITACION JUDICIAL RECHAZO
  6 CONCURSO SOLICITADO (mas de 3 AÑOS) le saca 25%
  7 CONCURSO SOLICITADO (ENTRE 1 Y 3 AÑOS) RECHAZO
  8 CONCURSOS Y PETICIONES DE CONCURSOS: QUE NO ESTEN DESISTIDOS / RECHAZADOS: ULTIMO AÑO RECHAZO
  */


 /* Quiebras */
 declare @quiebras int = (SELECT COUNT(*) 
 FROM juicios CYQ0
 left join tiempo_causas tc1 on tc1.cau_codigo = CYQ0.id_tipo_causa
 left join tiempo_causas tc2 on tc2.cau_codigo = CYQ0.cau_codigo
 WHERE CYQ0.cuit_demandado = @cuit
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc1.tiempo,5)*365))
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc2.tiempo,5)*365))
 AND ( CYQ0.id_tipo_causa IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 0 and grupo_score != 'CRC')
   OR CYQ0.cau_codigo IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 0 and grupo_score != 'CRC'))
 AND NOT EXISTS -- que no esten levantadas
  (SELECT id_juicio
  FROM juicios CYQ1
  WHERE CYQ1.cuit_demandado = @cuit
  AND (CYQ1.id_tipo_causa IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 1 and grupo_score != 'CRC')
    OR CYQ0.cau_codigo IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 1 and grupo_score != 'CRC'))
  AND CYQ1.fecha_causa >= CYQ0.fecha_causa))
 
 /* Concursos de más de 3 años */ 
 declare @concursos_anios_ant int = (SELECT COUNT(*) 
 FROM juicios CYQ0
 left join tiempo_causas tc1 on tc1.cau_codigo = CYQ0.id_tipo_causa
 left join tiempo_causas tc2 on tc2.cau_codigo = CYQ0.cau_codigo
 WHERE CYQ0.cuit_demandado = @cuit
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc1.tiempo,5)*365))
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc2.tiempo,5)*365))
 AND CYQ0.fecha_causa <= dateadd(year,(-3),getdate())
 AND ( CYQ0.id_tipo_causa IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 0 and grupo_score = 'CRC')
   OR CYQ0.cau_codigo IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 0 and grupo_score = 'CRC'))
 AND NOT EXISTS -- que no esten levantadas
  (SELECT id_juicio
  FROM juicios CYQ1
  WHERE CYQ1.cuit_demandado = @cuit
  AND (CYQ1.id_tipo_causa IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 1 and grupo_score = 'CRC')
    OR CYQ0.cau_codigo IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 1 and grupo_score = 'CRC'))
  AND CYQ1.fecha_causa >= CYQ0.fecha_causa))

 /* Concursos ultimos 3 años*/ 
 declare @concursos_ultimos_anios int = (SELECT COUNT(*) 
 FROM juicios CYQ0
 left join tiempo_causas tc1 on tc1.cau_codigo = CYQ0.id_tipo_causa
 left join tiempo_causas tc2 on tc2.cau_codigo = CYQ0.cau_codigo
 WHERE CYQ0.cuit_demandado = @cuit
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc1.tiempo,5)*365))
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc2.tiempo,5)*365))
 AND CYQ0.fecha_causa >= dateadd(year,(-3),getdate())
 AND ( CYQ0.id_tipo_causa IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 0 and grupo_score = 'CRC')
   OR CYQ0.cau_codigo IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 0 and grupo_score = 'CRC'))
 AND NOT EXISTS -- que no esten levantadas
  (SELECT id_juicio
  FROM juicios CYQ1
  WHERE CYQ1.cuit_demandado = @cuit
  AND (CYQ1.id_tipo_causa IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 1 and grupo_score = 'CRC')
    OR CYQ0.cau_codigo IN (SELECT id_tipo_causa FROM tipos_causas WHERE control_comercial = 1 and grupo_score = 'CRC'))
  AND CYQ1.fecha_causa >= CYQ0.fecha_causa))

 if (@quiebras > 0) 
 begin 
  set @puntaje_concursos_quiebras = -100
  if (@detalle is null) 
   set @detalle = 'ASPECTOS CONCURSALES / QUIEBRAS'
   else 
   set @detalle = @detalle + '; ASPECTOS CONCURSALES / QUIEBRAS'
 end 
 if (@concursos_ultimos_anios > 0) 
 begin
  set @puntaje_concursos_quiebras = -100
  if (@detalle is null) 
   set @detalle = 'CONCURSOS EN LOS ÚLTIMOS 3 AÑOS'
   else 
   set @detalle = @detalle + '; CONCURSOS EN LOS ÚLTIMOS 3 AÑOS' 
 end 

 if (@puntaje_concursos_quiebras != -100) 
 begin
  if (@concursos_anios_ant > 0) set @puntaje_concursos_quiebras = 75
 end 
 
 /*
  ASPECTOS JUDICIALES
  JUICIOS AÑOS ANTERIORES  >= 10 Y NO TIENE EN EL ULTIMO AÑO 20%
  JUICIOS AÑOS ANTERIORES  ENTRE 5 Y 10 Y NO TIENE EN EL ULTIMO AÑO 50%
  JUICIOS AÑOS ANTERIORES  < 5 Y NO TIENE EN EL ULTIMO AÑO 80%
  JUICIOS ULTIMO AÑO        RECHAZO
  */


 /* Juicios ultimo año */ 
 declare @juicios_ultimo_anio int = (SELECT COUNT(*) 
 FROM juicios CYQ0
 left join tiempo_causas tc1 on tc1.cau_codigo = CYQ0.id_tipo_causa
 left join tiempo_causas tc2 on tc2.cau_codigo = CYQ0.cau_codigo
 WHERE CYQ0.cuit_demandado = @cuit
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc1.tiempo,5)*365))
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc2.tiempo,5)*365))
 AND CYQ0.fecha_causa >= dateadd(year,(-1),getdate())
 /*
  AND ( CYQ0.id_tipo_causa IN (SELECT id_tipo_causa FROM tipos_causas WHERE grupo in ('JO','OJC','PQ'))
  OR CYQ0.cau_codigo IN (SELECT id_tipo_causa FROM tipos_causas WHERE grupo in ('JO','OJC','PQ')))
  */

 
 and ( CYQ0.id_tipo_causa IN
    ('JCA',--JUICIO DE APREMIO
                 'JE', --JUICIO EJECUTIVO
                 'EH', --JUICIO EJECUTIVO POR HONORARIOS
                 'JO', --JUICIO ORDINARIO
                 'JPC',--JUICIO POR COBRO DE PESOS
                 'JP', --JUICIO PRENDARIO
                 'JS', --JUICIO SUMARIO
                 'JSM',--JUICIO SUMARISIMO
                 'SPR',--SECUESTRO PRENDARIO
                 'STO',--SUSP.TRANSIT. P/OPERAR
                 'EJF',--EJECUCION FISCAL
                 'EJH',--EJECUCION HIPOTECARIA
                 'EJP',--EJECUCION PRENDARIA
                 'IDF')--INHAB.P/DESEMP.FUNC.ENTID.FIN.(ART.41/5 LEY 21526)
 OR CYQ0.cau_codigo IN 
    ('JCA',--JUICIO DE APREMIO
                 'JE', --JUICIO EJECUTIVO
                 'EH', --JUICIO EJECUTIVO POR HONORARIOS
                 'JO', --JUICIO ORDINARIO
                 'JPC',--JUICIO POR COBRO DE PESOS
                 'JP', --JUICIO PRENDARIO
                 'JS', --JUICIO SUMARIO
                 'JSM',--JUICIO SUMARISIMO
                 'SPR',--SECUESTRO PRENDARIO
                 'STO',--SUSP.TRANSIT. P/OPERAR
                 'EJF',--EJECUCION FISCAL
                 'EJH',--EJECUCION HIPOTECARIA
                 'EJP',--EJECUCION PRENDARIA
                 'IDF')--INHAB.P/DESEMP.FUNC.ENTID.FIN.(ART.41/5 LEY 21526)
  )
 )
 /* Juicios años anteriores */ 
 declare @juicios_anios_ant int = (SELECT COUNT(*) 
 FROM juicios CYQ0
 left join tiempo_causas tc1 on tc1.cau_codigo = CYQ0.id_tipo_causa
 left join tiempo_causas tc2 on tc2.cau_codigo = CYQ0.cau_codigo
 WHERE CYQ0.cuit_demandado = @cuit
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc1.tiempo,5)*365))
 AND CYQ0.fecha_causa > (GETDATE()-(isnull(tc2.tiempo,5)*365))
 AND CYQ0.fecha_causa < dateadd(year,(-1),getdate())
 AND ( CYQ0.id_tipo_causa IN (SELECT id_tipo_causa FROM tipos_causas WHERE grupo in ('JO','OJC','PQ'))
   OR CYQ0.cau_codigo IN (SELECT id_tipo_causa FROM tipos_causas WHERE grupo in ('JO','OJC','PQ'))))
 

 
 if (@juicios_ultimo_anio > 0) 
 begin 
  set @puntaje_juicios = -100
  if (@detalle is null) 
   set @detalle = 'ASPECTOS JUDICIALES EN EL ULTIMO AÑO'
   else 
   set @detalle = @detalle + '; ASPECTOS JUDICIALES EN EL ULTIMO AÑO'
 end 
 if (@puntaje_juicios != -100) 
 begin
  if (@juicios_anios_ant > 0) 
   set @puntaje_juicios = (case 
         when @juicios_anios_ant >= 10 then 20
         when @juicios_anios_ant >= 5 and @juicios_anios_ant < 10 then 50
         when @juicios_anios_ant < 5 then 80
         end)
 end 
end 

 
-- Detalle Killers 
 declare @gestion int = (select gestion 
       from LN_gestiones 
       where contraparte = @contraparte 
         AND LN_gestiones.desde <= GETDATE()
       AND LN_gestiones.hasta >= GETDATE())
 declare @indicador int = (select indicador
           from ln_indicadores 
        inner join LN_productos on ln_productos.producto = LN_indicadores.producto 
        where clave_indicador = 'SCORE_JUDICIAL'
        and nombre_producto = 'Antecedentes Judiciales')
 if exists (select * From ln_score_detalle_killer
             where contraparte = @contraparte 
      and gestion = @gestion 
      and indicador = @indicador
      and detalle_killer != isnull(@detalle,'')
      and visible = 1)
  UPDATE LN_score_detalle_killer
     set visible = 0, modified = getdate()
   where contraparte = @contraparte 
     and gestion = @gestion 
     and indicador = @indicador

 if (@detalle is not null) 
 begin
  if not exists (select * From ln_score_detalle_killer
     where contraparte = @contraparte 
       and gestion = @gestion 
       and indicador = @indicador
       and detalle_killer = isnull(@detalle,'')
       and visible = 1)

     insert into ln_score_detalle_killer
     (contraparte, gestion, indicador, detalle_killer, visible, created)
     values (@contraparte, @gestion, @indicador, @detalle, 1, getdate()) 
 end 

 --select @puntaje_concursos_quiebras as concursos, @puntaje_juicios as juicios
 if (@puntaje_juicios = -100 or @puntaje_concursos_quiebras = -100) 
 set @score_judicial = -100
 else set @score_judicial =  @puntaje_juicios * 50 / 100  +
              @puntaje_concursos_quiebras * 50 / 100

END

GO

Used By

No items found