Lokasi:
/
home
/
u847703118
/
domains
/
alphamed.cloud
/
public_html
/
Ubah: update_db_v2.php
<?php require('db_connect.php'); require('includes/auth_session.php'); if (!userCanAccess('admin', ['admin'])) { die("Access Denied"); } try { // 1. Update Users Table (Roles) // Note: We can't easily ALTER ENUM in MySQL without redefining it. // We'll change it to VARCHAR to be more flexible or redefine the ENUM. // Let's redefine the ENUM to include new roles. $sql = "ALTER TABLE users MODIFY COLUMN role ENUM('admin', 'staff', 'doctor', 'med_tech', 'chief_med_tech', 'cashier', 'patient') DEFAULT 'staff'"; $pdo->exec($sql); echo "Updated users table roles.<br>"; // 2. Update Invoices Table $sql = "ALTER TABLE invoices ADD COLUMN card_holder_name VARCHAR(100) NULL, ADD COLUMN card_number VARCHAR(20) NULL, ADD COLUMN void_status TINYINT(1) DEFAULT 0, ADD COLUMN void_reason TEXT NULL, ADD COLUMN voided_by INT NULL, ADD COLUMN voided_at DATETIME NULL"; // Check if column exists before adding (simple way: try-catch or just suppress error if exists) // For this script, we'll just try to run it. If it fails, it might be because columns exist. // Better approach: Check information_schema, but for simplicity in this env, we'll assume it's a fresh update. try { $pdo->exec($sql); echo "Updated invoices table with card and void details.<br>"; } catch (PDOException $e) { echo "Invoices table update skipped (might already exist): " . $e->getMessage() . "<br>"; } // 3. Update Test Results Table $sql = "ALTER TABLE test_results MODIFY COLUMN status ENUM('Pending', 'For Validation', 'Completed') DEFAULT 'Pending', ADD COLUMN validated_by INT NULL, ADD COLUMN validated_at DATETIME NULL"; try { $pdo->exec($sql); echo "Updated test_results table with validation fields.<br>"; } catch (PDOException $e) { echo "Test results table update skipped: " . $e->getMessage() . "<br>"; } // 4. Create Transaction Logs Table $sql = "CREATE TABLE IF NOT EXISTS transaction_logs ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NULL, action VARCHAR(50) NOT NULL, description TEXT, ip_address VARCHAR(45), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; $pdo->exec($sql); echo "Created transaction_logs table.<br>"; // 5. Create Company Settings Table $sql = "CREATE TABLE IF NOT EXISTS company_settings ( id INT AUTO_INCREMENT PRIMARY KEY, company_name VARCHAR(100) DEFAULT 'Diagnostic Center', company_address TEXT, company_email VARCHAR(100), company_phone VARCHAR(50), company_logo VARCHAR(255), theme_color VARCHAR(20) DEFAULT '#800000', -- Light Maroon default created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; $pdo->exec($sql); echo "Created company_settings table.<br>"; // Insert Default Company Settings $stmt = $pdo->query("SELECT COUNT(*) FROM company_settings"); if ($stmt->fetchColumn() == 0) { $sql = "INSERT INTO company_settings (company_name, company_address, company_email, company_phone) VALUES ('My Diagnostic Center', '123 Health Blvd, Wellness City', 'info@diagcenter.com', '(02) 8123-4567')"; $pdo->exec($sql); echo "Inserted default company settings.<br>"; } // 6. Create Default Users for New Roles (for testing) $password = password_hash('password123', PASSWORD_DEFAULT); // Cashier $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = 'cashier1'"); $stmt->execute(); if ($stmt->fetchColumn() == 0) { $sql = "INSERT INTO users (username, password, full_name, role) VALUES ('cashier1', '$password', 'Cashier User', 'cashier')"; $pdo->exec($sql); echo "Created cashier1 user.<br>"; } // Med Tech $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = 'medtech1'"); $stmt->execute(); if ($stmt->fetchColumn() == 0) { $sql = "INSERT INTO users (username, password, full_name, role) VALUES ('medtech1', '$password', 'Med Tech User', 'med_tech')"; $pdo->exec($sql); echo "Created medtech1 user.<br>"; } // Chief Med Tech $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = 'chief1'"); $stmt->execute(); if ($stmt->fetchColumn() == 0) { $sql = "INSERT INTO users (username, password, full_name, role) VALUES ('chief1', '$password', 'Chief Med Tech', 'chief_med_tech')"; $pdo->exec($sql); echo "Created chief1 user.<br>"; } echo "Database update completed successfully."; } catch (PDOException $e) { echo "Error updating database: " . $e->getMessage(); } ?>
Batal