

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_doc_tipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_tipoDocumento]
GO
CREATE PROCEDURE [reg_doc_tipoDocumento]
	@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],
		[tipoDocumento] AS [tipoDocumento],
		[tipoDocumento_codigo] AS [tipoDocumento_codigo],
		[esSalida] AS [esSalida],
		[mostrarBodega] AS [mostrarBodega],
		[mostrarCentroCosto] AS [mostrarCentroCosto],
		[mostrarIncoterm] AS [mostrarIncoterm],
		[puedeSerMayor] AS [puedeSerMayor],
		[puedeSerMenor] AS [puedeSerMenor],
		[esUrgente] AS [esUrgente]
	FROM [doc_tipoDocumento]
	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_tipoDocumento]')) DROP VIEW [dbo].[view_doc_tipoDocumento]
GO
CREATE  VIEW [view_doc_tipoDocumento] AS
SELECT 	[doc_tipoDocumento].[id] AS [id],
		[doc_tipoDocumento].[uid] AS [uid],
		[doc_tipoDocumento].[eid] AS [eid],
		[doc_tipoDocumento].[id_usuario_modifico] AS [id_usuario_modifico],
		[doc_tipoDocumento].[id_usuario_creo] AS [id_usuario_creo],
		[doc_tipoDocumento].[fechaModificacion] AS [fechaModificacion],
		[doc_tipoDocumento].[fechaCreacion] AS [fechaCreacion],
		[doc_tipoDocumento].[active] AS [active],
		[doc_tipoDocumento].[tipoDocumento] AS [tipoDocumento],
		[doc_tipoDocumento].[tipoDocumento_codigo] AS [tipoDocumento_codigo],
		[doc_tipoDocumento].[esSalida] AS [esSalida],
		[doc_tipoDocumento].[mostrarBodega] AS [mostrarBodega],
		[doc_tipoDocumento].[mostrarCentroCosto] AS [mostrarCentroCosto],
		[doc_tipoDocumento].[mostrarIncoterm] AS [mostrarIncoterm],
		[doc_tipoDocumento].[puedeSerMayor] AS [puedeSerMayor],
		[doc_tipoDocumento].[puedeSerMenor] AS [puedeSerMenor],
		[doc_tipoDocumento].[esUrgente] AS [esUrgente],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [doc_tipoDocumento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_tipoDocumento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_tipoDocumento].[id_usuario_creo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [doc_tipoDocumento].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [doc_tipoDocumento].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_tipoDocumentosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoDocumentosXusuario_modifico]
GO
CREATE PROCEDURE [sel_doc_tipoDocumentosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_tipoDocumento]
	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_tipoDocumentosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoDocumentosXusuario_creo]
GO
CREATE PROCEDURE [sel_doc_tipoDocumentosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_doc_tipoDocumento]
	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_tipoDocumentos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoDocumentos]
GO
CREATE PROCEDURE [sel_doc_tipoDocumentos]
	
	@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_tipoDocumento]
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_tipoDocumentos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_tipoDocumentos]
GO
CREATE PROCEDURE [lis_doc_tipoDocumentos]
	AS
BEGIN
	SELECT id AS id,
[tipoDocumento] AS [doc_tipoDocumento]
	FROM [doc_tipoDocumento]
	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_tipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_doc_tipoDocumento]
GO
CREATE PROCEDURE [upd_doc_tipoDocumento]
	@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_tipoDocumento varchar(300) = null,
		@p_tipoDocumento_codigo varchar(100) = null,
		@p_esSalida bit = null,
		@p_mostrarBodega bit = null,
		@p_mostrarCentroCosto bit = null,
		@p_mostrarIncoterm bit = null,
		@p_puedeSerMayor bit = null,
		@p_puedeSerMenor bit = null,
		@p_esUrgente bit = null
AS
BEGIN
		UPDATE [doc_tipoDocumento]
		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(),
			[tipoDocumento] = isnull(@p_tipoDocumento,[tipoDocumento]),
			[tipoDocumento_codigo] = isnull(@p_tipoDocumento_codigo,[tipoDocumento_codigo]),
			[esSalida] = isnull(@p_esSalida,[esSalida]),
			[mostrarBodega] = isnull(@p_mostrarBodega,[mostrarBodega]),
			[mostrarCentroCosto] = isnull(@p_mostrarCentroCosto,[mostrarCentroCosto]),
			[mostrarIncoterm] = isnull(@p_mostrarIncoterm,[mostrarIncoterm]),
			[puedeSerMayor] = isnull(@p_puedeSerMayor,[puedeSerMayor]),
			[puedeSerMenor] = isnull(@p_puedeSerMenor,[puedeSerMenor]),
			[esUrgente] = isnull(@p_esUrgente,[esUrgente])
		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_tipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_doc_tipoDocumento]
GO
CREATE PROCEDURE [ins_doc_tipoDocumento]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_tipoDocumento varchar(300),
		@p_tipoDocumento_codigo varchar(100),
		@p_esSalida bit,
		@p_mostrarBodega bit,
		@p_mostrarCentroCosto bit,
		@p_mostrarIncoterm bit,
		@p_puedeSerMayor bit,
		@p_puedeSerMenor bit,
		@p_esUrgente bit
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [doc_tipoDocumento]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[tipoDocumento],
			[tipoDocumento_codigo],
			[esSalida],
			[mostrarBodega],
			[mostrarCentroCosto],
			[mostrarIncoterm],
			[puedeSerMayor],
			[puedeSerMenor],
			[esUrgente])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_tipoDocumento,
			@p_tipoDocumento_codigo,
			@p_esSalida,
			@p_mostrarBodega,
			@p_mostrarCentroCosto,
			@p_mostrarIncoterm,
			@p_puedeSerMayor,
			@p_puedeSerMenor,
			@p_esUrgente)
		SET @v_id = scope_identity()
UPDATE [doc_tipoDocumento] 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_tipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_doc_tipoDocumento]
GO
CREATE PROCEDURE [act_doc_tipoDocumento]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [doc_tipoDocumento]
	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_tipoDocumento_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_doc_tipoDocumento_m]
GO
CREATE PROCEDURE [reg_doc_tipoDocumento_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],
		[tipoDocumento] AS [tipoDocumento],
		[tipoDocumento_codigo] AS [tipoDocumento_codigo],
		[esSalida] AS [esSalida],
		[mostrarBodega] AS [mostrarBodega],
		[mostrarCentroCosto] AS [mostrarCentroCosto],
		[mostrarIncoterm] AS [mostrarIncoterm],
		[puedeSerMayor] AS [puedeSerMayor],
		[puedeSerMenor] AS [puedeSerMenor],
		[esUrgente] AS [esUrgente]
	FROM [doc_tipoDocumento]
	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_tipoDocumentosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoDocumentosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_doc_tipoDocumentosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_tipoDocumento]
	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_tipoDocumentosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoDocumentosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_doc_tipoDocumentosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_doc_tipoDocumento]
	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_tipoDocumentos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_doc_tipoDocumentos_m]
GO
CREATE PROCEDURE [sel_doc_tipoDocumentos_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_tipoDocumento]
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_tipoDocumentos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_doc_tipoDocumentos_m]
GO
CREATE PROCEDURE [lis_doc_tipoDocumentos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[tipoDocumento] AS [doc_tipoDocumento]
	FROM [doc_tipoDocumento]
	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_tipoDocumento]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_doc_tipoDocumento]
GO
CREATE PROCEDURE [aud_doc_tipoDocumento]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [doc_tipoDocumento].id as id_tabla,
	[doc_tipoDocumento].id_usuario_modifico as id_usuario_modifico,
	[doc_tipoDocumento].id_usuario_creo as id_usuario_creo,
	[doc_tipoDocumento].fechaModificacion as fechaModificacion,
	[doc_tipoDocumento].fechaCreacion as fechaCreacion,
	[doc_tipoDocumento].active as active,
	[doc_tipoDocumento].[tipoDocumento] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [doc_tipoDocumento]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[doc_tipoDocumento].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[doc_tipoDocumento].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'doc_tipoDocumento'
-----------------------------------28/12/2016 11:05:36