IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE Id = object_id(N'[dbo].[InvoiceAutoSetting]') AND OBJECTPROPERTY(Id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[InvoiceAutoSetting]
  (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [PreBill] [bit] NULL,
    [NotificationDays] [int] NULL,
    [SendSMS] [bit] NULL,
    [InvoiceType] [smallint] NULL,
    [ArrivalCompDays] [int] NULL,
    [FK_RecurringPattern] [int] NULL,
    CONSTRAINT [PK_InvoiceAutoSetting] PRIMARY KEY CLUSTERED 
    (
      [Id] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[InvoiceAutoSetting]  WITH NOCHECK ADD  CONSTRAINT [FK_InvoiceAutoSetting_RecurringPattern] FOREIGN KEY([FK_RecurringPattern])
  REFERENCES [dbo].[RecurringPattern] ([Id])

  ALTER TABLE [dbo].[InvoiceAutoSetting] CHECK CONSTRAINT [FK_InvoiceAutoSetting_RecurringPattern]
END

IF NOT EXISTS
(
  SELECT SysColumns.Name 
  FROM SysObjects WITH (NOLOCK) 
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'FK_InvoiceAutoSettingId'
    WHERE SysObjects.Name = 'Customer'
)
BEGIN
  ALTER TABLE Customer ADD FK_InvoiceAutoSettingId INT NULL
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 = 'FK_InvoiceAutoSettingId'
    WHERE SysObjects.Name = 'Reservations'
)
BEGIN
  ALTER TABLE Reservations ADD FK_InvoiceAutoSettingId INT NULL
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 = 'FK_ReceiptLayoutId'
    WHERE SysObjects.Name = 'InvoiceAutoSetting'
)
BEGIN
  ALTER TABLE InvoiceAutoSetting ADD FK_ReceiptLayoutId INT NULL
END
GO