CREATE PROCEDURE DBO.LogUserDeletedArrangementLine @AArrangementLineNumber INT AS
BEGIN
SET NOCOUNT ON;

DECLARE @ReferenceNumber INT
DECLARE @GuestNumber INT
DECLARE @LineNumber INT
DECLARE @Count INT

DECLARE @PacketItemNumber INT
DECLARE @PacketItemWareNumber INT
DECLARE @PacketItemWareText VARCHAR(80)
DECLARE @PacketItemPrice money
DECLARE @PacketItemTime VARCHAR(5)
DECLARE @ArrDate VARCHAR(8)

--SELECT @ReferenceNumber = ReferenceNumber, @GuestNumber = GuestNumber 
--FROM ArrangementReservationsXtab WITH (NOLOCK) 
--WHERE ArrangementLineNumber = @AArrangementLineNumber

DECLARE TmpCursor CURSOR FOR
  SELECT RESERVATIONROOMLINEXTAB.ReferenceNumber, RESERVATIONROOMLINEXTAB.GuestNumber, ArrangementRoomXtab.Linenumber
  FROM ArrangementRoomXtab WITH (NOLOCK)
  INNER JOIN RESERVATIONROOMLINEXTAB WITH (NOLOCK) ON RESERVATIONROOMLINEXTAB.LINENUMBER = ArrangementRoomXtab.Linenumber
  WHERE ArrangementRoomXtab.ArrangementLineNumber = @AArrangementLineNumber
  UNION
  SELECT RESERVATIONCONFERENCELINE.ReferenceNumber, RESERVATIONCONFERENCELINE.GuestNo, ArrangementRoomXtab.Linenumber
  FROM ArrangementRoomXtab WITH (NOLOCK)
  INNER JOIN RESERVATIONCONFERENCELINE WITH (NOLOCK) ON RESERVATIONCONFERENCELINE.ReservationConfLineNumber = ArrangementRoomXtab.Linenumber
  WHERE ArrangementRoomXtab.ArrangementLineNumber = @AArrangementLineNumber
OPEN TmpCursor
FETCH NEXT FROM TmpCursor INTO @ReferenceNumber, @GuestNumber, @LineNumber
WHILE (@@FETCH_STATUS <> -1)
BEGIN
  SELECT 
    @ArrDate = Date,
    @PacketItemNumber = PacketitemNumber,
    @PacketItemWareNumber = PacketItemWareNumber,
	@PacketItemWareText = WareText,
	@PacketItemPrice = PacketItemPrice,
	@PacketItemTime = PacketItemTime
  FROM ArrangementLines WITH (NOLOCK)
  WHERE ArrangementLineNumber = @AArrangementLineNumber
    AND ISNULL(PacketItemNumber, 0) > 0

  SELECT @Count = COUNT(*)
  FROM ArrangementLinesDeleted WITH (NOLOCK)
  WHERE ReferenceNumber = @ReferenceNumber
    AND GuestNumber = @GuestNumber
	AND LineNumber = @LineNumber
	AND Date = @ArrDate
	AND PacketItemNumber = @PacketItemNumber
	AND PacketItemWarenumber = @PacketItemWareNumber
	AND PacketItemWaretext = @PacketItemWareText  
	AND PacketItemTime = @PacketItemTime

  IF @Count > 1
  BEGIN
    -- We need to cleanup the number of matching arrangementlines
    DELETE
    FROM ArrangementLinesDeleted
    WHERE ReferenceNumber = @ReferenceNumber
      AND GuestNumber = @GuestNumber
  	  AND LineNumber = @LineNumber
	  AND Date = @ArrDate
	  AND PacketItemNumber = @PacketItemNumber
	  AND PacketItemWarenumber = @PacketItemWareNumber
	  AND PacketItemWaretext = @PacketItemWareText  
	  AND PacketItemTime = @PacketItemTime
	SET @Count = 0
  END

  IF @Count = 0
  BEGIN
    INSERT INTO ArrangementLinesDeleted
      SELECT @ReferenceNumber , @GuestNumber, @LineNumber,
        Date, PacketItemNumber, Time, Price, Pieces,
  	    PlaceText, WareNumber, HallNumber, Note, DepartureTime,
  	    Priority, Correction, FK_ExDepartment, FK_PacketChargeDepartment,
        FK_HallNumber, WareText, FK_HallLineNumber, ReadOnly,
	    AttachedRoomLine, FK_CurrencyCountryNumber, CurrencyRate,
	    LastUpdDateStamp, IsPiecesUserModified, PiecesModifiedDiff, PacketItemTime,
	    PacketItemWareNumber, PacketItemWareText, PacketItemPrice
      FROM ArrangementLines WITH (NOLOCK)
      WHERE ArrangementLines.ArrangementLineNumber = @AArrangementLineNumber
        AND ISNULL(PacketItemNumber, 0) > 0
  END
  FETCH NEXT FROM TmpCursor INTO @ReferenceNumber, @GuestNumber, @LineNumber
END
CLOSE TmpCursor
DEALLOCATE TmpCursor

END