IF OBJECT_ID('P_DIVERSOS_EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA') IS NOT NULL
    BEGIN 
	   DROP PROCEDURE P_DIVERSOS_EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA
    END 
GO
CREATE PROCEDURE P_DIVERSOS_EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA (@CD_CTR_CAB INT,@CD_USU INT ) AS 
/*
TRUNCATE TABLE #DANFE_PROVISORIO  
TRUNCATE TABLE #DAR_ENTRADA 
*/
CREATE TABLE #DANFE_PROVISORIO
( 
    CD_CTR    INT,
    CD_PROD   INT,
    QTDE_PROD MONEY,
    QTDE_CHECKIN MONEY,
    QTDE_CHECKIN_NF MONEY,
    QTDE_SALDO   MONEY
);
CREATE TABLE #DAR_ENTRADA ( 
    CD_CTR    INT,
    CD_PROD   INT,
    QTDE_PROD MONEY,
    QTDE_CHECKIN MONEY,
    QTDE_CHECKIN_NF MONEY,
    QTDE_SALDO   MONEY
); 


CREATE TABLE #TMP_PROD ( 
    CD_CTR    INT,
    CD_PROD   INT,
    QTDE_PROD MONEY)


--SELECT * FROM EST_NF_ENT_CHECKIN_ENTRADA_TEMP_CAB
SET NOCOUNT ON;
 
------------------------------------------------------------------------------------------------------------------------------------------------------
--DECLARE @CD_CTR_CAB INT = 19
--TRUNCATE TABLE #DANFE_PROVISORIO
--TRUNCATE TABLE #DAR_ENTRADA
 /*
 PROD -> 57363 SOBRANDO 1
 PROD -> 62671 FALTANDO 1
 */
--SELECT * FROM EST_NF_ENT_CHECKIN_ENTRADA_TEMP WHERE CD_CTR_CAB = 16
INSERT INTO #DANFE_PROVISORIO 
    SELECT 
	   DANFE.CD_CTR,
	   DANFE.CD_PROD,
	   DANFE.QTDE_PROD,
	   0,
	   QT_PROD /
	   ISNULL (CASE FLAG_USAR_EMB_DANFE
                   WHEN 1
                   THEN DANFE.APRES_QT_CP
                   ELSE EST_PROD_FORN.APRES_QT_CP
               END,EST_PROD.APRES_QT_CP) QTDE_CHECKIN_NF,
			QT_PROD /
				   ISNULL (CASE FLAG_USAR_EMB_DANFE
                   WHEN 1
                   THEN DANFE.APRES_QT_CP
                   ELSE EST_PROD_FORN.APRES_QT_CP
               END,EST_PROD.APRES_QT_CP) QTDE_SALDO 
	FROM 
	   EST_NF_ENT_CHECKIN_ENTRADA_TEMP_CAB CAB INNER JOIN EST_NF_ENT_CHECKIN_ENTRADA_TEMP_CAB_DANFE_PROV REF ON CAB.CD_CTR =REF.CD_CTR_CAB 

									   INNER JOIN EST_NF_ENT_CHECKIN_ENTRADA_TEMP CHECKIN ON 
																	   CAB.CD_EMP = CHECKIN.CD_EMP
																    AND CAB.CD_FILIAL = CHECKIN.CD_FILIAL
																    AND CAB.CD_CTR = CHECKIN.CD_CTR_CAB 
									   INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_ITENS DANFE ON   DANFE.CD_EMP = CAB.CD_EMP
																				    AND DANFE.CD_CTR =  REF.CD_CTR_DANFE_PROV
																				    AND DANFE.CD_PROD = CHECKIN.CD_PROD
									   INNER JOIN EST_NF_ENT_DANFE_PROVISORIO CBC ON CBC.CD_EMP = DANFE.CD_EMP
																		    AND CBC.CD_CTR = DANFE.CD_CTR
									   LEFT JOIN EST_PROD_FORN  ON EST_PROD_FORN.CD_EMP = DANFE.CD_EMP 
															  AND EST_PROD_FORN.CD_PROD = DANFE.CD_PROD 
															  AND EST_PROD_FORN.CD_FORN = CBC.CD_FORN
									       INNER JOIN EST_PROD ON 
													   DANFE.CD_PROD = EST_PROD .CD_PROD
    WHERE DANFE.CD_EMP = 1 
		AND CAB.CD_CTR = @CD_CTR_CAB
------------------------------------------------------------------------------------------------------------------------------------------------------
 

WHILE EXISTS ( SELECT * 
			 FROM #DANFE_PROVISORIO 
			 WHERE 
				QTDE_SALDO>0 AND QTDE_CHECKIN = 0 )
 BEGIN 
 ----------------------------------------------------------------------------------------------------------------------------------------------------------
 --PRODUTOS ONDE BIPOU MENOS QUE A QUANTIDADE QUE DEVERIA DAR ENTRADA
 ----------------------------------------------------------------------------------------------------------------------------------------------------------
  ;TRUNCATE TABLE #TMP_PROD;
    INSERT INTO #TMP_PROD
    SELECT TOP 1
	  CD_CTR,
	  CD_PROD,
	  QTDE_PROD
    FROM 
	  #DANFE_PROVISORIO A
    WHERE
	  QTDE_PROD > QTDE_CHECKIN 
	  AND QTDE_SALDO >0
    ORDER BY	 
	  QTDE_PROD;
					   
----------------------------------------------------------------------------------------------------------------------------------------------------------

    UPDATE D
    SET 
	   QTDE_CHECKIN = ( CASE WHEN D.QTDE_SALDO > D.QTDE_PROD THEN
							 D.QTDE_PROD ELSE D.QTDE_SALDO END)
    FROM 	    
	   #DANFE_PROVISORIO D     
	   JOIN #TMP_PROD A ON 
	     A.CD_PROD = D.CD_PROD 
	   AND A.CD_CTR  = D.CD_CTR 
    WHERE 
	   D.QTDE_SALDO> 0 

----------------------------------------------------------------------------------------------------------------------------------------------------------	   
	       
    UPDATE A
    SET 
	   QTDE_SALDO = A.QTDE_CHECKIN_NF - D.QTDE_CHECKIN
    FROM 
	   #DANFE_PROVISORIO  A
	   JOIN (SELECT   
				D.CD_PROD , 
				SUM(D.QTDE_CHECKIN) QTDE_CHECKIN 
			 FROM #DANFE_PROVISORIO D 
				JOIN #TMP_PROD X ON 
				    D.CD_PROD = X.CD_PROD
				GROUP BY   D.CD_PROD ) D ON 
    A.CD_PROD = D.CD_PROD
END 
--TIRA POE RAPA DEIXA
WHILE EXISTS(SELECT * 
			 FROM #DANFE_PROVISORIO 
			 WHERE  QTDE_SALDO > 0)
BEGIN
    
    TRUNCATE TABLE #TMP_PROD;
    INSERT INTO #TMP_PROD
    SELECT TOP 1
	  CD_CTR,
	  CD_PROD,
	  QTDE_SALDO
    FROM 
	  #DANFE_PROVISORIO A
    WHERE
	   QTDE_SALDO >0
    ORDER BY	 
	  QTDE_PROD;
			   
----------------------------------------------------------------------------------------------------------------------------------------------------------
    UPDATE D
    SET 
	   QTDE_CHECKIN = QTDE_CHECKIN + 1
    FROM 	    
	   #DANFE_PROVISORIO D     
	   JOIN #TMP_PROD A ON 
	     A.CD_PROD = D.CD_PROD 
	   AND A.CD_CTR  = D.CD_CTR 
    WHERE 
	   D.QTDE_SALDO> 0 

----------------------------------------------------------------------------------------------------------------------------------------------------------	   
	       
    UPDATE A
    SET 
	   QTDE_SALDO = A.QTDE_CHECKIN_NF - D.QTDE_CHECKIN
    FROM 
	   #DANFE_PROVISORIO  A
	   JOIN (SELECT   
				D.CD_PROD , 
				SUM(D.QTDE_CHECKIN) QTDE_CHECKIN 
			 FROM #DANFE_PROVISORIO D 
				JOIN #TMP_PROD X ON 
				    D.CD_PROD = X.CD_PROD
				GROUP BY   D.CD_PROD ) D ON 
    A.CD_PROD = D.CD_PROD

END
   
 ---------------------------------------------------------------------------------------------------------------
 -- CHECKIN_DIVERGENCIA = 0 -> SEM DIVERGENCIA  CHECKIN_DIVERGENCIA = 1 -> COM DIVERGENCIA 
 ---------------------------------------------------------------------------------------------------------------
 
 -- GRAVANDO APENAS CHECKIN SEM DIVERGENCIA

		  INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA
		  SELECT DISTINCT
				CD_CTR,
				@CD_USU,
				GETDATE(),
				0,
				CONVERT (DATE,GETDATE()) AS DT_TRANSACAO,
				NULL
		 FROM #DANFE_PROVISORIO 
		 GROUP BY CD_CTR
		 HAVING SUM(QTDE_PROD)=SUM(QTDE_CHECKIN)		 
---------------------------------------------------------------------------------------------------------------
	   
	   ;WITH CHECKIN
		  AS (
			 SELECT SUM(QTDE_PROD)AS QTDE_PROD,
				    SUM(QTDE_CHECKIN) AS QTDE_CHECKIN,
				    CD_CTR
			 FROM #DANFE_PROVISORIO 
			 GROUP BY CD_CTR
			 HAVING SUM(QTDE_PROD)=SUM(QTDE_CHECKIN)
			 
			 )

		 INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA_CPL
		  SELECT	
				CHECKIN.CD_CTR,
				1 AS CD_EMP,
				#DANFE_PROVISORIO.CD_PROD,
				#DANFE_PROVISORIO.QTDE_CHECKIN,
				NULL
		  FROM #DANFE_PROVISORIO INNER JOIN CHECKIN  ON #DANFE_PROVISORIO.CD_CTR = CHECKIN.CD_CTR
		  -- SELECT * FROM #DANFE_PROVISORIO
-- GRAVANDO APENAS CHECKIN COM DIVERGENCIA

		  INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA
		  SELECT DISTINCT
				CD_CTR,
				@CD_USU,
				GETDATE(),
				1,
				CONVERT (DATE,GETDATE()) AS DT_TRANSACAO,
				NULL
		 FROM #DANFE_PROVISORIO 
		 GROUP BY CD_CTR
		 HAVING SUM(QTDE_PROD)<>SUM(QTDE_CHECKIN)

	   ;WITH CHECKIN
		  AS (
			 SELECT SUM(QTDE_PROD)AS QTDE_PROD,
				    SUM(QTDE_CHECKIN) AS QTDE_CHECKIN,
				    CD_CTR
			 FROM #DANFE_PROVISORIO 
			 GROUP BY CD_CTR
			 HAVING SUM(QTDE_PROD)<>SUM(QTDE_CHECKIN)
			 
			 )

 		 INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA_CPL
		  SELECT	
				CHECKIN.CD_CTR,
				1 AS CD_EMP,
				#DANFE_PROVISORIO.CD_PROD,
				#DANFE_PROVISORIO.QTDE_CHECKIN,
				NULL
		  FROM #DANFE_PROVISORIO INNER JOIN CHECKIN ON #DANFE_PROVISORIO.CD_CTR = CHECKIN.CD_CTR   

 
 DELETE FROM EST_NF_ENT_CHECKIN_ENTRADA_TEMP_CAB_DANFE_PROV WHERE CD_CTR_CAB = @CD_CTR_CAB
 DELETE FROM EST_NF_ENT_CHECKIN_ENTRADA_TEMP WHERE CD_CTR_CAB = @CD_CTR_CAB
 
 /*
SELECT *
FROM EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA
     INNER JOIN #DANFE_PROVISORIO ON EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA.CD_CTR = #DANFE_PROVISORIO.CD_CTR;
SELECT *
FROM EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA_CPL
     INNER JOIN #DANFE_PROVISORIO ON EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA_CPL.CD_CTR = #DANFE_PROVISORIO.CD_CTR;
*/ 