/****** Object:  Trigger [TRG_EST_NF_ENT_NFE_FORN_VALID_EST_NF_ENT]    Script Date: 03/08/2014 12:57:41 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_EST_NF_ENT_NFE_FORN_VALID_EST_NF_ENT]'))
DROP TRIGGER [dbo].[TRG_EST_NF_ENT_NFE_FORN_VALID_EST_NF_ENT]
GO
GO
GO
GO

CREATE TRIGGER [dbo].[TRG_EST_NF_ENT_NFE_FORN_VALID_EST_NF_ENT] ON [dbo].[EST_NF_ENT_NFE_FORN_VALID] FOR  INSERT AS
BEGIN
		
	---------------------------------------------------------------------------
    --Criando relacionamento da validação da NFE com a entrada da nota fiscal
	---------------------------------------------------------------------------
	INSERT INTO EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID
		(CD_EMP, 
		 CD_FILIAL, 
		 CD_NF, 
		 CD_CTR)
	SELECT
		ent.CD_EMP,
		ent.CD_FILIAL,
		ent.CD_NF,
		i.CD_CTR
	FROM INSERTED i INNER JOIN EST_NF_ENT ent ON
			i.CD_EMP			= ent.CD_EMP
			AND i.CD_FILIAL		= ent.CD_FILIAL
			AND i.CD_FORN		= ent.CD_FORN
			AND i.DT_EMI_NF		= ent.DT_EMI_NF
			AND i.NR_NF			= ent.NR_NF
		LEFT OUTER JOIN EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID ON
		ent.CD_EMP = EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID.CD_EMP AND
		ent.CD_FILIAL = EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID.CD_FILIAL AND
		ent.CD_NF = EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID.CD_NF 
	WHERE
		EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID.CD_NF  IS NULL
END
GO


