DECLARE @CustomerNumber INT
DECLARE @ValueKey INT
DECLARE @HotelSetupKey INT

IF NOT EXISTS(SELECT * FROM HotelSetup (NOLOCK) WHERE SearchKey IN ('DefaultPinCode', 'DefaultPinCodeMona'))
BEGIN
	

	DECLARE #cur CURSOR FOR
		SELECT CustomerNumber FROM HotelSetup hs (NOLOCK)
		INNER JOIN HotelSetupValueXTab hsvx (NOLOCK) ON hs.HotelSetUpNumber = hsvx.HotelSetupNumber
		INNER JOIN HotelSetupValue hsv (NOLOCK) ON hsvx.FK_ValueNumber = hsv.PK_ValueNumber
		WHERE searchkey = 'KeySystem' and hsv.KeyValue = '2'

	OPEN #CUR
	FETCH NEXT FROM #cur INTO  @CustomerNumber
	WHILE @@FETCH_STATUS = 0
	BEGIN
		INSERT INTO HotelSetupValue (KeyValue)
		VALUES ('1')
		SELECT @ValueKey = @@IDENTITY

		INSERT INTO HotelSetup (SearchKey, KeyValue)
		VALUES ('DefaultPinCode', '0')
		SELECT @HotelSetupKey = @@IDENTITY

		INSERT INTO HotelSetupValueXTab (HotelSetupNumber, CustomerNumber, FK_ValueNumber)
		VALUES (@HotelSetupKey, @CustomerNumber, @ValueKey)

		FETCH NEXT FROM #cur INTO  @CustomerNumber
	END

	CLOSE #cur
	DEALLOCATE #CUR
END

IF EXISTS(SELECT * FROM HotelSetup (NOLOCK) WHERE SearchKey = 'DefaultPinCode')
BEGIN

	DECLARE #cur CURSOR FOR
		SELECT CustomerNumber FROM HotelSetup hs (NOLOCK)
		INNER JOIN HotelSetupValueXTab hsvx (NOLOCK) ON hs.HotelSetUpNumber = hsvx.HotelSetupNumber
		INNER JOIN HotelSetupValue hsv (NOLOCK) ON hsvx.FK_ValueNumber = hsv.PK_ValueNumber
		WHERE searchkey = 'DefaultPinCode' and hsv.KeyValue = '1'

	OPEN #CUR
	FETCH NEXT FROM #cur INTO  @CustomerNumber
	WHILE @@FETCH_STATUS = 0
	BEGIN
		INSERT INTO HotelSetupValue (KeyValue)
		VALUES ('1')
		SELECT @ValueKey = @@IDENTITY

		INSERT INTO HotelSetup (SearchKey, KeyValue)
		VALUES ('DefaultPinCodeTerminal', '0')
		SELECT @HotelSetupKey = @@IDENTITY

		INSERT INTO HotelSetupValueXTab (HotelSetupNumber, CustomerNumber, FK_ValueNumber)
		VALUES (@HotelSetupKey, @CustomerNumber, @ValueKey)

		FETCH NEXT FROM #cur INTO  @CustomerNumber
	END

	CLOSE #cur
	DEALLOCATE #CUR


	UPDATE HotelSetup
	SET SearchKey = 'DefaultPinCodeMona'
	WHERE SearchKey = 'DefaultPinCode'
END

IF NOT EXISTS(SELECT * FROM HotelSetup (NOLOCK) WHERE SearchKey = 'DefaultPinCodePicasso')
BEGIN
	DECLARE #cur CURSOR FOR
		SELECT CustomerNumber FROM HotelSetup hs (NOLOCK)
		INNER JOIN HotelSetupValueXTab hsvx (NOLOCK) ON hs.HotelSetUpNumber = hsvx.HotelSetupNumber
		INNER JOIN HotelSetupValue hsv (NOLOCK) ON hsvx.FK_ValueNumber = hsv.PK_ValueNumber
		WHERE searchkey = 'DefaultPinCodeMona' and hsv.KeyValue = '1'

	OPEN #CUR
	FETCH NEXT FROM #cur INTO  @CustomerNumber
	WHILE @@FETCH_STATUS = 0
	BEGIN
		INSERT INTO HotelSetupValue (KeyValue)
		VALUES ('1')
		SELECT @ValueKey = @@IDENTITY

		INSERT INTO HotelSetup (SearchKey, KeyValue)
		VALUES ('DefaultPinCodePicasso', '0')
		SELECT @HotelSetupKey = @@IDENTITY

		INSERT INTO HotelSetupValueXTab (HotelSetupNumber, CustomerNumber, FK_ValueNumber)
		VALUES (@HotelSetupKey, @CustomerNumber, @ValueKey)

		FETCH NEXT FROM #cur INTO  @CustomerNumber
	END

	CLOSE #cur
	DEALLOCATE #CUR
END

IF NOT EXISTS(SELECT * FROM HotelSetup (NOLOCK) WHERE SearchKey = 'PinCodeByPicasso')
	UPDATE HotelSetup
	SET SearchKey = 'PinCodeByPicasso'
	WHERE SearchKey = 'TesaPinCodeByPicasso'
ELSE
BEGIN
	Delete From HotelSetupLog
	FRom HotelSetupLog hl (NOLOCK)
	INNER JOIN HotelSetup hs On hl.HotelSetupNumber = hs.HotelSetUpNumber
	WHERE SearchKey = 'TesaPinCodeByPicasso'

	Delete From HotelSetupValueXTab
	FRom HotelSetupValueXTab hl (NOLOCK)
	INNER JOIN HotelSetup hs On hl.HotelSetupNumber = hs.HotelSetUpNumber
	WHERE SearchKey = 'TesaPinCodeByPicasso'

	DELETE FROM HotelSetup
	WHERE SearchKey = 'TesaPinCodeByPicasso'
END

IF NOT EXISTS(SELECT * FROM HotelSetup (NOLOCK) WHERE SearchKey = 'PinCodeNumberOfDigits')
	UPDATE HotelSetup
	SET SearchKey = 'PinCodeNumberOfDigits'
	WHERE SEarchKey = 'TesaNumberOfDigits'
ELSE
BEGIN
	Delete From HotelSetupLog
	FRom HotelSetupLog hl (NOLOCK)
	INNER JOIN HotelSetup hs On hl.HotelSetupNumber = hs.HotelSetUpNumber
	WHERE SearchKey = 'TesaNumberOfDigits'


	Delete From HotelSetupValueXTab
	FRom HotelSetupValueXTab hl (NOLOCK)
	INNER JOIN HotelSetup hs On hl.HotelSetupNumber = hs.HotelSetUpNumber
	WHERE SearchKey = 'TesaNumberOfDigits'


	DELETE FROM HotelSetup
	WHERE SearchKey = 'TesaNumberOfDigits'


END

IF NOT EXISTS(SELECT * FROM HotelSetup (NOLOCK) WHERE SearchKey = 'PinCodeMakeCopyKey')
	UPDATE HotelSetup
	SET SearchKey = 'PinCodeMakeCopyKey'
	WHERE SEarchKey = 'TesaMakeCopyKey'
ELSE
BEGIN
	Delete From HotelSetupLog
	FRom HotelSetupLog hl (NOLOCK)
	INNER JOIN HotelSetup hs On hl.HotelSetupNumber = hs.HotelSetUpNumber
	WHERE SearchKey = 'TesaMakeCopyKey'

	Delete From HotelSetupValueXTab
	FRom HotelSetupValueXTab hl (NOLOCK)
	INNER JOIN HotelSetup hs On hl.HotelSetupNumber = hs.HotelSetUpNumber
	WHERE SearchKey = 'TesaMakeCopyKey'

	DELETE FROM HotelSetup
	WHERE SearchKey = 'TesaMakeCopyKey'
END
