/****** Object:  Trigger [TRG_BLOQ_EST_NF_SAI_IT_QT_IT_ZERO]    Script Date: 30/01/2015 11:44:19 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_BLOQ_EST_NF_SAI_IT_QT_IT_ZERO]'))
DROP TRIGGER [dbo].[TRG_BLOQ_EST_NF_SAI_IT_QT_IT_ZERO]
GO
GO
GO
GO

CREATE TRIGGER [dbo].[TRG_BLOQ_EST_NF_SAI_IT_QT_IT_ZERO] ON [dbo].[EST_NF_SAI_IT] 
FOR INSERT 
AS
begin
	IF EXISTS 
		(SELECT i.CD_PROD
			FROM         
				INSERTED i INNER JOIN EST_NF_SAI ON
				i.CD_EMP = EST_NF_SAI.CD_EMP AND 
				i.CD_FILIAL = EST_NF_SAI.CD_FILIAL AND
				i.CD_NF = EST_NF_SAI.CD_NF 
				INNER JOIN GLB_MOV_OP ON
				EST_NF_SAI.CD_NT_MOV = GLB_MOV_OP.CD_MOV_OP
			WHERE				    
				i.QT_IT <=0
				AND (GLB_MOV_OP.TP_MOV <> 25 AND GLB_MOV_OP.TP_MOV <> 58 AND GLB_MOV_OP.FLAG_MOV_OP_NFE_CPL <> 1))

		Begin
			RAISERROR ('Produto com quantidade zerada.',16, 1); 
			RETURN;		
		End
End
GO


