/****** Object:  StoredProcedure [dbo].[P_TRILHO_RODA_TRILHO]    Script Date: 01/08/2013 09:04:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_TRILHO_RODA_TRILHO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_TRILHO_RODA_TRILHO]
GO

SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON 
GO
---[P_TRILHO_RODA_TRILHO] 1,1
CREATE PROCEDURE [dbo].[P_TRILHO_RODA_TRILHO] (@CD_EMP AS INT=1, @CD_CTR_DEPO AS INT=1)
WITH ENCRYPTION AS 
	DECLARE @CD_MAQ AS INT

	print 'P_TRILHO_RODA_TRILHO - SELECT 1 ' + CONVERT(VARCHAR, GETDATE(), 108);
	--Se existir maq filial...
	if exists(SELECT 
					CD_EXCESSO_MAQ
				FROM 
					EST_EXCESSO_MAQ
				WHERE 
					CD_EMP=@cd_emp
					AND TP_REDIRECIONAMENTO = 0 
					AND STATUS=0
					AND TP_MAQ= 0)

	begin
		--Trilho Filial
		print 'EXEC 2 ' + CONVERT(VARCHAR, GETDATE(), 108);
		EXECUTE dbo.P_TRILHO_PROD_EXCESSO @CD_EMP, @CD_CTR_DEPO;	
 
		print 'SELECT 3 ' + CONVERT(VARCHAR, GETDATE(), 108);
		DECLARE CursorTemp CURSOR FOR  	
		SELECT 
			CD_EXCESSO_MAQ
		FROM 
			EST_EXCESSO_MAQ
		WHERE 
			CD_EMP=@CD_EMP 
			AND TP_REDIRECIONAMENTO = 0 
			AND STATUS=0
			AND TP_MAQ= 0;

		OPEN CursorTemp;

		FETCH NEXT FROM CursorTemp INTO @CD_MAQ;
		WHILE @@FETCH_STATUS = 0
		BEGIN 
			print 'EXEC 4 ' + CONVERT(VARCHAR, GETDATE(), 108);
			EXECUTE P_TRILHO_PROD_NECESSIDADE @CD_EMP,@CD_CTR_DEPO, @CD_MAQ; 
			print 'EXEC 5 ' + CONVERT(VARCHAR, GETDATE(), 108);
			EXECUTE P_TRILHO_MAQ_EXCESSO @CD_EMP, @CD_CTR_DEPO, @CD_MAQ; 
			FETCH NEXT FROM CursorTemp INTO  @CD_MAQ;
		END

		CLOSE CursorTemp;
		DEALLOCATE CursorTemp;
		
		print 'EXEC 6 ' + CONVERT(VARCHAR, GETDATE(), 108);
		EXECUTE P_TRILHO_EXCESSO @CD_EMP, 0;
	end
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/	
	--Trilho Fornecedores
	
		--Se existir maq filial...
		print 'SELECT 7 ' + CONVERT(VARCHAR, GETDATE(), 108);
	if exists(SELECT 
					CD_EXCESSO_MAQ
				FROM 
					EST_EXCESSO_MAQ
				WHERE 
					CD_EMP=@cd_emp
					AND STATUS=0
					AND TP_MAQ= 1)
	begin
		print 'EXEC 8 ' + CONVERT(VARCHAR, GETDATE(), 108);
		EXECUTE dbo.P_TRILHO_PROD_EXCESSO @CD_EMP, @CD_CTR_DEPO;	

		set @CD_MAQ =0;
		print 'SELECT 9 ' + CONVERT(VARCHAR, GETDATE(), 108);
		DECLARE CursorTemp CURSOR FOR   
		SELECT 
			CD_EXCESSO_MAQ
		FROM 
			EST_EXCESSO_MAQ
		WHERE 
			CD_EMP=@CD_EMP  
			AND STATUS=0
			AND TP_MAQ= 1; 
		OPEN CursorTemp; 
	
		FETCH NEXT FROM CursorTemp INTO @CD_MAQ;
		WHILE @@FETCH_STATUS = 0
		BEGIN 
			print 'EXEC 10 ' + CONVERT(VARCHAR, GETDATE(), 108);
			EXECUTE P_TRILHO_MAQ_EXCESSO @CD_EMP, @CD_CTR_DEPO, @CD_MAQ; 
			print 'EXEC 11 ' + CONVERT(VARCHAR, GETDATE(), 108);
			EXECUTE P_TRILHO_PROD_NECESSIDADE_FORN @CD_EMP, @CD_CTR_DEPO, @CD_MAQ; 
			FETCH NEXT FROM CursorTemp INTO  @CD_MAQ;
		END

		CLOSE CursorTemp;
		DEALLOCATE CursorTemp;
		print 'EXEC 12 ' + CONVERT(VARCHAR, GETDATE(), 108);
		EXECUTE P_TRILHO_EXCESSO_FORN @CD_EMP;	

	end
GO

print 'P_TRILHO_RODA_TRILHO FINALIZADA!';