IF  EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_PROCESSA_PENDENCIA_IBS_CBS]') AND type IN (N'P', N'PC'))
    DROP PROCEDURE [dbo].[P_PROCESSA_PENDENCIA_IBS_CBS]
GO 

CREATE PROCEDURE P_PROCESSA_PENDENCIA_IBS_CBS
(
    @CD_TRIB_IBS_CBS INT
)
AS
BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;


	IF EXISTS (SELECT 1 
				FROM EST_PROD_CPL_IBS_CBS_PEND
				WHERE CD_TRIB_IBS_CBS = @CD_TRIB_IBS_CBS
				AND STS_GERACAO = 0)
	BEGIN


		UPDATE EST_PROD_CPL_IBS_CBS_PEND
		SET STS_GERACAO = 1
		WHERE CD_TRIB_IBS_CBS = @CD_TRIB_IBS_CBS
		AND STS_GERACAO = 0;

		CREATE TABLE #TEMP (
			CD_EMP INT,
			CD_FILIAL INT,
			CD_TRIB_IBS_CBS INT,
			CD_PROD INT,
			CD_MOV_OP INT,
			ENVIA_PDV INT
		)

		BEGIN TRY
			BEGIN TRAN;

			DELETE FROM EST_PROD_CPL_IBS_CBS
			WHERE CD_TRIB_IBS_CBS = @CD_TRIB_IBS_CBS;

			INSERT INTO #TEMP
			(
				CD_EMP,
				CD_FILIAL,
				CD_TRIB_IBS_CBS,
				CD_PROD,
				CD_MOV_OP,
				ENVIA_PDV
			)
			SELECT DISTINCT
				1 AS CD_EMP,
				PRF.CD_FILIAL,
				E.CD_TRIB_IBS_CBS,
				P.CD_PROD,
				G.CD_MOV_OP,
				ISNULL(CFG.CD_EMP, 0) AS ENVIA_PDV
			FROM EST_TRIB_IBS_CBS E
			INNER JOIN EST_TRIB_IBS_CBS_NCM N
				ON E.CD_TRIB_IBS_CBS = N.CD_TRIB_IBS_CBS
			INNER JOIN EST_PROD P
				ON P.NR_NCM = N.NR_NCM
			INNER JOIN EST_TRIB_IBS_CBS_GLB_MOV_OP G
				ON E.CD_TRIB_IBS_CBS = G.CD_TRIB_IBS_CBS
			INNER JOIN PRC_REGIAO_FISCAL PR
				ON PR.CD_REGIAO_FISCAL = E.CD_REGIAO_FISCAL
			INNER JOIN PRC_REGIAO_FISCAL_PRC_FILIAL PRF
				ON PRF.CD_REGIAO_FISCAL = E.CD_REGIAO_FISCAL
			LEFT JOIN PRC_FILIAL_CONFIG CFG ON CFG.CD_FILIAL = PRF.CD_FILIAL
									AND CFG.CD_CHAVE = 'CD_MOV_OP_VENDA_PDV'
									AND CFG.VALOR = G.CD_MOV_OP
			WHERE E.CD_TRIB_IBS_CBS = @CD_TRIB_IBS_CBS
			AND NOT EXISTS
			(
				SELECT 1
				FROM EST_PROD_CPL_IBS_CBS FINAL WITH (UPDLOCK, HOLDLOCK)
				WHERE FINAL.CD_TRIB_IBS_CBS = E.CD_TRIB_IBS_CBS
				  AND FINAL.CD_FILIAL       = PRF.CD_FILIAL
				  AND FINAL.CD_PROD         = P.CD_PROD
				  AND FINAL.CD_MOV_OP       = G.CD_MOV_OP
			);

			INSERT INTO EST_PROD_CPL_IBS_CBS
			(
				CD_EMP,
				CD_FILIAL,
				CD_TRIB_IBS_CBS,
				CD_PROD,
				CD_MOV_OP
			)
			SELECT CD_EMP,
				CD_FILIAL,
				CD_TRIB_IBS_CBS,
				CD_PROD,
				CD_MOV_OP
			FROM #TEMP

			/*Força o envio pdv - No caso da natureza coincidir com a config da filial CD_MOV_OP_VENDA_PDV*/

			UPDATE CPL
			SET CD_TRIB_IBS_CBS_VD = T.CD_TRIB_IBS_CBS
			FROM #TEMP T
			INNER JOIN EST_PROD_CPL CPL ON CPL.CD_EMP = T.CD_EMP
										AND CPL.CD_FILIAL = T.CD_FILIAL
										AND CPL.CD_PROD = T.CD_PROD
			WHERE T.ENVIA_PDV = 1


			UPDATE EST_PROD_CPL_IBS_CBS_PEND
			SET STS_GERACAO = 2,
				DT_PROCESSAMENTO = GETDATE()
			WHERE CD_TRIB_IBS_CBS = @CD_TRIB_IBS_CBS
			AND STS_GERACAO = 1;

			COMMIT;
		END TRY
		BEGIN CATCH

			IF @@TRANCOUNT > 0

				UPDATE EST_PROD_CPL_IBS_CBS_PEND
				SET STS_GERACAO = 3,
					DT_PROCESSAMENTO = GETDATE()
				WHERE CD_TRIB_IBS_CBS = @CD_TRIB_IBS_CBS
				AND STS_GERACAO = 1;

				ROLLBACK;
			THROW;

		END CATCH
	END
END;