
-- PicassoOnlinePosteringDepartment
UPDATE HotelSetup SET KeyValue = CONVERT(VARCHAR, D.PK_Department)
FROM HotelSetup, Departments AS D
WHERE SearchKey = 'PicassoOnlinePosteringDepartment'
AND HotelSetup.KeyValue = D.DepartmentNumber
GO

-- Followup departments
-- FollowUpDefaultDepartmentsDeposits
DECLARE @tmpDepartments VARCHAR(60)
DECLARE @tmpPK_Departments VARCHAR(60)
DECLARE @Idx INT
DECLARE @CurrentDepartmentString CHAR(1)
DECLARE @PK_Department INT

SET @tmpDepartments = ''
SET @tmpPK_Departments = ''

SELECT @tmpDepartments = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'FollowUpDefaultDepartmentsDeposits'
SET @Idx = 1
WHILE @Idx <= Len(@tmpDepartments)     
BEGIN
  SET @CurrentDepartmentString = SUBSTRING(@tmpDepartments, @Idx, 1) 

  SELECT @PK_Department = PK_Department FROM Departments WITH (NOLOCK) WHERE DepartmentNumber = @CurrentDepartmentString
  IF ISNULL(@PK_Department, 0) > 0
  BEGIN
    if @tmpPK_Departments <> ''
      SET @tmpPK_Departments = @tmpPK_Departments + ',' + CONVERT(VARCHAR(4), @PK_Department)
    ELSE
      SET @tmpPK_Departments = CONVERT(VARCHAR(4), @PK_Department)
  END

  SET @Idx = @Idx + 1 
END
UPDATE HotelSetup SET KeyValue = @tmpPK_Departments WHERE SearchKey = 'FollowUpDefaultDepartmentsDeposits'
GO

-- FollowUpDefaultDepartmentsTentative
DECLARE @tmpDepartments VARCHAR(60)
DECLARE @tmpPK_Departments VARCHAR(60)
DECLARE @Idx INT
DECLARE @CurrentDepartmentString CHAR(1)
DECLARE @PK_Department INT

SET @tmpDepartments = ''
SET @tmpPK_Departments = ''

SELECT @tmpDepartments = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'FollowUpDefaultDepartmentsTentative'
SET @Idx = 1
WHILE @Idx <= Len(@tmpDepartments)     
BEGIN
  SET @CurrentDepartmentString = SUBSTRING(@tmpDepartments, @Idx, 1) 

  SELECT @PK_Department = PK_Department FROM Departments WITH (NOLOCK) WHERE DepartmentNumber = @CurrentDepartmentString
  IF ISNULL(@PK_Department, 0) > 0
  BEGIN
    if @tmpPK_Departments <> ''
      SET @tmpPK_Departments = @tmpPK_Departments + ',' + CONVERT(VARCHAR(4), @PK_Department)
    ELSE
      SET @tmpPK_Departments = CONVERT(VARCHAR(4), @PK_Department)
  END

  SET @Idx = @Idx + 1 
END
UPDATE HotelSetup SET KeyValue = @tmpPK_Departments WHERE SearchKey = 'FollowUpDefaultDepartmentsTentative'
GO

-- FollowUpDefaultDepartmentsGroups
DECLARE @tmpDepartments VARCHAR(60)
DECLARE @tmpPK_Departments VARCHAR(60)
DECLARE @Idx INT
DECLARE @CurrentDepartmentString CHAR(1)
DECLARE @PK_Department INT

SET @tmpDepartments = ''
SET @tmpPK_Departments = ''

SELECT @tmpDepartments = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'FollowUpDefaultDepartmentsGroups'
SET @Idx = 1
WHILE @Idx <= Len(@tmpDepartments)     
BEGIN
  SET @CurrentDepartmentString = SUBSTRING(@tmpDepartments, @Idx, 1) 

  SELECT @PK_Department = PK_Department FROM Departments WITH (NOLOCK) WHERE DepartmentNumber = @CurrentDepartmentString
  IF ISNULL(@PK_Department, 0) > 0
  BEGIN
    if @tmpPK_Departments <> ''
      SET @tmpPK_Departments = @tmpPK_Departments + ',' + CONVERT(VARCHAR(4), @PK_Department)
    ELSE
      SET @tmpPK_Departments = CONVERT(VARCHAR(4), @PK_Department)
  END

  SET @Idx = @Idx + 1 
END
UPDATE HotelSetup SET KeyValue = @tmpPK_Departments WHERE SearchKey = 'FollowUpDefaultDepartmentsGroups'
GO

-- FollowUpDefaultDepartmentsProvisional
DECLARE @tmpDepartments VARCHAR(60)
DECLARE @tmpPK_Departments VARCHAR(60)
DECLARE @Idx INT
DECLARE @CurrentDepartmentString CHAR(1)
DECLARE @PK_Department INT

SET @tmpDepartments = ''
SET @tmpPK_Departments = ''

SELECT @tmpDepartments = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'FollowUpDefaultDepartmentsProvisional'
SET @Idx = 1
WHILE @Idx <= Len(@tmpDepartments)     
BEGIN
  SET @CurrentDepartmentString = SUBSTRING(@tmpDepartments, @Idx, 1) 

  SELECT @PK_Department = PK_Department FROM Departments WITH (NOLOCK) WHERE DepartmentNumber = @CurrentDepartmentString
  IF ISNULL(@PK_Department, 0) > 0
  BEGIN
    if @tmpPK_Departments <> ''
      SET @tmpPK_Departments = @tmpPK_Departments + ',' + CONVERT(VARCHAR(4), @PK_Department)
    ELSE
      SET @tmpPK_Departments = CONVERT(VARCHAR(4), @PK_Department)
  END

  SET @Idx = @Idx + 1 
END
UPDATE HotelSetup SET KeyValue = @tmpPK_Departments WHERE SearchKey = 'FollowUpDefaultDepartmentsProvisional'
GO

-- FollowUpDefaultDepartmentsCustomer
DECLARE @tmpDepartments VARCHAR(60)
DECLARE @tmpPK_Departments VARCHAR(60)
DECLARE @Idx INT
DECLARE @CurrentDepartmentString CHAR(1)
DECLARE @PK_Department INT

SET @tmpDepartments = ''
SET @tmpPK_Departments = ''

SELECT @tmpDepartments = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'FollowUpDefaultDepartmentsCustomer'
SET @Idx = 1
WHILE @Idx <= Len(@tmpDepartments)     
BEGIN
  SET @CurrentDepartmentString = SUBSTRING(@tmpDepartments, @Idx, 1) 

  SELECT @PK_Department = PK_Department FROM Departments WITH (NOLOCK) WHERE DepartmentNumber = @CurrentDepartmentString
  IF ISNULL(@PK_Department, 0) > 0
  BEGIN
    if @tmpPK_Departments <> ''
      SET @tmpPK_Departments = @tmpPK_Departments + ',' + CONVERT(VARCHAR(4), @PK_Department)
    ELSE
      SET @tmpPK_Departments = CONVERT(VARCHAR(4), @PK_Department)
  END

  SET @Idx = @Idx + 1 
END
UPDATE HotelSetup SET KeyValue = @tmpPK_Departments WHERE SearchKey = 'FollowUpDefaultDepartmentsCustomer'
GO

-- FollowUpDefaultDepartmentsReservation
DECLARE @tmpDepartments VARCHAR(60)
DECLARE @tmpPK_Departments VARCHAR(60)
DECLARE @Idx INT
DECLARE @CurrentDepartmentString CHAR(1)
DECLARE @PK_Department INT

SET @tmpDepartments = ''
SET @tmpPK_Departments = ''

SELECT @tmpDepartments = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'FollowUpDefaultDepartmentsReservation'
SET @Idx = 1
WHILE @Idx <= Len(@tmpDepartments)     
BEGIN
  SET @CurrentDepartmentString = SUBSTRING(@tmpDepartments, @Idx, 1) 

  SELECT @PK_Department = PK_Department FROM Departments WITH (NOLOCK) WHERE DepartmentNumber = @CurrentDepartmentString
  IF ISNULL(@PK_Department, 0) > 0
  BEGIN
    if @tmpPK_Departments <> ''
      SET @tmpPK_Departments = @tmpPK_Departments + ',' + CONVERT(VARCHAR(4), @PK_Department)
    ELSE
      SET @tmpPK_Departments = CONVERT(VARCHAR(4), @PK_Department)
  END

  SET @Idx = @Idx + 1 
END
UPDATE HotelSetup SET KeyValue = @tmpPK_Departments WHERE SearchKey = 'FollowUpDefaultDepartmentsReservation'
GO

-- AutomaticLogoutDepartmentStr
DECLARE @tmpDepartments VARCHAR(60)
DECLARE @tmpPK_Departments VARCHAR(60)
DECLARE @Idx INT
DECLARE @CurrentDepartmentString CHAR(1)
DECLARE @PK_Department INT

SET @tmpDepartments = ''
SET @tmpPK_Departments = ''

SELECT @tmpDepartments = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'AutomaticLogoutDepartmentStr'
SET @Idx = 1
WHILE @Idx <= Len(@tmpDepartments)     
BEGIN
  SET @CurrentDepartmentString = SUBSTRING(@tmpDepartments, @Idx, 1) 

  SELECT @PK_Department = PK_Department FROM Departments WITH (NOLOCK) WHERE DepartmentNumber = @CurrentDepartmentString
  IF ISNULL(@PK_Department, 0) > 0
  BEGIN
    if @tmpPK_Departments <> ''
      SET @tmpPK_Departments = @tmpPK_Departments + ',' + CONVERT(VARCHAR(4), @PK_Department)
    ELSE
      SET @tmpPK_Departments = CONVERT(VARCHAR(4), @PK_Department)
  END

  SET @Idx = @Idx + 1 
END
UPDATE HotelSetup SET KeyValue = @tmpPK_Departments WHERE SearchKey = 'AutomaticLogoutDepartmentStr'
GO
