

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_pro_ejecutores]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_ejecutores]
GO
CREATE PROCEDURE [reg_pro_ejecutores]
	@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],
		[ejecutores] AS [ejecutores],
		[ejecutores_codigo] AS [ejecutores_codigo],
		[costo] AS [costo],
		[unidadTiempo] AS [unidadTiempo],
		[id_horarioTrabajo] AS [id_horarioTrabajo],
		[id_usuario] AS [id_usuario]
	FROM [pro_ejecutores]
	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_pro_ejecutores]')) DROP VIEW [dbo].[view_pro_ejecutores]
GO
CREATE  VIEW [view_pro_ejecutores] AS
SELECT 	[pro_ejecutores].[id] AS [id],
		[pro_ejecutores].[uid] AS [uid],
		[pro_ejecutores].[eid] AS [eid],
		[pro_ejecutores].[id_usuario_modifico] AS [id_usuario_modifico],
		[pro_ejecutores].[id_usuario_creo] AS [id_usuario_creo],
		[pro_ejecutores].[fechaModificacion] AS [fechaModificacion],
		[pro_ejecutores].[fechaCreacion] AS [fechaCreacion],
		[pro_ejecutores].[active] AS [active],
		[pro_ejecutores].[ejecutores] AS [ejecutores],
		[pro_ejecutores].[ejecutores_codigo] AS [ejecutores_codigo],
		[pro_ejecutores].[costo] AS [costo],
		[pro_ejecutores].[unidadTiempo] AS [unidadTiempo],
		[pro_ejecutores].[id_horarioTrabajo] AS [id_horarioTrabajo],
[pro_horarioTrabajo].[horarioTrabajo] as [pro_horarioTrabajo_horarioTrabajo],
[pro_horarioTrabajo].[horarioTrabajo_codigo] as [pro_horarioTrabajo_horarioTrabajo_codigo],
		[pro_ejecutores].[id_usuario] AS [id_usuario],
[seg_usuario].[usuario] as [seg_usuario_usuario],
[seg_usuario].[usuario_codigo] as [seg_usuario_usuario_codigo],
[seg_usuario].[intentosFallidos] as [seg_usuario_intentosFallidos],
[seg_usuario].[numeroIngresos] as [seg_usuario_numeroIngresos],
[seg_usuario].[fechaUltimoIngreso_fh] as [seg_usuario_fechaUltimoIngreso_fh],
[seg_usuario].[fechaIntentoFallido_fh] as [seg_usuario_fechaIntentoFallido_fh],
[seg_usuario].[clave] as [seg_usuario_clave],
[seg_usuario].[nombre] as [seg_usuario_nombre],
[seg_usuario].[profesion] as [seg_usuario_profesion],
[seg_usuario].[email] as [seg_usuario_email],
[seg_usuario].[direccion] as [seg_usuario_direccion],
[seg_usuario].[telefono] as [seg_usuario_telefono],
[seg_usuario].[costoHora] as [seg_usuario_costoHora],
[seg_usuario].[porcentajeComision] as [seg_usuario_porcentajeComision],
[seg_usuario].[accesoActivo] as [seg_usuario_accesoActivo],
[seg_usuario].[cambiarClave] as [seg_usuario_cambiarClave],
[seg_usuario].[fechaUltimoCambioClave_fh] as [seg_usuario_fechaUltimoCambioClave_fh],
[seg_usuario].[aplicaRecargo] as [seg_usuario_aplicaRecargo],
[seg_usuario].[sexo] as [seg_usuario_sexo],
[seg_usuario].[id_perfil] as [seg_usuario_id_perfil],
[seg_usuario].[id_cargo] as [seg_usuario_id_cargo],
[seg_usuario].[id_grupo] as [seg_usuario_id_grupo],
[seg_usuario].[id_zona] as [seg_usuario_id_zona],
[seg_usuario].[identificacion] as [seg_usuario_identificacion],
[seg_usuario].[id_usuario] as [seg_usuario_id_usuario],
[seg_usuario].[preferencias] as [seg_usuario_preferencias],
[seg_usuario].[habilitado] as [seg_usuario_habilitado],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [pro_ejecutores]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_ejecutores].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_ejecutores].[id_usuario_creo]
	INNER JOIN [pro_horarioTrabajo] ON [pro_horarioTrabajo].id=[pro_ejecutores].[id_horarioTrabajo]
	INNER JOIN [seg_usuario] ON [seg_usuario].id=[pro_ejecutores].[id_usuario]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [pro_ejecutores].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [pro_ejecutores].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_pro_ejecutoresesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoresesXusuario_modifico]
GO
CREATE PROCEDURE [sel_pro_ejecutoresesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_ejecutores]
	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_pro_ejecutoresesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoresesXusuario_creo]
GO
CREATE PROCEDURE [sel_pro_ejecutoresesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_ejecutores]
	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_pro_ejecutoresesXhorarioTrabajo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoresesXhorarioTrabajo]
GO
CREATE PROCEDURE [sel_pro_ejecutoresesXhorarioTrabajo]
	@p_id_horarioTrabajo INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_ejecutores]
	WHERE ([id_horarioTrabajo]= @p_id_horarioTrabajo)
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_pro_ejecutoresesXusuario]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoresesXusuario]
GO
CREATE PROCEDURE [sel_pro_ejecutoresesXusuario]
	@p_id_usuario INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_ejecutores]
	WHERE ([id_usuario]= @p_id_usuario)
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_pro_ejecutoreses]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoreses]
GO
CREATE PROCEDURE [sel_pro_ejecutoreses]
	
	@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_pro_ejecutores]
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_pro_ejecutoreses]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_ejecutoreses]
GO
CREATE PROCEDURE [lis_pro_ejecutoreses]
	AS
BEGIN
	SELECT id AS id,
[ejecutores] AS [pro_ejecutores]
	FROM [pro_ejecutores]
	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_pro_ejecutores]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_pro_ejecutores]
GO
CREATE PROCEDURE [upd_pro_ejecutores]
	@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_ejecutores varchar(300) = null,
		@p_ejecutores_codigo varchar(100) = null,
		@p_costo float = null,
		@p_unidadTiempo varchar(1) = null,
		@p_id_horarioTrabajo int = null,
		@p_id_usuario int = null
AS
BEGIN
		UPDATE [pro_ejecutores]
		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(),
			[ejecutores] = isnull(@p_ejecutores,[ejecutores]),
			[ejecutores_codigo] = isnull(@p_ejecutores_codigo,[ejecutores_codigo]),
			[costo] = isnull(@p_costo,[costo]),
			[unidadTiempo] = isnull(@p_unidadTiempo,[unidadTiempo]),
			[id_horarioTrabajo] = isnull(@p_id_horarioTrabajo,[id_horarioTrabajo]),
			[id_usuario] = isnull(@p_id_usuario,[id_usuario])
		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_pro_ejecutores]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_pro_ejecutores]
GO
CREATE PROCEDURE [ins_pro_ejecutores]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_ejecutores varchar(300),
		@p_ejecutores_codigo varchar(100),
		@p_costo float,
		@p_unidadTiempo varchar(1),
		@p_id_horarioTrabajo int,
		@p_id_usuario int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [pro_ejecutores]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[ejecutores],
			[ejecutores_codigo],
			[costo],
			[unidadTiempo],
			[id_horarioTrabajo],
			[id_usuario])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_ejecutores,
			@p_ejecutores_codigo,
			@p_costo,
			@p_unidadTiempo,
			@p_id_horarioTrabajo,
			@p_id_usuario)
		SET @v_id = scope_identity()
UPDATE [pro_ejecutores] 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_pro_ejecutores]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_pro_ejecutores]
GO
CREATE PROCEDURE [act_pro_ejecutores]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [pro_ejecutores]
	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_pro_ejecutores_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_ejecutores_m]
GO
CREATE PROCEDURE [reg_pro_ejecutores_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],
		[ejecutores] AS [ejecutores],
		[ejecutores_codigo] AS [ejecutores_codigo],
		[costo] AS [costo],
		[unidadTiempo] AS [unidadTiempo],
		[id_horarioTrabajo] AS [id_horarioTrabajo],
		[id_usuario] AS [id_usuario]
	FROM [pro_ejecutores]
	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_pro_ejecutoresesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoresesXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_pro_ejecutoresesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_ejecutores]
	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_pro_ejecutoresesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoresesXusuario_creo_m]
GO
CREATE PROCEDURE [sel_pro_ejecutoresesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_ejecutores]
	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_pro_ejecutoresesXhorarioTrabajo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoresesXhorarioTrabajo_m]
GO
CREATE PROCEDURE [sel_pro_ejecutoresesXhorarioTrabajo_m]
	@p_id_horarioTrabajo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_ejecutores]
	WHERE ([id_horarioTrabajo]= @p_id_horarioTrabajo)
	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_pro_ejecutoresesXusuario_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoresesXusuario_m]
GO
CREATE PROCEDURE [sel_pro_ejecutoresesXusuario_m]
	@p_id_usuario INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_ejecutores]
	WHERE ([id_usuario]= @p_id_usuario)
	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_pro_ejecutoreses_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_ejecutoreses_m]
GO
CREATE PROCEDURE [sel_pro_ejecutoreses_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_pro_ejecutores]
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_pro_ejecutoreses_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_ejecutoreses_m]
GO
CREATE PROCEDURE [lis_pro_ejecutoreses_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[ejecutores] AS [pro_ejecutores]
	FROM [pro_ejecutores]
	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_pro_ejecutores]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_pro_ejecutores]
GO
CREATE PROCEDURE [aud_pro_ejecutores]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [pro_ejecutores].id as id_tabla,
	[pro_ejecutores].id_usuario_modifico as id_usuario_modifico,
	[pro_ejecutores].id_usuario_creo as id_usuario_creo,
	[pro_ejecutores].fechaModificacion as fechaModificacion,
	[pro_ejecutores].fechaCreacion as fechaCreacion,
	[pro_ejecutores].active as active,
	[pro_ejecutores].[ejecutores] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [pro_ejecutores]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_ejecutores].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_ejecutores].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'pro_ejecutores'
-----------------------------------07/07/2015 11:24:24 a.m.