﻿CREATE TABLE [dbo].[fds__invoices] (
    [Id]                 VARCHAR (10)     NOT NULL,
    [Version]            INT              CONSTRAINT [DF_fds__invoices_Version] DEFAULT ((0)) NOT NULL,
    [InvoiceId]          NVARCHAR (25)    NULL,
    [InvoiceType]        CHAR (1)         NOT NULL,
    [InvoiceTitle]       VARCHAR (100)    CONSTRAINT [DF_fds__invoices_InvoiceTitle] DEFAULT ('Rechnung') NULL,
    [DocumentName]       VARCHAR (50)     NULL,
    [InvoiceBalance]     NUMERIC (10, 2)  NOT NULL,
    [InvoiceBalance_net] NUMERIC (10, 2)  NOT NULL,
    [InvoiceVAT_net1]    NUMERIC (10, 2)  NOT NULL,
    [InvoiceVAT_1]       NUMERIC (5, 2)   NOT NULL,
    [InvoiceVAT_net2]    NUMERIC (10, 2)  NULL,
    [InvoiceVAT_2]       NUMERIC (5, 2)   NULL,
    [PaymentTerm]        VARCHAR (5)      NOT NULL,
    [DueDate]            DATE             NOT NULL,
    [CustomerId]         BIGINT           NULL,
    [SendToAddress]      NVARCHAR (1000)  NULL,
    [SendToEmail]        NVARCHAR (255)   NULL,
    [ProvisionPeriod]    VARCHAR (50)     NULL,
    [ProvisionLocation]  NVARCHAR (1000)  NULL,
    [PaymentStatus]      CHAR (1)         NULL,
    [IsPayed]            BIT              CONSTRAINT [DF_fds__invoices_IsPayed] DEFAULT ((0)) NOT NULL,
    [IsSent]             BIT              CONSTRAINT [DF_fds__invoices_IsSent] DEFAULT ((0)) NOT NULL,
    [IsFinal]            AS               (CONVERT([bit],case when [DateFinalized] IS NULL then (0) else (1) end)),
    [IsCanceled]         AS               (CONVERT([bit],case when [DateCancelled] IS NULL then (0) else (1) end)),
    [Replaces_InvId]     VARCHAR (50)     NULL,
    [CustomValues]       NVARCHAR (MAX)   NULL,
    [SendToAddressJson]  NVARCHAR (MAX)   NULL,
    [DateSent]           DATETIME         NULL,
    [UserSent]           VARCHAR (25)     NULL,
    [DateFinalized]      DATETIME         NULL,
    [UserFinalized]      VARCHAR (25)     NULL,
    [DateCancelled]      DATETIME         NULL,
    [UserCancelled]      VARCHAR (25)     NULL,
    [DateCreated]        DATETIME         NOT NULL,
    [UserCreated]        VARCHAR (25)     NOT NULL,
    [DateModified]       DATETIME         NOT NULL,
    [UserModified]       VARCHAR (25)     NOT NULL,
    [ExternalId]         VARCHAR (25)     NULL,
    [IsExternal]         BIT              CONSTRAINT [DF_fds__invoices_external] DEFAULT ((0)) NULL,
    [file]               VARBINARY (MAX)  NULL,
    [file_guid]          UNIQUEIDENTIFIER CONSTRAINT [DF_fds__invoices_file_guid] DEFAULT (newid()) NOT NULL,
    CONSTRAINT [PK_fds__invoices] PRIMARY KEY CLUSTERED ([Id] ASC)
);


GO
CREATE NONCLUSTERED INDEX [fds__invoices__ix1]
    ON [dbo].[fds__invoices]([IsFinal] ASC, [InvoiceBalance] ASC)
    INCLUDE([IsPayed], [IsCanceled], [DateFinalized]);

