/*
  BA/20160323: Fixing a rare condition where both products Test and Unmapped exists.
               at the same time. This causes a 'Key violation'.
  BA/20150420: Klaus whises to use the name of Unmapped instead of Test when
               OTA Reservations version 8.1 isn't mapped.

*/

IF EXISTS(SELECT * 
          FROM Products 
		  WHERE ProductName = 'Test')
BEGIN
  IF NOT EXISTS(SELECT * 
                FROM Products 
		        WHERE ProductName = 'Unmapped')
  BEGIN
    UPDATE Products
    SET ProductName = 'Unmapped'
    WHERE ProductName = 'Test'
  END
END ELSE
BEGIN  
  IF NOT EXISTS(SELECT * 
                FROM Products 
		        WHERE ProductName = 'Unmapped')
  BEGIN
    DECLARE @ProductNumber INT
    EXEC GetNextNumber 'PRODUCTSIDENTITY', 1, @ProductNumber OUTPUT

    INSERT INTO [dbo].[Products]
               ([ProductNumber], [MarkAsDeleted], [ProductName], [Description], [DayProduct], [FixedPrice], [PriceOverwrite])
         VALUES
               (@ProductNumber, 0, 'Unmapped', 'OTA Dummy product', 0, 0, 1)
  END
END
