CREATE PROCEDURE [dbo].[OnlineUpdateGuestLogin] 
  @ReturnMessage VARCHAR(1024) = '' OUTPUT, 
  @CustomerNumber INT, @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), 
  @NoNews BIT = -1

AS SET NOCOUNT ON;

-- =============================================
-- Author:		Ken Sørensen
-- Create date: November 2015
-- Description:	Updates an user profile.
-- Changes:
/*
   XXX YYYYMMDD: ................................................
*/
-- =============================================

/* Test SQL:
SELECT Cust.CustomerNumber, Inet.CustomerLogin as Username, Inet.CustomerPassword as Password,
Firstname, Lastname, Address1, Address2, ZipCodeOverwrite as ZipCode, CityOverWrite as City, 
CountryNumber, PhoneNumber, CellPhoneNumber, Mail.MailAddress, NoNews, Mail.MailAddressType
FROM Customer Cust WITH (NOLOCK)
INNER JOIN InetCustomer Inet WITH (NOLOCK) ON Inet.CustomerNumber = Cust.CustomerNumber 
INNER JOIN MailAddresses Mail WITH (NOLOCK) ON Mail.CustomerNumber = Cust.CustomerNumber 
WHERE Cust.MarkAsDeleted <> 1
AND Cust.CustomerNumber = 9999
*/

DECLARE @ReturnCode INT  
SELECT @ReturnCode = @@ERROR

DECLARE @ErrorStep VARCHAR(200)

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


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

	DECLARE @CustomerCount INT
	SELECT @ErrorStep = 'Error in selecting CustomerCount'
	SELECT @CustomerCount = count(*) FROM Customer Cust WITH (NOLOCK)
	INNER JOIN InetCustomer Inet WITH (NOLOCK) ON Inet.CustomerNumber = Cust.CustomerNumber 
	WHERE Cust.MarkAsDeleted <> 1 AND Cust.CustomerNumber = @CustomerNumber

	IF @CustomerCount = 1
	BEGIN
		BEGIN TRAN

		SELECT @ErrorStep = 'Error in updating Customer'
		UPDATE Customer SET 
		  FirstName = @FirstName,
		  LastName = @LastName,
		  Address1 = @Address1,
		  Address2 = @Address2,
		  ZipCodeOverwrite = @ZipCode,
		  CityOverWrite = @City,
		  CountryNumber = @CountryNumber,
		  PhoneNumber = @PhoneNumber,
		  CellPhoneNumber = @CellPhoneNumber
		WHERE CustomerNumber = @CustomerNumber
		AND MarkAsDeleted <> '1'

		IF @NoNews <> -1
		BEGIN
		  SELECT @ErrorStep = 'Error in updating Customer (NoNews)'
		  UPDATE Customer SET NoNews = @NoNews WHERE CustomerNumber = @CustomerNumber AND MarkAsDeleted <> '1'
		END

	    SELECT @ErrorStep = 'Error in updating MailAddresses'
		Update MailAddresses SET 
		  MailAddress = @EmailAddress
		WHERE CustomerNumber = @CustomerNumber AND MailAddressType < 8

		IF @Password <> ''
		BEGIN
	      SELECT @ErrorStep = 'Error in updating InetCustomer'
		  Update InetCustomer SET
			CustomerPassword = @Password
		  WHERE CustomerNumber = @CustomerNumber
		END

		COMMIT TRAN

		SELECT @ReturnCode = 0, @ReturnMessage = 'Guest Login updated sucessfully'
	END
	ELSE
	BEGIN
	    SET @ReturnCode = 2
		SET @ReturnMessage = 'Wrong CustomerNumber'
	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
