CREATE PROCEDURE [dbo].[CreateMeetingNameList] @ConfLineNumber INT, @FirstAddressNumber INT, @ReservationStatus INT, @UserID VARCHAR (50), @CreatedNameList SMALLINT OUTPUT AS
SET NOCOUNT ON
/*
    DEPENDENCIES: UpdateArrivalAndDeparture2

    Created by Jacob Pedersen

    Last updated: 
    20120124 RN: CR039157, Replaced @DefaultResStatus with @ReservationStatus parameter
    20110412: Bug: Fix for "Children 2 info not saved if namelist per room"
	20060831 JP: EXT: Added support for Children2
	20060831 JP: BUG: Fix for missing AddressTypeNumber
	20050613 JP: EXT: use AddressTypeNumber
	10-03-2003 Jacob Pedersen

    NOTE: - 

*/

SET @CreatedNameList = 0

DECLARE @ReferenceNumber INT
DECLARE @NumberOfRooms INT
DECLARE @ArrivalDate CHAR (8)
DECLARE @ArrivalTime CHAR (5)
DECLARE @DepartureDate CHAR (8)
DECLARE @DepartureTime CHAR (5)
DECLARE @ProductNumber INT
DECLARE @Adults INT
DECLARE @Children INT
DECLARE @Children2 INT
DECLARE @Price REAL
DECLARE @TotalPrice REAL
DECLARE @NewAddressNumber INT

SELECT @ReferenceNumber=ReferenceNumber, @NumberOfRooms=NumberOfRooms, @ArrivalDate=ArrivalDate, @ArrivalTime=ArrivalTime, @DepartureDate=DepartureDate, @DepartureTime=DepartureTime, @ProductNumber=ProductNumber,
               @Adults=Adults, @Children=Children1, @Children2=Children2, @ProductNumber=ProductNumber, @Price=Price, @TotalPrice=TotalPrice FROM ReservationConferenceLine WITH (NOLOCK) WHERE ReservationConfLineNumber=@ConfLineNumber



SET @Price=0  -- Price and TotalPrice is not used because PaysAdults and PaysChildren er now both 0, hence no price!
SET @TotalPrice=0


SELECT * INTO #TempReservations
FROM Reservations WITH (NOLOCK)
WHERE Reservations.ReferenceNumber = @ReferenceNumber AND Reservations.GuestNumber = 0

UPDATE #TempReservations
SET 
  ArrivalDate = @ArrivalDate,
  ArrivalTime = @ArrivalTime,
  DepartureDate = @DepartureDate,
  DepartureTime = @DepartureTime


IF (@NumberOfRooms = 1)
BEGIN
  DECLARE @CompanyTitle VARCHAR(50)
  DECLARE @CompanyInitial CHAR(10)
  DECLARE @CompanyFirstName VARCHAR (50)
  DECLARE @CompanyLastName VARCHAR (50)
  DECLARE @CompanyAddress1 VARCHAR (50)
  DECLARE @CompanyAddress2 VARCHAR (50)
  DECLARE @CompanyAddress3 VARCHAR (50)
  DECLARE @CompanyCountryCode VARCHAR (3)
  DECLARE @CompanyZipCode VARCHAR (10)
  DECLARE @CompanyCity VARCHAR (50)
  DECLARE @CompanyPhone VARCHAR (30)
  DECLARE @CompanyCell VARCHAR (30)
  DECLARE @CompanyFax VARCHAR (30)
  DECLARE @CompanyEmail VARCHAR (100)
  DECLARE @CompanyAttName VARCHAR (50)
  DECLARE @CompanyProvisionType SMALLINT
  DECLARE @CompanyProvisionRate MONEY
  DECLARE @CompanyDiscountRate MONEY
  DECLARE @CompanyRoomDiscountRate MONEY
  DECLARE @CompanyMeetingDiscountRate MONEY
  DECLARE @CompanyHallDiscountRate MONEY
  DECLARE @CompanyPhoneDiscountRate MONEY
  DECLARE @CompanyLink INTEGER
  DECLARE @CompanyPaymentAfterMonth SMALLINT
  DECLARE @CompanyPaymentAfterDays SMALLINT
  DECLARE @CompanyPaymentTypeWareNumber INTEGER
  DECLARE @CompanyCountryNumber INTEGER

  SELECT @CompanyTitle=Title, @CompanyInitial=Initial, @CompanyFirstName=FirstName, @CompanyLastName=LastName, @CompanyAddress1=Address1, @CompanyAddress2=Address2, @CompanyAddress3=Address3, @CompanyCountryCode=CountryCode, @CompanyZipCode=ZipCode, @CompanyCity=City,
                 @CompanyPhone=Phone,@CompanyCell = Cell, @CompanyFax=Fax, @CompanyEmail=Email, @CompanyAttName=AttentionName, @CompanyProvisionType=ProvisionType, @CompanyProvisionRate=ProvisionRate, @CompanyDiscountRate=DiscountRate, @CompanyRoomDiscountRate=RoomDiscountRate,
                 @CompanyMeetingDiscountRate=MeetingDiscountRate, @CompanyHallDiscountRate=HallDiscountRate, @CompanyPhoneDiscountRate=PhoneDiscountRate, @CompanyLink=Link, @CompanyPaymentAfterMonth=PaymentAfterMonth, @CompanyPaymentAfterDays=PaymentAfterDays,
                 @CompanyPaymentTypeWareNumber=PaymentTypeWareNumber, @CompanyCountryNumber=CountryNumber
  FROM ReservationAddressXTab WITH (NOLOCK) INNER JOIN ReservationAddress WITH (NOLOCK) ON ReservationAddressXTab.AddressNumber = ReservationAddress.AddressNumber
  WHERE ReservationAddressXTab.ReferenceNumber = @ReferenceNumber AND ReservationAddressXTab.GuestNumber = 0 AND AddressTypeNumber=2

  DECLARE @AgencyTitle VARCHAR(50)
  DECLARE @AgencyInitial CHAR(10)
  DECLARE @AgencyFirstName VARCHAR (50)
  DECLARE @AgencyLastName VARCHAR (50)
  DECLARE @AgencyAddress1 VARCHAR (50)
  DECLARE @AgencyAddress2 VARCHAR (50)
  DECLARE @AgencyAddress3 VARCHAR (50)
  DECLARE @AgencyCountryCode VARCHAR (3)
  DECLARE @AgencyZipCode VARCHAR (10)
  DECLARE @AgencyCity VARCHAR (50)
  DECLARE @AgencyPhone VARCHAR (30)
  DECLARE @AgencyCell VARCHAR (30)
  DECLARE @AgencyFax VARCHAR (30)
  DECLARE @AgencyEmail VARCHAR (100)
  DECLARE @AgencyAttName VARCHAR (50)
  DECLARE @AgencyProvisionType SMALLINT
  DECLARE @AgencyProvisionRate MONEY
  DECLARE @AgencyDiscountRate MONEY
  DECLARE @AgencyRoomDiscountRate MONEY
  DECLARE @AgencyMeetingDiscountRate MONEY
  DECLARE @AgencyHallDiscountRate MONEY
  DECLARE @AgencyPhoneDiscountRate MONEY
  DECLARE @AgencyLink INTEGER
  DECLARE @AgencyPaymentAfterMonth SMALLINT
  DECLARE @AgencyPaymentAfterDays SMALLINT
  DECLARE @AgencyPaymentTypeWareNumber INTEGER
  DECLARE @AgencyCountryNumber INTEGER

  SELECT @AgencyTitle=Title, @AgencyInitial=Initial, @AgencyFirstName=FirstName, @AgencyLastName=LastName, @AgencyAddress1=Address1, @AgencyAddress2=Address2, @AgencyAddress3=Address3, @AgencyCountryCode=CountryCode, @AgencyZipCode=ZipCode, @AgencyCity=City,
                 @AgencyPhone=Phone, @AgencyCell = Cell, @AgencyFax=Fax, @AgencyEmail=Email, @AgencyAttName=AttentionName, @AgencyProvisionType=ProvisionType, @AgencyProvisionRate=ProvisionRate, @AgencyDiscountRate=DiscountRate, @AgencyRoomDiscountRate=RoomDiscountRate,
                 @AgencyMeetingDiscountRate=MeetingDiscountRate, @AgencyHallDiscountRate=HallDiscountRate, @AgencyPhoneDiscountRate=PhoneDiscountRate, @AgencyLink=Link, @AgencyPaymentAfterMonth=PaymentAfterMonth, @AgencyPaymentAfterDays=PaymentAfterDays,
                 @AgencyPaymentTypeWareNumber=PaymentTypeWareNumber, @AgencyCountryNumber=CountryNumber
  FROM ReservationAddressXTab WITH (NOLOCK) INNER JOIN ReservationAddress WITH (NOLOCK) ON ReservationAddressXTab.AddressNumber = ReservationAddress.AddressNumber
  WHERE ReservationAddressXTab.ReferenceNumber = @ReferenceNumber AND ReservationAddressXTab.GuestNumber = 0 AND AddressTypeNumber=3

  DECLARE @AgentTitle VARCHAR(50)
  DECLARE @AgentInitial CHAR(10)
  DECLARE @AgentFirstName VARCHAR (50)
  DECLARE @AgentLastName VARCHAR (50)
  DECLARE @AgentAddress1 VARCHAR (50)
  DECLARE @AgentAddress2 VARCHAR (50)
  DECLARE @AgentAddress3 VARCHAR (50)
  DECLARE @AgentCountryCode VARCHAR (3)
  DECLARE @AgentZipCode VARCHAR (10)
  DECLARE @AgentCity VARCHAR (50)
  DECLARE @AgentPhone VARCHAR (30)
  DECLARE @AgentCell VARCHAR (30)
  DECLARE @AgentFax VARCHAR (30)
  DECLARE @AgentEmail VARCHAR (100)
  DECLARE @AgentAttName VARCHAR (50)
  DECLARE @AgentProvisionType SMALLINT
  DECLARE @AgentProvisionRate MONEY
  DECLARE @AgentDiscountRate MONEY
  DECLARE @AgentRoomDiscountRate MONEY
  DECLARE @AgentMeetingDiscountRate MONEY
  DECLARE @AgentHallDiscountRate MONEY
  DECLARE @AgentPhoneDiscountRate MONEY
  DECLARE @AgentLink INTEGER
  DECLARE @AgentPaymentAfterMonth SMALLINT
  DECLARE @AgentPaymentAfterDays SMALLINT
  DECLARE @AgentPaymentTypeWareNumber INTEGER
  DECLARE @AgentCountryNumber INTEGER

  SELECT @AgentTitle=Title, @AgentInitial=Initial, @AgentFirstName=FirstName, @AgentLastName=LastName, @AgentAddress1=Address1, @AgentAddress2=Address2, @AgentAddress3=Address3, @AgentCountryCode=CountryCode, @AgentZipCode=ZipCode, @AgentCity=City,
                 @AgentPhone=Phone, @AgentCell = Cell, @AgentFax=Fax, @AgentEmail=Email, @AgentAttName=AttentionName, @AgentProvisionType=ProvisionType, @AgentProvisionRate=ProvisionRate, @AgentDiscountRate=DiscountRate, @AgentRoomDiscountRate=RoomDiscountRate,
                 @AgentMeetingDiscountRate=MeetingDiscountRate, @AgentHallDiscountRate=HallDiscountRate, @AgentPhoneDiscountRate=PhoneDiscountRate, @AgentLink=Link, @AgentPaymentAfterMonth=PaymentAfterMonth, @AgentPaymentAfterDays=PaymentAfterDays,
                 @AgentPaymentTypeWareNumber=PaymentTypeWareNumber, @AgentCountryNumber=CountryNumber
  FROM ReservationAddressXTab WITH (NOLOCK) INNER JOIN ReservationAddress WITH (NOLOCK) ON ReservationAddressXTab.AddressNumber = ReservationAddress.AddressNumber
  WHERE ReservationAddressXTab.ReferenceNumber = @ReferenceNumber AND ReservationAddressXTab.GuestNumber = 0 AND AddressTypeNumber=4


  DECLARE @MaxGuestNumber INT
  DECLARE @CurrentGuestNumber INT
  SELECT @MaxGuestNumber = MAX (GuestNumber) FROM Reservations WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber
  SET @CurrentGuestNumber = @MaxGuestNumber+1

  DECLARE @MarketSegment INT
  DECLARE @PointOfSale INT
  DECLARE @SplitBill INT
  SELECT @SplitBill = SplitBill, @MarketSegment = MarketSegment, @PointOfSale = PointOfSale FROM Reservations WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber=0

  DECLARE @NamelistReservationPerPerson INT
  SELECT @NamelistReservationPerPerson = Convert (Integer, KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey)=Upper ('NamelistReservationPerPerson')

  DECLARE @SystemDate DATETIME
  SELECT  @SystemDate = CONVERT (DATETIME, SYSTEMDATE, 112) FROM System WITH (NOLOCK) WHERE NUMBER = 1  -- Only one record

  DECLARE @EnableHallBookInPast INT
  SELECT @EnableHallBookInPast = CONVERT(Integer, ISNULL(KeyValue, 0)) FROM HotelSetUp WITH (NOLOCK) WHERE SearchKey = 'EnableHallBookingInPastTime'

  -- TODO: add time to @systemdate  !!!   strangely this seems to be done somehow

  DECLARE @DoUpdateArrivalAndDeparture BIT
  IF (@EnableHallBookInPast = 0) AND (@ArrivalDate < @SystemDate)
  BEGIN
    SET @DepartureDate = CONVERT (CHAR (8), @SystemDate+CONVERT (DATETIME, @DepartureDate, 112)-CONVERT (DATETIME, @ArrivalDate, 112), 112)
    SET @ArrivalDate = CONVERT (CHAR (8), @SystemDate, 112)
    SET @DoUpdateArrivalAndDeparture=1
  END
  ELSE
    SET @DoUpdateArrivalAndDeparture=0

  DECLARE @UserIDNumber INT
  SELECT @UserIDNumber = UserIDNumber FROM Personel WITH (NOLOCK) WHERE Upper (UserIDCode) = Upper (@UserID)

  -- Create Adults
  IF (@NamelistReservationPerPerson = 1)
  BEGIN

  WHILE (@CurrentGuestNumber < @MaxGuestNumber+1+@Adults)
  BEGIN
    EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT

	UPDATE #TempReservations
	SET GuestNumber = @CurrentGuestNumber

	INSERT INTO Reservations SELECT * FROM #TempReservations WITH (NOLOCK)
	UPDATE Reservations
	SET
	  Adults = 1,
	  Children1 = 0,
	  Children2 = 0,
	  PaysAdults = 0,
	  PaysChildren1 = 0,
	  PaysChildren2 = 0
	WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = @CurrentGuestNumber

    --INSERT INTO Reservations (ReferenceNumber, GuestNumber, Status, MarketSegment, PointOfSale, UserID, ArrivalDate, ArrivalTime, DepartureDate, DepartureTime, Adults, Children1, Children2, ProductNumber, Price, TotalPrice, PaysAdults, PaysChildren1, PaysChildren2, SPLITBILL) VALUES
    --                                              (@ReferenceNumber, @CurrentGuestNumber, @ReservationStatus, @MarketSegment, @PointOfSale, @UserID, @ArrivalDate, @ArrivalTime, @DepartureDate, @DepartureTime, 1, 0, 0, @ProductNumber, @Price, @TotalPrice, 0, 0, 0, @SplitBill)
    INSERT INTO ReservationMeetingXTab (ReferenceNumber, GuestNumber, ReservationConfLineNumber, Adults, Children, Children2, PaysAdults, PaysChildren, PaysChildren2) VALUES (@ReferenceNumber, @CurrentGuestNumber, @ConfLineNumber, 1, 0, 0, 0, 0, 0)

    -- atGuest
    INSERT INTO ReservationAddress (AddressNumber, AddressType, LastName, IsNameAutoGenerated) VALUES (@NewAddressNumber, 1, '', 1)  -- 1 = atGuest
    INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 1) -- 1 = atGuest
    --SET @FirstAddressNumber = @FirstAddressNumber+1

    INSERT INTO FollowUp (Date, Type, Priority, State, Note, UserCreated, SubType, CreatedBy, CompletedBy) VALUES (@SystemDate, 5, 17, 3, 'Created without Customer', 1, 1, @UserIDNumber, @UserIDNumber)
    INSERT INTO ReservationFollowUpXTab (ReferenceNumber, GuestNumber, FollowUpNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @@identity)

    -- atCompany
    IF (@CompanyLastName <> '') and not (@CompanyLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 2, @CompanyTitle, @CompanyInitial, @CompanyFirstName, @CompanyLastName, @CompanyAddress1, @CompanyAddress2, @CompanyAddress3,
                                                               @CompanyCountryCode, @CompanyZipCode, @CompanyCity, @CompanyPhone, @CompanyCell, @CompanyFax, @CompanyEmail, @CompanyAttName, @CompanyProvisionType, @CompanyProvisionRate, @CompanyDiscountRate, @CompanyRoomDiscountRate, @CompanyMeetingDiscountRate,
                                                               @CompanyHallDiscountRate, @CompanyPhoneDiscountRate, @CompanyLink, @CompanyPaymentAfterMonth, @CompanyPaymentAfterDays, @CompanyPaymentTypeWareNumber, @CompanyCountryNumber, 0)  -- 2 = atCompany
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 2)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    -- atAgency
    IF (@AgencyLastName <> '') and not (@AgencyLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 3, @AgencyTitle, @AgencyInitial, @AgencyFirstName, @AgencyLastName, @AgencyAddress1, @AgencyAddress2, @AgencyAddress3,
                                                               @AgencyCountryCode, @AgencyZipCode, @AgencyCity, @AgencyPhone, @AgencyCell, @AgencyFax, @AgencyEmail, @AgencyAttName, @AgencyProvisionType, @AgencyProvisionRate, @AgencyDiscountRate, @AgencyRoomDiscountRate, @AgencyMeetingDiscountRate,
                                                               @AgencyHallDiscountRate, @AgencyPhoneDiscountRate, @AgencyLink, @AgencyPaymentAfterMonth, @AgencyPaymentAfterDays, @AgencyPaymentTypeWareNumber, @AgencyCountryNumber, 0)  -- 3 = atAgency
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 3)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    -- atAgent
    IF (@AgentLastName <> '') and not (@AgentLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 4, @AgentTitle, @AgentInitial, @AgentFirstName, @AgentLastName, @AgentAddress1, @AgentAddress2, @AgentAddress3,
                                                               @AgentCountryCode, @AgentZipCode, @AgentCity, @AgentPhone, @AgentCell, @AgentFax, @AgentEmail, @AgentAttName, @AgentProvisionType, @AgentProvisionRate, @AgentDiscountRate, @AgentRoomDiscountRate, @AgentMeetingDiscountRate,
                                                               @AgentHallDiscountRate, @AgentPhoneDiscountRate, @AgentLink, @AgentPaymentAfterMonth, @AgentPaymentAfterDays, @AgentPaymentTypeWareNumber, @AgentCountryNumber, 0)  -- 4 = atAgent
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 4)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    SET @Price=0
    SET @TotalPrice=0

    SET @CurrentGuestNumber = @CurrentGuestNumber+1
  END

  -- Create Children 1
  WHILE (@CurrentGuestNumber < @MaxGuestNumber+1+@Adults+@Children)
  BEGIN
    EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT

	UPDATE #TempReservations
	SET GuestNumber = @CurrentGuestNumber

	INSERT INTO Reservations SELECT * FROM #TempReservations WITH (NOLOCK)
	UPDATE Reservations
	SET
	  Adults = 0,
	  Children1 = 1,
	  Children2 = 0,
	  PaysAdults = 0,
	  PaysChildren1 = 0,
	  PaysChildren2 = 0
	WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = @CurrentGuestNumber

    --INSERT INTO Reservations (ReferenceNumber, GuestNumber, Status, MarketSegment, PointOfSale, UserID, ArrivalDate, ArrivalTime, DepartureDate, DepartureTime, Adults, Children1, Children2, ProductNumber, Price, TotalPrice, PaysAdults, PaysChildren1, PaysChildren2, SplitBill) VALUES
    --                                              (@ReferenceNumber, @CurrentGuestNumber, @ReservationStatus, @MarketSegment, @PointOfSale, @UserID, @ArrivalDate, @ArrivalTime, @DepartureDate, @DepartureTime, 0, 1, 0, @ProductNumber, @Price, @TotalPrice, 0, 0, 0, @SplitBill)

    INSERT INTO ReservationMeetingXTab (ReferenceNumber, GuestNumber, ReservationConfLineNumber, Adults, Children, Children2, PaysAdults, PaysChildren, PaysChildren2) VALUES (@ReferenceNumber, @CurrentGuestNumber, @ConfLineNumber, 0, 1, 0, 0, 0, 0)

    -- atGuest
    INSERT INTO ReservationAddress (AddressNumber, AddressType, LastName, IsNameAutoGenerated) VALUES (@NewAddressNumber, 1, '', 1)  -- 1 = atGuest
    INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 1) -- 1 = atGuest
    --SET @FirstAddressNumber = @FirstAddressNumber+1

    INSERT INTO FollowUp (Date, Type, Priority, State, Note, UserCreated, SubType, CreatedBy, CompletedBy) VALUES (@SystemDate, 5, 17, 3, 'Created without Customer', 1, 1, @UserIDNumber, @UserIDNumber)
    INSERT INTO ReservationFollowUpXTab (ReferenceNumber, GuestNumber, FollowUpNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @@identity)

    -- atCompany
    IF (@CompanyLastName <> '') and not (@CompanyLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 2, @CompanyTitle, @CompanyInitial, @CompanyFirstName, @CompanyLastName, @CompanyAddress1, @CompanyAddress2, @CompanyAddress3, 
                                                               @CompanyCountryCode, @CompanyZipCode, @CompanyCity, @CompanyPhone, @CompanyCell, @CompanyFax, @CompanyEmail, @CompanyAttName, @CompanyProvisionType, @CompanyProvisionRate, @CompanyDiscountRate, @CompanyRoomDiscountRate, @CompanyMeetingDiscountRate,
                                                               @CompanyHallDiscountRate, @CompanyPhoneDiscountRate, @CompanyLink, @CompanyPaymentAfterMonth, @CompanyPaymentAfterDays, @CompanyPaymentTypeWareNumber, @CompanyCountryNumber, 0)  -- 2 = atCompany
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 2)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    -- atAgency
    IF (@AgencyLastName <> '') and not (@AgencyLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 3, @AgencyTitle, @AgencyInitial, @AgencyFirstName, @AgencyLastName, @AgencyAddress1, @AgencyAddress2, @AgencyAddress3,
                                                  @AgencyCountryCode, @AgencyZipCode, @AgencyCity, @AgencyPhone, @AgencyCell, @AgencyFax, @AgencyEmail, @AgencyAttName, @AgencyProvisionType, @AgencyProvisionRate, @AgencyDiscountRate, @AgencyRoomDiscountRate, @AgencyMeetingDiscountRate,
                                                               @AgencyHallDiscountRate, @AgencyPhoneDiscountRate, @AgencyLink, @AgencyPaymentAfterMonth, @AgencyPaymentAfterDays, @AgencyPaymentTypeWareNumber, @AgencyCountryNumber, 0)  -- 3 = atAgency
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 3)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    -- atAgent
    IF (@AgentLastName <> '') and not (@AgentLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 4, @AgentTitle, @AgentInitial, @AgentFirstName, @AgentLastName, @AgentAddress1, @AgentAddress2, @AgentAddress3,
                                                               @AgentCountryCode, @AgentZipCode, @AgentCity, @AgentPhone, @AgentCell, @AgentFax, @AgentEmail, @AgentAttName, @AgentProvisionType, @AgentProvisionRate, @AgentDiscountRate, @AgentRoomDiscountRate, @AgentMeetingDiscountRate,
                                                               @AgentHallDiscountRate, @AgentPhoneDiscountRate, @AgentLink, @AgentPaymentAfterMonth, @AgentPaymentAfterDays, @AgentPaymentTypeWareNumber, @AgentCountryNumber, 0)  -- 4 = atAgent
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 4)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    SET @Price=0
    SET @TotalPrice=0

    SET @CurrentGuestNumber = @CurrentGuestNumber+1
  END
  -- Create Children 2
  WHILE (@CurrentGuestNumber < @MaxGuestNumber+1+@Adults+@Children+@Children2)
  BEGIN
    EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT

	UPDATE #TempReservations
	SET GuestNumber = @CurrentGuestNumber

	INSERT INTO Reservations SELECT * FROM #TempReservations WITH (NOLOCK)
	UPDATE Reservations
	SET
	  Adults = 0,
	  Children1 = 0,
	  Children2 = 1,
	  PaysAdults = 0,
	  PaysChildren1 = 0,
	  PaysChildren2 = 0
	WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = @CurrentGuestNumber

    --INSERT INTO Reservations (ReferenceNumber, GuestNumber, Status, MarketSegment, PointOfSale, UserID, ArrivalDate, ArrivalTime, DepartureDate, DepartureTime, Adults, Children1, Children2, ProductNumber, Price, TotalPrice, PaysAdults, PaysChildren1, PaysChildren2, SplitBill) VALUES
    --                                              (@ReferenceNumber, @CurrentGuestNumber, @ReservationStatus, @MarketSegment, @PointOfSale, @UserID, @ArrivalDate, @ArrivalTime, @DepartureDate, @DepartureTime, 0, 0, 1, @ProductNumber, @Price, @TotalPrice, 0, 0, 0, @SplitBill)

    INSERT INTO ReservationMeetingXTab (ReferenceNumber, GuestNumber, ReservationConfLineNumber, Adults, Children, Children2, PaysAdults, PaysChildren, PaysChildren2) VALUES (@ReferenceNumber, @CurrentGuestNumber, @ConfLineNumber, 0, 1, 0, 0, 0, 0)

    -- atGuest
    INSERT INTO ReservationAddress (AddressNumber, AddressType, LastName, IsNameAutoGenerated) VALUES (@NewAddressNumber, 1, '', 1)  -- 1 = atGuest
    INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 1) -- 1 = atGuest
    --SET @FirstAddressNumber = @FirstAddressNumber+1

    INSERT INTO FollowUp (Date, Type, Priority, State, Note, UserCreated, SubType, CreatedBy, CompletedBy) VALUES (@SystemDate, 5, 17, 3, 'Created without Customer', 1, 1, @UserIDNumber, @UserIDNumber)
    INSERT INTO ReservationFollowUpXTab (ReferenceNumber, GuestNumber, FollowUpNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @@identity)

    -- atCompany
    IF (@CompanyLastName <> '') and not (@CompanyLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 2, @CompanyTitle, @CompanyInitial, @CompanyFirstName, @CompanyLastName, @CompanyAddress1, @CompanyAddress2, @CompanyAddress3, 
                                                               @CompanyCountryCode, @CompanyZipCode, @CompanyCity, @CompanyPhone, @CompanyCell, @CompanyFax, @CompanyEmail, @CompanyAttName, @CompanyProvisionType, @CompanyProvisionRate, @CompanyDiscountRate, @CompanyRoomDiscountRate, @CompanyMeetingDiscountRate,
                                                               @CompanyHallDiscountRate, @CompanyPhoneDiscountRate, @CompanyLink, @CompanyPaymentAfterMonth, @CompanyPaymentAfterDays, @CompanyPaymentTypeWareNumber, @CompanyCountryNumber, 0)  -- 2 = atCompany
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 2)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    -- atAgency
    IF (@AgencyLastName <> '') and not (@AgencyLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 3, @AgencyTitle, @AgencyInitial, @AgencyFirstName, @AgencyLastName, @AgencyAddress1, @AgencyAddress2, @AgencyAddress3,
                                                               @AgencyCountryCode, @AgencyZipCode, @AgencyCity, @AgencyPhone, @AgencyCell, @AgencyFax, @AgencyEmail, @AgencyAttName, @AgencyProvisionType, @AgencyProvisionRate, @AgencyDiscountRate, @AgencyRoomDiscountRate, @AgencyMeetingDiscountRate,
                                                               @AgencyHallDiscountRate, @AgencyPhoneDiscountRate, @AgencyLink, @AgencyPaymentAfterMonth, @AgencyPaymentAfterDays, @AgencyPaymentTypeWareNumber, @AgencyCountryNumber, 0)  -- 3 = atAgency
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 3)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    -- atAgent
    IF (@AgentLastName <> '') and not (@AgentLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 4, @AgentTitle, @AgentInitial, @AgentFirstName, @AgentLastName, @AgentAddress1, @AgentAddress2, @AgentAddress3,
                                                               @AgentCountryCode, @AgentZipCode, @AgentCity, @AgentPhone, @AgentCell, @AgentFax, @AgentEmail, @AgentAttName, @AgentProvisionType, @AgentProvisionRate, @AgentDiscountRate, @AgentRoomDiscountRate, @AgentMeetingDiscountRate,
                                                               @AgentHallDiscountRate, @AgentPhoneDiscountRate, @AgentLink, @AgentPaymentAfterMonth, @AgentPaymentAfterDays, @AgentPaymentTypeWareNumber, @AgentCountryNumber, 0)  -- 4 = atAgent
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 4)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    SET @Price=0
    SET @TotalPrice=0

    SET @CurrentGuestNumber = @CurrentGuestNumber+1
  END

  END
  ELSE  -- (@NamelistReservationPerPerson = 0)
  BEGIN
    EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT

	UPDATE #TempReservations
	SET GuestNumber = @CurrentGuestNumber

	INSERT INTO Reservations SELECT * FROM #TempReservations WITH (NOLOCK)
	UPDATE Reservations
	SET
	  PaysAdults = 0,
	  PaysChildren1 = 0,
	  PaysChildren2 = 0
	WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = @CurrentGuestNumber

    --INSERT INTO Reservations (ReferenceNumber, GuestNumber, Status, MarketSegment, PointOfSale, UserID, ArrivalDate, ArrivalTime, DepartureDate, DepartureTime, Adults, Children1, ProductNumber, Price, TotalPrice, PaysAdults, PaysChildren1, SPLITBILL) VALUES
    --                                              (@ReferenceNumber, @CurrentGuestNumber, @ReservationStatus, @MarketSegment, @PointOfSale, @UserID, @ArrivalDate, @ArrivalTime, @DepartureDate, @DepartureTime, @Adults, @Children, @ProductNumber, @Price, @TotalPrice, 0, 0, @SplitBill)

    INSERT INTO ReservationMeetingXTab (ReferenceNumber, GuestNumber, ReservationConfLineNumber, Adults, Children, Children2, PaysAdults, PaysChildren, PaysChildren2) VALUES (@ReferenceNumber, @CurrentGuestNumber, @ConfLineNumber, @Adults, @Children, 0, 0, 0, 0)

    -- atGuest
    INSERT INTO ReservationAddress (AddressNumber, AddressType, LastName, IsNameAutoGenerated) VALUES (@NewAddressNumber, 1, '', 1)  -- 1 = atGuest
    INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber,AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 1) -- 1 = atGuest

    INSERT INTO FollowUp (Date, Type, Priority, State, Note, UserCreated, SubType, CreatedBy, CompletedBy) VALUES (@SystemDate, 5, 17, 3, 'Created without Customer', 1, 1, @UserIDNumber, @UserIDNumber)
    INSERT INTO ReservationFollowUpXTab (ReferenceNumber, GuestNumber, FollowUpNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @@identity)

    --SET @FirstAddressNumber = @FirstAddressNumber+1

    -- atCompany
    IF (@CompanyLastName <> '') and not (@CompanyLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 2, @CompanyTitle, @CompanyInitial, @CompanyFirstName, @CompanyLastName, @CompanyAddress1, @CompanyAddress2, @CompanyAddress3,
                                                               @CompanyCountryCode, @CompanyZipCode, @CompanyCity, @CompanyPhone, @CompanyCell, @CompanyFax, @CompanyEmail, @CompanyAttName, @CompanyProvisionType, @CompanyProvisionRate, @CompanyDiscountRate, @CompanyRoomDiscountRate, @CompanyMeetingDiscountRate,
                                                               @CompanyHallDiscountRate, @CompanyPhoneDiscountRate, @CompanyLink, @CompanyPaymentAfterMonth, @CompanyPaymentAfterDays, @CompanyPaymentTypeWareNumber, @CompanyCountryNumber, 0)  -- 2 = atCompany
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 2)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    -- atAgency
    IF (@AgencyLastName <> '') and not (@AgencyLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 3, @AgencyTitle, @AgencyInitial, @AgencyFirstName, @AgencyLastName, @AgencyAddress1, @AgencyAddress2, @AgencyAddress3,
                                                               @AgencyCountryCode, @AgencyZipCode, @AgencyCity, @AgencyPhone, @AgencyCell, @AgencyFax, @AgencyEmail, @AgencyAttName, @AgencyProvisionType, @AgencyProvisionRate, @AgencyDiscountRate, @AgencyRoomDiscountRate, @AgencyMeetingDiscountRate,
                                                               @AgencyHallDiscountRate, @AgencyPhoneDiscountRate, @AgencyLink, @AgencyPaymentAfterMonth, @AgencyPaymentAfterDays, @AgencyPaymentTypeWareNumber, @AgencyCountryNumber, 0)  -- 3 = atAgency
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 3)
      --SET @FirstAddressNumber = @FirstAddressNumber+1
    END

    -- atAgent
    IF (@AgentLastName <> '') and not (@AgentLastName is null)
    BEGIN
      EXEC GetNextNumber 'ReservationAddressIdentity', 1, @NewAddressNumber OUTPUT
      INSERT INTO ReservationAddress (AddressNumber, AddressType, Title, Initial, FirstName, LastName, Address1, Address2, Address3, CountryCode, ZipCode, City, Phone, Cell, Fax, Email, AttentionName, ProvisionType, ProvisionRate, DiscountRate, RoomDiscountRate, MeetingDiscountRate, HallDiscountRate,
                                                               PhoneDiscountRate, Link, PaymentAfterMonth, PaymentAfterDays, PaymentTypeWareNumber, CountryNumber, IsNameAutoGenerated)
                                               VALUES (@NewAddressNumber, 4, @AgentTitle, @AgentInitial, @AgentFirstName, @AgentLastName, @AgentAddress1, @AgentAddress2, @AgentAddress3,
                                                               @AgentCountryCode, @AgentZipCode, @AgentCity, @AgentPhone, @AgentCell, @AgentFax, @AgentEmail, @AgentAttName, @AgentProvisionType, @AgentProvisionRate, @AgentDiscountRate, @AgentRoomDiscountRate, @AgentMeetingDiscountRate,
                                                               @AgentHallDiscountRate, @AgentPhoneDiscountRate, @AgentLink, @AgentPaymentAfterMonth, @AgentPaymentAfterDays, @AgentPaymentTypeWareNumber, @AgentCountryNumber, 0)  -- 4 = atAgent
      INSERT INTO ReservationAddressXTab (ReferenceNumber, GuestNumber, AddressNumber, AddressTypeNumber) VALUES (@ReferenceNumber, @CurrentGuestNumber, @NewAddressNumber, 4)
-- not needed      SET @FirstAddressNumber = @FirstAddressNumber+1
    END
  END

  DROP TABLE #TempReservations

  IF @DoUpdateArrivalAndDeparture=1
    EXEC UpdateArrivalAndDeparture2 @ReferenceNumber, 0

  SET @CreatedNameList = 1
END