IF OBJECT_ID('P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE') IS NOT NULL
	BEGIN
		DROP PROCEDURE P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE
	END
GO 
--P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE 1,2
CREATE PROCEDURE P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE @CD_EMP INT=1, 
																						@CD_FILIAL INT=0, 
																						@DT_INI AS DATETIME=NULL, 
																						@DT_FIM DATETIME=NULL AS
SET NOCOUNT ON;
IF @DT_INI IS NULL
BEGIN
	SET @DT_FIM=CONVERT(CHAR(10),GETDATE(),112)
	SET @DT_INI=@DT_FIM-20
END

INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_CIENCIA_OPERACAO_PENDENTE
SELECT
	EST_NF_ENT_DANFE_PROVISORIO_CIENCIA_OPERACAO_PENDENTE.CD_CTR,
	GETDATE()
FROM 
	 EST_NF_ENT_DANFE_PROVISORIO INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_PENDENTE ON
	 EST_NF_ENT_DANFE_PROVISORIO.CD_CTR =EST_NF_ENT_DANFE_PROVISORIO_PENDENTE.CD_CTR 
	 LEFT OUTER  JOIN EST_NF_ENT_DANFE_PROVISORIO_CIENCIA_OPERACAO ON
	 EST_NF_ENT_DANFE_PROVISORIO.CD_CTR =EST_NF_ENT_DANFE_PROVISORIO_CIENCIA_OPERACAO.CD_CTR
	 LEFT OUTER  JOIN EST_NF_ENT_DANFE_PROVISORIO_CIENCIA_OPERACAO_PENDENTE ON
	 EST_NF_ENT_DANFE_PROVISORIO.CD_CTR =EST_NF_ENT_DANFE_PROVISORIO_CIENCIA_OPERACAO_PENDENTE.CD_CTR
 WHERE
	 EST_NF_ENT_DANFE_PROVISORIO.CD_EMP =@CD_EMP
	 AND EST_NF_ENT_DANFE_PROVISORIO.CD_FILIAL = @CD_FILIAL
	 AND EST_NF_ENT_DANFE_PROVISORIO_CIENCIA_OPERACAO.CD_CTR IS NULL
	 AND EST_NF_ENT_DANFE_PROVISORIO_PENDENTE.CD_CTR IS NULL

INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE
SELECT
	EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_EMP,
	EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_FILIAL,
	EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_NF,
	EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_CTR,
	GETDATE()
FROM 
	 EST_NF_ENT INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT ON
	 EST_NF_ENT.CD_EMP =EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_EMP AND 
	 EST_NF_ENT.CD_FILIAL = EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_FILIAL AND 
	 EST_NF_ENT.CD_NF = EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_NF
	 LEFT OUTER  JOIN EST_NF_ENT_DANFE_PROVISORIO_CONFIRMACAO_OPERACAO ON
	 EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_CTR =EST_NF_ENT_DANFE_PROVISORIO_CONFIRMACAO_OPERACAO.CD_CTR
	 LEFT OUTER  JOIN EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE ON
	 EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_CTR =EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE.CD_CTR
 
 WHERE
	 EST_NF_ENT.CD_EMP =@CD_EMP
	 AND EST_NF_ENT.DT_ENT BETWEEN @DT_INI AND @DT_FIM
	 AND EST_NF_ENT.CD_FILIAL = @CD_FILIAL
	 AND EST_NF_ENT_DANFE_PROVISORIO_CONFIRMACAO_OPERACAO.CD_CTR IS NULL
	 AND EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE.CD_CTR IS NULL


INSERT INTO EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE
(CD_EMP,
CD_FIL_ORIG,
CD_FIL_DEST,
CD_NF_SAI,
CD_NF_ENT,
TIPO_MANIFESTO,
DT_CAD)
SELECT
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_EMP,
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_FIL_ORIG,
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_FIL_DEST,
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_NF_SAI,
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_NF_ENT,
	 0,
	GETDATE()
FROM 
	 EST_NF_ENT INNER JOIN EST_NF_ENT_EST_NF_SAI_TRANSF ON
	 EST_NF_ENT.CD_EMP =EST_NF_ENT_EST_NF_SAI_TRANSF.CD_EMP AND 
	 EST_NF_ENT.CD_FILIAL = EST_NF_ENT_EST_NF_SAI_TRANSF.CD_FIL_DEST  AND 
	 EST_NF_ENT.CD_NF = EST_NF_ENT_EST_NF_SAI_TRANSF.CD_NF_ENT
	 LEFT OUTER  JOIN EST_NF_ENT_TRANSF_CIENCIA_OPERACAO ON
	 EST_NF_ENT.CD_EMP =EST_NF_ENT_TRANSF_CIENCIA_OPERACAO.CD_EMP AND 
	 EST_NF_ENT.CD_FILIAL = EST_NF_ENT_TRANSF_CIENCIA_OPERACAO.CD_FILIAL  AND 
	 EST_NF_ENT.CD_NF = EST_NF_ENT_TRANSF_CIENCIA_OPERACAO.CD_NF
	 LEFT OUTER  JOIN EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE ON
	 EST_NF_ENT.CD_EMP =EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.CD_EMP AND 
	 EST_NF_ENT.CD_FILIAL = EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.CD_FIL_DEST  AND 
	 EST_NF_ENT.CD_NF = EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.CD_NF_ENT AND 
	 0 = EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.TIPO_MANIFESTO
 WHERE
	 EST_NF_ENT.CD_EMP =@CD_EMP
	 AND EST_NF_ENT.DT_ENT BETWEEN @DT_INI AND @DT_FIM
	 AND EST_NF_ENT.CD_FILIAL = @CD_FILIAL
	 AND EST_NF_ENT_TRANSF_CIENCIA_OPERACAO.CD_NF IS NULL
	 AND EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.CD_NF_ENT IS NULL

INSERT INTO EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE
(CD_EMP,
CD_FIL_ORIG,
CD_FIL_DEST,
CD_NF_SAI,
CD_NF_ENT,
TIPO_MANIFESTO,
DT_CAD)
SELECT
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_EMP,
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_FIL_ORIG,
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_FIL_DEST,
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_NF_SAI,
	 EST_NF_ENT_EST_NF_SAI_TRANSF.CD_NF_ENT,
	 1,
	GETDATE()
FROM 
	 EST_NF_ENT INNER JOIN EST_NF_ENT_EST_NF_SAI_TRANSF ON
	 EST_NF_ENT.CD_EMP =EST_NF_ENT_EST_NF_SAI_TRANSF.CD_EMP AND 
	 EST_NF_ENT.CD_FILIAL = EST_NF_ENT_EST_NF_SAI_TRANSF.CD_FIL_DEST  AND 
	 EST_NF_ENT.CD_NF = EST_NF_ENT_EST_NF_SAI_TRANSF.CD_NF_ENT
	 LEFT OUTER  JOIN EST_NF_ENT_TRANSF_CONFIRMACAO_OPERACAO ON
	 EST_NF_ENT.CD_EMP =EST_NF_ENT_TRANSF_CONFIRMACAO_OPERACAO.CD_EMP AND 
	 EST_NF_ENT.CD_FILIAL = EST_NF_ENT_TRANSF_CONFIRMACAO_OPERACAO.CD_FILIAL  AND 
	 EST_NF_ENT.CD_NF = EST_NF_ENT_TRANSF_CONFIRMACAO_OPERACAO.CD_NF
	 LEFT OUTER  JOIN EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE ON
	 EST_NF_ENT.CD_EMP =EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.CD_EMP AND 
	 EST_NF_ENT.CD_FILIAL = EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.CD_FIL_DEST  AND 
	 EST_NF_ENT.CD_NF = EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.CD_NF_ENT AND 
	 1 = EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.TIPO_MANIFESTO
 WHERE
	 EST_NF_ENT.CD_EMP =@CD_EMP
	 AND EST_NF_ENT.DT_ENT BETWEEN @DT_INI AND @DT_FIM
	 AND EST_NF_ENT.CD_FILIAL = @CD_FILIAL
	 AND EST_NF_ENT_TRANSF_CONFIRMACAO_OPERACAO.CD_NF IS NULL
	 AND EST_NF_ENT_EST_NF_SAI_TRANSF_MANIFESTO_PENDENTE.CD_NF_ENT IS NULL
