/****** Object:  Trigger [TRG_EST_PED_CP_INCLUI_PENDENCIA]  ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_EST_PED_CP_INCLUI_PENDENCIA]'))
DROP TRIGGER [dbo].[TRG_EST_PED_CP_INCLUI_PENDENCIA]
GO
GO
--------------------------------------INSERÇÃO EM EST_PED_CP--------------------------------------
CREATE TRIGGER [dbo].[TRG_EST_PED_CP_INCLUI_PENDENCIA] ON [dbo].[EST_PED_CP] FOR INSERT AS
--Pendência de entrada pedido de compras
INSERT INTO EST_PED_CP_PENDENCIA (CD_EMP, CD_FILIAL, CD_PED)
	SELECT CD_EMP, CD_FILIAL, CD_PED
	FROM INSERTED c
	WHERE c.STS_PED NOT IN (2,4) AND c.NAO_CONSTAR_PEND_ENTR=0
		AND NOT EXISTS (SELECT * FROM EST_NF_ENT_PED_CP
						WHERE c.CD_EMP=CD_EMP AND c.CD_FILIAL=CD_FILIAL AND c.CD_PED=CD_PED)

INSERT INTO EST_PED_CP_ORC_FINAN_PROC_PENDENTE (CD_EMP, CD_FILIAL, CD_PED, DT_PROC, DT_CAD)
	SELECT CD_EMP, CD_FILIAL, CD_PED, CONVERT(CHAR(10),GETDATE(),112),GETDATE()
	FROM INSERTED c
	WHERE c.STS_PED NOT IN (2,4) AND c.NAO_CONSTAR_PEND_ENTR=0
		AND NOT EXISTS (SELECT * FROM EST_NF_ENT_PED_CP
						WHERE c.CD_EMP=CD_EMP AND c.CD_FILIAL=CD_FILIAL AND c.CD_PED=CD_PED)
GO
ALTER TABLE [dbo].[EST_PED_CP] ENABLE TRIGGER [TRG_EST_PED_CP_INCLUI_PENDENCIA]
GO


