IF NOT EXISTS
(
  SELECT SysColumns.Name
  FROM SysObjects WITH (NOLOCK)
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID
    AND SysColumns.Name = 'FkWarePayment'
    WHERE SysObjects.Name = 'PrinterSetupXTab'
)
BEGIN
  ALTER TABLE PrinterSetupXTab ADD FkWarePayment INT NULL
  ALTER TABLE [dbo].[PrinterSetupXTab] WITH NOCHECK ADD CONSTRAINT [FK_PrinterSetupXTab_Ware] FOREIGN KEY([FkWarePayment])
    REFERENCES [dbo].[Ware] ([WareNumber])
END
GO

IF NOT EXISTS (SELECT Name FROM SysIndexes  WITH (NOLOCK) WHERE Name = 'IX_KitchenNote_Type')
begin
  CREATE NONCLUSTERED INDEX [IX_KitchenNote_Type] ON [dbo].[KitchenNote]
  (
    [Type] ASC
  )
end
GO

IF NOT EXISTS (SELECT Name FROM SysIndexes  WITH (NOLOCK) WHERE Name = 'IX_KitchenNote_SortNumber')
begin
  CREATE NONCLUSTERED INDEX [IX_KitchenNote_SortNumber] ON [dbo].[KitchenNote]
  (
    [SortNumber] ASC
  )
end
GO
IF NOT EXISTS
(
  SELECT SysColumns.Name
  FROM SysObjects WITH (NOLOCK)
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID
    AND SysColumns.Name = 'NoteOnFolioOpen'
    WHERE SysObjects.Name = 'Departments'
)
  ALTER TABLE Departments ADD NoteOnFolioOpen BIT NULL
GO
IF (SELECT COUNT(*) FROM Departments (NOLOCK) WHERE NOT NoteOnFolioOpen IS NULL) = 0
BEGIN
  DECLARE @Value BIT
  SELECT @Value = KeyValue FROM HotelSetup (NOLOCK) WHERE SearchKey = 'StartWithNote'
  IF @Value IS NULL
    UPDATE Departments SET NoteOnFolioOpen = 1
  ELSE
    UPDATE Departments SET NoteOnFolioOpen = @Value
END
GO
IF NOT EXISTS
  (
    SELECT SysColumns.Name
    FROM SysObjects WITH (NOLOCK)
      INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID
      AND SysColumns.Name = 'Kind'
      WHERE SysObjects.Name = 'BillTypes'
  )
    ALTER TABLE BillTypes ADD Kind SMALLINT NULL
GO

IF NOT EXISTS
  (
    SELECT SysColumns.Name
    FROM SysObjects WITH (NOLOCK)
      INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID
      AND SysColumns.Name = 'ItemId'
      WHERE SysObjects.Name = 'WareBillSortTypesXTab'
  )
BEGIN
  CREATE TABLE dbo.[Tmp_WareBillSortTypesXTab] (
  [WareNumber] Int NOT NULL,
  [SortTypeNumber] Int NOT NULL,
  [BillTypeNumber] Int NOT NULL,
  [OrderNumber] Int NOT NULL DEFAULT ((0)),
  [TextOverwrite] Bit NOT NULL DEFAULT ((0)),
  [Text] Char (50) NULL,
  [FKBillSortOrder] Int NULL,
  [FKBillSortPeriod] Int NULL DEFAULT ((1)),
  [ItemId] Int IDENTITY (1, 1) NOT NULL) ON [Primary]

  IF EXISTS(SELECT * FROM WareBillSortTypesXTab)
    INSERT INTO dbo.Tmp_WareBillSortTypesXTab ([WareNumber],[BillTypeNumber],[SortTypeNumber],[OrderNumber],[TextOverwrite],[Text],[FKBillSortOrder],[FKBillSortPeriod])
    SELECT [WareNumber],[BillTypeNumber],[SortTypeNumber],[OrderNumber],[TextOverwrite],[Text],[FKBillSortOrder],[FKBillSortPeriod] FROM WareBillSortTypesXTab TABLOCKX

  DROP TABLE WareBillSortTypesXTab

  EXEC sp_rename Tmp_WareBillSortTypesXTab, WareBillSortTypesXTab
END
GO

IF NOT EXISTS
  (
    SELECT SysColumns.Name
    FROM SysObjects WITH (NOLOCK)
      INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID
      AND SysColumns.Name = 'ItemId'
      WHERE SysObjects.Name = 'WaregroupBillSortTypesXTab'
  )
BEGIN
  CREATE TABLE dbo.[Tmp_WareGroupBillSortTypesXTab] (
  [WaregroupNumber] Int NOT NULL,
  [SortTypeNumber] Int NOT NULL,
  [BillTypeNumber] Int NOT NULL,
  [OrderNumber] Int NOT NULL DEFAULT ((0)),
  [TextOverwrite] Bit NOT NULL DEFAULT ((0)),
  [Text] Char (50) NULL,
  [FKBillSortOrder] Int NULL,
  [FKBillSortPeriod] Int NULL DEFAULT ((1)),
  [GroupIndex] Int NULL,
  [ItemId] Int IDENTITY (1, 1) NOT NULL) ON [Primary]

  IF EXISTS(SELECT * FROM WareGroupBillSortTypesXTab)
    INSERT INTO dbo.Tmp_WareGroupBillSortTypesXTab ([WaregroupNumber],[BillTypeNumber],[SortTypeNumber],[OrderNumber],[TextOverwrite],[Text],[FKBillSortOrder],[FKBillSortPeriod],[GroupIndex])
    SELECT [WaregroupNumber],[BillTypeNumber],[SortTypeNumber],[OrderNumber],[TextOverwrite],[Text],[FKBillSortOrder],[FKBillSortPeriod],[GroupIndex] FROM WareGroupBillSortTypesXTab TABLOCKX

  DROP TABLE WaregroupBillSortTypesXTab

  EXEC sp_rename Tmp_WaregroupBillSortTypesXTab, WaregroupBillSortTypesXTab
END
GO
IF NOT EXISTS
  (
    SELECT SysColumns.Name
    FROM SysObjects WITH (NOLOCK)
      INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID
      AND SysColumns.Name = 'Status'
      WHERE SysObjects.Name = 'RecentlyUsedObjects'
  )
    ALTER TABLE RecentlyUsedObjects ADD Status INT NULL DEFAULT ((0))
GO
IF EXISTS
  (
    SELECT SysObjects.Name
    FROM SysObjects WITH (NOLOCK)
      WHERE SysObjects.Name = 'WareXTab'
  ) AND NOT EXISTS
  (
    SELECT SysColumns.Name
    FROM SysObjects WITH (NOLOCK)
      INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID
      AND SysColumns.Name = 'Pieces'
      WHERE SysObjects.Name = 'WareXTab'
  )
    ALTER TABLE WareXTab ADD Pieces INT NULL
GO
IF NOT EXISTS
(
  SELECT SysColumns.Name
  FROM SysObjects WITH (NOLOCK)
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID
    AND SysColumns.Name = 'BarWareColorBg'
    WHERE SysObjects.Name = 'Departments'
)
  ALTER TABLE Departments ADD BarWareColorBg BIT NULL
GO
IF NOT EXISTS
(
  SELECT SysColumns.Name
  FROM SysObjects WITH (NOLOCK)
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID
    AND SysColumns.Name = 'FkColor'
    WHERE SysObjects.Name = 'Waregroups'
)
BEGIN
  ALTER TABLE Waregroups ADD FkColor INT NULL
  ALTER TABLE [dbo].[Waregroups] WITH NOCHECK ADD CONSTRAINT [FK_Waregroups_Color] FOREIGN KEY([FkColor])
    REFERENCES [dbo].[Color] ([ColorId])
END
GO
