CREATE PROCEDURE dbo.ReservationAddressAnonymize_Do @AHotelCustomerNumber INT, @AReplaceText VARCHAR(10) AS

/*
  Created by Kent Johansen, October 2021
*/

BEGIN
  UPDATE ReservationAddress
  SET
    Title = SUBSTRING(SUBSTRING(Title, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataTitle', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 30),
    Initial = SUBSTRING(SUBSTRING(Initial, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataInitial', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 10),
    FirstName = SUBSTRING(SUBSTRING(FirstName, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataFirstName', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 50),
    LastName = 
      CASE WHEN SUBSTRING(LastName, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataLastName', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText = '' THEN 
        '*' 
      ELSE 
        SUBSTRING(SUBSTRING(LastName, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataLastName', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 50)  
      END,
    Address1 = SUBSTRING(SUBSTRING(Address1, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataAddress1', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 100),
    Address2 = SUBSTRING(SUBSTRING(Address2, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataAddress2', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 100),
    Address3 = SUBSTRING(SUBSTRING(Address3, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataAddress3', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 100),
    ZipCode = SUBSTRING(SUBSTRING(ZipCode, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataZipCode', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 10),
    City = SUBSTRING(SUBSTRING(City, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataCity', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 50),
    Phone = SUBSTRING(SUBSTRING(Phone, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataPhone', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 255),
    Fax = SUBSTRING(SUBSTRING(Fax, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataFax', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 30),
    Email = SUBSTRING(SUBSTRING(Email, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataMail', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 96),
    AttentionName = SUBSTRING(SUBSTRING(AttentionName, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataAttentionName', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 50),
    Link = CASE WHEN (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataClearCustLink', @AHotelCustomerNumber), 0) AS BIT)) = 1 THEN NULL ELSE Link END,
    ContactpersonCustomerNumber = CASE WHEN (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataClearCustAttention', @AHotelCustomerNumber), 1) AS BIT)) = 1 THEN NULL ELSE ContactpersonCustomerNumber END,
    AllContactsLinkID = CASE WHEN (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataClearCustAttention', @AHotelCustomerNumber), 1) AS BIT)) = 1 THEN NULL ELSE AllContactsLinkID END,    
    Cell = SUBSTRING(SUBSTRING(Cell, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataMobileCell', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 255),
    MemberCardNumber = CASE WHEN (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataClearMemberCard', @AHotelCustomerNumber), 1) AS BIT)) = 1 THEN NULL ELSE MemberCardNumber END,
    MiddleName = SUBSTRING(SUBSTRING(MiddleName, 1, (CAST(ISNULL(dbo.GetHotelSetupKeyValue('AnonymizeReservationDataMiddleName', @AHotelCustomerNumber), 0) AS INT))) + @AReplaceText, 1, 40),
    AddressAnonymized = 1,
    AnonymizeDate = GETDATE()
  FROM ReservationAddress WITH (NOLOCK)
  WHERE ReservationAddress.AddressNumber IN (SELECT AddressNumber FROM #ResTable)
END