
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_doc_tipoPendiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_tipoPendiente]
GO
CREATE PROCEDURE [reg_doc_tipoPendiente]
	@p_id INT
AS
BEGIN
	SELECT 	[id] AS [id],
		[uid] AS [uid],
		[eid] AS [eid],
		[id_usuario_modifico] AS [id_usuario_modifico],
		[id_usuario_creo] AS [id_usuario_creo],
		[fechaModificacion] AS [fechaModificacion],
		[fechaCreacion] AS [fechaCreacion],
		[active] AS [active],
		[tipoPendiente] AS [tipoPendiente],
		[tipoPendiente_codigo] AS [tipoPendiente_codigo]
	FROM [doc_tipoPendiente]
	WHERE	(id = @p_id)

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[view_doc_tipoPendiente]')) DROP VIEW [dbo].[view_doc_tipoPendiente]
GO
CREATE  VIEW [view_doc_tipoPendiente] AS
SELECT 	[doc_tipoPendiente].[id] AS [id],
		[doc_tipoPendiente].[uid] AS [uid],
		[doc_tipoPendiente].[eid] AS [eid],
		[doc_tipoPendiente].[id_usuario_modifico] AS [id_usuario_modifico],
		[doc_tipoPendiente].[id_usuario_creo] AS [id_usuario_creo],
		[doc_tipoPendiente].[fechaModificacion] AS [fechaModificacion],
		[doc_tipoPendiente].[fechaCreacion] AS [fechaCreacion],
		[doc_tipoPendiente].[active] AS [active],
		[doc_tipoPendiente].[tipoPendiente] AS [tipoPendiente],
		[doc_tipoPendiente].[tipoPendiente_codigo] AS [tipoPendiente_codigo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [doc_tipoPendiente]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_tipoPendiente].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_tipoPendiente].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [doc_tipoPendiente].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [doc_tipoPendiente].active=1
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_doc_tipoPendientesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoPendientesXusuario_modifico]
GO
CREATE PROCEDURE [sel_doc_tipoPendientesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_tipoPendiente]
	WHERE ([id_usuario_modifico]= @p_id_usuario_modifico)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_doc_tipoPendientesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoPendientesXusuario_creo]
GO
CREATE PROCEDURE [sel_doc_tipoPendientesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_tipoPendiente]
	WHERE ([id_usuario_creo]= @p_id_usuario_creo)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_doc_tipoPendientes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoPendientes]
GO
CREATE PROCEDURE [sel_doc_tipoPendientes]
	
	@p_filtro as varchar (8000) = '1=1',
	@p_orden as varchar (8000) = 'id',
	@p_campos as varchar (8000) = '*' AS
BEGIN
exec ('SELECT '+ @p_campos +
' FROM [view_doc_tipoPendiente]
WHERE (' + @p_filtro + ') ORDER BY ' + @p_orden)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[lis_doc_tipoPendientes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_tipoPendientes]
GO
CREATE PROCEDURE [lis_doc_tipoPendientes]
	AS
BEGIN
	SELECT id AS id,
[tipoPendiente] AS [doc_tipoPendiente]
	FROM [doc_tipoPendiente]
	WHERE active = 1
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[upd_doc_tipoPendiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_doc_tipoPendiente]
GO
CREATE PROCEDURE [upd_doc_tipoPendiente]
	@p_id int = null,
		@p_uid varchar(500) = null,
		@p_eid varchar(50) = null,
		@p_id_usuario_modifico int = null,
		@p_id_usuario_creo int = null,
		@p_fechaCreacion smalldatetime = null,
		@p_tipoPendiente varchar(300) = null,
		@p_tipoPendiente_codigo varchar(100) = null
AS
BEGIN
		UPDATE [doc_tipoPendiente]
		SET	[uid] = isnull(@p_uid,[uid]),
			[eid] = isnull(@p_eid,[eid]),
			[id_usuario_modifico] = isnull(@p_id_usuario_modifico,[id_usuario_modifico]),
			[id_usuario_creo] = isnull(@p_id_usuario_creo,[id_usuario_creo]),
			[fechaModificacion] = GETDATE(),
			[tipoPendiente] = isnull(@p_tipoPendiente,[tipoPendiente]),
			[tipoPendiente_codigo] = isnull(@p_tipoPendiente_codigo,[tipoPendiente_codigo])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ins_doc_tipoPendiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_doc_tipoPendiente]
GO
CREATE PROCEDURE [ins_doc_tipoPendiente]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_tipoPendiente varchar(300),
		@p_tipoPendiente_codigo varchar(100)
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [doc_tipoPendiente]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[tipoPendiente],
			[tipoPendiente_codigo])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_tipoPendiente,
			@p_tipoPendiente_codigo)
		SET @v_id = scope_identity()
UPDATE [doc_tipoPendiente] set [uid] = [uid] + '_' + convert(varchar(20),@v_id) where [id]=@v_id
SELECT @v_id as id
RETURN @v_id
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[act_doc_tipoPendiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_doc_tipoPendiente]
GO
CREATE PROCEDURE [act_doc_tipoPendiente]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [doc_tipoPendiente]
	SET active=@p_active,
	[id_usuario_modifico] = isnull(@p_id_usuario_modifico,[id_usuario_modifico]),
	[fechaModificacion] = GETDATE()
	WHERE	(id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_doc_tipoPendiente_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_tipoPendiente_m]
GO
CREATE PROCEDURE [reg_doc_tipoPendiente_m]
	@p_id INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT 	[id] AS [id],
		[uid] AS [uid],
		[eid] AS [eid],
		[id_usuario_modifico] AS [id_usuario_modifico],
		[id_usuario_creo] AS [id_usuario_creo],
		[fechaModificacion] AS [fechaModificacion],
		[fechaCreacion] AS [fechaCreacion],
		[active] AS [active],
		[tipoPendiente] AS [tipoPendiente],
		[tipoPendiente_codigo] AS [tipoPendiente_codigo]
	FROM [doc_tipoPendiente]
	WHERE	(id = @p_id)
	AND	(eid LIKE @p_eid+'%')

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_doc_tipoPendientesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoPendientesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_doc_tipoPendientesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_tipoPendiente]
	WHERE ([id_usuario_modifico]= @p_id_usuario_modifico)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_doc_tipoPendientesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoPendientesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_doc_tipoPendientesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_tipoPendiente]
	WHERE ([id_usuario_creo]= @p_id_usuario_creo)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_doc_tipoPendientes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoPendientes_m]
GO
CREATE PROCEDURE [sel_doc_tipoPendientes_m]
	@p_eid as varchar (50)='',
	@p_filtro as varchar (8000) = '1=1',
	@p_orden as varchar (8000) = 'id',
	@p_campos as varchar (8000) = '*' AS
BEGIN
exec ('SELECT '+ @p_campos +
' FROM [view_doc_tipoPendiente]
WHERE  (eid like '''+@p_eid+'%'+''' ) AND (' + @p_filtro + ') ORDER BY ' + @p_orden)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[lis_doc_tipoPendientes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_tipoPendientes_m]
GO
CREATE PROCEDURE [lis_doc_tipoPendientes_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[tipoPendiente] AS [doc_tipoPendiente]
	FROM [doc_tipoPendiente]
	WHERE active = 1
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[aud_doc_tipoPendiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_doc_tipoPendiente]
GO
CREATE PROCEDURE [aud_doc_tipoPendiente]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [doc_tipoPendiente].id as id_tabla,
	[doc_tipoPendiente].id_usuario_modifico as id_usuario_modifico,
	[doc_tipoPendiente].id_usuario_creo as id_usuario_creo,
	[doc_tipoPendiente].fechaModificacion as fechaModificacion,
	[doc_tipoPendiente].fechaCreacion as fechaCreacion,
	[doc_tipoPendiente].active as active,
	[doc_tipoPendiente].[tipoPendiente] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [doc_tipoPendiente]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_tipoPendiente].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_tipoPendiente].[id_usuario_creo]
	where ' + @p_filtro)
END
GO


