CREATE TABLE [dbo].[acc_invoices]
(
[inv_invoice_id] [int] NOT NULL,
[inv_policy_situation_id] [int] NOT NULL,
[inv_start_date] [date] NOT NULL,
[inv_end_date] [date] NOT NULL,
[inv_ratio] [dbo].[ais_decimal4] NULL,
[inv_operation_code__last] [dbo].[ais_code_short] NOT NULL,
[inv_cr_date] [date] NOT NULL,
[inv_db_date] [date] NULL,
[inv_an_date] [date] NULL,
[inv_ec_date] [date] NULL,
[inv_rmb_date] [date] NULL,
[inv_lettering] [int] NULL,
[inv_type_code__cts] [dbo].[ais_code_short] NULL,
[inv_cts_fee] [dbo].[ais_decimal2] NULL,
[inv_cts_date] [date] NULL,
[inv_awr_date] [date] NULL,
[inv_inf_date] [date] NULL,
[inv_monthly_payment_status_code] [dbo].[ais_code_short] NULL,
[inv_monthly_payment_reject_count] [tinyint] NULL,
[inv_monthly_payment_date] [date] NULL,
[inv_observations] [nvarchar] (256) COLLATE French_CI_AS NULL,
[inv_taxes_manual_input] [bit] NOT NULL CONSTRAINT [DF__acc_invoices__inv_taxes_manual_input] DEFAULT ((0))
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[acc_invoices] ADD CONSTRAINT [CHK_acc_invoices_dates_db_ec] CHECK (([inv_ec_date] IS NULL OR [inv_db_date] IS NOT NULL))
GO
ALTER TABLE [dbo].[acc_invoices] ADD CONSTRAINT [CK_acc_invoices_dates_db_rmb] CHECK (([inv_rmb_date] IS NULL OR [inv_db_date] IS NOT NULL))
GO
ALTER TABLE [dbo].[acc_invoices] ADD CONSTRAINT [CK_acc_invoices_start_end_dates] CHECK (([inv_start_date]<=[inv_end_date]))
GO
ALTER TABLE [dbo].[acc_invoices] ADD CONSTRAINT [PK_acc_invoices] PRIMARY KEY CLUSTERED  ([inv_invoice_id]) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_acc_invoices__invoice_id__policy_situation_id] ON [dbo].[acc_invoices] ([inv_invoice_id], [inv_policy_situation_id]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[acc_invoices] ADD CONSTRAINT [FK_acc_invoices_acc_accounts] FOREIGN KEY ([inv_invoice_id]) REFERENCES [dbo].[acc_accounts] ([a_account_id])
GO
ALTER TABLE [dbo].[acc_invoices] ADD CONSTRAINT [FK_acc_invoices_acc_cts_types1] FOREIGN KEY ([inv_type_code__cts]) REFERENCES [dbo].[acc_cts_types] ([ctstyp_type_code])
GO
ALTER TABLE [dbo].[acc_invoices] ADD CONSTRAINT [FK_acc_invoices_nm_invoices_nm_monthly_payments_statuses] FOREIGN KEY ([inv_monthly_payment_status_code]) REFERENCES [dbo].[nm_invoices_nm_monthly_payments_statuses] ([nminmps_monthly_payment_status_code])
GO
ALTER TABLE [dbo].[acc_invoices] ADD CONSTRAINT [FK_acc_invoices_nm_invoices_operations] FOREIGN KEY ([inv_operation_code__last]) REFERENCES [dbo].[nm_invoices_operations] ([op_operation_code])
GO
ALTER TABLE [dbo].[acc_invoices] ADD CONSTRAINT [FK_acc_invoices_pol_policies_situations] FOREIGN KEY ([inv_policy_situation_id]) REFERENCES [dbo].[pol_policies_situations] ([pols_policy_situation_id])
GO
