
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_cat_atributo_opcionAtributo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_atributo_opcionAtributo]
GO
CREATE PROCEDURE [reg_cat_atributo_opcionAtributo]
	@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],
		[atributo_opcionAtributo] AS [atributo_opcionAtributo],
		[id_atributo] AS [id_atributo],
		[id_opcionAtributo] AS [id_opcionAtributo]
	FROM [cat_atributo_opcionAtributo]
	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_cat_atributo_opcionAtributo]')) DROP VIEW [dbo].[view_cat_atributo_opcionAtributo]
GO
CREATE  VIEW [view_cat_atributo_opcionAtributo] AS
SELECT 	[cat_atributo_opcionAtributo].[id] AS [id],
		[cat_atributo_opcionAtributo].[uid] AS [uid],
		[cat_atributo_opcionAtributo].[eid] AS [eid],
		[cat_atributo_opcionAtributo].[id_usuario_modifico] AS [id_usuario_modifico],
		[cat_atributo_opcionAtributo].[id_usuario_creo] AS [id_usuario_creo],
		[cat_atributo_opcionAtributo].[fechaModificacion] AS [fechaModificacion],
		[cat_atributo_opcionAtributo].[fechaCreacion] AS [fechaCreacion],
		[cat_atributo_opcionAtributo].[active] AS [active],
		[cat_atributo_opcionAtributo].[atributo_opcionAtributo] AS [atributo_opcionAtributo],
		[cat_atributo_opcionAtributo].[id_atributo] AS [id_atributo],
[cat_atributo].[atributo] as [cat_atributo_atributo],
[cat_atributo].[atributo_codigo] as [cat_atributo_atributo_codigo],
[cat_atributo].[nombreControl] as [cat_atributo_nombreControl],
[cat_atributo].[esVariable] as [cat_atributo_esVariable],
[cat_atributo].[resaltado] as [cat_atributo_resaltado],
[cat_atributo].[valorMinimo] as [cat_atributo_valorMinimo],
[cat_atributo].[ValorMaximo] as [cat_atributo_ValorMaximo],
[cat_atributo].[id_seccionAtributo] as [cat_atributo_id_seccionAtributo],
[cat_atributo].[id_tipoAtributo] as [cat_atributo_id_tipoAtributo],
[cat_atributo].[id_unidad] as [cat_atributo_id_unidad],
[cat_atributo].[esObligatorio] as [cat_atributo_esObligatorio],
		[cat_atributo_opcionAtributo].[id_opcionAtributo] AS [id_opcionAtributo],
[equ_opcionAtributo].[opcionAtributo] as [equ_opcionAtributo_opcionAtributo],
[equ_opcionAtributo].[opcionAtributo_codigo] as [equ_opcionAtributo_opcionAtributo_codigo],
[equ_opcionAtributo].[orden] as [equ_opcionAtributo_orden],
[equ_opcionAtributo].[id_atributo] as [equ_opcionAtributo_id_atributo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [cat_atributo_opcionAtributo]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_atributo_opcionAtributo].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_atributo_opcionAtributo].[id_usuario_creo]
	INNER JOIN [cat_atributo] ON [cat_atributo].id=[cat_atributo_opcionAtributo].[id_atributo]
	INNER JOIN [equ_opcionAtributo] ON [equ_opcionAtributo].id=[cat_atributo_opcionAtributo].[id_opcionAtributo]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [cat_atributo_opcionAtributo].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_cat_atributo_opcionAtributosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributosXusuario_modifico]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_atributo_opcionAtributo]
	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_cat_atributo_opcionAtributosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributosXusuario_creo]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_atributo_opcionAtributo]
	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_cat_atributo_opcionAtributosXatributo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributosXatributo]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributosXatributo]
	@p_id_atributo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_atributo_opcionAtributo]
	WHERE ([id_atributo]= @p_id_atributo)
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_cat_atributo_opcionAtributosXopcionAtributo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributosXopcionAtributo]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributosXopcionAtributo]
	@p_id_opcionAtributo INT
AS
BEGIN
	SELECT * 
	FROM [view_cat_atributo_opcionAtributo]
	WHERE ([id_opcionAtributo]= @p_id_opcionAtributo)
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_cat_atributo_opcionAtributos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributos]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributos]
	
	@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_cat_atributo_opcionAtributo]
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_cat_atributo_opcionAtributos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_atributo_opcionAtributos]
GO
CREATE PROCEDURE [lis_cat_atributo_opcionAtributos]
	AS
BEGIN
	SELECT id AS id,
[atributo_opcionAtributo] AS [cat_atributo_opcionAtributo]
	FROM [cat_atributo_opcionAtributo]
	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_cat_atributo_opcionAtributo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_cat_atributo_opcionAtributo]
GO
CREATE PROCEDURE [upd_cat_atributo_opcionAtributo]
	@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_atributo_opcionAtributo varchar(300) = null,
		@p_id_atributo int = null,
		@p_id_opcionAtributo int = null
AS
BEGIN
		UPDATE [cat_atributo_opcionAtributo]
		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(),
			[atributo_opcionAtributo] = isnull(@p_atributo_opcionAtributo,[atributo_opcionAtributo]),
			[id_atributo] = isnull(@p_id_atributo,[id_atributo]),
			[id_opcionAtributo] = isnull(@p_id_opcionAtributo,[id_opcionAtributo])
		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_cat_atributo_opcionAtributo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_cat_atributo_opcionAtributo]
GO
CREATE PROCEDURE [ins_cat_atributo_opcionAtributo]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_atributo_opcionAtributo varchar(300),
		@p_id_atributo int,
		@p_id_opcionAtributo int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [cat_atributo_opcionAtributo]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[atributo_opcionAtributo],
			[id_atributo],
			[id_opcionAtributo])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_atributo_opcionAtributo,
			@p_id_atributo,
			@p_id_opcionAtributo)
		SET @v_id = scope_identity()
UPDATE [cat_atributo_opcionAtributo] 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_cat_atributo_opcionAtributo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_cat_atributo_opcionAtributo]
GO
CREATE PROCEDURE [act_cat_atributo_opcionAtributo]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [cat_atributo_opcionAtributo]
	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_cat_atributo_opcionAtributo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_cat_atributo_opcionAtributo_m]
GO
CREATE PROCEDURE [reg_cat_atributo_opcionAtributo_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],
		[atributo_opcionAtributo] AS [atributo_opcionAtributo],
		[id_atributo] AS [id_atributo],
		[id_opcionAtributo] AS [id_opcionAtributo]
	FROM [cat_atributo_opcionAtributo]
	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_cat_atributo_opcionAtributosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_atributo_opcionAtributo]
	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_cat_atributo_opcionAtributosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_atributo_opcionAtributo]
	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_cat_atributo_opcionAtributosXatributo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributosXatributo_m]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributosXatributo_m]
	@p_id_atributo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_atributo_opcionAtributo]
	WHERE ([id_atributo]= @p_id_atributo)
	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_cat_atributo_opcionAtributosXopcionAtributo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributosXopcionAtributo_m]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributosXopcionAtributo_m]
	@p_id_opcionAtributo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_cat_atributo_opcionAtributo]
	WHERE ([id_opcionAtributo]= @p_id_opcionAtributo)
	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_cat_atributo_opcionAtributos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_cat_atributo_opcionAtributos_m]
GO
CREATE PROCEDURE [sel_cat_atributo_opcionAtributos_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_cat_atributo_opcionAtributo]
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_cat_atributo_opcionAtributos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_cat_atributo_opcionAtributos_m]
GO
CREATE PROCEDURE [lis_cat_atributo_opcionAtributos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[atributo_opcionAtributo] AS [cat_atributo_opcionAtributo]
	FROM [cat_atributo_opcionAtributo]
	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_cat_atributo_opcionAtributo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_cat_atributo_opcionAtributo]
GO
CREATE PROCEDURE [aud_cat_atributo_opcionAtributo]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [cat_atributo_opcionAtributo].id as id_tabla,
	[cat_atributo_opcionAtributo].id_usuario_modifico as id_usuario_modifico,
	[cat_atributo_opcionAtributo].id_usuario_creo as id_usuario_creo,
	[cat_atributo_opcionAtributo].fechaModificacion as fechaModificacion,
	[cat_atributo_opcionAtributo].fechaCreacion as fechaCreacion,
	[cat_atributo_opcionAtributo].active as active,
	[cat_atributo_opcionAtributo].[atributo_opcionAtributo] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [cat_atributo_opcionAtributo]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[cat_atributo_opcionAtributo].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[cat_atributo_opcionAtributo].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'cat_atributo_opcionAtributo'
-----------------------------------25/10/2019 16:16:18