Files
MyClub/database/migrations/20250112000002_invoice_tables.up.sql
T
Tomas Dvorak dfc079288f hot fix #1
2026-01-26 08:13:18 +01:00

201 lines
6.0 KiB
SQL

-- Invoice management tables
-- Customers table
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
ico VARCHAR(20),
dic VARCHAR(20),
address TEXT,
city VARCHAR(100),
zip VARCHAR(10),
country VARCHAR(100) DEFAULT 'Česká republika',
email VARCHAR(255),
phone VARCHAR(50),
notes TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by INTEGER,
updated_by INTEGER
);
-- Invoices table
CREATE TABLE invoices (
id SERIAL PRIMARY KEY,
invoice_number VARCHAR(50) NOT NULL UNIQUE,
invoice_type VARCHAR(20) DEFAULT 'faktura',
variable_symbol VARCHAR(20),
constant_symbol VARCHAR(20),
specific_symbol VARCHAR(20),
issue_date TIMESTAMP NOT NULL,
due_date TIMESTAMP NOT NULL,
taxable_supply_date TIMESTAMP,
-- Supplier information (auto-filled from club settings)
supplier_name VARCHAR(255) NOT NULL,
supplier_ico VARCHAR(20),
supplier_dic VARCHAR(20),
supplier_address TEXT,
supplier_city VARCHAR(100),
supplier_zip VARCHAR(10),
supplier_country VARCHAR(100) DEFAULT 'Česká republika',
-- Supplier bank information
bank_name VARCHAR(255),
bank_account VARCHAR(50),
bank_iban VARCHAR(50),
bank_swift VARCHAR(20),
-- Customer information
customer_id INTEGER REFERENCES customers(id),
customer_name VARCHAR(255) NOT NULL,
customer_ico VARCHAR(20),
customer_dic VARCHAR(20),
customer_address TEXT,
customer_city VARCHAR(100),
customer_zip VARCHAR(10),
customer_country VARCHAR(100) DEFAULT 'Česká republika',
customer_email VARCHAR(255),
customer_phone VARCHAR(50),
-- Financial summary
subtotal_ex_vat DECIMAL(12,2) DEFAULT 0,
vat_amount DECIMAL(12,2) DEFAULT 0,
total_amount DECIMAL(12,2) NOT NULL,
paid_amount DECIMAL(12,2) DEFAULT 0,
-- Status and dates
status VARCHAR(50) DEFAULT 'draft',
payment_status VARCHAR(50) DEFAULT 'unpaid',
sent_at TIMESTAMP,
paid_at TIMESTAMP,
-- Notes and metadata
notes TEXT,
internal_notes TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by INTEGER NOT NULL,
updated_by INTEGER
);
-- Invoice items table
CREATE TABLE invoice_items (
id SERIAL PRIMARY KEY,
invoice_id INTEGER NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
description TEXT,
quantity DECIMAL(10,2) NOT NULL DEFAULT 1,
unit VARCHAR(50) DEFAULT 'ks',
unit_price DECIMAL(12,2) NOT NULL,
vat_rate DECIMAL(5,2) DEFAULT 21.00,
vat_amount DECIMAL(12,2),
total_price_ex_vat DECIMAL(12,2),
total_price_with_vat DECIMAL(12,2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Invoice payments table
CREATE TABLE invoice_payments (
id SERIAL PRIMARY KEY,
invoice_id INTEGER NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
amount DECIMAL(12,2) NOT NULL,
payment_date TIMESTAMP NOT NULL,
payment_method VARCHAR(100),
reference_number VARCHAR(255),
notes TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by INTEGER,
updated_by INTEGER
);
-- Invoice documents table
CREATE TABLE invoice_documents (
id SERIAL PRIMARY KEY,
invoice_id INTEGER NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
type VARCHAR(100),
file_name VARCHAR(500),
file_path VARCHAR(500),
file_size BIGINT,
mime_type VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by INTEGER,
updated_by INTEGER
);
-- Invoice settings table (singleton)
CREATE TABLE invoice_settings (
id SERIAL PRIMARY KEY,
-- Default supplier information
default_supplier_name VARCHAR(255),
default_supplier_ico VARCHAR(20),
default_supplier_dic VARCHAR(20),
default_supplier_address TEXT,
default_supplier_city VARCHAR(100),
default_supplier_zip VARCHAR(10),
default_supplier_country VARCHAR(100) DEFAULT 'Česká republika',
-- Default bank information
default_bank_name VARCHAR(255),
default_bank_account VARCHAR(50),
default_bank_iban VARCHAR(50),
default_bank_swift VARCHAR(20),
-- Invoice defaults
default_due_days INTEGER DEFAULT 14,
default_vat_rate DECIMAL(5,2) DEFAULT 21.00,
default_payment_method VARCHAR(100),
invoice_number_prefix VARCHAR(20) DEFAULT 'FV',
next_invoice_number INTEGER DEFAULT 1,
-- Email settings
email_subject VARCHAR(255),
email_body TEXT,
send_emails BOOLEAN DEFAULT false,
-- Other settings
currency VARCHAR(3) DEFAULT 'CZK',
language VARCHAR(10) DEFAULT 'cs',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by INTEGER,
updated_by INTEGER
);
-- Indexes for performance
CREATE INDEX idx_customers_ico ON customers(ico);
CREATE INDEX idx_customers_email ON customers(email);
CREATE INDEX idx_invoices_number ON invoices(invoice_number);
CREATE INDEX idx_invoices_status ON invoices(status);
CREATE INDEX idx_invoices_payment_status ON invoices(payment_status);
CREATE INDEX idx_invoices_customer_id ON invoices(customer_id);
CREATE INDEX idx_invoices_issue_date ON invoices(issue_date);
CREATE INDEX idx_invoices_due_date ON invoices(due_date);
CREATE INDEX idx_invoice_items_invoice_id ON invoice_items(invoice_id);
CREATE INDEX idx_invoice_payments_invoice_id ON invoice_payments(invoice_id);
CREATE INDEX idx_invoice_documents_invoice_id ON invoice_documents(invoice_id);
-- Insert default invoice settings
INSERT INTO invoice_settings (
default_supplier_name,
default_due_days,
default_vat_rate,
invoice_number_prefix,
next_invoice_number,
currency,
language
) VALUES (
'Fotbalový klub',
14,
21.00,
'FV',
1,
'CZK',
'cs'
);