/****** Object:  Trigger [TRG_EST_NF_SAI_PED_VD_INCLUI_PENDENCIA]    Script Date: 02/11/2014 19:04:09 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_EST_NF_SAI_PED_VD_INCLUI_PENDENCIA]'))
DROP TRIGGER [dbo].[TRG_EST_NF_SAI_PED_VD_INCLUI_PENDENCIA]
GO
GO
GO
GO

-----------------------------------DELEÇÃO EM EST_NF_SAI_PED_VD-----------------------------------
CREATE TRIGGER [dbo].[TRG_EST_NF_SAI_PED_VD_INCLUI_PENDENCIA] ON [dbo].[EST_NF_SAI_PED_VD] FOR DELETE AS
--Pendência de entrada pré-faturas transferências (SEPARADO NO ESTOQUE)
INSERT INTO EST_PED_VD_PENDENCIA (CD_EMP, CD_FILIAL, CD_PED, CD_FILIAL_DEST,FLAG_REALIZAR_FAT)
	SELECT p.CD_EMP, p.CD_FILIAL, p.CD_PED, p.CD_CLI,ISNULL(p.FLAG_REALIZAR_FAT,0)
    FROM  DELETED d INNER JOIN EST_PED_VD p ON p.CD_EMP=d.CD_EMP AND p.CD_FILIAL=d.CD_FILIAL AND p.CD_PED=d.CD_PED
	WHERE p.STS_PED <> 2 AND CD_NT_MOV IN (SELECT CD_MOV_OP FROM GLB_MOV_OP WHERE DEST_ORIG=2);
--Pendência de entrada pré-faturas vendas grupo econômico
INSERT INTO EST_PED_VD_PENDENCIA (CD_EMP, CD_FILIAL, CD_PED, CD_FILIAL_DEST,FLAG_REALIZAR_FAT)
	SELECT p.CD_EMP, p.CD_FILIAL, p.CD_PED, r.CD_FILIAL,ISNULL(p.FLAG_REALIZAR_FAT,0)
    FROM  DELETED d INNER JOIN EST_PED_VD p ON p.CD_EMP=d.CD_EMP AND p.CD_FILIAL=d.CD_FILIAL AND p.CD_PED=d.CD_PED
		INNER JOIN PRC_FILIAL_RC_CLI r ON p.CD_EMP=r.CD_EMP AND p.CD_CLI=r.CD_CLI
	WHERE p.STS_PED <> 2 AND CD_NT_MOV IN (SELECT CD_MOV_OP FROM GLB_MOV_OP WHERE DEST_ORIG=0);

