  
/****** Object:  Trigger [TRG_EST_PROD_TRANSF_INCLUI_PENDENCIA]    Script Date: 01/23/2014 17:09:58 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_EST_PROD_TRANSF_INCLUI_PENDENCIA]'))
DROP TRIGGER [dbo].[TRG_EST_PROD_TRANSF_INCLUI_PENDENCIA]
GO
 
/****** Object:  Trigger [dbo].[TRG_EST_PROD_TRANSF_INCLUI_PENDENCIA]    Script Date: 01/23/2014 17:10:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
 
------------------------------------INSERÇÃO EM EST_PROD_TRANSF------------------------------------
CREATE TRIGGER [dbo].[TRG_EST_PROD_TRANSF_INCLUI_PENDENCIA] ON [dbo].[EST_PROD_TRANSF] FOR INSERT,UPDATE AS

--Deletando registro se houver
	DELETE FROM 
		p
	FROM
		EST_PROD_TRANSF_PENDENCIA p 
		INNER JOIN INSERTED i ON
		p.CD_EMP = i.CD_EMP AND
		p.CD_FILIAL = i.CD_FILIAL AND
		p.CD_CTR = i.CD_CTR

--Pendência de entrada de ressuprimento (RESERVADO NO ESTOQUE)
	INSERT INTO EST_PROD_TRANSF_PENDENCIA (CD_EMP,CD_FILIAL,CD_FILIAL_ORIG, CD_CTR, CD_FILIAL_DEST,DT_REGISTRO)
	SELECT 
		t.CD_EMP, 
		t.CD_FILIAL, 
		t.CD_FIL_ORIG,
		t.CD_CTR, 
		t.CD_FIL_DEST,
		GETDATE()
    FROM 
		INSERTED t
		LEFT JOIN EST_PROD_TRANSF_PED_VD P ON 
		T.CD_EMP	= P.CD_EMP AND 
		T.CD_FILIAL = P.CD_FILIAL AND 
		T.CD_CTR	= P.CD_CTR
	WHERE 
		t.STS_TRANSF in (0,3,5,6)
		AND P.CD_EMP IS NULL



GO


