IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.ArrangementLineIllegalWare') AND OBJECTPROPERTY(id, N'IsTrigger') = 1) DROP TRIGGER dbo.ArrangementLineIllegalWare 
GO
CREATE TRIGGER dbo.ArrangementLineIllegalWare ON ArrangementLines AFTER INSERT, UPDATE AS
BEGIN
  SET NOCOUNT ON
  DECLARE @cnt INT
  DECLARE @Systemdate CHAR(8)

  SELECT TOP 1 @Systemdate = SYSTEMDATE FROM System WITH (NOLOCK)

  SELECT @cnt = COUNT(*)
  FROM WareGroups WITH (NOLOCK)
  INNER JOIN Ware WITH (NOLOCK) ON Ware.WareGroupNumber = WareGroups.WareGroupNumber
    AND Ware.WareNumber = (SELECT TOP 1 WareNumber FROM INSERTED)
  WHERE WareGroups.Payment = 1
    AND (SELECT TOP 1 Date FROM INSERTED) >= @Systemdate

  IF ISNULL(@cnt, 0) > 0
  BEGIN
    ROLLBACK TRAN
    RAISERROR ( 'Trigger.ArrangementLineIllegalWare: Illegal ware on arrangement line', 16, 1)
  END
END
