IF SCHEMA_ID(N'efimis-1.0') IS NULL EXEC(N'CREATE SCHEMA [efimis-1.0];');
GO


CREATE TABLE [efimis-1.0].[BillingGrade] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    CONSTRAINT [PK_BillingGrade] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[BillingRates] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    [RateSetName] nvarchar(max) NULL,
    [Rate] decimal(18,2) NOT NULL,
    [MatterId] nvarchar(max) NULL,
    [EmployeeId] nvarchar(max) NULL,
    [Memo] nvarchar(max) NULL,
    [EffectiveFrom] datetime2 NULL,
    [EffectiveTo] datetime2 NULL,
    CONSTRAINT [PK_BillingRates] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Clients] (
    [ImportId] nvarchar(450) NOT NULL,
    [EntityId] nvarchar(max) NULL,
    [State] int NOT NULL,
    [ClientNumber] nvarchar(max) NULL,
    [TaxCode] nvarchar(max) NULL,
    [BillingContactId] nvarchar(max) NULL,
    [BillingChannel] int NOT NULL,
    [MarketingChannel] nvarchar(max) NULL,
    [IntroducerId] nvarchar(max) NULL,
    [Notes] nvarchar(max) NULL,
    [AutoGenerateStatements] bit NOT NULL,
    [ReferrerEntityId] nvarchar(max) NULL,
    CONSTRAINT [PK_Clients] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[CmaAccounts] (
    [ImportId] nvarchar(450) NOT NULL,
    [DisplayName] nvarchar(max) NULL,
    [MatterId] nvarchar(max) NULL,
    [AccountType] int NOT NULL,
    [Description] nvarchar(max) NULL,
    [State] int NOT NULL,
    [Currency] nvarchar(max) NULL,
    [Balance] decimal(18,2) NOT NULL,
    [BankAccount_RoutingCode] nvarchar(max) NULL,
    [BankAccount_BranchCode] nvarchar(max) NULL,
    [BankAccount_Number] nvarchar(max) NULL,
    [BankAccount_Name] nvarchar(max) NULL,
    [BankAccount_BankName] nvarchar(max) NULL,
    [BankAccount_BranchName] nvarchar(max) NULL,
    [BankAccount_ImportId] nvarchar(max) NULL,
    [MaturesOn] datetime2 NULL,
    [DateOpened] datetime2 NULL,
    [InterestPayableFrom] datetime2 NULL,
    [Duration] int NULL,
    [DurationUnit] int NULL,
    [InterestPayablePeriod] int NULL,
    [InterestRate] decimal(18,2) NULL,
    [ExpiryAction] int NULL,
    [TaxFileNumber] nvarchar(max) NULL,
    [Beneficiaries] nvarchar(max) NULL,
    [InitialInvestment] decimal(18,2) NOT NULL,
    CONSTRAINT [PK_CmaAccounts] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[CmaTransactions] (
    [ImportId] nvarchar(450) NOT NULL,
    [AccountId] nvarchar(max) NULL,
    [Date] datetime2 NOT NULL,
    [OriginalDate] datetime2 NOT NULL,
    [OtherJournalId] nvarchar(max) NULL,
    [OtherAccountId] nvarchar(max) NULL,
    [OtherAccountName] nvarchar(max) NULL,
    [OtherEntityId] nvarchar(max) NULL,
    [OtherEntityName] nvarchar(max) NULL,
    [PaymentMethodDetail] nvarchar(max) NULL,
    [IsDebit] bit NOT NULL,
    [IsInterest] bit NOT NULL,
    [Reason] nvarchar(max) NULL,
    [Memo] nvarchar(max) NULL,
    [Reference] nvarchar(max) NULL,
    [Amount] decimal(18,2) NOT NULL,
    [AuthorisedBy] nvarchar(max) NULL,
    [State] int NOT NULL,
    [IsReversed] bit NOT NULL,
    [PaymentMethodID] nvarchar(max) NULL,
    [ReceiptMethodID] nvarchar(max) NULL,
    CONSTRAINT [PK_CmaTransactions] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Disbursements] (
    [ImportId] nvarchar(450) NOT NULL,
    [Type] int NOT NULL,
    [Code] nvarchar(max) NULL,
    [Date] datetime2 NOT NULL,
    [Reference] nvarchar(max) NULL,
    [SupplierReference] nvarchar(max) NULL,
    [Description] nvarchar(max) NULL,
    [MatterId] nvarchar(max) NULL,
    [SupplierId] nvarchar(max) NULL,
    [PurchaseId] nvarchar(max) NULL,
    [BillingAmount] decimal(18,2) NULL,
    [BillingAmountBase] decimal(18,2) NULL,
    [BillingTaxCode] nvarchar(max) NULL,
    [BillingTax] decimal(18,2) NOT NULL,
    [BillingTaxBase] decimal(18,2) NULL,
    [IsBilled] bit NOT NULL,
    [Amount] decimal(18,2) NOT NULL,
    [AmountBase] decimal(18,2) NULL,
    [TaxCode] nvarchar(max) NULL,
    [Tax] decimal(18,2) NOT NULL,
    [TaxBase] decimal(18,2) NULL,
    [Currency] nvarchar(max) NULL,
    [ClientBalanceAmount] decimal(18,2) NOT NULL,
    [ClientBalanceTax] decimal(18,2) NOT NULL,
    [SupplierBalanceAmountBase] decimal(18,2) NULL,
    [SupplierBalanceTaxBase] decimal(18,2) NULL,
    [UTBMSExpenseCode] nvarchar(max) NULL,
    [IsBillable] bit NOT NULL,
    CONSTRAINT [PK_Disbursements] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Divisions] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    [OfficeJournalId] nvarchar(max) NULL,
    [TrustJournalId] nvarchar(max) NULL,
    CONSTRAINT [PK_Divisions] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Employees] (
    [ImportId] nvarchar(450) NOT NULL,
    [EntityId] nvarchar(max) NULL,
    [TeamId] nvarchar(max) NULL,
    [JobTitleId] nvarchar(max) NULL,
    [BillingGradeId] nvarchar(max) NULL,
    [EmployeeNumber] nvarchar(max) NULL,
    [NominalCostPerHour] decimal(18,2) NOT NULL,
    [Notes] nvarchar(max) NULL,
    [Email] nvarchar(max) NULL,
    [State] int NOT NULL,
    CONSTRAINT [PK_Employees] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Entities] (
    [ImportId] nvarchar(450) NOT NULL,
    [EmployeeId] nvarchar(max) NULL,
    [SupplierId] nvarchar(max) NULL,
    [ClientId] nvarchar(max) NULL,
    [EntityType] int NOT NULL,
    [IsVerified] bit NOT NULL,
    [Currency] nvarchar(max) NULL,
    [Salutation] nvarchar(max) NULL,
    [FirstName] nvarchar(max) NULL,
    [MiddleNames] nvarchar(max) NULL,
    [LastName] nvarchar(max) NULL,
    [Initials] nvarchar(max) NULL,
    [OrganisationName] nvarchar(max) NULL,
    [OrganisationNumber] nvarchar(max) NULL,
    [OrganisationNumber2] nvarchar(max) NULL,
    [OrganisationType] nvarchar(max) NULL,
    [OrganisationContact] nvarchar(max) NULL,
    [PreferredName] nvarchar(max) NULL,
    [Title] nvarchar(max) NULL,
    [EntityNumber] nvarchar(max) NULL,
    [Email] nvarchar(max) NULL,
    [BankAccount_RoutingCode] nvarchar(max) NULL,
    [BankAccount_BranchCode] nvarchar(max) NULL,
    [BankAccount_Number] nvarchar(max) NULL,
    [BankAccount_Name] nvarchar(max) NULL,
    [BankAccount_BankName] nvarchar(max) NULL,
    [BankAccount_BranchName] nvarchar(max) NULL,
    [BankAccount_ImportId] nvarchar(max) NULL,
    [GroupMembers] nvarchar(max) NULL,
    CONSTRAINT [PK_Entities] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[HistoricalTransactions] (
    [ImportId] nvarchar(450) NOT NULL,
    [Type] int NOT NULL,
    [TransactionType] int NOT NULL,
    [JournalId] nvarchar(max) NULL,
    [JournalType] int NOT NULL,
    [AccountId] nvarchar(max) NULL,
    [Reference] nvarchar(max) NULL,
    [OtherPartyId] nvarchar(max) NULL,
    [OtherPartyName] nvarchar(max) NULL,
    [OtherPartyAddress] nvarchar(max) NULL,
    [Reason] nvarchar(max) NULL,
    [Date] datetime2 NOT NULL,
    [OriginalDate] datetime2 NOT NULL,
    [EnteredOn] datetimeoffset NULL,
    [Amount] decimal(18,2) NOT NULL,
    [Memo] nvarchar(max) NULL,
    [IsReconciled] bit NOT NULL,
    [ClearanceDate] datetime2 NULL,
    CONSTRAINT [PK_HistoricalTransactions] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Invoices] (
    [ImportId] nvarchar(450) NOT NULL,
    [MatterId] nvarchar(max) NULL,
    [ActingPersonId] nvarchar(max) NULL,
    [ResponsiblePersonId] nvarchar(max) NULL,
    [ClientId] nvarchar(max) NULL,
    [InvoiceNumber] nvarchar(max) NULL,
    [InvoiceDate] datetime2 NOT NULL,
    [DueDate] datetime2 NOT NULL,
    [SentDate] datetime2 NULL,
    [Currency] nvarchar(max) NULL,
    [ExchangeRate] decimal(18,2) NOT NULL,
    [TemplateId] nvarchar(max) NULL,
    [BillToEntityId] nvarchar(max) NULL,
    [BillingAddress] nvarchar(max) NULL,
    [Title] nvarchar(max) NULL,
    [Comments] nvarchar(max) NULL,
    [RetainedAmount] decimal(18,2) NULL,
    [TotalAmount] decimal(18,2) NOT NULL,
    [TotalTax] decimal(18,2) NOT NULL,
    [TotalAmountBase] decimal(18,2) NULL,
    [TotalTaxBase] decimal(18,2) NULL,
    [Balance] decimal(18,2) NOT NULL,
    [BalanceBase] decimal(18,2) NOT NULL,
    CONSTRAINT [PK_Invoices] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[JobTitle] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    CONSTRAINT [PK_JobTitle] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Journals] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    [Type] int NOT NULL,
    [IsDefault] bit NOT NULL,
    CONSTRAINT [PK_Journals] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[LedgerBalances] (
    [Id] int NOT NULL IDENTITY,
    [Type] int NOT NULL,
    [OwnerId] nvarchar(450) NOT NULL,
    [AccountID] nvarchar(max) NOT NULL,
    [Balance] decimal(18,2) NOT NULL,
    CONSTRAINT [PK_LedgerBalances] PRIMARY KEY ([Id])
);
GO


CREATE TABLE [efimis-1.0].[Matters] (
    [ImportId] nvarchar(450) NOT NULL,
    [State] int NOT NULL,
    [DivisionId] nvarchar(max) NULL,
    [ClientId] nvarchar(max) NULL,
    [ResponsiblePersonId] nvarchar(max) NULL,
    [ActingPersonId] nvarchar(max) NULL,
    [AssistingPersonId] nvarchar(max) NULL,
    [IntroducerId] nvarchar(max) NULL,
    [MatterTypeId] nvarchar(max) NULL,
    [MatterNumber] nvarchar(max) NULL,
    [Title] nvarchar(max) NULL,
    [SortTitle] nvarchar(max) NULL,
    [DateOpened] datetime2 NULL,
    [DateClosed] datetime2 NULL,
    [InvoiceEmailCC] nvarchar(max) NULL,
    [ExchangeRate] decimal(18,2) NULL,
    [BillingBasis] int NULL,
    [AutoGenerateStatements] bit NOT NULL,
    [FeeEstimate] decimal(18,2) NULL,
    [DisbursementEstimate] decimal(18,2) NULL,
    [CreditNoteBalance] decimal(18,2) NOT NULL,
    [OfficeFundsBalance] decimal(18,2) NOT NULL,
    [DateArchived] datetime2 NULL,
    [DateDestroyed] datetime2 NULL,
    [StorageLocation] nvarchar(max) NULL,
    [ArchiveNumber] nvarchar(max) NULL,
    [BoxNumber] nvarchar(max) NULL,
    [ArchivedById] nvarchar(max) NULL,
    [DestroyedById] nvarchar(max) NULL,
    CONSTRAINT [PK_Matters] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[MatterTypes] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    [Description] nvarchar(max) NULL,
    CONSTRAINT [PK_MatterTypes] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Purchases] (
    [ImportId] nvarchar(450) NOT NULL,
    [JournalId] nvarchar(max) NULL,
    [PurchaseNumber] nvarchar(max) NULL,
    [InvoiceNumber] nvarchar(max) NULL,
    [PaymentReference] nvarchar(max) NULL,
    [Date] datetime2 NULL,
    [DueDate] datetime2 NULL,
    [OriginalDate] datetime2 NULL,
    [SupplierId] nvarchar(max) NULL,
    [Currency] nvarchar(max) NULL,
    [ExchangeRate] decimal(18,2) NOT NULL,
    [SupplierComments] nvarchar(max) NULL,
    [TotalAmount] decimal(18,2) NOT NULL,
    [TotalTax] decimal(18,2) NOT NULL,
    [Balance] decimal(18,2) NOT NULL,
    [TotalAmountBase] decimal(18,2) NULL,
    [TotalTaxBase] decimal(18,2) NULL,
    [BalanceBase] decimal(18,2) NULL,
    [Description] nvarchar(max) NULL,
    CONSTRAINT [PK_Purchases] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Suppliers] (
    [ImportId] nvarchar(450) NOT NULL,
    [EntityId] nvarchar(max) NULL,
    [State] int NOT NULL,
    [SupplierNumber] nvarchar(max) NULL,
    [DefaultPaymentTerm] int NULL,
    [RecogniseTaxOnPayment] bit NOT NULL,
    [TaxCode] nvarchar(max) NULL,
    [Notes] nvarchar(max) NULL,
    CONSTRAINT [PK_Suppliers] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[TaskTypes] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    [Code] nvarchar(max) NULL,
    [Itemised] bit NOT NULL,
    [Billable] bit NOT NULL,
    [Narration] nvarchar(max) NULL,
    [Description] nvarchar(max) NULL,
    [Shortcut] nvarchar(max) NULL,
    CONSTRAINT [PK_TaskTypes] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Teams] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    CONSTRAINT [PK_Teams] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[WorkItems] (
    [ImportId] nvarchar(450) NOT NULL,
    [Title] nvarchar(max) NULL,
    [ItemType] int NOT NULL,
    [FileNote] nvarchar(max) NULL,
    [TaskTypeId] nvarchar(max) NULL,
    [EmployeeId] nvarchar(max) NULL,
    [EmployeeTeamId] nvarchar(max) NULL,
    [MatterId] nvarchar(max) NULL,
    [InvoiceId] nvarchar(max) NULL,
    [EntryDate] datetimeoffset NOT NULL,
    [DurationTicks] bigint NULL,
    [Units] decimal(18,2) NULL,
    [Rate] decimal(18,2) NULL,
    [Total] decimal(18,2) NULL,
    [TaxCode] nvarchar(max) NULL,
    [Tax] decimal(18,2) NULL,
    [State] int NOT NULL,
    CONSTRAINT [PK_WorkItems] PRIMARY KEY ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[CmaPaymentMethodDetails] (
    [Id] int NOT NULL IDENTITY,
    [Type] int NOT NULL,
    [CmaTransactionId] nvarchar(450) NOT NULL,
    [PaymentMethodId] nvarchar(50) NULL,
    [PaymentMethodType] int NULL,
    [GeneralDetails] nvarchar(max) NULL,
    CONSTRAINT [PK_CmaPaymentMethodDetails] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_CmaPaymentMethodDetails_CmaTransactions_CmaTransactionId] FOREIGN KEY ([CmaTransactionId]) REFERENCES [efimis-1.0].[CmaTransactions] ([ImportId]) ON DELETE CASCADE
);
GO


CREATE TABLE [efimis-1.0].[Addresses] (
    [ImportId] nvarchar(450) NOT NULL,
    [AttentionOf] nvarchar(max) NULL,
    [AddressType] int NOT NULL,
    [Street] nvarchar(max) NULL,
    [Locality] nvarchar(max) NULL,
    [Suburb] nvarchar(max) NULL,
    [State] nvarchar(max) NULL,
    [PostCode] nvarchar(max) NULL,
    [Country] nvarchar(max) NULL,
    [Default] bit NOT NULL,
    [EntityID] nvarchar(450) NULL,
    CONSTRAINT [PK_Addresses] PRIMARY KEY ([ImportId]),
    CONSTRAINT [FK_Addresses_Entities_EntityID] FOREIGN KEY ([EntityID]) REFERENCES [efimis-1.0].[Entities] ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Associations] (
    [ImportId] nvarchar(450) NOT NULL,
    [Type] nvarchar(max) NULL,
    [EntityId] nvarchar(max) NULL,
    [SourceEntity] nvarchar(450) NULL,
    CONSTRAINT [PK_Associations] PRIMARY KEY ([ImportId]),
    CONSTRAINT [FK_Associations_Entities_SourceEntity] FOREIGN KEY ([SourceEntity]) REFERENCES [efimis-1.0].[Entities] ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Numbers] (
    [ImportId] nvarchar(450) NOT NULL,
    [Type] int NOT NULL,
    [Value] nvarchar(max) NULL,
    [Default] bit NOT NULL,
    [EntityID] nvarchar(450) NULL,
    CONSTRAINT [PK_Numbers] PRIMARY KEY ([ImportId]),
    CONSTRAINT [FK_Numbers_Entities_EntityID] FOREIGN KEY ([EntityID]) REFERENCES [efimis-1.0].[Entities] ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[HistoricalTransactionSplits] (
    [ImportId] nvarchar(450) NOT NULL,
    [AccountId] nvarchar(max) NULL,
    [MatterId] nvarchar(max) NULL,
    [SupplierId] nvarchar(max) NULL,
    [InvoiceId] nvarchar(max) NULL,
    [LedgerType] int NULL,
    [Memo] nvarchar(max) NULL,
    [Amount] decimal(18,2) NOT NULL,
    [Tax] decimal(18,2) NOT NULL,
    [TaxCode] nvarchar(max) NULL,
    [TaxType] int NOT NULL,
    [HistoricalTransactionImportId] nvarchar(450) NULL,
    CONSTRAINT [PK_HistoricalTransactionSplits] PRIMARY KEY ([ImportId]),
    CONSTRAINT [FK_HistoricalTransactionSplits_HistoricalTransactions_HistoricalTransactionImportId] FOREIGN KEY ([HistoricalTransactionImportId]) REFERENCES [efimis-1.0].[HistoricalTransactions] ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[InvoiceItems] (
    [ImportId] nvarchar(450) NOT NULL,
    [InvoiceId] nvarchar(450) NULL,
    [Type] int NOT NULL,
    [DisbursementId] nvarchar(max) NULL,
    [Date] datetime2 NOT NULL,
    [Title] nvarchar(max) NULL,
    [Description] nvarchar(max) NULL,
    [BillerId] nvarchar(max) NULL,
    [Units] decimal(18,2) NOT NULL,
    [UnitPrice] decimal(18,2) NOT NULL,
    [BillingUnits] decimal(18,2) NULL,
    [BillingUnitPrice] decimal(18,2) NULL,
    [BillingAmount] decimal(18,2) NULL,
    [Amount] decimal(18,2) NOT NULL,
    [Tax] decimal(18,2) NOT NULL,
    [TaxCode] nvarchar(max) NULL,
    [SortOrder] int NULL,
    CONSTRAINT [PK_InvoiceItems] PRIMARY KEY ([ImportId]),
    CONSTRAINT [FK_InvoiceItems_Invoices_InvoiceId] FOREIGN KEY ([InvoiceId]) REFERENCES [efimis-1.0].[Invoices] ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Accounts] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    [JournalId] nvarchar(450) NULL,
    [Type] int NOT NULL,
    [AccountRoles] nvarchar(max) NULL,
    [AccountCategory] int NOT NULL,
    [AccountCode] nvarchar(max) NULL,
    [BankName] nvarchar(max) NULL,
    [BankBSB] nvarchar(max) NULL,
    [BankAccountNumber] nvarchar(max) NULL,
    [TaxCode] nvarchar(max) NULL,
    [Currency] nvarchar(max) NULL,
    [Balance] decimal(18,2) NOT NULL,
    CONSTRAINT [PK_Accounts] PRIMARY KEY ([ImportId]),
    CONSTRAINT [FK_Accounts_Journals_JournalId] FOREIGN KEY ([JournalId]) REFERENCES [efimis-1.0].[Journals] ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[CustomFieldDetails] (
    [ImportId] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NULL,
    [Value] nvarchar(max) NULL,
    [EntityImportId] nvarchar(450) NULL,
    [MatterImportId] nvarchar(450) NULL,
    CONSTRAINT [PK_CustomFieldDetails] PRIMARY KEY ([ImportId]),
    CONSTRAINT [FK_CustomFieldDetails_Entities_EntityImportId] FOREIGN KEY ([EntityImportId]) REFERENCES [efimis-1.0].[Entities] ([ImportId]),
    CONSTRAINT [FK_CustomFieldDetails_Matters_MatterImportId] FOREIGN KEY ([MatterImportId]) REFERENCES [efimis-1.0].[Matters] ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[Parties] (
    [ImportId] nvarchar(450) NOT NULL,
    [Role] nvarchar(max) NULL,
    [Reference] nvarchar(max) NULL,
    [Subject] nvarchar(max) NULL,
    [Memo] nvarchar(max) NULL,
    [EntityId] nvarchar(max) NULL,
    [Type] int NOT NULL,
    [MatterId] nvarchar(450) NULL,
    CONSTRAINT [PK_Parties] PRIMARY KEY ([ImportId]),
    CONSTRAINT [FK_Parties_Matters_MatterId] FOREIGN KEY ([MatterId]) REFERENCES [efimis-1.0].[Matters] ([ImportId])
);
GO


CREATE TABLE [efimis-1.0].[PurchaseItems] (
    [ImportId] nvarchar(450) NOT NULL,
    [PurchaseId] nvarchar(450) NULL,
    [Type] int NOT NULL,
    [DisbursementId] nvarchar(max) NULL,
    [AccountId] nvarchar(max) NULL,
    [Reference] nvarchar(max) NULL,
    [Title] nvarchar(max) NULL,
    [Description] nvarchar(max) NULL,
    [Amount] decimal(18,2) NOT NULL,
    [Tax] decimal(18,2) NOT NULL,
    [AmountBase] decimal(18,2) NULL,
    [TaxBase] decimal(18,2) NOT NULL,
    [TaxCode] nvarchar(max) NULL,
    CONSTRAINT [PK_PurchaseItems] PRIMARY KEY ([ImportId]),
    CONSTRAINT [FK_PurchaseItems_Purchases_PurchaseId] FOREIGN KEY ([PurchaseId]) REFERENCES [efimis-1.0].[Purchases] ([ImportId])
);
GO


CREATE INDEX [IX_Accounts_JournalId] ON [efimis-1.0].[Accounts] ([JournalId]);
GO


CREATE INDEX [IX_Addresses_EntityID] ON [efimis-1.0].[Addresses] ([EntityID]);
GO


CREATE INDEX [IX_Associations_SourceEntity] ON [efimis-1.0].[Associations] ([SourceEntity]);
GO


CREATE INDEX [IX_CmaPaymentMethodDetails_CmaTransactionId] ON [efimis-1.0].[CmaPaymentMethodDetails] ([CmaTransactionId]);
GO


CREATE UNIQUE INDEX [IX_CmaPaymentMethodDetails_Type_CmaTransactionId] ON [efimis-1.0].[CmaPaymentMethodDetails] ([Type], [CmaTransactionId]);
GO


CREATE INDEX [IX_CustomFieldDetails_EntityImportId] ON [efimis-1.0].[CustomFieldDetails] ([EntityImportId]);
GO


CREATE INDEX [IX_CustomFieldDetails_MatterImportId] ON [efimis-1.0].[CustomFieldDetails] ([MatterImportId]);
GO


CREATE INDEX [IX_HistoricalTransactionSplits_HistoricalTransactionImportId] ON [efimis-1.0].[HistoricalTransactionSplits] ([HistoricalTransactionImportId]);
GO


CREATE INDEX [IX_InvoiceItems_InvoiceId] ON [efimis-1.0].[InvoiceItems] ([InvoiceId]);
GO


CREATE UNIQUE INDEX [IX_LedgerBalances_Type_OwnerId] ON [efimis-1.0].[LedgerBalances] ([Type], [OwnerId]);
GO


CREATE INDEX [IX_Numbers_EntityID] ON [efimis-1.0].[Numbers] ([EntityID]);
GO


CREATE INDEX [IX_Parties_MatterId] ON [efimis-1.0].[Parties] ([MatterId]);
GO


CREATE INDEX [IX_PurchaseItems_PurchaseId] ON [efimis-1.0].[PurchaseItems] ([PurchaseId]);
GO


