SET NOCOUNT ON

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SubscriptionOnlineInitialPayment]') AND type in (N'U'))
BEGIN
  CREATE TABLE [dbo].[SubscriptionOnlineInitialPayment]
  (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FK_ArrangementLineUniqueID] [int] NOT NULL,
    [Active] [bit] NULL,
    [Comment] [varchar](500) NULL,
    [Currency] [varchar](10) NULL,
    [DateAndTime] [datetime] NULL,
    [InitialAmount] [money] NULL,
    [PaymentProvider] [varchar](100) NULL,
    [PaymentStatus] [varchar](100) NULL,
    [ResultText] [varchar](100) NULL,
    [PaymentComplete] [bit] NULL,
    CONSTRAINT [PK_SubscriptionOnlineInitialPayment] PRIMARY KEY CLUSTERED 
    (
      [Id] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[SubscriptionOnlineInitialPayment]  WITH NOCHECK ADD  CONSTRAINT [FK_SubscriptionOnlineInitialPayment_ArrangementLines] FOREIGN KEY([FK_ArrangementLineUniqueID])
  REFERENCES [dbo].[ArrangementLines] ([ArrangementLineUniqueID])

  ALTER TABLE [dbo].[SubscriptionOnlineInitialPayment] NOCHECK CONSTRAINT [FK_SubscriptionOnlineInitialPayment_ArrangementLines]
END

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SubscriptionOnlineAgreementPayment]') AND type in (N'U'))
BEGIN
  CREATE TABLE [dbo].[SubscriptionOnlineAgreementPayment]
  (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FK_SubscriptionOnlineInitialPaymentId] [int] NOT NULL,
    [Amount] [money] NULL,
    [Currency] [varchar](10) NULL,
    [DateAndTime] [datetime] NULL,
    [PaymentId] [varchar](100) NULL,
    [PaymentProvider] [varchar](100) NULL,
    [PaymentStatus] [varchar](100) NULL,
    [PaymentComplete] [bit] NULL,
    CONSTRAINT [PK_SubscriptionOnlineAgreementPayment] PRIMARY KEY CLUSTERED 
    (
      [Id] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[SubscriptionOnlineAgreementPayment]  WITH CHECK ADD  CONSTRAINT [FK_SubscriptionOnlineAgreementPayment_SubscriptionOnlineInitialPayment] FOREIGN KEY([FK_SubscriptionOnlineInitialPaymentId])
  REFERENCES [dbo].[SubscriptionOnlineInitialPayment] ([Id])

  ALTER TABLE [dbo].[SubscriptionOnlineAgreementPayment] CHECK CONSTRAINT [FK_SubscriptionOnlineAgreementPayment_SubscriptionOnlineInitialPayment]
END