CREATE PROCEDURE [dbo].[OnlineUpdateParticipants]  @Referencenumber INT, @RoomLineNumber INT = 0,
  @Firstname VARCHAR(50), @Lastname VARCHAR(50), @CompanyName VARCHAR(50), @Fullname VARCHAR(100),
  @Address VARCHAR(100), @Zipcode VARCHAR(50), @City VARCHAR(50), @Phone VARCHAR(50), @Email VARCHAR(100),  @Guestnumber INT,
  @HotelCustomerNumber int = -1
 AS SET NOCOUNT ON
/*
    Created by Troels Buch Nov 2014
    
    Changes:
	KJ 20160613: Added requestorId -2 means Online
    SG 2016-01-20: AutoComfirmed added to Reservations, new optional parameter @HotelCustomerNumber
    SG 2016-01-19: IsNameAutoGenerated has been added to ReservationAddress
    SG 2015-06-22: Added @RoomLineNumber as parameter
    TBU20150220: Added PaysAdults, PaysChildren, PaysChildren2
    TBU20141210: Changed UPDATE RESERVATIONADDRESS    

    NOTES: 	SG/2015-05-11: Called by OnlineUpdateParticipantsAll
*/

DECLARE @NumberOfParticipants INT
DECLARE @ConfLineNumber INT
DECLARE @HasNameList INT
DECLARE @AddressNumber INT
Declare @AutoConfirmationDefaultValue Char(1);

SELECT  @AutoConfirmationDefaultValue = dbo.GetHotelSetupKeyValue('AutoConfirmationDefaultValue',@HotelCustomerNumber)
Set @AutoConfirmationDefaultValue = ISNULL(@AutoConfirmationDefaultValue, '1');

select @HasNameList=Count (*) from RESERVATIONS  with (nolock)
where ReferenceNumber = @Referencenumber
and GuestNumber = 1

print '  @HasNameList ' + convert(char(2), @HasNameList)

select @NumberOfParticipants= adults from RESERVATIONS with (nolock)
where ReferenceNumber = @Referencenumber
and GuestNumber = 0

select @ConfLineNumber = ReservationConfLineNumber from RESERVATIONCONFERENCELINE with (nolock)
where ReferenceNumber = @Referencenumber AND (ReservationConfLineNumber = @RoomLineNumber OR @RoomLineNumber = 0)

DECLARE @DefaultName VARCHAR(50)
Declare @IsNameAuto INT;
DECLARE @GuestCountIndex  INT
SET @GuestCountIndex = 1

-- Check Fullname
IF @Fullname <> ''
   SET @Lastname = @Fullname;

If CHARINDEX('Guest/', @Lastname) > 0
Begin
  Set @IsNameAuto = 1
End Else
Begin
  Set @IsNameAuto = 0
End
Print 'Lastname=' + @Lastname + ' IsNameAuto=' + convert(char(1), @IsNameAuto)

IF @HasNameList = 0
BEGIN
   -- Namelist donīt exists
   
   -- Build Empty namelist for all participants
   WHILE @GuestCountIndex <= @NumberOfParticipants
   BEGIN
   
      INSERT INTO RESERVATIONS (REFERENCENUMBER, GUESTNUMBER, [Status], POINTOFSALE, AUTOCONFIRMED, RequestorId)
      VALUES (@Referencenumber, @GuestCountIndex, 2, 1, @AutoConfirmationDefaultValue, -2)      
	  
      INSERT INTO ReservationMeetingXTab (ReferenceNumber, GuestNumber, ReservationConfLineNumber, Adults, Children, Children2, PaysAdults, PaysChildren, PaysChildren2)
	  VALUES (@Referencenumber, @GuestCountIndex, @ConfLineNumber, 1, 0, 0, 0, 0, 0)
	  	  
	  EXEC GetNextNumber 'ReservationAddressIdentity', 1, @AddressNumber OUTPUT
	  SET @DefaultName = 'Guest/'+ CAST( @GuestCountIndex AS VARCHAR(50) ) 
      INSERT INTO RESERVATIONADDRESS (AddressNumber, ADDRESSTYPE, LASTNAME, IsNameAutoGenerated)
      VALUES (@AddressNumber, 1, @DefaultName, 1)
   
      INSERT INTO RESERVATIONADDRESSXTAB (REFERENCENUMBER, GUESTNUMBER, ADDRESSNUMBER, AddressTypeNumber)
      VALUES (@Referencenumber, @GuestCountIndex, @AddressNumber, 1) 
      
      UPDATE RESERVATIONADDRESS SET FIRSTNAME=@Firstname, LASTNAME=@Lastname, 
             ADDRESS3=@CompanyName, ADDRESS1=@Address, ZIPCODE=@Zipcode, CITY=@City, PHONE=@Phone, EMAIL=@Email,
             IsNameAutoGenerated = @IsNameAuto 
      WHERE AddressNumber =  (select ADDRESSNUMBER from RESERVATIONS res with (nolock)
      inner join RESERVATIONADDRESSXTAB resax with (nolock) on resax.REFERENCENUMBER = res.ReferenceNumber
      where res.ReferenceNumber = @Referencenumber
      and res.GUESTNUMBER = 0
      and AddressTypeNumber = 1
      and resax.GUESTNUMBER = @Guestnumber) 
	  
	  
	  print @DefaultName
	  print '@GuestCountIndex'
	  print @GuestCountIndex	
	  print '@ConfLineNumber'
	  print @ConfLineNumber
      SET @GuestCountIndex = @GuestCountIndex +1
   END
   
   
   print '  Not Exist'
END
ELSE
BEGIN
   -- Namelist donīt exists
   print '  Exist'
   
   UPDATE RESERVATIONADDRESS SET FIRSTNAME=@Firstname, LASTNAME=@Lastname, 
          ADDRESS3=@CompanyName, ADDRESS1=@Address, ZIPCODE=@Zipcode, CITY=@City, PHONE=@Phone, EMAIL=@Email,
          IsNameAutoGenerated = @IsNameAuto 
   WHERE AddressNumber =  (select ADDRESSNUMBER from RESERVATIONS res with (nolock)
   inner join RESERVATIONADDRESSXTAB resax with (nolock) on resax.REFERENCENUMBER = res.ReferenceNumber
   where res.ReferenceNumber = @Referencenumber
   and res.GUESTNUMBER = 0
   and AddressTypeNumber = 1
   and resax.GUESTNUMBER = @Guestnumber) 
   
END

