dbo.LN_score_prensa

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

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
DECLARE @score_datos decimal(10,2)
EXEC [dbo].[LN_score_prensa] 148051, @score_datos output
select @score_datos as score_datos
*/

CREATE OR ALTER PROCEDURE dbo.LN_score_prensa 
  @contraparte int,
  @score_prensa decimal(10,2) = null output
AS
BEGIN
 SET NOCOUNT ON;
 declare @detalle varchar(max) = null
 declare @detallep varchar(max) = null
 declare @score_final decimal(10,2) = null 
 declare @indicador int = null
 declare @etiqueta varchar(100) = null
 
 declare @cliente int = (select cliente from ln_contrapartes where contraparte = @contraparte)

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

 select @indicador = indicador
   from ln_indicadores 
   inner join LN_productos on ln_productos.producto = LN_indicadores.producto 
   where clave_indicador = 'PRENSA'

 declare @prensa char(1) = (select valor_texto 
                              from LN_contraparte_indicadores 
           where indicador = @indicador and contraparte = @contraparte and gestion = @gestion) 
 if (@prensa is not null) 
 begin 
  select @score_final = puntos
      from ln_dias_alertas where tabla = 'prensa' and cliente = @Cliente and contenido = @prensa 
  if (@score_final is null) 
  begin
   if (@prensa = 'V') set @score_final = 100.00
   if (@prensa = 'A') set @score_final = 50.00
   if (@prensa = 'R') set @score_final = 0.00
  end 
  if (@score_final = -100)
  begin
   if (@prensa = 'V') set @detalle = 'PRENSA POSITIVA'
   if (@prensa = 'A') set @detalle = 'PRENSA RELATIVA'
   if (@prensa = 'R') set @detalle = 'PRENSA NEGATIVA' 
  end
  
  if (@score_final != -100 and @score_final != 100 and @score_final is not null )
  begin
   if (@prensa = 'V') set @detallep = 'PRENSA POSITIVA'
   if (@prensa = 'A') set @detallep = 'PRENSA RELATIVA'
   if (@prensa = 'R') set @detallep = 'PRENSA NEGATIVA'
  end
 end
   
-- Detalle Killers 

 set @indicador = null
 set @etiqueta  = 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_PRENSA'
 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_prensa = @score_final
END 

        
   

GO