CREATE PROCEDURE [dbo].[OnlineUpdateReservationRegistrationCard]
(
  @ReferenceNumber int
  ,@GuestNumber smallint
  ,@CompanyName varchar(50)
  ,@LASTNAME varchar(50)
  ,@FIRSTNAME varchar(50)
  ,@ADDRESS1 varchar(100)
  ,@ADDRESS2 varchar(100)
  ,@ZIPCODE varchar(10)
  ,@CITY varchar(50)
  ,@COUNTRYCODE varchar(3)
  ,@EMAIL varchar(150)
  ,@PHONE varchar(255)
  ,@DateOfBirth datetime
  ,@PassPortNumber varchar(25)
  ,@Note varchar(max) = NULL
  ,@FollowUpSubType2 smallint = NULL
  ,@SignatureImage varbinary(MAX)=NULL
)
/*
	Changes:
  2024-07-11 MLJ Set Subtype=2 in followup
  2024-04-04 MLJ Fixed missing default value in @SignatureImage
  2024-03-27 MLJ Added @SignatureImage
*/
AS
BEGIN
	SET NOCOUNT ON;
	DECLARE @OK bit = 0;
	SET LOCK_TIMEOUT 0;

	IF @FollowUpSubType2 IS NULL
	BEGIN
		IF EXISTS(SELECT * FROM ReservationTablet T WHERE T.ReferenceNumber=@ReferenceNumber AND T.GuestNumber=@GuestNumber)
			SET @FollowUpSubType2=14 -- TFollowUpSubType2.fstRegCardUpdateTablet
		ELSE
			SET @FollowUpSubType2=13 -- TFollowUpSubType2.fst2RegCardUpdate
	END

	BEGIN TRY
		IF NOT EXISTS(SELECT * FROM dbo.LockedReservations WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber=@GuestNumber)
		BEGIN
			EXEC dbo.UpdateReservationRegistrationCard
				@ReferenceNumber 
				,@GuestNumber 
				,@CompanyName 
				,@LASTNAME 
				,@FIRSTNAME
				,@ADDRESS1 
				,@ADDRESS2 
				,@ZIPCODE 
				,@CITY 
				,@COUNTRYCODE 
				,@EMAIL 
				,@PHONE 
				,@DateOfBirth 
				,@PassPortNumber
				,'Registration card saved online'
				,@Note
				,@FollowUpSubType2
        ,@SignatureImage

			SET @OK = 1
		END
	END TRY
	BEGIN CATCH
	END CATCH

	IF @OK = 0 
	BEGIN
		MERGE dbo.ReservationRegistrationCard T
		USING (SELECT @ReferenceNumber ReferenceNumber, @GuestNumber GuestNumber) S
		ON (T.ReferenceNumber=S.ReferenceNumber AND T.GuestNumber=S.GuestNumber)
		WHEN NOT MATCHED BY TARGET THEN
			INSERT (ReferenceNumber, GuestNumber, LASTNAME,FIRSTNAME,ADDRESS1,ADDRESS2,ZIPCODE,CITY,COUNTRYCODE,EMAIL,PHONE,DateOfBirth,PassPortNumber,CompanyName, Note, FollowUpSubType2,SignatureImage)
			VALUES (@ReferenceNumber, @GuestNumber, @LASTNAME,@FIRSTNAME,@ADDRESS1,@ADDRESS2,@ZIPCODE,@CITY,@COUNTRYCODE,@EMAIL,@PHONE,@DateOfBirth,@PassPortNumber,@CompanyName,@Note,@FollowUpSubType2,@SignatureImage)
		WHEN MATCHED THEN
			UPDATE SET LASTNAME=@LASTNAME,FIRSTNAME=@FIRSTNAME,ADDRESS1=@ADDRESS1,ADDRESS2=@ADDRESS2,ZIPCODE=@ZIPCODE,CITY=@CITY,COUNTRYCODE=@COUNTRYCODE,EMAIL=@EMAIL,PHONE=@PHONE,DateOfBirth=@DateOfBirth, PassportNumber=@PassportNumber, CompanyName=@CompanyName, Note=@Note, FollowUpSubType2=@FollowUpSubType2, SignatureImage=@SignatureImage
		;

		DECLARE @UserID int
		SELECT @UserID=UserIDNumber FROM dbo.Personel WHERE UserIDCode like 'picassoonline'
		IF @UserID IS NULL
			SELECT @UserID=UserIDNumber FROM dbo.Personel WHERE UserIDCode like 'TECHOTEL'
		DECLARE @HotelNumber int
		SELECT @HotelNumber=HotelNumber FROM dbo.RESERVATIONMAIN WHERE REFERENCENUMBER=@ReferenceNumber
		EXEC [dbo].[OnlineCreateReservationFollowup] 
			@AReferenceNumber=@ReferenceNumber,
			@AGuestNumber=@GuestNumber,
			@AUserIdNumber=@UserID, 
			@AFollowupText='Registration card saved online to temporay storage', 
			@AHotelNumber=@HotelNumber,
  		@AFollowupSubType2=@FollowUpSubType2,
      @ASubtype=2

	END

END