PDO::ERRMODE_EXCEPTION]
);
} catch (PDOException $e) {
die('
Database Connection Failed
Error: ' . htmlspecialchars($e->getMessage()) . '
Please check your database credentials and try again.
');
}
// Check if admin exists
try {
$stmt = $pdo->query("SELECT COUNT(*) FROM users WHERE role = 'admin'");
$adminCount = $stmt->fetchColumn();
if ($adminCount > 0) {
die('
Setup Already Complete
An admin user already exists. Please delete this file.
Go to Login
');
}
} catch (PDOException $e) {
// Table might not exist, continue
}
// Handle form submission
if ($_POST) {
$admin_username = trim($_POST['username'] ?? '');
$admin_email = trim($_POST['email'] ?? '');
$admin_name = trim($_POST['full_name'] ?? '');
$admin_password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
// Validation
if (empty($admin_username) || empty($admin_email) || empty($admin_name) || empty($admin_password)) {
$error = 'All fields are required.';
} elseif ($admin_password !== $confirm_password) {
$error = 'Passwords do not match.';
} elseif (strlen($admin_password) < 8) {
$error = 'Password must be at least 8 characters.';
} else {
try {
// Create tables first
createTables($pdo);
// Create admin user
$hashedPassword = password_hash($admin_password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (username, email, full_name, password, role, status) VALUES (?, ?, ?, ?, 'admin', 'active')");
$stmt->execute([$admin_username, $admin_email, $admin_name, $hashedPassword]);
$success = 'Admin user created successfully! You can now delete this file and login.';
} catch (PDOException $e) {
$error = 'Error creating admin user: ' . $e->getMessage();
}
}
}
function createTables($pdo) {
$tables = [
"CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
full_name VARCHAR(100) NOT NULL,
role ENUM('admin', 'manager', 'staff') DEFAULT 'staff',
phone VARCHAR(15),
status ENUM('active', 'inactive') DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS business_segments (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
status ENUM('active', 'inactive') DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS categories (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
type ENUM('expense', 'revenue') NOT NULL,
segment_id INT NULL,
description TEXT,
status ENUM('active', 'inactive') DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS sales (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
amount DECIMAL(12,2) NOT NULL,
quantity DECIMAL(10,2) DEFAULT 1,
unit_price DECIMAL(10,2) NULL,
category_id INT NULL,
segment_id INT NULL,
sale_date DATE NOT NULL,
customer_name VARCHAR(100) NULL,
customer_phone VARCHAR(15) NULL,
description TEXT,
invoice_number VARCHAR(50) NULL,
created_by INT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS expenses (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
amount DECIMAL(12,2) NOT NULL,
category_id INT NULL,
segment_id INT NULL,
expense_date DATE NOT NULL,
description TEXT,
vendor VARCHAR(200) NULL,
payment_method VARCHAR(50) NULL,
created_by INT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS investments (
id INT AUTO_INCREMENT PRIMARY KEY,
type ENUM('investment', 'loan') NOT NULL,
title VARCHAR(200) NOT NULL,
amount DECIMAL(12,2) NOT NULL,
interest_rate DECIMAL(5,2) DEFAULT 0,
start_date DATE NOT NULL,
end_date DATE NULL,
segment_id INT NULL,
description TEXT,
status ENUM('active', 'completed', 'cancelled') DEFAULT 'active',
created_by INT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS settings (
id INT AUTO_INCREMENT PRIMARY KEY,
setting_key VARCHAR(100) UNIQUE NOT NULL,
setting_value TEXT,
description TEXT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)"
];
foreach ($tables as $sql) {
$pdo->exec($sql);
}
// Insert default data
$pdo->exec("INSERT IGNORE INTO business_segments (id, name, description) VALUES
(1, 'Fish Sales', 'Fresh fish retail and wholesale'),
(2, 'Dry Fish Sales', 'Processed dry fish products'),
(3, 'Ornamental Fish Sales', 'Decorative fish for aquariums'),
(4, 'Fish Fry/Roast Shop', 'Cooked fish products'),
(5, 'Fish Cutting Service', 'Fish processing and cutting service')");
$pdo->exec("INSERT IGNORE INTO categories (id, name, type, segment_id, description) VALUES
(1, 'Fresh Fish Sales', 'revenue', 1, 'Revenue from fresh fish sales'),
(2, 'Dry Fish Sales', 'revenue', 2, 'Revenue from dry fish products'),
(3, 'Fish Purchase', 'expense', 1, 'Cost of purchasing fish'),
(4, 'Transportation', 'expense', NULL, 'Transportation costs'),
(5, 'Utilities', 'expense', NULL, 'Electricity, water, etc.')");
}
?>
Simple Setup - Kayal Aqua
🐟 Kayal Aqua Setup
Important: Delete this simple_setup.php file after setup is complete for security.