CREATE PROCEDURE [dbo].[OnlineCreateGuestLogin] 
  @ReturnMessage VARCHAR(1024) = '' OUTPUT, 
  @UserName VARCHAR(50), @Password VARCHAR(50),
  @FirstName VARCHAR(50), @LastName VARCHAR(50), 
  @Address1 VARCHAR(50), @Address2 VARCHAR(50), @ZipCode VARCHAR(10), 
  @City VARCHAR(50), @CountryCode VARCHAR(3), @PhoneNumber VARCHAR(30), 
  @CellPhoneNumber VARCHAR(30), @EmailAddress VARCHAR (100), 
  @CustomerNumber INT OUTPUT, 
  @NoNews BIT = 1
AS SET NOCOUNT ON;

-- =============================================
-- Author:		Ken Sørensen
-- Create date: November 2015
-- Description:	Creates a minimum user profile.
-- Changes:
/*
  VD 20170810 GDPR: NoNews is no longer in table Customer
*/
-- =============================================

DECLARE @ReturnCode INT  
SELECT @ReturnCode = @@ERROR

DECLARE @ErrorStep VARCHAR(200)

IF @UserName = '' OR @Password = '' OR @FirstName = '' OR @LastName = ''
BEGIN
	SET @ReturnCode = 1
	SET @ReturnMessage = 'One or more parameters are invalid'
	RETURN @ReturnCode
END


BEGIN TRY
	DECLARE @CustomerTypeNumber INT
	DECLARE @PointOfSaleNumber INT
	DECLARE @SegmentNumber INT
	DECLARE @GuestTypeNumber INT
	DECLARE @PurposeCodeNumber INT
	DECLARE @UserIdNumber INT

	DECLARE @CountryNumber INT
	SELECT @ErrorStep = 'Error in selecting CountryNumber'
	SELECT @CountryNumber = CountryNumber FROM Countries WITH (NOLOCK) WHERE CountryCode = @CountryCode

	DECLARE @UseProductType BIT
	SELECT @ErrorStep = 'Error in selecting UseProductType'
	SELECT @UseProductType = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey) = Upper('PicassoOnlineUseProductTypes')

	/* Read HotelSetup's */
	SELECT @ErrorStep = 'Error in selecting HotelSetup'
	IF @UseProductType = 1
		SELECT @PointOfSaleNumber = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey) = Upper('PicassoOnlinePointOfSaleNumber')    
	ELSE
	BEGIN
		SELECT @PointOfSaleNumber = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey) = Upper('PicassoOnlinePointOfSaleNumber')
		IF @PointOfSaleNumber = 0
			SET @PointOfSaleNumber = null
	END

	SELECT @SegmentNumber = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey) = Upper('PicassoOnlineSegmentNumber')
	IF @SegmentNumber = 0
		SET @SegmentNumber = null

	SELECT @GuestTypeNumber = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey) = Upper('PicassoOnlineGuestTypeNumber')
	IF @GuestTypeNumber = 0
		SET @GuestTypeNumber = null

	SELECT @PurposeCodeNumber = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey) = Upper('PicassoOnlinePurposeCodeNumber')
	IF @PurposeCodeNumber = 0
		SET @PurposeCodeNumber = null


	SELECT @ErrorStep = 'Error in selecting UserIdNumber'
	SELECT @UserIdNumber = UserIdNumber FROM Personel WITH (NOLOCK)  WHERE userIdCode = 'PicassoOnline' AND UserTypeNumber = 5

	SET @CustomerTypeNumber = 1

	DECLARE @Establishdate DATETIME
	DECLARE @CurrentDateStr VARCHAR(8)

	SELECT @Establishdate = GETDATE()
	SET @CurrentDateStr = CONVERT (CHAR (8), @Establishdate, 112)

	SELECT @ErrorStep = 'Error in executing GetNextNumber'
	EXEC GetNextNumber 'CUSTOMERIDENTITY', 1, @CustomerNumber OUTPUT


	IF @CustomerNumber > 0
	BEGIN
		BEGIN TRAN

		SELECT @ErrorStep = 'Error in inserting into Customer'
		INSERT INTO Customer 
		(CustomerNumber, CustomerTypeNumber, FirstName, LastName, Address1, Address2, 
		ZipCodeoverwrite, Cityoverwrite, CountryNumber, Phonenumber, CellPhonenumber,
		Segmentnumber, PointOfSaleNumber, GuestTypeNumber, PurposeCodeNumber, Salesmannumber, Establishdate, Archivedate)  
		VALUES
		(@CustomerNumber, @CustomerTypeNumber, @FirstName,  @LastName, @Address1, @Address2, 
		@ZipCode, @City, @CountryNumber, @PhoneNumber, @CellPhoneNumber, 
		@SegmentNumber, @PointOfSaleNumber, @GuestTypeNumber, 
		@PurposeCodeNumber, @UserIdNumber, @CurrentDateStr, @CurrentDateStr)


		SELECT @ErrorStep = 'Error in inserting into InetCustomer'
		INSERT INTO InetCustomer (CustomerNumber, Customerlogin, CustomerPassword) VALUES (@CustomerNumber, @UserName, @Password) 
 
		SELECT @ErrorStep = 'Error in inserting into MailAddresses'
		INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EmailAddress, 0)	
		INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EmailAddress, 1)	
		INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EmailAddress, 2)	
		INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EmailAddress, 3)
		INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EmailAddress, 4)	
		INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EmailAddress, 5)	
		INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EmailAddress, 6)	
		INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EmailAddress, 7) 

		COMMIT TRAN

		SELECT @ReturnCode = 0, @ReturnMessage = 'Guest Login created sucessfully'
	END
	ELSE
	BEGIN
	    SET @ReturnCode = 2
		SET @ReturnMessage = 'Next CustomerNumber could not be retrieved'
	END

    RETURN @ReturnCode
END TRY

BEGIN CATCH
    IF @@TRANCOUNT > 0 ROLLBACK

    SELECT @ReturnCode = ERROR_NUMBER()
         , @ReturnMessage = @ErrorStep + ' '
         + cast(ERROR_NUMBER() AS VARCHAR(20)) + ' line: '
         + cast(ERROR_LINE() AS VARCHAR(20)) + ' ' 
         + ERROR_MESSAGE() + ' > ' 
         + ERROR_PROCEDURE()
    RETURN @ReturnCode
END CATCH
