



CREATE PROCEDURE DBO.UpdateFolioStatus AS
SET NOCOUNT ON
--Senest rettet d.28/6-2001 af Klaus Stenå Nielsen
Declare @FolioStatus bit
Declare @LINENUMBER Int
Declare @PrevLineNumber int

Declare @ReferenceNumber Int
Declare @GuestNumber Int

Declare @PrevReferenceNumberHalls Int
Declare @PrevGuestNumberHalls Int

Declare @PrevReferenceNumberRooms Int
Declare @PrevGuestNumberRooms Int

Declare @DepartureDate Char(8)
Declare @ArrivalDate Char(8)

Declare @ToDayDate Char(8)

Declare @ToDay Datetime
Declare @DepartureDay Datetime
Declare @ArrivalDay Datetime

Declare @ServerDateTime DateTime
Declare @TransactionDatetime DateTime

Declare @UserIDNumber Integer

Declare @PostDayproducts Bit 

Declare @PostStay Bit

Select @UserIDNumber = UserIDNumber from Personel WITH (NOLOCK) WHERE UserTypeNumber = 4

SELECT  @ToDayDate =  SYSTEMDATE FROM System WITH (NOLOCK)  WHERE NUMBER = 1  -- Only one record

--SET @ToDayDate = '19980221'

SET @ToDay =  Convert(DATETIME, @ToDayDate)

SET @PostDayproducts = 1

SET @PostStay = 1

SET @PrevLineNumber = -1 -- så @PrevLineNumber<>@LINENUMBER altid vil være sandt første gang

SET @FolioStatus = 1

While (@PostStay=1)
begin
  if (@PostDayproducts=1)
  begin
    -- HER ER DET DAYPRODUCTS DER BLIVER UDKONTERET!!!!!!!!
    Declare ReservationCur Cursor Forward_Only For
    SELECT
       RESERVATIONConferenceLINE.ArrivalDate,
       RESERVATIONConferenceLINE.DepartureDate,
       RESERVATIONConferenceLINE.ReservationConfLineNumber,
       RESERVATIONConferenceLINE.ReferenceNumber,
       RESERVATIONS.GuestNumber
    from RESERVATIONConferenceLINE WITH (NOLOCK) INNER JOIN
       Reservations WITH (NOLOCK) ON
         Reservations.ReferenceNumber=RESERVATIONConferenceLINE.ReferenceNumber
        INNER JOIN RESERVATIONMAIN WITH (NOLOCK) ON
             RESERVATIONMAIN.Referencenumber =  RESERVATIONS.ReferenceNumber 
    where (RESERVATIONS.Status=5) AND  ((RESERVATIONMAIN.HotelNumber=0) or (RESERVATIONMAIN.HotelNumber is null))  --5 = rsInhouse

    Open ReservationCur
    Fetch Next From ReservationCur
      Into @ArrivalDate, @DepartureDate, @LINENUMBER, @ReferenceNumber, @GuestNumber
  end
  else
  begin
    Declare ReservationCur Cursor Forward_Only For
    SELECT 
       RESERVATIONS.ArrivalDate,
       RESERVATIONS.DepartureDate,
       RESERVATIONROOMLINE.LINENUMBER,
       RESERVATIONROOMLINEXTAB.ReferenceNumber,
       RESERVATIONROOMLINEXTAB.GuestNumber
    FROM RESERVATIONROOMLINE WITH (NOLOCK) INNER JOIN
          RESERVATIONROOMLINEXTAB WITH (NOLOCK) ON
            RESERVATIONROOMLINE.LINENUMBER = RESERVATIONROOMLINEXTAB.LINENUMBER
          INNER JOIN
            RESERVATIONS WITH (NOLOCK) ON
             RESERVATIONROOMLINEXTAB.ReferenceNumber = RESERVATIONS.ReferenceNumber
           AND
             RESERVATIONROOMLINEXTAB.GuestNumber = RESERVATIONS.GuestNumber

          INNER JOIN RESERVATIONMAIN WITH (NOLOCK) ON
             RESERVATIONMAIN.Referencenumber =  RESERVATIONS.ReferenceNumber 

    WHERE ((RESERVATIONS.Status=5) AND ((RESERVATIONMAIN.HotelNumber=0) or (RESERVATIONMAIN.HotelNumber is null))) --5 = rsInhouse
    ORDER BY RESERVATIONROOMLINE.linenumber -- VIGTIG (PREVLineNumber<>LineNumber)

    Open ReservationCur
    Fetch Next From ReservationCur
      Into @ArrivalDate, @DepartureDate, @LINENUMBER, @ReferenceNumber, @GuestNumber
  end

  While (@@FETCH_STATUS = 0) -- kan gøres mere smart
  Begin
    Update Folios SET
      OpenFolio = @FolioStatus
    where referencenumber=@ReferenceNumber AND GuestNumber = @GuestNumber

    Fetch Next From ReservationCur
      Into @ArrivalDate, @DepartureDate, @LINENUMBER, @ReferenceNumber, @GuestNumber
  end

  if (@PostDayproducts=0)
    SET @PostStay = 0

  SET @PostDayproducts = 0

  Close ReservationCur
  Deallocate ReservationCur
end

