39 lines
1.4 KiB
SQL
39 lines
1.4 KiB
SQL
-- Migration to add Invoice functionality
|
|
-- Run this on your existing database
|
|
|
|
-- Create invoices table
|
|
CREATE TABLE IF NOT EXISTS invoices (
|
|
id SERIAL PRIMARY KEY,
|
|
invoice_number VARCHAR(50) UNIQUE NOT NULL,
|
|
customer_id INTEGER REFERENCES customers(id),
|
|
invoice_date DATE NOT NULL,
|
|
terms VARCHAR(100) DEFAULT 'Net 30',
|
|
auth_code VARCHAR(255),
|
|
tax_exempt BOOLEAN DEFAULT FALSE,
|
|
tax_rate DECIMAL(5,2) DEFAULT 8.25,
|
|
subtotal DECIMAL(10,2) DEFAULT 0,
|
|
tax_amount DECIMAL(10,2) DEFAULT 0,
|
|
total DECIMAL(10,2) DEFAULT 0,
|
|
created_from_quote_id INTEGER REFERENCES quotes(id),
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Create invoice_items table
|
|
CREATE TABLE IF NOT EXISTS invoice_items (
|
|
id SERIAL PRIMARY KEY,
|
|
invoice_id INTEGER REFERENCES invoices(id) ON DELETE CASCADE,
|
|
quantity VARCHAR(20) NOT NULL,
|
|
description TEXT NOT NULL,
|
|
rate VARCHAR(50) NOT NULL,
|
|
amount VARCHAR(50) NOT NULL,
|
|
item_order INTEGER NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Create indexes
|
|
CREATE INDEX IF NOT EXISTS idx_invoices_invoice_number ON invoices(invoice_number);
|
|
CREATE INDEX IF NOT EXISTS idx_invoices_customer_id ON invoices(customer_id);
|
|
CREATE INDEX IF NOT EXISTS idx_invoice_items_invoice_id ON invoice_items(invoice_id);
|
|
CREATE INDEX IF NOT EXISTS idx_invoices_created_from_quote ON invoices(created_from_quote_id);
|