IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_EST_NF_ENT_PRE_ENTRADA_CPL_CONF_WMS_ALL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_EST_NF_ENT_PRE_ENTRADA_CPL_CONF_WMS_ALL]
GO
--EXEC P_EST_NF_ENT_PRE_ENTRADA_CPL_CONF_WMS_ALL
CREATE PROCEDURE [dbo].[P_EST_NF_ENT_PRE_ENTRADA_CPL_CONF_WMS_ALL] AS
SET NOCOUNT ON
DECLARE @CD_CTR INT

DECLARE CPreEntradaProc CURSOR FAST_FORWARD FOR 
	SELECT
		 DISTINCT EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_PRE_ENTRADA.CD_CTR
	FROM
		EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_PRE_ENTRADA INNER JOIN EST_NF_ENT_PRE_ENTRADA_CPL_CONF ON
		EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_PRE_ENTRADA.CD_EMP = EST_NF_ENT_PRE_ENTRADA_CPL_CONF.CD_EMP AND
		EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_PRE_ENTRADA.CD_CTR_PRE_ENTRADA = EST_NF_ENT_PRE_ENTRADA_CPL_CONF.CD_CTR_PRE_ENTRADA
		INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_PENDENTE ON
		EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_PRE_ENTRADA.CD_CTR = EST_NF_ENT_DANFE_PROVISORIO_PENDENTE.CD_CTR
		LEFT OUTER JOIN EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA ON
		EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_PRE_ENTRADA.CD_CTR = EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA.CD_CTR
	WHERE
		EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA.CD_CTR IS NULL		
		
OPEN CPreEntradaProc
FETCH NEXT FROM CPreEntradaProc INTO @CD_CTR
	WHILE @@FETCH_STATUS=0
	  begin
		PRINT 'Processando danfe ' + CONVERT(VARCHAR, @CD_CTR) + ' ' + CONVERT(VARCHAR, GETDATE (),108)
		
	    EXEC P_EST_NF_ENT_PRE_ENTRADA_CPL_CONF_WMS @CD_CTR

		FETCH NEXT FROM CPreEntradaProc INTO @CD_CTR
	  end

PRINT 'Concluido.'
CLOSE CPreEntradaProc;
DEALLOCATE CPreEntradaProc;

GO


