dbo.LN_contraparte_comentarios_

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

5
Name Data Type Length Description
@p_contraparte int 4
@p_fecha_gestion varchar 10
@campos bit 1
@indicador int 4
@producto int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
/*
exec [LN_contraparte_comentarios_] 201458,'2023-05-03',null,520 
exec [LN_contraparte_comentarios_] 162668,'2021-12-06',1
exec [LN_trazabilidad_] 162668,'2021-12-06',1

delete from LN_contraparte_indicadores where contraparte = 201458 and indicador = 520 

*/

CREATE OR ALTER PROCEDURE dbo.LN_contraparte_comentarios_ 
 @p_contraparte int, 
 @p_fecha_gestion varchar(10) = null,
 @campos bit = 0, 
 @indicador int = null, 
 @producto int = null
AS
BEGIN
 SET NOCOUNT ON;
 declare @fecha_gestion date 
 declare @gestion int 
 declare @fecha varchar(16)
 declare @fecha1 date 
 declare @detalle varchar(max) = ''
 declare @detalle2 varchar(max) = ''
 declare @resultado varchar(max) = ''
 declare @usuario varchar(200) = ''

 if (isnull(@p_fecha_gestion,'') = '') 
 begin
  select @gestion = gestion from ln_gestiones 
  where contraparte = @p_contraparte and vigente = 1
 end 
 else
 begin 
  select @gestion = gestion from ln_gestiones 
  where contraparte = @p_contraparte and desde = @p_fecha_gestion 
 end 

 if @producto is not null 
 begin
  SELECT distinct convert(varchar,FORMAT(created, 'yyyy-MM-dd HH:mm')) as created, users.user_id,
  convert(varchar,FORMAT(created, 'dd-MM-yyyy HH:mm')) as fecha,
  texto_fidelitas as texto, texto_cliente,
  ISNULL(users.first_name,'') + ' ' + ISNULL(users.last_name,'') as usuario, 
  username
    FROM LN_contraparte_comentarios 
    left join users on users.user_id = LN_contraparte_comentarios.created_by 
     WHERE contraparte = @p_contraparte 
     and gestion = @gestion and visible = 1
     and indicador in (select indicador from ln_indicadores where producto = @producto) 
     order by created desc
 end
 else 
 begin
 if @indicador is not null 
 begin
  SELECT distinct convert(varchar,FORMAT(created, 'yyyy-MM-dd HH:mm')) as created, users.user_id,
  convert(varchar,FORMAT(created, 'dd-MM-yyyy HH:mm')) as fecha,
  texto_fidelitas as texto, texto_cliente,
  ISNULL(users.first_name,'') + ' ' + ISNULL(users.last_name,'') as usuario, 
  username
    FROM LN_contraparte_comentarios 
    left join users on users.user_id = LN_contraparte_comentarios.created_by 
     WHERE contraparte = @p_contraparte 
     and gestion = @gestion and visible = 1
     and indicador = @indicador
     order by created desc
 end 
 else 
 begin
  if @campos = 0
  begin
   DECLARE C_Cursor CURSOR FOR
   SELECT distinct convert(varchar,FORMAT(created, 'yyyy-MM-dd HH:mm')) as created, --users.user_id,
 
   /*convert(varchar,created,103), */
   convert(varchar,FORMAT(created, 'dd-MM-yyyy HH:mm')),

   replace(texto_fidelitas,';',','), replace(texto_cliente,';',','), 
   ISNULL(users.first_name,'') + ' ' + ISNULL(users.last_name,'') as usuario
     FROM LN_contraparte_comentarios 
     left join users on users.user_id = LN_contraparte_comentarios.created_by 
      WHERE contraparte = @p_contraparte 
      and gestion = @gestion and visible = 1
      and texto_fidelitas = 'COMENTARIO INTERNO CLIENTE'
      and indicador not in (454)
      order by created desc
   OPEN C_Cursor
   FETCH NEXT FROM C_Cursor INTO @fecha1, @fecha, @detalle, @detalle2, @usuario
   WHILE @@fetch_status = 0
   BEGIN 
    if (isnull(@detalle,'')!='' or isnull(@detalle2,'') != '')
    begin
     if (@resultado != '') set @resultado = @resultado + '; '
     set @resultado = @resultado + @fecha + ': USUARIO ' +  @usuario + ': ' + isnull(@detalle,'') + ' - ' + isnull(@detalle2,'') 
    end 
    FETCH NEXT FROM C_Cursor INTO @fecha1, @fecha, @detalle, @detalle2, @usuario 
   END 
   CLOSE C_Cursor
   DEALLOCATE C_Cursor
   if (@resultado = '') set @resultado = null
 
   select @resultado as detalle
  end 
  end

  if @campos = 1
  begin
  SELECT distinct convert(varchar,FORMAT(created, 'yyyy-MM-dd HH:mm')) as created, users.user_id,
  convert(varchar,FORMAT(created, 'dd-MM-yyyy HH:mm')) as fecha,
  texto_fidelitas as texto, texto_cliente,
  ISNULL(users.first_name,'') + ' ' + ISNULL(users.last_name,'') as usuario, 
  username
    FROM LN_contraparte_comentarios 
    left join users on users.user_id = LN_contraparte_comentarios.created_by 
     WHERE contraparte = @p_contraparte 
     and gestion = @gestion and visible = 1
     and texto_fidelitas != 'COMENTARIO INTERNO CLIENTE'
     and indicador not in (454,621,622) -- 621 y 622 se muestra por la trazabilidad 
     order by created
  end 
 end
END

GO