  IF OBJECT_ID('P_EST_NF_ENT_PRE_ENTRADA_CPL_CONF_WMS') IS NOT NULL
    BEGIN
        DROP PROCEDURE P_EST_NF_ENT_PRE_ENTRADA_CPL_CONF_WMS;
END;
GO
--P_EST_NF_ENT_PRE_ENTRADA_CPL_CONF_WMS 381230
CREATE PROCEDURE P_EST_NF_ENT_PRE_ENTRADA_CPL_CONF_WMS (@CD_CTR INT)
WITH ENCRYPTION AS
SET NOCOUNT ON;

CREATE TABLE #RsProdConf (CD_CTR INT,
					  CD_EMP INT,
					  CD_USU INT,
					  DT_CAD DATETIME,
					  CD_PROD INT,
					  QT_IT_CONF MONEY)

INSERT INTO #RsProdConf
(CD_CTR,
CD_EMP,
CD_USU,
DT_CAD,
CD_PROD,
QT_IT_CONF)
SELECT
	 EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_PRE_ENTRADA.CD_CTR,
	 EST_NF_ENT_PRE_ENTRADA_CPL_CONF.CD_EMP,
	 EST_NF_ENT_PRE_ENTRADA_CPL_CONF.CD_USU,
	 EST_NF_ENT_PRE_ENTRADA_CPL_CONF.DT_CAD,
	 EST_NF_ENT_PRE_ENTRADA_CPL_CONF.CD_PROD,
	 EST_NF_ENT_PRE_ENTRADA_CPL_CONF.QT_IT_CONF
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
	AND EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_PRE_ENTRADA.CD_CTR=@CD_CTR

INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA
(CD_CTR,
CD_USU,
DT_CAD,
CHECKIN_DIVERGENCIA,
DT_TRANSACAO)
SELECT
	DISTINCT 
		CD_CTR,
		CD_USU,
		DT_CAD,
		0,
		GETDATE()
FROM
	#RsProdConf;

INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA_CPL
(CD_CTR,
CD_EMP,
CD_PROD,
QT_IT_CHECK)
SELECT
	DISTINCT 
		CD_CTR,
		CD_EMP,
		CD_PROD,
		QT_IT_CONF
FROM
	#RsProdConf;

DROP TABLE #RsProdConf;