dbo.LN_score_fidecheck

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_fide decimal 9

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
-- =============================================
-- Author: <María Inés Soria>
-- Create date: <30/09/2019>
-- Description: Cálculo de Score de datos aportados por el cliente
-- =============================================
DECLARE @score_datos decimal(10,2)
EXEC [dbo].[LN_score_fidecheck] 167, @score_datos output
select @score_datos as score_datos

SELECT * from ln_contrapartes where contraparte = 127
select * from padrones where id_padron = 281159

*/

CREATE OR ALTER PROCEDURE dbo.LN_score_fidecheck 
  @contraparte int,
  @score_fide decimal(10,2) = null output
AS
BEGIN
 SET NOCOUNT ON;
 declare @detalle varchar(max) = null
 declare @detallep varchar(max) = null

 declare @cliente int = (select cliente from ln_contrapartes where contraparte = @contraparte)
 declare @score_final decimal(10,2) = null 

 declare @causal int 
 declare @puntos1 int = null, -- Tipo P Positovo
         @puntos2 int = null, -- Tipo E PEP 
   @puntos3 int = null, -- Tipo S Sancionado
   @puntos4 int = null, -- Tipo R Relacionado
   @puntos5 int = null  -- Tipo N Negativo


 select @puntos1 = puntos
     from ln_dias_alertas where tabla = 'fidecheck' and cliente = @Cliente and causal = 1 
 select @puntos2 = puntos
     from ln_dias_alertas where tabla = 'fidecheck' and cliente = @Cliente and causal = 2
 select @puntos3 = puntos
     from ln_dias_alertas where tabla = 'fidecheck' and cliente = @Cliente and causal = 3
 select @puntos4 = puntos
     from ln_dias_alertas where tabla = 'fidecheck' and cliente = @Cliente and causal = 4
    select @puntos5 = puntos
     from ln_dias_alertas where tabla = 'fidecheck' and cliente = @Cliente and causal = 5

 if (@puntos1 is null) set @puntos1 = -100.00
 if (@puntos2 is null) set @puntos2 = 10.00
 if (@puntos3 is null) set @puntos3 = 90.00
 if (@puntos4 is null) set @puntos4 = 90.00
 if (@puntos5 is null) set @puntos5 = 100.00

 --select @puntos1, @puntos2, @puntos3, @puntos4, @puntos5

 declare @gestion int = 0 
 set @gestion = (select gestion 
     from LN_gestiones 
     where contraparte = @contraparte 
     AND LN_gestiones.desde <= GETDATE()
     AND LN_gestiones.hasta >= GETDATE()
     and ln_gestiones.vigente = 1 )


 IF (select count(*) from LN_demo_fidecheck
  where contraparte = @contraparte and visible = 1 and gestion = @gestion
  and fidecheck = 'P') > 0 
 begin
  if (@puntos1 is null) set @score_final = -100
  else set @score_final = @puntos1

  if (@score_final = -100)
  begin
  if (@detalle is null) 
   set @detalle = 'FIDECHECK POSITIVE'
   else 
   set @detalle = @detalle + '; FIDECHECK POSITIVE'
  end
  
  if (@score_final != -100 and @score_final != 100 and @score_final is not null)
  begin
  if (@detallep is null) 
   set @detallep = 'FIDECHECK POSITIVE'
   else 
   set @detallep = @detallep + '; FIDECHECK POSITIVE'
  end
 end

 if (@score_final is null) 
    begin 
  IF (select count(*) from LN_demo_fidecheck
   where contraparte = @contraparte and visible = 1 and gestion = @gestion  
   and fidecheck = 'E') > 0 
  begin
   if (@puntos2 is null) set @score_final = -100
    else set @score_final = @puntos2

   if (@score_final = -100)
   begin
    if (@detalle is null) 
    set @detalle = 'FIDECHECK PEP'
    else 
    set @detalle = @detalle + '; FIDECHECK PEP'
   end 
   if (@score_final != -100 and @score_final != 100 and @score_final is not null)
   begin
   if (@detallep is null) 
    set @detallep = 'FIDECHECK PEP'
    else 
    set @detallep = @detallep + '; FIDECHECK PEP'
   end


  end
 end

 if (@score_final is null) 
    begin 
  IF (select count(*) from LN_demo_fidecheck
   where contraparte = @contraparte and visible = 1 and gestion = @gestion  
   and fidecheck = 'S') > 0 
  begin
   if (@puntos2 is null) set @score_final = -100
    else set @score_final = @puntos3

   if (@score_final = -100)
   begin
    if (@detalle is null) 
    set @detalle = 'FIDECHECK SUJETO OBLIGADO'
    else 
    set @detalle = @detalle + '; FIDECHECK SUJETO OBLIGADO'
   end 
   if (@score_final != -100 and @score_final != 100 and @score_final is not null)
   begin
   if (@detallep is null) 
    set @detallep = 'FIDECHECK SUJETO OBLIGADO'
    else 
    set @detallep = @detallep + '; FIDECHECK SUJETO OBLIGADO'
   end


  end
 end

 if (@score_final is null) 
    begin
  IF (select count(*) from LN_demo_fidecheck
   where contraparte = @contraparte and visible = 1 and gestion = @gestion  
   and fidecheck = 'R') > 0 
  begin
   if (@puntos3 is null) set @score_final = -100
    else set @score_final = @puntos4

   if (@score_final = -100)
   begin
    if (@detalle is null) 
    set @detalle = 'FIDECHECK RELATED'
    else 
    set @detalle = @detalle + '; FIDECHECK RELATED'
   end 
   if (@score_final != -100 and @score_final != 100 and @score_final is not null)
   begin
   if (@detallep is null) 
    set @detallep = 'FIDECHECK RELATED'
    else 
    set @detallep = @detallep + '; FIDECHECK RELATED'
   end


  end
 end

 if (@score_final is null) 
    begin
  IF (select count(*) from LN_demo_fidecheck
   where contraparte = @contraparte and visible = 1 and gestion = @gestion  
   and fidecheck = 'N') > 0
  begin
   if (@puntos4 is null) set @score_final = 100
    else set @score_final = @puntos5

   if (@score_final = -100)
   begin
    if (@detalle is null) 
    set @detalle = 'FIDECHECK NEGATIVE'
    else 
    set @detalle = @detalle + '; FIDECHECK NEGATIVE'
   end 
   if (@score_final != -100 and @score_final != 100 and @score_final is not null)
   begin
   if (@detallep is null) 
    set @detallep = 'FIDECHECK NEGATIVE'
    else 
    set @detallep = @detallep + '; FIDECHECK NEGATIVE'
   end
  end
 end
   

   
-- Detalle Killers 

 declare @indicador int = null
 declare @etiqueta varchar(100) = null
 select @indicador = indicador, @etiqueta = LN_productos.etiqueta
           from ln_indicadores 
        inner join LN_productos on ln_productos.producto = LN_indicadores.producto 
        where clave_indicador = 'SCORE_FIDECHECK'
 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 

 if (@detallep is not null)
  set @detallep = isnull(@etiqueta, '') + ': ' + @detallep
 
 if exists (select * From ln_score_detalle_puntos
             where contraparte = @contraparte 
      and gestion = @gestion 
      and indicador = @indicador
      and detalle != isnull(@detallep,'')
      and visible = 1)
  UPDATE LN_score_detalle_puntos
     set visible = 0, modified = getdate()
   where contraparte = @contraparte 
     and gestion = @gestion 
     and indicador = @indicador

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

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

 set @score_fide = @score_final
END 

        
   

GO