35464-vm/db/setup.php
2025-11-04 16:06:10 +00:00

101 lines
3.9 KiB
PHP

<?php
// Simple, idempotent script to create necessary tables.
// This should be run once manually or via a simple admin panel.
require_once __DIR__ . '/config.php';
try {
// Connect without DB to create it if it doesn't exist
$pdo_admin = new PDO('mysql:host='.DB_HOST.';charset=utf8mb4', DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$pdo_admin->exec("CREATE DATABASE IF NOT EXISTS `".DB_NAME."`");
// Now connect to the app DB
$pdo = db();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statements = [
// Users table for login
"CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
role ENUM('maker', 'approver') NOT NULL,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);",
// Payroll batches table
"CREATE TABLE IF NOT EXISTS payrolls (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
fileName VARCHAR(255) NOT NULL,
status ENUM('pending', 'approved', 'processing', 'delivered', 'failed') NOT NULL DEFAULT 'pending',
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
createdBy INT,
approvedAt TIMESTAMP NULL,
approvedBy INT,
FOREIGN KEY (createdBy) REFERENCES users(id),
FOREIGN KEY (approvedBy) REFERENCES users(id)
);",
// Payroll details (individual records from CSV)
"CREATE TABLE IF NOT EXISTS payroll_details (
id INT AUTO_INCREMENT PRIMARY KEY,
payroll_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
accountNumber VARCHAR(50) NOT NULL,
accountName VARCHAR(255) NOT NULL,
amount DECIMAL(15, 2) NOT NULL,
status ENUM('pending', 'processing', 'delivered', 'failed') NOT NULL DEFAULT 'pending',
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
createdBy INT,
approvedAt TIMESTAMP NULL,
approvedBy INT,
api_response TEXT,
FOREIGN KEY (payroll_id) REFERENCES payrolls(id) ON DELETE CASCADE,
FOREIGN KEY (createdBy) REFERENCES users(id),
FOREIGN KEY (approvedBy) REFERENCES users(id)
);",
// API logs
"CREATE TABLE IF NOT EXISTS api_logs (
id INT AUTO_INCREMENT PRIMARY KEY,
payroll_detail_id INT,
request_payload TEXT,
response_payload TEXT,
status_code INT,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (payroll_detail_id) REFERENCES payroll_details(id) ON DELETE SET NULL
);"
];
foreach ($statements as $statement) {
$pdo->exec($statement);
}
echo "Database and tables created or already exist successfully." . PHP_EOL;
// --- User Seeding ---
// To ensure a clean state, we'll remove existing dummy users and recreate them.
// This makes the script safe to re-run.
$pdo->exec("DELETE FROM users WHERE username IN ('maker', 'approver')");
// Use prepared statements for security and reliability
$stmt = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (:username, :password, :role)");
$users_to_seed = [
['username' => 'maker', 'password' => password_hash('password123', PASSWORD_DEFAULT), 'role' => 'maker'],
['username' => 'approver', 'password' => password_hash('password123', PASSWORD_DEFAULT), 'role' => 'approver']
];
foreach ($users_to_seed as $user) {
$stmt->execute($user);
}
echo "Dummy users 'maker' and 'approver' created/reset successfully with password 'password123'." . PHP_EOL;
} catch (PDOException $e) {
die("Database setup failed: " . $e->getMessage());
}