USE inventory_db; CREATE TABLE IF NOT EXISTS invoices ( id INT(11) AUTO_INCREMENT PRIMARY KEY, client_id INT(11) NOT NULL, invoice_number VARCHAR(50) NOT NULL UNIQUE, issue_date DATE NOT NULL, due_date DATE NOT NULL, status ENUM('draft', 'sent', 'paid', 'overdue', 'cancelled') DEFAULT 'draft', subtotal DECIMAL(10,2) NOT NULL DEFAULT 0.00, tax_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00, total_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00, notes TEXT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE RESTRICT, INDEX idx_client_status (client_id, status), INDEX idx_invoice_number (invoice_number), INDEX idx_due_date (due_date) ); CREATE TABLE IF NOT EXISTS invoice_items ( id INT(11) AUTO_INCREMENT PRIMARY KEY, invoice_id INT(11) NOT NULL, item_id INT(11) NOT NULL, description VARCHAR(255) NOT NULL, quantity DECIMAL(10,2) NOT NULL DEFAULT 1.00, unit_price DECIMAL(10,2) NOT NULL DEFAULT 0.00, line_total DECIMAL(10,2) NOT NULL DEFAULT 0.00, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE CASCADE, FOREIGN KEY (item_id) REFERENCES items(id) ON DELETE RESTRICT, INDEX idx_invoice (invoice_id), INDEX idx_item (item_id) ); CREATE TABLE IF NOT EXISTS payments ( id INT(11) AUTO_INCREMENT PRIMARY KEY, invoice_id INT(11) NULL, client_id INT(11) NOT NULL, payment_date DATE NOT NULL, amount DECIMAL(10,2) NOT NULL DEFAULT 0.00, payment_method ENUM('cash', 'check', 'credit_card', 'bank_transfer', 'other') DEFAULT 'cash', reference_number VARCHAR(50) NULL, notes TEXT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE SET NULL, FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE RESTRICT, INDEX idx_invoice (invoice_id), INDEX idx_client (client_id), INDEX idx_payment_date (payment_date) );