CREATE PROCEDURE dbo.CopyArrangementToMonths  @ACopyType INT, @AReferenceNumber INT, @AGuestNumber INT, @ASourceDate DATETIME,  @AArrangementLineNumber INT AS

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

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 =  DATEADD(MONTH, 1,  @ASourceDate)
    WHILE @CurrentDate <=@MainResDeparture
    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

      SET @CurrentDate =  DATEADD(MONTH, 1,  @CurrentDate)
    END   
    FETCH NEXT FROM Copyday INTO @ArrNumber,@Time, @DepTime,  @Price, @TotalPrice, @Pieces, @WareText ,@FK_ExDepartment,@PlaceText,@WareNumber, @Note, @hallnumber
  END


  CLOSE Copyday
  DEALLOCATE CopyDay


