dbo.LN_score_datos

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_datos 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_datos] 44764, @score_datos output
select @score_datos as score_datos
select * from ln_contraparte_datos_aportados
*/

CREATE OR ALTER PROCEDURE dbo.LN_score_datos 
  @contraparte int,
  @score_datos 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 

 if (exists (select * from LN_contraparte_datos_aportados_puntajes where cliente =  @cliente))
 begin 
  set @score_final = 100 
  declare @nro_item int, @item varchar(200)
  declare @nro_opcion int, @valor varchar(200)
  declare @cantidad int = 0
  declare @puntos decimal(12,2) = 0
  declare @valor_item decimal(12,2) 
  declare @valor_opcion decimal(12,2)

  DECLARE C_datos CURSOR FOR
    SELECT nro_item, upper(item) as item, nro_opcion, upper(valor) as valor
    FROM LN_contraparte_datos_aportados a
    where a.contraparte = @contraparte
    and a.fecha = (select max(fecha) from LN_contraparte_datos_aportados b 
        where b.contraparte = a.contraparte 
         and b.item = a.item) 

  OPEN C_datos
  FETCH NEXT FROM C_datos INTO @nro_item, @item, @nro_opcion, @valor
  WHILE @@fetch_status = 0
  BEGIN  
   select @valor_item = isnull(valor_item,0),  @valor_opcion = isnull(valor_opcion ,0)
     from LN_contraparte_datos_aportados_puntajes   
     where cliente = @cliente 
    and nro_item = @nro_item
    and nro_opcion = @nro_opcion 
   set @cantidad = @cantidad + 1
   set @valor_item = ISNULL(@valor_item, 0)
   set @valor_opcion = ISNULL(@valor_opcion, 0)
   /*
    select @nro_item, @item, @nro_opcion, @valor
    select @valor_item, @valor_opcion, @cantidad
    */

   if (@valor_opcion = -100) 
   begin
    set @puntos = -100
    if (@detalle is null) 
     set @detalle = @item + ': ' + @valor
     else 
     set @detalle = @detalle + '; ' + @item + ': ' + @valor
   end 

   if (@valor_opcion != -100 and @valor_opcion !=  @valor_item) 
   begin
    if (@detallep is null) 
     set @detallep = @item + ': ' + @valor
     else 
     set @detallep = @detallep + '; ' + @item + ': ' + @valor
   end 

   if (@puntos != -100 and @valor_item != 0) 
       set @puntos = @puntos + (@valor_opcion /  @valor_item) * 100 -- porcentaje de puntos para ese item }
    
   FETCH NEXT FROM C_datos INTO @nro_item, @item, @nro_opcion, @valor
  END 
  CLOSE C_datos
  DEALLOCATE C_datos 

  if (@cantidad = 0) set @score_final = 100
  else 
   if (@puntos = -100) set @score_final = -100
   else set @score_final = @puntos / @cantidad
 end 

-- Detalle Killers / puntos
 declare @gestion int = (select gestion 
       from LN_gestiones 
       where contraparte = @contraparte 
         AND LN_gestiones.desde <= GETDATE()
       AND LN_gestiones.hasta >= GETDATE())
 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_DATOS'
 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_datos = @score_final
END 

        
   

GO