CREATE PROCEDURE dbo.CopyArrangementToWeekDays  @ACopyType INT, @AReferenceNumber INT, @AGuestNumber INT, @ASourceDate DATETIME, @AWeekDayString VARCHAR(7), @AArrangementLineNumber INT AS

--ACopyType: 0 = Copy Day, 1 = Copy Line
--Created AV 2008 0612

DECLARE @ArrNumber INT
DECLARE @Time CHAR (5)
DECLARE @DepTime CHAR (5)

DECLARE @Price MONEY
DECLARE @TotalPrice MONEY
DECLARE @Pieces INT
DECLARE @WareText VARCHAR (50)

DECLARE @PK_Department int
DECLARE @FK_Exdepartment int
DECLARE @PlaceText char (20)
DECLARE @WareNumber int
DECLARE @Note varchar (4000)
DECLARE @hallnumber int

DECLARE @IdentityValue int
DECLARE @WeekDay INT


DECLARE @MainResArrival DATETIME
DECLARE @MainResDeparture DATETIME
DECLARE @CurrentDate DATETIME
SELECT @MainResArrival = CONVERT(DATETIME, ArrivalDate, 112), @MainResDeparture = CONVERT(DATETIME, DepartureDate, 112)  FROM Reservations WITH (NOLOCK) WHERE ReferenceNumber = @AReferenceNumber AND GuestNumber = 0



  DECLARE CopyDay Cursor For
  SELECT ArrangementLineNumber,Time, DepartureTime, Price, TotalPrice, Pieces, WareText, FK_ExDepartment,PlaceText,WareNumber, note, Hallnumber
  FROM TArrangementLinesView WITH (NOLOCK) 
  WHERE  ReferenceNumber = @AReferenceNumber 
  AND TArrangementLinesView.date = CONVERT(VARCHAR(8),@ASourceDate, 112)
  AND ((@ACopyType = 1 AND TArrangementLinesView.ArrangementLineNumber = @AArrangementLineNumber ) OR @ACopyType = 0 )

  OPEN Copyday
  FETCH NEXT FROM Copyday
  INTO @ArrNumber,@Time, @DepTime, @Price, @TotalPrice, @Pieces, @WareText, @FK_ExDepartment,@PlaceText,@WareNumber, @note, @hallnumber 

   -- Loop through records of sourcedate
  WHILE  (@@FETCH_STATUS = 0)
  BEGIN
    SET @CurrentDate = @ASourceDate + 1
    WHILE @CurrentDate <=@MainResDeparture
    BEGIN
       SELECT  @WeekDay = DATEPART(WEEKDAY, @CurrentDate)
       IF @Weekday = 1
         SET @WeekDay = 6
       ELSE IF @Weekday = 2
         SET @WeekDay = 0
       ELSE IF @Weekday = 3
         SET @WeekDay = 1
       ELSE IF @Weekday = 4
         SET @WeekDay = 2
       ELSE IF @Weekday = 5
         SET @WeekDay = 3
       ELSE IF @Weekday = 6
         SET @WeekDay = 4
       ELSE IF @Weekday = 7
         SET @WeekDay = 5

       IF CHARINDEX(CONVERT(CHAR(1), @WeekDay), @AWeekDayString) > 0
       BEGIN
         INSERT INTO ArrangementLines (Date,Time, Departuretime, Price, /*TotalPrice,*/ Pieces,WareText, FK_Exdepartment,PlaceText, WareNumber, note, hallnumber) VALUES (CONVERT(CHAR(8),@CurrentDate, 112 ), @Time, @DepTime, @Price, /*@TotalPrice,*/@Pieces,@waretext, @FK_exdepartment,@PlaceText,@WareNumber,@note,@hallnumber)
         SELECT @IdentityValue=@@identity from ArrangementLines WITH (NOLOCK)        
         INSERT INTO arrangementReservationsXtab (ArrangementLineNumber, ReferenceNumber, GuestNumber, PlusProductLineNumber) VALUES (@IdentityValue,@AReferencenumber,@AGuestNumber, 0)

        -- ArrangmentLineDepartmentXTab
         DECLARE DepCur Cursor Forward_Only For
         SELECT DISTINCT FK_DepartmentNumber FROM ArrangementLineDepartmentXTab  WITH (NOLOCK)
          WHERE FK_ArrangementLineNumber=@ArrNumber 
          OPEN DepCur
          FETCH NEXT FROM DepCur INTO @PK_Department
          WHILE  (@@FETCH_STATUS <> -1)
          BEGIN
             IF NOT EXISTS (SELECT FK_ArrangementLineNumber FROM ArrangementLineDepartmentXTab WITH (NOLOCK) WHERE FK_ArrangementLineNumber = @IdentityValue AND FK_DepartmentNumber = @PK_Department)
               INSERT INTO ArrangementLineDepartmentXTab (FK_ArrangementLineNumber, FK_DepartmentNumber) VALUES (@IdentityValue, @PK_Department)
           FETCH NEXT FROM DepCur INTO @PK_Department
           END
           CLOSE DepCur
           DEALLOCATE DepCur
         --If the lines we are about to copy is linked to a linenumber and a PacketItemNumber then we can not lose this link we have to transfer the links to the new day

          -- EXEC CheckArrangementTolerance @IdentityValue, @AReferenceNumber, @ArrLineDeleted  
         END
      SET @CurrentDate = @CurrentDate + 1 
    END   
    FETCH NEXT FROM Copyday INTO @ArrNumber,@Time, @DepTime,  @Price, @TotalPrice, @Pieces, @WareText ,@FK_ExDepartment,@PlaceText,@WareNumber, @Note, @hallnumber
  END


  CLOSE Copyday
  DEALLOCATE CopyDay
