
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_factorexito]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_factorexito]
GO
CREATE PROCEDURE [reg_pro_factorexito]
	@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],
		[factorexito] AS [factorexito],
		[factorexito_codigo] AS [factorexito_codigo],
		[id_documento.proyecto] AS [id_documento.proyecto]
	FROM [pro_factorexito]
	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_factorexito]')) DROP VIEW [dbo].[view_pro_factorexito]
GO
CREATE  VIEW [view_pro_factorexito] AS
SELECT 	[pro_factorexito].[id] AS [id],
		[pro_factorexito].[uid] AS [uid],
		[pro_factorexito].[eid] AS [eid],
		[pro_factorexito].[id_usuario_modifico] AS [id_usuario_modifico],
		[pro_factorexito].[id_usuario_creo] AS [id_usuario_creo],
		[pro_factorexito].[fechaModificacion] AS [fechaModificacion],
		[pro_factorexito].[fechaCreacion] AS [fechaCreacion],
		[pro_factorexito].[active] AS [active],
		[pro_factorexito].[factorexito] AS [factorexito],
		[pro_factorexito].[factorexito_codigo] AS [factorexito_codigo],
		[pro_factorexito].[id_documento.proyecto] AS [id_documento.proyecto],
[doc_documento.proyecto].[documento.proyecto] as [doc_documento.proyecto_documento.proyecto],
		[doc_documento_proyecto].[documento_codigo] AS [doc_documento_proyecto_documento_codigo],
		[doc_documento_proyecto].[documento_numero] AS [doc_documento_proyecto_documento_numero],
		[doc_documento_proyecto].[prefijo] AS [doc_documento_proyecto_prefijo],
		[doc_documento_proyecto].[subtotal] AS [doc_documento_proyecto_subtotal],
		[doc_documento_proyecto].[descuento] AS [doc_documento_proyecto_descuento],
		[doc_documento_proyecto].[iva] AS [doc_documento_proyecto_iva],
		[doc_documento_proyecto].[total] AS [doc_documento_proyecto_total],
		[doc_documento_proyecto].[costo] AS [doc_documento_proyecto_costo],
		[doc_documento_proyecto].[rentabilidad] AS [doc_documento_proyecto_rentabilidad],
		[doc_documento_proyecto].[trm] AS [doc_documento_proyecto_trm],
		[doc_documento_proyecto].[fecha_fh] AS [doc_documento_proyecto_fecha_fh],
		[doc_documento_proyecto].[fechaSugerida_fh] AS [doc_documento_proyecto_fechaSugerida_fh],
		[doc_documento_proyecto].[fechaCierre_ff] AS [doc_documento_proyecto_fechaCierre_ff],
		[doc_documento_proyecto].[fechaLimite_fh] AS [doc_documento_proyecto_fechaLimite_fh],
		[doc_documento_proyecto].[id_subtipoDocumento] AS [doc_documento_proyecto_id_subtipoDocumento],
		[doc_documento_proyecto].[id_tercero_cliente] AS [doc_documento_proyecto_id_tercero_cliente],
		[doc_documento_proyecto].[id_documento] AS [doc_documento_proyecto_id_documento],
		[doc_documento_proyecto].[id_tercero_proveedor] AS [doc_documento_proyecto_id_tercero_proveedor],
		[doc_documento_proyecto].[id_prioridadDocumento] AS [doc_documento_proyecto_id_prioridadDocumento],
		[doc_documento_proyecto].[id_moneda] AS [doc_documento_proyecto_id_moneda],
		[doc_documento_proyecto].[id_estadoTipoDocumento] AS [doc_documento_proyecto_id_estadoTipoDocumento],
		[doc_documento_proyecto].[id_motivoestado] AS [doc_documento_proyecto_id_motivoestado],
		[doc_documento_proyecto].[subtotalPlaneado] AS [doc_documento_proyecto_subtotalPlaneado],
		[doc_documento_proyecto].[descuentoPlaneado] AS [doc_documento_proyecto_descuentoPlaneado],
		[doc_documento_proyecto].[ivaPlaneado] AS [doc_documento_proyecto_ivaPlaneado],
		[doc_documento_proyecto].[totalPlaneado] AS [doc_documento_proyecto_totalPlaneado],
		[doc_documento_proyecto].[costoPlaneado] AS [doc_documento_proyecto_costoPlaneado],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [pro_factorexito]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_factorexito].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_factorexito].[id_usuario_creo]
	INNER JOIN [doc_documento.proyecto] ON [doc_documento.proyecto].id=[pro_factorexito].[id_documento.proyecto]
	INNER JOIN [doc_documento] aS [doc_documento_proyecto] ON [doc_documento.proyecto].id = [doc_documento_proyecto].id
	INNER JOIN [gen_empresa] AS empresaCodigo ON [pro_factorexito].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [pro_factorexito].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_factorexitosXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_factorexitosXusuario_modifico]
GO
CREATE PROCEDURE [sel_pro_factorexitosXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_factorexito]
	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_factorexitosXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_factorexitosXusuario_creo]
GO
CREATE PROCEDURE [sel_pro_factorexitosXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_factorexito]
	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_factorexitosXdocumento_proyecto]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_factorexitosXdocumento_proyecto]
GO
CREATE PROCEDURE [sel_pro_factorexitosXdocumento_proyecto]
	@p_id_documento_proyecto INT
AS
BEGIN
	SELECT * 
	FROM [view_pro_factorexito]
	WHERE ([id_documento.proyecto]= @p_id_documento_proyecto)
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_factorexitos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_factorexitos]
GO
CREATE PROCEDURE [sel_pro_factorexitos]
	
	@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_factorexito]
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_factorexitos]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_factorexitos]
GO
CREATE PROCEDURE [lis_pro_factorexitos]
	AS
BEGIN
	SELECT id AS id,
[factorexito] AS [pro_factorexito]
	FROM [pro_factorexito]
	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_factorexito]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_pro_factorexito]
GO
CREATE PROCEDURE [upd_pro_factorexito]
	@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_factorexito varchar(300) = null,
		@p_factorexito_codigo varchar(100) = null,
		@p_id_documento_proyecto int = null
AS
BEGIN
		UPDATE [pro_factorexito]
		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(),
			[factorexito] = isnull(@p_factorexito,[factorexito]),
			[factorexito_codigo] = isnull(@p_factorexito_codigo,[factorexito_codigo]),
			[id_documento.proyecto] = isnull(@p_id_documento_proyecto,[id_documento.proyecto])
		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_factorexito]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_pro_factorexito]
GO
CREATE PROCEDURE [ins_pro_factorexito]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_factorexito varchar(300),
		@p_factorexito_codigo varchar(100),
		@p_id_documento_proyecto int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [pro_factorexito]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[factorexito],
			[factorexito_codigo],
			[id_documento.proyecto])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_factorexito,
			@p_factorexito_codigo,
			@p_id_documento_proyecto)
		SET @v_id = scope_identity()
UPDATE [pro_factorexito] 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_factorexito]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_pro_factorexito]
GO
CREATE PROCEDURE [act_pro_factorexito]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [pro_factorexito]
	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_factorexito_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_pro_factorexito_m]
GO
CREATE PROCEDURE [reg_pro_factorexito_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],
		[factorexito] AS [factorexito],
		[factorexito_codigo] AS [factorexito_codigo],
		[id_documento.proyecto] AS [id_documento.proyecto]
	FROM [pro_factorexito]
	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_factorexitosXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_factorexitosXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_pro_factorexitosXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_factorexito]
	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_factorexitosXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_factorexitosXusuario_creo_m]
GO
CREATE PROCEDURE [sel_pro_factorexitosXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_factorexito]
	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_factorexitosXdocumento_proyecto_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_factorexitosXdocumento_proyecto_m]
GO
CREATE PROCEDURE [sel_pro_factorexitosXdocumento_proyecto_m]
	@p_id_documento_proyecto INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_pro_factorexito]
	WHERE ([id_documento.proyecto]= @p_id_documento_proyecto)
	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_factorexitos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_pro_factorexitos_m]
GO
CREATE PROCEDURE [sel_pro_factorexitos_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_factorexito]
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_factorexitos_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_pro_factorexitos_m]
GO
CREATE PROCEDURE [lis_pro_factorexitos_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[factorexito] AS [pro_factorexito]
	FROM [pro_factorexito]
	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_factorexito]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_pro_factorexito]
GO
CREATE PROCEDURE [aud_pro_factorexito]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [pro_factorexito].id as id_tabla,
	[pro_factorexito].id_usuario_modifico as id_usuario_modifico,
	[pro_factorexito].id_usuario_creo as id_usuario_creo,
	[pro_factorexito].fechaModificacion as fechaModificacion,
	[pro_factorexito].fechaCreacion as fechaCreacion,
	[pro_factorexito].active as active,
	[pro_factorexito].[factorexito] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [pro_factorexito]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[pro_factorexito].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[pro_factorexito].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
print 'pro_factorexito'
-----------------------------------26/06/2015 8:36:56