dbo.LN_excepciones_final_

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

7
Name Data Type Length Description
@accion varchar 50
@p_contraparte int 4
@p_vigencia date 3
@p_texto_excepcion text -1
@p_user_id int 4
@p_excepcion int 4
@p_tipo_excepcion varchar 1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
/*

*/

CREATE OR ALTER PROCEDURE dbo.LN_excepciones_final_
 -- Add the parameters for the stored procedure here

 @accion         varchar(50),
 @p_contraparte       int = null,
 @p_vigencia        date = null,
 @p_texto_excepcion      text = null,
 @p_user_id        int = null,
 @p_excepcion       int = null,
 @p_tipo_excepcion      varchar(1) = null

 
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
 DECLARE @usuario varchar(200) = ''
 DECLARE @exc int = null, @gest int = null
 DECLARE @prevtxt VARCHAR(2048) = null, @newtext varchar(2048)
 DECLARE @fh_modif varchar(16) = CONVERT(varchar, GETDATE(), 103) + ' ' + CONVERT(varchar(5), GETDATE(), 14)
 DECLARE @fhv datetime, @fhv_anterior datetime

 if (@accion = 'search') begin


  SELECT LN_excepciones_final.excepcion, LN_excepciones_final.contraparte, LN_excepciones_final.gestion,
   CONVERT(VARCHAR, LN_excepciones_final.vigencia, 103) + ' ' + CONVERT(VARCHAR(5), LN_excepciones_final.vigencia, 14) AS vigencia,
   CONVERT(VARCHAR, LN_excepciones_final.vigencia, 121) AS vigencia_ymd, LN_excepciones_final.tipo_excepcion,
   LN_excepciones_final.estado, LN_excepciones_final.texto_excepcion, clientes.nombre_cliente,
   CONVERT(VARCHAR, LN_excepciones_final.created,103) + ' ' + CONVERT(VARCHAR(5), LN_excepciones_final.created,14) AS created,
   CONVERT(VARCHAR, LN_excepciones_final.created,121) AS created_ymd,
   CONVERT(VARCHAR, LN_excepciones_final.modified,103) + ' ' + CONVERT(VARCHAR(5), LN_excepciones_final.modified,14) AS modified,
   CONVERT(VARCHAR, LN_excepciones_final.modified,121) AS modified_ymd,
   LN_excepciones_final.created_by, LN_excepciones_final.modified_by,
   users.first_name + ' ' + users.last_name as created_by_user,
   users2.first_name + ' ' + users2.last_name as modified_by_user,

   padrones.nombre_persona, padrones.identificacion, padrones.numero,
   identificadores.identificador, identificadores.abreviatura, identificadores.country_code,
   country.country_name, LN_contrapartes.id_padron, LN_contrapartes.activo, 
   LN_contrapartes.subcontratista, LN_contrapartes.id_padre,

   padrones2.nombre_persona AS nombre_persona_padre, 
   padrones2.identificacion AS identificacion_padre,
   padrones2.numero AS numero_padre,
   identificadores2.identificador AS identificador_padre, 
   identificadores2.abreviatura AS abreviatura_padre, 
   identificadores2.country_code AS country_code_padre,
   country2.country_name AS country_name_padre

  FROM LN_excepciones_final
  INNER JOIN LN_contrapartes on LN_excepciones_final.contraparte = LN_contrapartes.contraparte
  INNER JOIN padrones on LN_contrapartes.id_padron = padrones.id_padron
  INNER JOIN identificadores on padrones.identificador = identificadores.identificador
  INNER JOIN country on identificadores.country_code = country.country_code
  INNER JOIN clientes on LN_contrapartes.cliente = clientes.cliente

  INNER JOIN LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte

  left outer join LN_contrapartes padre on LN_contrapartes.id_padre = padre.contraparte
  left outer join padrones padrones2 on padre.id_padron = padrones2.id_padron
  left outer join identificadores identificadores2 on padrones2.identificador = identificadores2.identificador
  left outer join country country2 on identificadores2.country_code = country2.country_code

  LEFT OUTER JOIN users on LN_excepciones_final.created_by = users.user_id
  LEFT OUTER JOIN users users2 on LN_excepciones_final.modified_by = users2.user_id

  WHERE LN_excepciones_final.contraparte = @p_contraparte
  AND LN_gestiones.desde <= GETDATE() AND LN_gestiones.hasta >= GETDATE()
  ORDER BY LN_excepciones_final.created desc

 end else

  if (@accion = 'activa') begin

   SELECT LN_excepciones_final.excepcion, LN_excepciones_final.contraparte, LN_excepciones_final.gestion,
    CONVERT(VARCHAR, LN_excepciones_final.vigencia, 103) + ' ' + CONVERT(VARCHAR(5), LN_excepciones_final.vigencia, 14) AS vigencia,
    CONVERT(VARCHAR, LN_excepciones_final.vigencia, 121) AS vigencia_ymd, LN_excepciones_final.tipo_excepcion,
    LN_excepciones_final.estado, LN_excepciones_final.texto_excepcion, clientes.nombre_cliente,
    CONVERT(VARCHAR, LN_excepciones_final.created,103) + ' ' + CONVERT(VARCHAR(5), LN_excepciones_final.created,14) AS created,
    CONVERT(VARCHAR, LN_excepciones_final.created,121) AS created_ymd,
    CONVERT(VARCHAR, LN_excepciones_final.modified,103) + ' ' + CONVERT(VARCHAR(5), LN_excepciones_final.modified,14) AS modified,
    CONVERT(VARCHAR, LN_excepciones_final.modified,121) AS modified_ymd,
    LN_excepciones_final.created_by, LN_excepciones_final.modified_by,
    users.first_name + ' ' + users.last_name as created_by_user,
    users2.first_name + ' ' + users2.last_name as modified_by_user,

    padrones.nombre_persona, padrones.identificacion, padrones.numero,
    identificadores.identificador, identificadores.abreviatura, identificadores.country_code,
    country.country_name, LN_contrapartes.id_padron, LN_contrapartes.activo, 
    LN_contrapartes.subcontratista, LN_contrapartes.id_padre,

    padrones2.nombre_persona AS nombre_persona_padre, 
    padrones2.identificacion AS identificacion_padre,
    padrones2.numero AS numero_padre,
    identificadores2.identificador AS identificador_padre, 
    identificadores2.abreviatura AS abreviatura_padre, 
    identificadores2.country_code AS country_code_padre,
    country2.country_name AS country_name_padre

   FROM LN_excepciones_final
   INNER JOIN LN_contrapartes on LN_excepciones_final.contraparte = LN_contrapartes.contraparte
   INNER JOIN padrones on LN_contrapartes.id_padron = padrones.id_padron
   INNER JOIN identificadores on padrones.identificador = identificadores.identificador
   INNER JOIN country on identificadores.country_code = country.country_code
   INNER JOIN clientes on LN_contrapartes.cliente = clientes.cliente

   INNER JOIN LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte

   left outer join LN_contrapartes padre on LN_contrapartes.id_padre = padre.contraparte
   left outer join padrones padrones2 on padre.id_padron = padrones2.id_padron
   left outer join identificadores identificadores2 on padrones2.identificador = identificadores2.identificador
   left outer join country country2 on identificadores2.country_code = country2.country_code

   LEFT OUTER JOIN users on LN_excepciones_final.created_by = users.user_id
   LEFT OUTER JOIN users users2 on LN_excepciones_final.modified_by = users2.user_id

   WHERE LN_excepciones_final.contraparte = @p_contraparte
   AND LN_excepciones_final.estado = 1
   AND LN_excepciones_final.vigencia >= GETDATE()
   AND LN_gestiones.desde <= GETDATE() AND LN_gestiones.hasta >= GETDATE()
   ORDER BY LN_excepciones_final.estado desc


  end else

   if (@accion = 'nueva') begin

    if NOT EXISTS (select LN_excepciones_final.excepcion
        from LN_excepciones_final
        inner join LN_contrapartes on LN_excepciones_final.contraparte = LN_contrapartes.contraparte
        inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
        where LN_excepciones_final.contraparte = @p_contraparte
        and LN_excepciones_final.estado = 1
        and LN_excepciones_final.vigencia >= GETDATE()
        and LN_gestiones.desde <= GETDATE() and LN_gestiones.hasta >= GETDATE() ) begin

     DECLARE @nueva_excepcion int = null     
     DECLARE @fh_creacion varchar(16) = CONVERT(varchar, GETDATE(), 103) + ' ' + CONVERT(varchar(5), GETDATE(), 14)

     if (@p_user_id is not null) 
       select @usuario = first_name + ' ' + last_name from users where user_id = @p_user_id

     if (@p_vigencia is null)
      set @fhv = DATETIMEFROMPARTS(2099,12,31,23,59,59,0)
     else
      set @fhv = DATEADD(second,-1,DATEADD(day,1, convert(datetime,@p_vigencia)))

     
     declare @fecha_consulta smalldatetime = (select MAX(fecha_consulta) from SAAS_Historial_Consultas where contraparte = @p_contraparte) 
     declare @fecha_cambio   smalldatetime = getdate()
     
     /* 14/01/2022: Se aclara que estado tiene al momento de cargar la excepcion */ 
     declare @aclaracion varchar(max) = ''
     declare @estado_actual varchar(100) = null 
     set @estado_actual = 
     (select upper(convert(varchar,valor_memo)) from ln_contraparte_indicadores 
     INNER JOIN LN_gestiones on (ln_contraparte_indicadores.contraparte = LN_gestiones.contraparte 
                                 and ln_contraparte_indicadores.gestion = ln_gestiones.gestion)
     where ln_contraparte_indicadores.contraparte = @p_contraparte
     and indicador = 251 
     and LN_gestiones.desde <= GETDATE() AND LN_gestiones.hasta >= GETDATE())

     if (@estado_actual is not null)
      set @aclaracion = 'Estado Actual: ' + @estado_actual
     if @p_tipo_excepcion = 'V'
      set @aclaracion = @aclaracion + ' - Se carga Excepción para APROBAR'
     if @p_tipo_excepcion = 'R'
      set @aclaracion = @aclaracion + ' - se carga Excepción para RECHAZAR'
     /*---*/

     
     INSERT INTO LN_excepciones_final
     (contraparte, gestion, vigencia, texto_excepcion, estado, tipo_excepcion, created, created_by)
     SELECT @p_contraparte AS contraparte, LN_gestiones.gestion, 
       @fhv as vigencia, 
       @fh_creacion + ' - ' + @usuario + ': ' + 
       /* 14/01/2022 */
       '(' + @aclaracion + ') ' + 
       /*---*/
       convert(varchar(2048), @p_texto_excepcion) as texto_excepcion, 
       1 as estado, @p_tipo_excepcion,
       @fecha_cambio AS created, @p_user_id as created_by
     FROM LN_contrapartes
     INNER JOIN LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
     WHERE LN_contrapartes.contraparte = @p_contraparte
     AND LN_gestiones.desde <= GETDATE() AND LN_gestiones.hasta >= GETDATE()
     
     select @nueva_excepcion = @@IDENTITY

     --EXEC LN_dashboard_cliente 'excepcion', null, null, null, @p_contraparte
     
     SELECT @nueva_excepcion as excepcion, CONVERT(varchar, @fecha_cambio, 103) as fecha_cambio, CONVERT(varchar,@fecha_consulta,103) as fecha_consulta
     
    end

   end else

    if (@accion = 'desactivar') begin

     select 
       @exc = LN_excepciones_final.excepcion,
       @prevtxt = CONVERT(VARCHAR(2048), LN_excepciones_final.texto_excepcion),
       @gest = LN_gestiones.gestion

     from LN_excepciones_final
     inner join LN_contrapartes on LN_excepciones_final.contraparte = LN_contrapartes.contraparte
     inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
     where LN_excepciones_final.contraparte = @p_contraparte
     and LN_excepciones_final.estado = 1
     and LN_excepciones_final.vigencia >= GETDATE()
     and LN_gestiones.desde <= GETDATE() and LN_gestiones.hasta >= GETDATE()

     if (@exc is not null) begin

      if (@prevtxt is not null)
       set @prevtxt = @prevtxt + '<br />'
      else
       set @prevtxt = ''

      if (@p_user_id is not null) 
       select @usuario = first_name + ' ' + last_name from users where user_id = @p_user_id

      if (@p_texto_excepcion is null)

       UPDATE LN_excepciones_final SET estado = 0,
       texto_excepcion = @prevtxt + @fh_modif + iif(@p_user_id is not null, ' - Excepción desactivada por ' + @usuario, ' - Excepción desactivada.'),
       modified = GETDATE(),
       modified_by = @p_user_id
       WHERE excepcion = @exc

      else

       UPDATE LN_excepciones_final SET estado = 0,
       texto_excepcion = @prevtxt + @fh_modif + iif(@p_user_id is not null, ' - Excepción desactivada por ' + @usuario + ': ', ' - Excepción desactivada: ') + CONVERT(VARCHAR(2048), @p_texto_excepcion),
       modified = GETDATE(),
       modified_by = @p_user_id
       WHERE excepcion = @exc


      --EXEC LN_dashboard_cliente 'desactivar_excepcion', null, null, null, @p_contraparte


     end

    end else

     /* agrega un texto al existente para una excepcion activa */
     if (@accion = 'actualizar') begin

      select @prevtxt = CONVERT(VARCHAR(2048), LN_excepciones_final.texto_excepcion),
        @fhv_anterior = vigencia
      from LN_excepciones_final
      where excepcion = @p_excepcion

      if (@p_vigencia is null)
       set @fhv = DATETIMEFROMPARTS(2099,12,31,23,59,59,0)
      else
       set @fhv = DATEADD(second,-1,DATEADD(day,1, convert(datetime,@p_vigencia)))

      
      if (@p_user_id is not null) 
       select @usuario = first_name + ' ' + last_name from users where user_id = @p_user_id

      if (@p_texto_excepcion is not null) 
       set @newtext = @prevtxt + '<br />' + @fh_modif + ' - ' + @usuario + ': ' + CONVERT(varchar(2048),@p_texto_excepcion)
      else
       set @newtext = @prevtxt

      if (@fhv_anterior != @fhv)
       set @newtext = @newtext + '<br />Nueva fecha de vigencia: ' + CONVERT(varchar(10),@fhv,103)
      
      UPDATE LN_excepciones_final SET
       texto_excepcion = @newtext,
       tipo_excepcion = @p_tipo_excepcion,
       vigencia = @fhv
      WHERE excepcion = @p_excepcion
      
     end
 
END
GO

Used By

No items found