IF NOT EXISTS(SELECT * FROM HotelSETUp (NOLOCK) WHERE SearchKey = 'CleaningSubRoomEnabled')
BEGIN

	DECLARE @SubRoomEnabled VARCHAR(2000),
	@SubRoomStayDays VARCHAR(2000),
	@SubRoomDepDays VARCHAR(2000),
	@SubRoomDepGuests VARCHAR(2000),
	@CustomerNumber INT,
	@EnabledSetupID INT,
	@StayDaysSetupID INT,
	@DepDaysSetupID INT,
	@DepGuestsSetupID INT,
	@ValueID INT

	SELECT @SubRoomEnabled = KeyValue FROM HotelSetup (NOLOCK)
	WHERE SearchKey = 'CleaningMainRoomEnabled'

	IF @SubRoomEnabled = '1'
	BEGIN

		SELECT @SubRoomStayDays = KeyValue FROM HotelSetup (NOLOCK)
		WHERE SearchKey = 'CleaningMainRoomStayDays'

		SELECT @SubRoomDepDays = KeyValue FROM HotelSetup (NOLOCK)
		WHERE SearchKey = 'CleaningMainRoomDepDays'

		SELECT @SubRoomDepGuests = KeyValue FROM HotelSetup (NOLOCK)
		WHERE SearchKey = 'CleaningMainRoomDepGuests'

		INSERT INTO HotelSetup (SearchKey, LabelName)
		VALUES ('CleaningSubRoomEnabled','Enable')

		SELECT  @EnabledSetupID = @@IDENTITY

		INSERT INTO HotelSetup (SearchKey, LabelName)
		VALUES ('CleaningSubRoomStayDays','Days between stay cleaning')

		SELECT  @StayDaysSetupID = @@IDENTITY

		INSERT INTO HotelSetup (SearchKey, LabelName)
		VALUES ('CleaningSubRoomDepDays','')

		SELECT  @DepDaysSetupID = @@IDENTITY

		INSERT INTO HotelSetup (SearchKey, LabelName)
		VALUES ('CleaningSubRoomDepGuests','No. guests for departure cleaning')

		SELECT  @DepGuestsSetupID = @@IDENTITY

		DECLARE #CUR CURSOR FOR

			select hvx.CustomerNumber from HotelSetup hs (nolock)
			inner join HotelSetupValueXTab hvx (nolock) on hs.HotelSetUpNumber = hvx.HotelSetupNumber
			inner join HotelSetupValue hv (NOLOCK) on hvx.FK_ValueNumber = hv.PK_ValueNumber
			INNER join customer c (NOLOCK) ON hvx.CustomerNumber = c.CustomerNumber and MarkAsDeleted = 0
			where SearchKey = 'EnableRoomInRoom' and hv.keyvalue = 1

		OPEN #CUR

		FETCH NEXT FROM #CUR   
		INTO @CustomerNumber  

		WHILE @@FETCH_STATUS = 0  
		BEGIN

			INSERT INTO HotelSetupValue (KeyValue)
			VALUES (@SubRoomEnabled)

			SELECT  @ValueID = @@IDENTITY

			INSERT INTO HotelSetupValueXTab(HotelSetupNumber, CustomerNumber, FK_ValueNumber)
			VALUES (@EnabledSetupID, @CustomerNumber,@ValueID) 

		
			

			INSERT INTO HotelSetupValue (KeyValue)
			VALUES (@SubRoomStayDays)

			SELECT  @ValueID = @@IDENTITY

			INSERT INTO HotelSetupValueXTab(HotelSetupNumber, CustomerNumber, FK_ValueNumber)
			VALUES (@StayDaysSetupID, @CustomerNumber,@ValueID) 

			INSERT INTO HotelSetupValue (KeyValue)
			VALUES (@SubRoomDepDays)

			SELECT  @ValueID = @@IDENTITY

			INSERT INTO HotelSetupValueXTab(HotelSetupNumber, CustomerNumber, FK_ValueNumber)
			VALUES (@DepDaysSetupID, @CustomerNumber,@ValueID) 


			INSERT INTO HotelSetupValue (KeyValue)
			VALUES (@SubRoomDepGuests)

			SELECT  @ValueID = @@IDENTITY

			INSERT INTO HotelSetupValueXTab(HotelSetupNumber, CustomerNumber, FK_ValueNumber)
			VALUES (@DepGuestsSetupID, @CustomerNumber,@ValueID) 

		


			FETCH NEXT FROM #CUR   
			INTO @CustomerNumber
		END

		CLOSE #CUR
		DEALLOCATE #CUR


	END

END