
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_seg_perfil_subtipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_seg_perfil_subtipoDocumento]
GO
CREATE PROCEDURE [reg_seg_perfil_subtipoDocumento]
	@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],
		[perfil_subtipoDocumento] AS [perfil_subtipoDocumento],
		[id_perfil] AS [id_perfil],
		[id_subtipoDocumento] AS [id_subtipoDocumento]
	FROM [seg_perfil_subtipoDocumento]
	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_seg_perfil_subtipoDocumento]')) DROP VIEW [dbo].[view_seg_perfil_subtipoDocumento]
GO
CREATE  VIEW [view_seg_perfil_subtipoDocumento] AS
SELECT 	[seg_perfil_subtipoDocumento].[id] AS [id],
		[seg_perfil_subtipoDocumento].[uid] AS [uid],
		[seg_perfil_subtipoDocumento].[eid] AS [eid],
		[seg_perfil_subtipoDocumento].[id_usuario_modifico] AS [id_usuario_modifico],
		[seg_perfil_subtipoDocumento].[id_usuario_creo] AS [id_usuario_creo],
		[seg_perfil_subtipoDocumento].[fechaModificacion] AS [fechaModificacion],
		[seg_perfil_subtipoDocumento].[fechaCreacion] AS [fechaCreacion],
		[seg_perfil_subtipoDocumento].[active] AS [active],
		[seg_perfil_subtipoDocumento].[perfil_subtipoDocumento] AS [perfil_subtipoDocumento],
		[seg_perfil_subtipoDocumento].[id_perfil] AS [id_perfil],
[seg_perfil].[perfil] as [seg_perfil_perfil],
[seg_perfil].[perfil_codigo] as [seg_perfil_perfil_codigo],
[seg_perfil].[descripcion] as [seg_perfil_descripcion],
[seg_perfil].[mostrarBotones] as [seg_perfil_mostrarBotones],
[seg_perfil].[paraCliente] as [seg_perfil_paraCliente],
[seg_perfil].[paraProveedor] as [seg_perfil_paraProveedor],
		[seg_perfil_subtipoDocumento].[id_subtipoDocumento] AS [id_subtipoDocumento],
[doc_subtipoDocumento].[subtipoDocumento] as [doc_subtipoDocumento_subtipoDocumento],
[doc_subtipoDocumento].[subtipoDocumento_codigo] as [doc_subtipoDocumento_subtipoDocumento_codigo],
[doc_subtipoDocumento].[prefijo] as [doc_subtipoDocumento_prefijo],
[doc_subtipoDocumento].[consecutivo] as [doc_subtipoDocumento_consecutivo],
[doc_subtipoDocumento].[id_tipoDocumento] as [doc_subtipoDocumento_id_tipoDocumento],
[doc_subtipoDocumento].[programarPlaneadas] as [doc_subtipoDocumento_programarPlaneadas],
[doc_subtipoDocumento].[cargarSolicitante] as [doc_subtipoDocumento_cargarSolicitante],
[doc_subtipoDocumento].[verCentroCosto] as [doc_subtipoDocumento_verCentroCosto],
[doc_subtipoDocumento].[verIncoterm] as [doc_subtipoDocumento_verIncoterm],
[doc_subtipoDocumento].[valUrgente] as [doc_subtipoDocumento_valUrgente],
[doc_subtipoDocumento].[bloquearDespacho] as [doc_subtipoDocumento_bloquearDespacho],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [seg_perfil_subtipoDocumento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[seg_perfil_subtipoDocumento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[seg_perfil_subtipoDocumento].[id_usuario_creo]
	INNER JOIN [seg_perfil] ON [seg_perfil].id=[seg_perfil_subtipoDocumento].[id_perfil]
	INNER JOIN [doc_subtipoDocumento] ON [doc_subtipoDocumento].id=[seg_perfil_subtipoDocumento].[id_subtipoDocumento]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [seg_perfil_subtipoDocumento].eid = [empresaCodigo].[codigo] and empresaCodigo.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_seg_perfil_subtipoDocumentosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentosXusuario_modifico]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_seg_perfil_subtipoDocumento]
	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_seg_perfil_subtipoDocumentosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentosXusuario_creo]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_seg_perfil_subtipoDocumento]
	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_seg_perfil_subtipoDocumentosXperfil]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentosXperfil]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentosXperfil]
	@p_id_perfil INT
AS
BEGIN
	SELECT * 
	FROM [view_seg_perfil_subtipoDocumento]
	WHERE ([id_perfil]= @p_id_perfil)
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_seg_perfil_subtipoDocumentosXsubtipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentosXsubtipoDocumento]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentosXsubtipoDocumento]
	@p_id_subtipoDocumento INT
AS
BEGIN
	SELECT * 
	FROM [view_seg_perfil_subtipoDocumento]
	WHERE ([id_subtipoDocumento]= @p_id_subtipoDocumento)
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_seg_perfil_subtipoDocumentos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentos]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentos]
	
	@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_seg_perfil_subtipoDocumento]
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_seg_perfil_subtipoDocumentos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_seg_perfil_subtipoDocumentos]
GO
CREATE PROCEDURE [lis_seg_perfil_subtipoDocumentos]
	AS
BEGIN
	SELECT id AS id,
[perfil_subtipoDocumento] AS [seg_perfil_subtipoDocumento]
	FROM [seg_perfil_subtipoDocumento]
	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_seg_perfil_subtipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_seg_perfil_subtipoDocumento]
GO
CREATE PROCEDURE [upd_seg_perfil_subtipoDocumento]
	@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_perfil_subtipoDocumento varchar(300) = null,
		@p_id_perfil int = null,
		@p_id_subtipoDocumento int = null
AS
BEGIN
		UPDATE [seg_perfil_subtipoDocumento]
		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(),
			[perfil_subtipoDocumento] = isnull(@p_perfil_subtipoDocumento,[perfil_subtipoDocumento]),
			[id_perfil] = isnull(@p_id_perfil,[id_perfil]),
			[id_subtipoDocumento] = isnull(@p_id_subtipoDocumento,[id_subtipoDocumento])
		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_seg_perfil_subtipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_seg_perfil_subtipoDocumento]
GO
CREATE PROCEDURE [ins_seg_perfil_subtipoDocumento]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_perfil_subtipoDocumento varchar(300),
		@p_id_perfil int,
		@p_id_subtipoDocumento int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [seg_perfil_subtipoDocumento]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[perfil_subtipoDocumento],
			[id_perfil],
			[id_subtipoDocumento])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_perfil_subtipoDocumento,
			@p_id_perfil,
			@p_id_subtipoDocumento)
		SET @v_id = scope_identity()
UPDATE [seg_perfil_subtipoDocumento] 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_seg_perfil_subtipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_seg_perfil_subtipoDocumento]
GO
CREATE PROCEDURE [act_seg_perfil_subtipoDocumento]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [seg_perfil_subtipoDocumento]
	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_seg_perfil_subtipoDocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_seg_perfil_subtipoDocumento_m]
GO
CREATE PROCEDURE [reg_seg_perfil_subtipoDocumento_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],
		[perfil_subtipoDocumento] AS [perfil_subtipoDocumento],
		[id_perfil] AS [id_perfil],
		[id_subtipoDocumento] AS [id_subtipoDocumento]
	FROM [seg_perfil_subtipoDocumento]
	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_seg_perfil_subtipoDocumentosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_seg_perfil_subtipoDocumento]
	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_seg_perfil_subtipoDocumentosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_seg_perfil_subtipoDocumento]
	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_seg_perfil_subtipoDocumentosXperfil_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentosXperfil_m]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentosXperfil_m]
	@p_id_perfil INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_seg_perfil_subtipoDocumento]
	WHERE ([id_perfil]= @p_id_perfil)
	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_seg_perfil_subtipoDocumentosXsubtipoDocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentosXsubtipoDocumento_m]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentosXsubtipoDocumento_m]
	@p_id_subtipoDocumento INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_seg_perfil_subtipoDocumento]
	WHERE ([id_subtipoDocumento]= @p_id_subtipoDocumento)
	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_seg_perfil_subtipoDocumentos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_seg_perfil_subtipoDocumentos_m]
GO
CREATE PROCEDURE [sel_seg_perfil_subtipoDocumentos_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_seg_perfil_subtipoDocumento]
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_seg_perfil_subtipoDocumentos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_seg_perfil_subtipoDocumentos_m]
GO
CREATE PROCEDURE [lis_seg_perfil_subtipoDocumentos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[perfil_subtipoDocumento] AS [seg_perfil_subtipoDocumento]
	FROM [seg_perfil_subtipoDocumento]
	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_seg_perfil_subtipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_seg_perfil_subtipoDocumento]
GO
CREATE PROCEDURE [aud_seg_perfil_subtipoDocumento]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [seg_perfil_subtipoDocumento].id as id_tabla,
	[seg_perfil_subtipoDocumento].id_usuario_modifico as id_usuario_modifico,
	[seg_perfil_subtipoDocumento].id_usuario_creo as id_usuario_creo,
	[seg_perfil_subtipoDocumento].fechaModificacion as fechaModificacion,
	[seg_perfil_subtipoDocumento].fechaCreacion as fechaCreacion,
	[seg_perfil_subtipoDocumento].active as active,
	[seg_perfil_subtipoDocumento].[perfil_subtipoDocumento] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [seg_perfil_subtipoDocumento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[seg_perfil_subtipoDocumento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[seg_perfil_subtipoDocumento].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'seg_perfil_subtipoDocumento'
-----------------------------------24/05/2017 11:48:00