dbo.LN_score_detalle_killer_

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@p_contraparte int 4
@p_fecha_gestion varchar 10
@p_productos varchar -1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
/*
exec [LN_score_detalle_killer_] 510, '1900-01-01'
exec [LN_score_detalle_killer_] 196579, '2022-12-16', '2101'
exec [LN_score_detalle_puntos_] 196579, '2022-12-16', '2101'
exec [LN_score_detalle_puntos_] 194064, '2022-10-19', '2101'
select * from ln_gestiones where contraparte = 196579
select * from ln_score_detalle_killer where contraparte = 196579
*/

CREATE OR ALTER PROCEDURE dbo.LN_score_detalle_killer_ 
 @p_contraparte int, 
 @p_fecha_gestion varchar(10) = null, 
 @p_productos varchar(max) = '0'
AS
BEGIN
 SET NOCOUNT ON;
 declare @fecha_gestion date 
 if (isnull(@p_fecha_gestion,'') = '') 
 begin
  set @fecha_gestion = getdate()
 end 
 else
 begin 
  set @fecha_gestion = convert(date, @p_fecha_gestion) 
 end 

 if (@p_productos != '2101')
  set @p_productos = '0' -- Para que muestre el detalle de todos, no solo de los que el usuario puede ver
 if (@p_productos = '0')
 begin 
 SELECT contraparte, gestion, detalle=STUFF(
   (SELECT distinct '; ' + detalle_killer AS [text()]
    FROM ln_score_detalle_killer XT 
    WHERE XT.contraparte = T.contraparte and XT.visible = 1 and XT.gestion = T.gestion 
    FOR XML PATH('')), 1, 2, '') 
  FROM ln_score_detalle_killer T 
  where T.contraparte = @p_contraparte 
  and T.visible = 1
  and T.gestion = (select gestion from ln_gestiones where contraparte = @p_contraparte and 
       desde <= @fecha_gestion and hasta >= @fecha_gestion) 
  -- agregado 18/07/2024: solo muestra motivos si tiene el indicador score_final
  and exists (select * from ln_contraparte_indicadores i 
     inner join ln_indicadores ind on (ind.indicador = i.indicador and ind.clave_indicador = 'SCORE_FINAL')
     where i.contraparte = @p_contraparte and i.gestion = T.gestion)
  --
  GROUP BY contraparte, gestion
 end 
 else 
 begin 
 declare @cant int = 0 
 if (@p_productos != '2101')
  set @cant = (select COUNT(*) 
                        from ln_score_detalle_killer 
        inner join LN_gestiones on (LN_gestiones.contraparte = ln_score_detalle_killer.contraparte and LN_gestiones.gestion = ln_score_detalle_killer.gestion) 
        where ln_score_detalle_killer.contraparte = @p_contraparte 
          and  LN_gestiones.desde <= @fecha_gestion and LN_gestiones.hasta >= @fecha_gestion 
       -- agregado 18/07/2024: solo muestra motivos si tiene el indicador score_final
       and exists (select * from ln_contraparte_indicadores i 
          inner join ln_indicadores ind on (ind.indicador = i.indicador and ind.clave_indicador = 'SCORE_FINAL')
          where i.contraparte = @p_contraparte and i.gestion = LN_gestiones.gestion)
       --
       /*and LN_gestiones.vigente = 1*/
       ) 
  
  
 /* Si tiene detalle de Killer (@cant != 0), 
     pero el detalle del select de abajo da vacio, 
     es porque son motivos de otro producto 
     que no esta en la lista de los productos visibles para el usuario */

 select contraparte, gestion, 
         iif((detalle is null and @cant != 0), 'CONSULTAR MOTIVOS CON EL USUARIO ADMINISTRADOR',detalle) as detalle
 from 
 (SELECT contraparte, gestion, 
        detalle = STUFF(
   (SELECT distinct '; ' + detalle_killer AS [text()]
    FROM ln_score_detalle_killer XT 
    INNER JOIN LN_indicadores I on I.indicador = XT.indicador 
    INNER JOIN dbo.DelimitedSplit8K(@p_productos, ',') P ON p.item = I.producto 
    WHERE XT.contraparte = T.contraparte and XT.visible = 1 and XT.gestion = T.gestion 
    FOR XML PATH('')), 1, 2, '')
  FROM ln_score_detalle_killer T 
  where T.contraparte = @p_contraparte 
  and T.visible = 1
  and T.gestion = (select gestion from ln_gestiones where contraparte = @p_contraparte and 
       desde <= @fecha_gestion and hasta >= @fecha_gestion) 
  -- agregado 18/07/2024: solo muestra motivos si tiene el indicador score_final
  and exists (select * from ln_contraparte_indicadores i 
     inner join ln_indicadores ind on (ind.indicador = i.indicador and ind.clave_indicador = 'SCORE_FINAL')
     where i.contraparte = @p_contraparte and i.gestion = T.gestion)
  --
  GROUP BY contraparte, gestion) FINAL
 end 
END

GO