u
This commit is contained in:
@@ -1,34 +1,94 @@
|
||||
-- FibDash Database Schema
|
||||
-- Run these commands in your MSSQL database
|
||||
|
||||
-- Create Users table
|
||||
CREATE TABLE Users (
|
||||
-- Create Kreditor table
|
||||
CREATE TABLE Kreditor (
|
||||
id INT IDENTITY(1,1) PRIMARY KEY,
|
||||
google_id NVARCHAR(255) UNIQUE NOT NULL,
|
||||
email NVARCHAR(255) UNIQUE NOT NULL,
|
||||
iban NVARCHAR(34) NOT NULL,
|
||||
name NVARCHAR(255) NOT NULL,
|
||||
picture NVARCHAR(500),
|
||||
created_at DATETIME2 DEFAULT GETDATE(),
|
||||
last_login DATETIME2,
|
||||
is_active BIT DEFAULT 1
|
||||
kreditorId NVARCHAR(50) NOT NULL
|
||||
);
|
||||
|
||||
-- Create UserPreferences table
|
||||
CREATE TABLE UserPreferences (
|
||||
-- Ensure kreditorId is unique to support FK references
|
||||
ALTER TABLE Kreditor
|
||||
ADD CONSTRAINT UQ_Kreditor_kreditorId UNIQUE (kreditorId);
|
||||
|
||||
-- Create AccountingItems table
|
||||
-- Based on CSV structure: umsatz brutto, soll/haben kz, konto, gegenkonto, bu, buchungsdatum, rechnungsnummer, buchungstext, beleglink
|
||||
CREATE TABLE AccountingItems (
|
||||
id INT IDENTITY(1,1) PRIMARY KEY,
|
||||
user_id INT NOT NULL,
|
||||
theme NVARCHAR(50) DEFAULT 'light',
|
||||
language NVARCHAR(10) DEFAULT 'en',
|
||||
notifications_enabled BIT DEFAULT 1,
|
||||
created_at DATETIME2 DEFAULT GETDATE(),
|
||||
updated_at DATETIME2 DEFAULT GETDATE(),
|
||||
FOREIGN KEY (user_id) REFERENCES Users(id) ON DELETE CASCADE
|
||||
umsatz_brutto DECIMAL(15,2) NOT NULL, -- gross turnover amount
|
||||
soll_haben_kz CHAR(1) NOT NULL CHECK (soll_haben_kz IN ('S', 'H')), -- S = eingang (debit), H = ausgang (credit)
|
||||
konto NVARCHAR(10) NOT NULL, -- account (e.g. 5400 = wareneingang 19%)
|
||||
gegenkonto NVARCHAR(50) NOT NULL, -- counter account references Kreditor(kreditorId)
|
||||
bu NVARCHAR(10), -- tax code (9 = 19%vst, 8 = 7%vst, 506 = dienstleistung aus EU, 511 = dienstleistung ausserhalb EU)
|
||||
buchungsdatum DATE NOT NULL, -- booking date
|
||||
rechnungsnummer NVARCHAR(100), -- invoice number (belegfeld 1)
|
||||
buchungstext NVARCHAR(500), -- booking text (supplier/customer name + purpose)
|
||||
beleglink NVARCHAR(500) -- document link
|
||||
);
|
||||
|
||||
-- Create Konto table
|
||||
CREATE TABLE Konto (
|
||||
id INT IDENTITY(1,1) PRIMARY KEY,
|
||||
konto NVARCHAR(10) NOT NULL,
|
||||
name NVARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
-- Create BU table
|
||||
CREATE TABLE BU (
|
||||
id INT IDENTITY(1,1) PRIMARY KEY,
|
||||
bu NVARCHAR(10) NOT NULL
|
||||
name NVARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
/*
|
||||
CSV
|
||||
umsatz brutto ,
|
||||
soll / haben kz ( S = eingang, H = ausgang),
|
||||
,,,
|
||||
konto (XXXX , z.b. 5400 = wareneingang 19%),
|
||||
gegenkonto (70XXX),
|
||||
bu (9 = 19%vst , 8 = 7%vst, 506 = dienstleistung aus EU, 511 = dienstleistung ausserhalb EU),
|
||||
buchungsdatum, (MDD)
|
||||
rechnungsnummer (belegfeld 1),
|
||||
,,
|
||||
buchungstext (lierferantenname / kundenname , + verwendungszweck)
|
||||
,,,,,
|
||||
beleglink
|
||||
|
||||
|
||||
--
|
||||
nicht abziehbare vorstreuer buchen auf 5600
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
-- Create indexes for better performance
|
||||
CREATE INDEX IX_Users_Email ON Users(email);
|
||||
CREATE INDEX IX_Users_GoogleId ON Users(google_id);
|
||||
CREATE INDEX IX_UserPreferences_UserId ON UserPreferences(user_id);
|
||||
CREATE INDEX IX_Kreditor_IBAN ON Kreditor(iban);
|
||||
CREATE INDEX IX_Kreditor_KreditorId ON Kreditor(kreditorId);
|
||||
CREATE INDEX IX_AccountingItems_Buchungsdatum ON AccountingItems(buchungsdatum);
|
||||
CREATE INDEX IX_AccountingItems_Konto ON AccountingItems(konto);
|
||||
CREATE INDEX IX_AccountingItems_Rechnungsnummer ON AccountingItems(rechnungsnummer);
|
||||
CREATE INDEX IX_AccountingItems_SollHabenKz ON AccountingItems(soll_haben_kz);
|
||||
|
||||
-- Add FK from AccountingItems.bu -> BU(bu)
|
||||
ALTER TABLE AccountingItems
|
||||
ADD CONSTRAINT FK_AccountingItems_BU_BU
|
||||
FOREIGN KEY (bu) REFERENCES BU(bu);
|
||||
|
||||
-- Add FK from AccountingItems.gegenkonto -> Kreditor(kreditorId)
|
||||
ALTER TABLE AccountingItems
|
||||
ADD CONSTRAINT FK_AccountingItems_Gegenkonto_Kreditor
|
||||
FOREIGN KEY (gegenkonto) REFERENCES Kreditor(kreditorId);
|
||||
|
||||
-- Add FK from AccountingItems.konto -> Konto(konto)
|
||||
ALTER TABLE AccountingItems
|
||||
ADD CONSTRAINT FK_AccountingItems_Konto_Konto
|
||||
FOREIGN KEY (konto) REFERENCES Konto(konto);
|
||||
|
||||
-- Insert sample data (optional)
|
||||
-- Note: This will only work after you have real Google user data
|
||||
|
||||
Reference in New Issue
Block a user