CREATE PROCEDURE [dbo].[OnlineCreateContact]
  @Firstname VARCHAR(50), @Lastname VARCHAR(50), @Email VARCHAR (100)
 AS SET NOCOUNT ON
/*
    Created by SG 2015-08-27

    Changes:
    2016-05-25 SG: Added FlagD (initials taken from Email, only for DI)
*/    

DECLARE @Customernumber INT
DECLARE @CustomerNumberExisting INT
DECLARE @PointOfSaleNumber INT
DECLARE @SegmentNumber INT
DECLARE @GuestTypeNumber INT
DECLARE @PurposeCodeNumber INT
DECLARE @UserIdNumber INT

DECLARE @UseProductType BIT
DECLARE @UseNoNews BIT
SELECT @UseProductType =KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey)=Upper('PicassoOnlineUseProductTypes')

/* Read HotelSetup's */
SELECT @PointOfSaleNumber= KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey)=Upper('PicassoOnlinePointOfSaleNumber')
IF  @PointOfSaleNumber = 0
   SET @PointOfSaleNumber = null

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 @UserIdNumber = UserIdNumber  FROM Personel WITH (NOLOCK)  WHERE userIdCode = 'PicassoOnline' AND UserTypeNumber = 5


DECLARE @Establishdate DATETIME
DECLARE @CurrentDateStr VARCHAR(8)
DECLARE @FlagD VARCHAR(100)
DECLARE @PosSnabelA int

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

IF ISNULL(@EMail, '') <> ''  
BEGIN
  SET @PosSnabelA = CHARINDEX('@', @Email)
  SET @FlagD = SUBSTRING(@Email, 0, @PosSnabelA)
END ELSE
BEGIN
  SET @FlagD = ''
END

Set @CustomerNumberExisting = 0 --Vi checker ikke

IF ISNULL(@CustomerNumberExisting, 0) = 0
BEGIN
  EXEC GetNextNumber 'CUSTOMERIDENTITY', 1, @Customernumber OUTPUT
  INSERT INTO Customer (CustomerNumber, CustomerTypeNumber, Firstname, Lastname, 
    Segmentnumber, PointOfSaleNumber, GuestTypeNumber, 
    PurposeCodeNumber, Salesmannumber, Establishdate, Archivedate, FLAGD)  
  VALUES(@Customernumber, 8, @Firstname,  @Lastname,
    @SegmentNumber, @PointOfSaleNumber, @GuestTypeNumber, 
    @PurposeCodeNumber, @UserIdNumber, @CurrentDateStr, @CurrentDateStr, @FlagD)  

  
  IF ISNULL(@EMail, '') <> ''  
  BEGIN
    INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EMail, 0)	
    INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EMail, 1)	
    INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EMail, 2)	
    INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EMail, 3)
    INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EMail, 4)	
    INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EMail, 5)	
    INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EMail, 6)	
    INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) VALUES (@CustomerNumber, @EMail, 7) 
  END  

END
