dbo.LN_docs_chequeo

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

7
Name Data Type Length Description
@p_accion varchar 20
@p_plataforma int 4
@p_doc_id bigint 8
@p_user_id int 4
@p_tiempo int 4
@p_error bit 1
@p_mensaje varchar 200

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: MIS
-- Create date: 12/02/2025
-- Description: Para chequear si en la verificacion de documentos, un documento está siendo trabajado por otro usuario 
-- =============================================
/*
declare @p_error bit = null, @p_mensaje varchar(200) = null 
--exec [ln_docs_chequeo] 'CHEQUEO', '47', 657923, 94437923, 30, @p_error output, @p_mensaje output 
--select @p_error, @p_mensaje
exec [ln_docs_chequeo] 'CHEQUEO', '47', 657923, 15342812, 30, @p_error output, @p_mensaje output 
select @p_error, @p_mensaje

select * from control_documentos

2025-02-12 12:32:10.693
*/


CREATE OR ALTER PROCEDURE dbo.LN_docs_chequeo 
@p_accion     varchar(20) = 'CHEQUEO',
@p_plataforma int, 
@p_doc_id     bigint, 
@p_user_id    int, 
@p_tiempo     int = 30, 
@p_error   bit output, 
@p_mensaje varchar(200) output 
AS
BEGIN
 SET NOCOUNT ON
 declare @id      int = null 
 declare @user_id int = null 
 declare @fecha   datetime = null 
 declare @usuario varchar(200) = null 
 set @p_tiempo = @p_tiempo * (-1)
 

 if @p_accion = 'LIBERA'
 begin
  SET @user_id = @p_user_id;
  delete from control_documentos where user_id = @user_id and doc_id = @p_doc_id and plataforma = @p_plataforma
 end 

 if @p_accion = 'CHEQUEO'
 begin
  select top 1 @id = id, 
               @user_id = user_id, 
      @fecha = created 
    from control_documentos
   where plataforma = @p_plataforma 
     and doc_id = @p_doc_id
   order by created desc 

  if @user_id = @p_user_id
  begin
   update control_documentos set created = getdate() where id = @id 
   set @p_error = 0
   set @p_mensaje = 'ok'
   return 
  end 
  if @fecha is null or @fecha <= dateadd(minute,@p_tiempo,getdate())
  begin 
   delete from control_documentos where id = @id
   insert into control_documentos(plataforma, doc_id, user_id, created) values (@p_plataforma, @p_doc_id, @p_user_id, getdate())
   set @p_error = 0
   set @p_mensaje = 'ok'
   return 
  end 

  
  select @usuario = iif((isnull(first_name,'')!='' or isnull(last_name,'')!=''), 
                         isnull(first_name,'') + ' ' + isnull(last_name,''),
          username) from users where user_id = @user_id
  set @p_error = 1
  declare @cuanto int = DATEDIFF(minute, @fecha, getdate())
  set @p_mensaje = 'El documento está siendo trabajado por ' + @usuario + 
                   ' desde el ' + convert(varchar,@fecha,103) + ' ' + convert(varchar(8),@fecha,114) + 
       IIF(@cuanto != 0, ' (hace ' + convert(varchar,@cuanto) + ' minutos)', '')

 end 
END
GO