-- ==========================================
-- DESIGNER CONCEPTS DATABASE STRUCTURAL SCHEMA
-- ==========================================
CREATE DATABASE IF NOT EXISTS designer_concepts_erp DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE designer_concepts_erp;
-- 1. Branches Reference Table
CREATE TABLE branches (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE,
location VARCHAR(255) NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 2. System Users & Roles Matrix (RBAC)
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
full_name VARCHAR(150) NOT NULL,
role ENUM('Super Admin', 'Manager', 'Stock Controller', 'Sales', 'Tills', 'Finance') NOT NULL,
branch_id INT NULL,
FOREIGN KEY (branch_id) REFERENCES branches(id) ON DELETE SET NULL
);
-- 3. Core Products Table
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
sku VARCHAR(50) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
category VARCHAR(100) NOT NULL,
retail_price DECIMAL(10,2) NOT NULL,
trade_price DECIMAL(10,2) NOT NULL,
min_safety_level INT DEFAULT 5,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 4. Multi-Branch Inventory Ledger
CREATE TABLE inventory (
id INT AUTO_INCREMENT PRIMARY KEY,
branch_id INT NOT NULL,
product_id INT NOT NULL,
quantity_on_hand INT DEFAULT 0,
FOREIGN KEY (branch_id) REFERENCES branches(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
UNIQUE KEY branch_product_uq (branch_id, product_id)
);
-- 5. Inter-Branch Stock Transfers
CREATE TABLE stock_transfers (
id INT AUTO_INCREMENT PRIMARY KEY,
from_branch_id INT NOT NULL,
to_branch_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
status ENUM('Requested', 'Dispatched', 'Received', 'Cancelled') DEFAULT 'Requested',
raised_by INT NOT NULL,
approved_by INT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (from_branch_id) REFERENCES branches(id),
FOREIGN KEY (to_branch_id) REFERENCES branches(id),
FOREIGN KEY (product_id) REFERENCES products(id),
FOREIGN KEY (raised_by) REFERENCES users(id)
);
-- 6. Customers Table (Wholesale & Retail support)
CREATE TABLE customers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(150) NOT NULL,
phone VARCHAR(20) NOT NULL,
tier ENUM('Retail', 'Trade') DEFAULT 'Retail',
custom_discount_pct DECIMAL(5,2) DEFAULT 0.00,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 7. Document Ledgers (Quotes, Invoices, Orders)
CREATE TABLE documents (
id INT AUTO_INCREMENT PRIMARY KEY,
doc_number VARCHAR(50) NOT NULL UNIQUE,
branch_id INT NOT NULL,
customer_id INT NULL,
creator_id INT NOT NULL,
doc_type ENUM('Quote', 'Invoice', 'Credit Note', 'Purchase Order', 'Custom Order') NOT NULL,
subtotal DECIMAL(10,2) NOT NULL,
vat DECIMAL(10,2) NOT NULL,
grand_total DECIMAL(10,2) NOT NULL,
amount_paid DECIMAL(10,2) DEFAULT 0.00,
status VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (branch_id) REFERENCES branches(id),
FOREIGN KEY (customer_id) REFERENCES customers(id),
FOREIGN KEY (creator_id) REFERENCES users(id)
);
-- 8. Document Itemized Line Items
CREATE TABLE document_items (
id INT AUTO_INCREMENT PRIMARY KEY,
document_id INT NOT NULL,
product_id INT NULL,
product_name_override VARCHAR(255) NULL, -- Custom fabrication tracking
quantity INT NOT NULL,
unit_price DECIMAL(10,2) NOT NULL,
line_total DECIMAL(10,2) NOT NULL,
FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE SET NULL
);
-- 9. Custom Tailored Product Specifications
CREATE TABLE custom_order_details (
id INT AUTO_INCREMENT PRIMARY KEY,
document_id INT NOT NULL,
fabric_choice VARCHAR(150) NOT NULL,
custom_dimensions VARCHAR(255) NOT NULL,
production_lead_weeks INT DEFAULT 4,
production_status ENUM('Quote', 'Sent to Workshop', 'In Production', 'Ready', 'Completed') DEFAULT 'Quote',
FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
);
-- 10. Financial Payments & Split Gateway Log
CREATE TABLE payments (
id INT AUTO_INCREMENT PRIMARY KEY,
document_id INT NOT NULL,
amount DECIMAL(10,2) NOT NULL,
payment_method ENUM('Cash', 'Card', 'Payfast', 'Payflex', 'Laybuy', 'Split') NOT NULL,
gateway_txn_ref VARCHAR(100) NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
);
-- 11. Daily Shift Terminal Cashups (Z-Reports)
CREATE TABLE daily_cashups (
id INT AUTO_INCREMENT PRIMARY KEY,
branch_id INT NOT NULL,
user_id INT NOT NULL,
opening_float DECIMAL(10,2) NOT NULL,
actual_cash DECIMAL(10,2) NOT NULL,
actual_card DECIMAL(10,2) NOT NULL,
actual_payfast DECIMAL(10,2) NOT NULL,
actual_payflex DECIMAL(10,2) NOT NULL,
actual_laybuy DECIMAL(10,2) NOT NULL,
expected_totals DECIMAL(10,2) NOT NULL,
counted_totals DECIMAL(10,2) NOT NULL,
variance DECIMAL(10,2) NOT NULL,
signature_data TEXT NOT NULL, -- Base64 Canvas Drawing String
status ENUM('Approved', 'Flagged') DEFAULT 'Approved',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (branch_id) REFERENCES branches(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
-- 12. CRM Activity Tracker (Kanban Task cards)
CREATE TABLE crm_pipeline (
id INT AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(150) NOT NULL,
deal_name VARCHAR(255) NOT NULL,
expected_value DECIMAL(10,2) NOT NULL,
stage ENUM('Lead', 'Quote', 'Fabrication', 'Delivered') DEFAULT 'Lead',
assigned_user_id INT NULL,
notes TEXT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (assigned_user_id) REFERENCES users(id)
);