IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OnlineReservationOrder]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[OnlineReservationOrder](
		[ReservationOrderId] [int] IDENTITY(1,1) NOT NULL,
		[ReferenceNumber] [int] NULL,
		[GuestNumber] [smallint] NULL,
		[DeliveryPlace] [nvarchar](100) NOT NULL,
		[DeliveryTime] [datetime] NULL,
		[CreateTime] [datetime] NOT NULL,
		[CreateTimeLocal] [datetime] NOT NULL,
		[FolioNumber] [int] NULL,
		[Comment] [nvarchar](MAX) NULL
	 CONSTRAINT [PK_OnlineReservationOrder] PRIMARY KEY CLUSTERED 
	(
		[ReservationOrderId] ASC
	) ON [PRIMARY]
	) ON [PRIMARY]

	CREATE NONCLUSTERED INDEX [IX_OnlineReservationOrder] ON [dbo].[OnlineReservationOrder]
	(
		[ReferenceNumber] ASC,
		[GuestNumber] ASC
	) ON [PRIMARY]

	ALTER TABLE [dbo].[OnlineReservationOrder]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrder_Reservations] FOREIGN KEY([ReferenceNumber], [GuestNumber])
	REFERENCES [dbo].[Reservations] ([ReferenceNumber], [GuestNumber])

	ALTER TABLE [dbo].[OnlineReservationOrder]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrder_Folios] FOREIGN KEY([FolioNumber])
	REFERENCES [dbo].[Folios] ([FolioNumber])

	ALTER TABLE [dbo].[OnlineReservationOrder] CHECK CONSTRAINT [FK_OnlineReservationOrder_Reservations]

END
ELSE
BEGIN
	IF NOT EXISTS
	(
	  SELECT SysColumns.Name 
	  FROM SysObjects WITH (NOLOCK) 
		INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
		AND SysColumns.Name = 'FolioNumber'
		WHERE SysObjects.Name = 'OnlineReservationOrder'
	)
	BEGIN
		ALTER TABLE [dbo].[OnlineReservationOrder] ALTER COLUMN [ReferenceNumber] [int] NULL
		ALTER TABLE [dbo].[OnlineReservationOrder] ALTER COLUMN [GuestNumber] [smallint] NULL
		ALTER TABLE [dbo].[OnlineReservationOrder] ADD [FolioNumber] [int] NULL
		ALTER TABLE [dbo].[OnlineReservationOrder]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrder_Folios] FOREIGN KEY([FolioNumber])
		REFERENCES [dbo].[Folios] ([FolioNumber])
	END
	IF NOT EXISTS
	(
	  SELECT SysColumns.Name 
	  FROM SysObjects WITH (NOLOCK) 
		INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
		AND SysColumns.Name = 'Comment'
		WHERE SysObjects.Name = 'OnlineReservationOrder'
	)
	BEGIN
		ALTER TABLE [dbo].[OnlineReservationOrder] ADD [Comment] [nvarchar](MAX) NULL
  END

END

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OnlineReservationOrderItem]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[OnlineReservationOrderItem](
		[ReservationOrderId] [int] NOT NULL,
		[TransactionNumber] [int] NOT NULL,
	 CONSTRAINT [PK_OnlineReservationOrderItem] PRIMARY KEY CLUSTERED 
	(
		[ReservationOrderId] ASC,
		[TransactionNumber] ASC
	) ON [PRIMARY]
	) ON [PRIMARY]
	ALTER TABLE [dbo].[OnlineReservationOrderItem]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrderItem_OnlineReservationOrder] FOREIGN KEY([ReservationOrderId])
	REFERENCES [dbo].[OnlineReservationOrder] ([ReservationOrderId])

	ALTER TABLE [dbo].[OnlineReservationOrderItem] CHECK CONSTRAINT [FK_OnlineReservationOrderItem_OnlineReservationOrder]

	ALTER TABLE [dbo].[OnlineReservationOrderItem]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrderItem_Transactions] FOREIGN KEY([TransactionNumber])
	REFERENCES [dbo].[Transactions] ([TransactionNumber])

	ALTER TABLE [dbo].[OnlineReservationOrderItem] CHECK CONSTRAINT [FK_OnlineReservationOrderItem_Transactions]
END

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_OnlineReservationOrderArrangementLine_ArrangementLines]') AND parent_object_id = OBJECT_ID(N'[dbo].[OnlineReservationOrderArrangementLine]'))
ALTER TABLE [dbo].[OnlineReservationOrderArrangementLine] DROP CONSTRAINT [FK_OnlineReservationOrderArrangementLine_ArrangementLines]

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OnlineReservationOrderArrangementLine]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[OnlineReservationOrderArrangementLine](
		[ReservationOrderId] [int] NOT NULL,
		[ArrangementLineNumber] [int] NOT NULL,
	 CONSTRAINT [PK_OnlineReservationOrderArrangementLine] PRIMARY KEY CLUSTERED 
	(
		[ReservationOrderId] ASC,
		[ArrangementLineNumber] ASC
	) ON [PRIMARY]
	) ON [PRIMARY]
	ALTER TABLE [dbo].[OnlineReservationOrderArrangementLine]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrderArrangementLine_OnlineReservationOrder] FOREIGN KEY([ReservationOrderId])
	REFERENCES [dbo].[OnlineReservationOrder] ([ReservationOrderId])

	ALTER TABLE [dbo].[OnlineReservationOrderArrangementLine] CHECK CONSTRAINT [FK_OnlineReservationOrderArrangementLine_OnlineReservationOrder]
END

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_OnlineReservationOrderArrangementLine_ArrangementLines2]') AND parent_object_id = OBJECT_ID(N'[dbo].[OnlineReservationOrderArrangementLine]'))
ALTER TABLE [dbo].[OnlineReservationOrderArrangementLine] DROP CONSTRAINT [FK_OnlineReservationOrderArrangementLine_ArrangementLines2]

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OnlineReservationOrderTempTicket]') AND type in (N'U'))
BEGIN
  CREATE TABLE [dbo].[OnlineReservationOrderTempTicket](
	  [ReservationOrderId] [int] NOT NULL,
	  [WareNumber] [int] NOT NULL,
	  [Indx] [int] NOT NULL,
	  [Firstname] [varchar](50) NULL,
	  [Lastname] [varchar](50) NULL,
	  [Phone] [varchar](50) NULL,
	  [ImageContentType] [varchar](50) NULL,
	  [Image] [varbinary](max) NULL,
    [DateOfBirth] [date] NULL
   CONSTRAINT [PK_OnlineReservationOrderTempTicket] PRIMARY KEY CLUSTERED 
  (
	  [ReservationOrderId] ASC,
	  [WareNumber] ASC,
	  [Indx] ASC
  ) ON [PRIMARY]
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

  ALTER TABLE [dbo].[OnlineReservationOrderTempTicket]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrderTempTicket_OnlineReservationOrder] FOREIGN KEY([ReservationOrderId])
  REFERENCES [dbo].[OnlineReservationOrder] ([ReservationOrderId])
  ON DELETE CASCADE

  ALTER TABLE [dbo].[OnlineReservationOrderTempTicket] CHECK CONSTRAINT [FK_OnlineReservationOrderTempTicket_OnlineReservationOrder]

  ALTER TABLE [dbo].[OnlineReservationOrderTempTicket]  WITH CHECK ADD  CONSTRAINT [FK_OnlineReservationOrderTempTicket_WareTicket] FOREIGN KEY([WareNumber])
  REFERENCES [dbo].[Ware] ([WareNumber])
  ON DELETE CASCADE

  ALTER TABLE [dbo].[OnlineReservationOrderTempTicket] CHECK CONSTRAINT [FK_OnlineReservationOrderTempTicket_WareTicket]

END
ELSE
BEGIN
	IF NOT EXISTS
	(
	  SELECT *
	  FROM SysObjects WITH (NOLOCK) 
		INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
		AND SysColumns.Name = 'DateOfBirth'
		WHERE SysObjects.Name = 'OnlineReservationOrderTempTicket'
	)
	BEGIN
		ALTER TABLE [dbo].[OnlineReservationOrderTempTicket] ADD [DateOfBirth] [date] NULL
  END
END
