
DECLARE @ArrangementLineNumber INT
DECLARE @Time CHAR(5)
DECLARE @WareText VARCHAR(255)
DECLARE @WareNumber INT
DECLARE @Price money
DECLARE @PacketItemNumber INT
DECLARE @PNTime CHAR(5)
DECLARE @PNWareText VARCHAR(255)
DECLARE @PNWareNumber INT
DECLARE @PNPrice money
DECLARE @Count INT

SELECT @Count = COUNT(*) FROM ArrangementLines WITH (NOLOCK) WHERE IsUserModified = 1

IF @Count = 0
BEGIN

  IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.ArrangementLineIllegalWare') AND OBJECTPROPERTY(id, N'IsTrigger') = 1)
    DISABLE TRIGGER ArrangementLineIllegalWare ON dbo.ArrangementLines

  DECLARE TmpCursor CURSOR FOR
    SELECT AL.ArrangementLineNumber, AL.Time, AL.WareText, AL.WareNumber, AL.Price, AL.PacketItemNumber,
      PN.Time, PN.TextOverwrite, PN.WareNumber, PN.PriceOverwrite
    FROM ArrangementLines AL WITH (NOLOCK)
    INNER JOIN PacketItem_NEW PN WITH (NOLOCK) ON PN.PacketItemNumber = AL.PacketItemNumber
  OPEN TmpCursor
  FETCH NEXT FROM TmpCursor INTO @ArrangementLineNumber, @Time, @WareText, @WareNumber, @Price, @PacketItemNumber, @PNTime, @PNWareText, @PNWareNumber, @PNPrice
  WHILE (@@FETCH_STATUS <> -1)
  BEGIN
    IF (@Time <> @PNTime) OR
       (@WareText <> @PNWareText) OR
	   (@WareNumber <> @WareNumber) OR
	   (@Price <> @PNPrice)
    BEGIN
      UPDATE ArrangementLines
      SET IsUserModified = 1
      WHERE ArrangementLineNumber = @ArrangementLineNumber

	  IF (@Time <> @PNTime)
	  BEGIN
        UPDATE ArrangementLines
        SET PacketItemTime = @PNTime
        WHERE ArrangementLineNumber = @ArrangementLineNumber
	  END
    END
    ELSE
    BEGIN
      UPDATE ArrangementLines
      SET IsUserModified = 0
      WHERE ArrangementLineNumber = @ArrangementLineNumber
    END
    FETCH NEXT FROM TmpCursor INTO @ArrangementLineNumber, @Time, @WareText, @WareNumber, @Price, @PacketItemNumber, @PNTime, @PNWareText, @PNWareNumber, @PNPrice
  END

  IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.ArrangementLineIllegalWare') AND OBJECTPROPERTY(id, N'IsTrigger') = 1)
    ENABLE TRIGGER ArrangementLineIllegalWare ON dbo.ArrangementLines

  CLOSE TmpCursor
  DEALLOCATE TmpCursor
END
