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

    Changes:
    2021-09-23 MLJ: supports separate number series for contact persons
	vd20170810 Universallog stuff.
    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
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
  DECLARE @SeparateNumberSeriesOnContactPersonsEnabled bit = 0
  SELECT @SeparateNumberSeriesOnContactPersonsEnabled=CAST(KeyValue AS bit) FROM HotelSetup WITH (NOLOCK) WHERE SEARCHKEY LIKE 'SeparateNumberSeriesOnContactPersonsEnabled'
  IF @SeparateNumberSeriesOnContactPersonsEnabled=0
    EXEC GetNextNumber 'CUSTOMERIDENTITY', 1, @Customernumber OUTPUT
  ELSE
    EXEC GetNextNumber 'CONTACTPERSONIDENTITY', 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

  DECLARE @SystemDate CHAR(8)
  DECLARE @SystemTime CHAR(5)
  SELECT @SystemDate = SystemDate FROM System WITH (NOLOCK)
  SELECT @Systemtime = Convert(CHAR(5), GetDate(), 108)

  INSERT INTO UniversalLog (DATESTAMP, TIME, USERID, SUBTYPE, LINK, DESCRIPTION,  AText)
    VALUES (@SystemDate, @SystemTime, 'PicassoOnline', 1, @Customernumber, 'Contact person created', 'Contact person created online')

END

