sadiq
This commit is contained in:
parent
980e61a17b
commit
b2346417d7
@ -9,11 +9,11 @@ $userId = $_SESSION['user_id'];
|
||||
// Get Installments
|
||||
$installments = $pdo->prepare("
|
||||
SELECT i.*, c.brand, c.model
|
||||
FROM installments i
|
||||
JOIN sales s ON i.sale_id = s.id
|
||||
JOIN cars c ON s.car_id = c.id
|
||||
WHERE s.buyer_id = ?
|
||||
");
|
||||
FROM installments i
|
||||
JOIN sales s ON i.sale_id = s.id
|
||||
JOIN cars c ON s.car_id = c.id
|
||||
WHERE s.user_id = ?
|
||||
");
|
||||
$installments->execute([$userId]);
|
||||
$myInstallments = $installments->fetchAll();
|
||||
?>
|
||||
|
||||
1
db/setup_done.flag
Normal file
1
db/setup_done.flag
Normal file
@ -0,0 +1 @@
|
||||
2026-02-17 08:38:35
|
||||
@ -11,7 +11,7 @@ $myCars = $pdo->prepare("SELECT COUNT(*) FROM cars WHERE dealer_id = ?");
|
||||
$myCars->execute([$dealerId]);
|
||||
$myCarsCount = $myCars->fetchColumn();
|
||||
|
||||
$mySales = $pdo->prepare("SELECT COUNT(*) FROM sales WHERE seller_id = ?");
|
||||
$mySales = $pdo->prepare("SELECT COUNT(*) FROM sales s JOIN cars c ON s.car_id = c.id WHERE c.dealer_id = ?");
|
||||
$mySales->execute([$dealerId]);
|
||||
$mySalesCount = $mySales->fetchColumn();
|
||||
?>
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
require_once __DIR__ . '/../db/config.php';
|
||||
require_once __DIR__ . '/init_db.php';
|
||||
ensure_db_setup();
|
||||
require_once __DIR__ . '/auth.php'; // Ensure session is started and auth helpers are available
|
||||
|
||||
$projectTitle = "AFG_CARS - Supreme Automotive";
|
||||
|
||||
189
includes/init_db.php
Normal file
189
includes/init_db.php
Normal file
@ -0,0 +1,189 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../db/config.php';
|
||||
|
||||
function ensure_db_setup() {
|
||||
$flagFile = __DIR__ . '/../db/setup_done.flag';
|
||||
if (file_exists($flagFile)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = db();
|
||||
|
||||
// Schema Creation (IF NOT EXISTS to be safe)
|
||||
$db->exec("SET FOREIGN_KEY_CHECKS = 0");
|
||||
|
||||
// Branches
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS branches (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
city VARCHAR(100) NOT NULL,
|
||||
address VARCHAR(255),
|
||||
phone VARCHAR(50),
|
||||
hours VARCHAR(100)
|
||||
)");
|
||||
|
||||
// Users
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(100) UNIQUE NOT NULL,
|
||||
email VARCHAR(100) UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
role ENUM('Guest', 'Customer', 'Dealer', 'Employee', 'Manager', 'Admin', 'Super Admin') DEFAULT 'Customer',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)");
|
||||
|
||||
// Cars
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS cars (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
vin VARCHAR(50) UNIQUE NOT NULL,
|
||||
brand VARCHAR(100) NOT NULL,
|
||||
model VARCHAR(100) NOT NULL,
|
||||
year INT NOT NULL,
|
||||
price DECIMAL(15, 2) NOT NULL,
|
||||
mileage INT NOT NULL,
|
||||
transmission VARCHAR(50),
|
||||
fuel_type VARCHAR(50),
|
||||
status ENUM('Available', 'Reserved', 'Sold') DEFAULT 'Available',
|
||||
branch_id INT,
|
||||
dealer_id INT DEFAULT NULL,
|
||||
installment_available BOOLEAN DEFAULT 0,
|
||||
is_featured BOOLEAN DEFAULT 0,
|
||||
image_url VARCHAR(255),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (branch_id) REFERENCES branches(id),
|
||||
FOREIGN KEY (dealer_id) REFERENCES users(id)
|
||||
)");
|
||||
|
||||
// Car Images
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS car_images (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
car_id INT NOT NULL,
|
||||
image_path VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE
|
||||
)");
|
||||
|
||||
// Reviews
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS reviews (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
car_id INT NOT NULL,
|
||||
user_id INT NOT NULL,
|
||||
rating INT NOT NULL CHECK (rating >= 1 AND rating <= 5),
|
||||
comment TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
)");
|
||||
|
||||
// Sales
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS sales (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT NOT NULL,
|
||||
car_id INT NOT NULL,
|
||||
amount DECIMAL(15, 2) NOT NULL,
|
||||
sale_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
status ENUM('Pending', 'Completed', 'Cancelled') DEFAULT 'Pending',
|
||||
FOREIGN KEY (user_id) REFERENCES users(id),
|
||||
FOREIGN KEY (car_id) REFERENCES cars(id)
|
||||
)");
|
||||
|
||||
// Installments
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS installments (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
sale_id INT NOT NULL,
|
||||
total_amount DECIMAL(15, 2) NOT NULL,
|
||||
paid_amount DECIMAL(15, 2) DEFAULT 0,
|
||||
monthly_payment DECIMAL(15, 2) NOT NULL,
|
||||
status ENUM('Active', 'Completed', 'Overdue') DEFAULT 'Active',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (sale_id) REFERENCES sales(id) ON DELETE CASCADE
|
||||
)");
|
||||
|
||||
// Activity Logs
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS activity_logs (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT,
|
||||
action VARCHAR(255) NOT NULL,
|
||||
ip_address VARCHAR(50),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
|
||||
)");
|
||||
|
||||
// Notifications
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS notifications (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
is_read BOOLEAN DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
)");
|
||||
|
||||
$db->exec("SET FOREIGN_KEY_CHECKS = 1");
|
||||
|
||||
// Seeding (Only if empty)
|
||||
$stmt = $db->query("SELECT COUNT(*) FROM branches");
|
||||
if ($stmt->fetchColumn() == 0) {
|
||||
$branches = [
|
||||
['Kabul Main', 'Kabul', 'Shar-e-Naw, Kabul', '+93 700 111 222', '08:00 AM - 06:00 PM'],
|
||||
['Herat Branch', 'Herat', 'Main Road, Herat', '+93 700 333 444', '08:30 AM - 05:30 PM'],
|
||||
['Mazar Center', 'Mazar-i-Sharif', 'Balkh Street, Mazar', '+93 700 555 666', '08:00 AM - 05:00 PM'],
|
||||
['Kandahar Hub', 'Kandahar', 'Airport Road, Kandahar', '+93 700 777 888', '09:00 AM - 04:00 PM']
|
||||
];
|
||||
$stmt = $db->prepare("INSERT INTO branches (name, city, address, phone, hours) VALUES (?, ?, ?, ?, ?)");
|
||||
foreach ($branches as $branch) {
|
||||
$stmt->execute($branch);
|
||||
}
|
||||
}
|
||||
|
||||
$stmt = $db->query("SELECT COUNT(*) FROM cars");
|
||||
if ($stmt->fetchColumn() == 0) {
|
||||
$brands = ['Toyota', 'Lexus', 'Mercedes-Benz', 'BMW', 'Audi', 'Land Rover', 'Porsche', 'Tesla'];
|
||||
$models = [
|
||||
'Toyota' => ['Camry', 'Land Cruiser', 'Corolla', 'RAV4'],
|
||||
'Lexus' => ['LX 600', 'RX 350', 'ES 350'],
|
||||
'Mercedes-Benz' => ['S-Class', 'G-Wagon', 'E-Class'],
|
||||
'BMW' => ['X7', 'X5', '7 Series'],
|
||||
'Audi' => ['Q8', 'A8', 'RS7'],
|
||||
'Land Rover' => ['Defender', 'Range Rover'],
|
||||
'Porsche' => ['911 Carrera', 'Cayenne'],
|
||||
'Tesla' => ['Model S', 'Model X']
|
||||
];
|
||||
|
||||
$stmt = $db->prepare("INSERT INTO cars (vin, brand, model, year, price, mileage, transmission, fuel_type, branch_id, is_featured, image_url, installment_available) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
|
||||
for ($i = 1; $i <= 20; $i++) {
|
||||
$brand = $brands[array_rand($brands)];
|
||||
$model = $models[$brand][array_rand($models[$brand])];
|
||||
$year = rand(2020, 2024);
|
||||
$price = rand(45000, 180000);
|
||||
$mileage = rand(0, 15000);
|
||||
$branch_id = rand(1, 4);
|
||||
$is_featured = ($i <= 8) ? 1 : 0;
|
||||
$installment_available = rand(0, 1);
|
||||
$image_url = "assets/images/cars/car{$i}.jpg";
|
||||
$vin = "VIN" . str_pad((string)$i, 10, "0", STR_PAD_LEFT);
|
||||
|
||||
$stmt->execute([
|
||||
$vin, $brand, $model, $year, $price, $mileage,
|
||||
'Automatic', rand(0,1) ? 'Gasoline' : 'Hybrid',
|
||||
$branch_id, $is_featured, $image_url, $installment_available
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$stmt = $db->query("SELECT COUNT(*) FROM users");
|
||||
if ($stmt->fetchColumn() == 0) {
|
||||
$stmt = $db->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)");
|
||||
$stmt->execute(['admin', password_hash('admin123', PASSWORD_DEFAULT), 'Super Admin']);
|
||||
}
|
||||
|
||||
// Create flag file to prevent re-running on every request
|
||||
file_put_contents($flagFile, date('Y-m-d H:i:s'));
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("DB Setup Failed: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
?>
|
||||
97
setup.php
97
setup.php
@ -6,6 +6,12 @@ try {
|
||||
|
||||
// Drop tables if they exist for a clean re-seed
|
||||
$db->exec("SET FOREIGN_KEY_CHECKS = 0");
|
||||
$db->exec("DROP TABLE IF EXISTS notifications");
|
||||
$db->exec("DROP TABLE IF EXISTS activity_logs");
|
||||
$db->exec("DROP TABLE IF EXISTS installments");
|
||||
$db->exec("DROP TABLE IF EXISTS sales");
|
||||
$db->exec("DROP TABLE IF EXISTS reviews");
|
||||
$db->exec("DROP TABLE IF EXISTS car_images");
|
||||
$db->exec("DROP TABLE IF EXISTS cars");
|
||||
$db->exec("DROP TABLE IF EXISTS branches");
|
||||
$db->exec("DROP TABLE IF EXISTS users");
|
||||
@ -21,6 +27,16 @@ try {
|
||||
hours VARCHAR(100)
|
||||
)");
|
||||
|
||||
// Create Users table
|
||||
$db->exec("CREATE TABLE users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(100) UNIQUE NOT NULL,
|
||||
email VARCHAR(100) UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
role ENUM('Guest', 'Customer', 'Dealer', 'Employee', 'Manager', 'Admin', 'Super Admin') DEFAULT 'Customer',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)");
|
||||
|
||||
// Create Cars table
|
||||
$db->exec("CREATE TABLE cars (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
@ -34,19 +50,78 @@ try {
|
||||
fuel_type VARCHAR(50),
|
||||
status ENUM('Available', 'Reserved', 'Sold') DEFAULT 'Available',
|
||||
branch_id INT,
|
||||
dealer_id INT DEFAULT NULL,
|
||||
installment_available BOOLEAN DEFAULT 0,
|
||||
is_featured BOOLEAN DEFAULT 0,
|
||||
image_url VARCHAR(255),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (branch_id) REFERENCES branches(id)
|
||||
FOREIGN KEY (branch_id) REFERENCES branches(id),
|
||||
FOREIGN KEY (dealer_id) REFERENCES users(id)
|
||||
)");
|
||||
|
||||
// Create Users table
|
||||
$db->exec("CREATE TABLE users (
|
||||
// Create Car Images table
|
||||
$db->exec("CREATE TABLE car_images (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(100) UNIQUE NOT NULL,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
role ENUM('Guest', 'Customer', 'Dealer', 'Employee', 'Manager', 'Admin', 'Super Admin') DEFAULT 'Customer',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
car_id INT NOT NULL,
|
||||
image_path VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE
|
||||
)");
|
||||
|
||||
// Create Reviews table
|
||||
$db->exec("CREATE TABLE reviews (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
car_id INT NOT NULL,
|
||||
user_id INT NOT NULL,
|
||||
rating INT NOT NULL CHECK (rating >= 1 AND rating <= 5),
|
||||
comment TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
)");
|
||||
|
||||
// Create Sales table
|
||||
$db->exec("CREATE TABLE sales (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT NOT NULL,
|
||||
car_id INT NOT NULL,
|
||||
amount DECIMAL(15, 2) NOT NULL,
|
||||
sale_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
status ENUM('Pending', 'Completed', 'Cancelled') DEFAULT 'Pending',
|
||||
FOREIGN KEY (user_id) REFERENCES users(id),
|
||||
FOREIGN KEY (car_id) REFERENCES cars(id)
|
||||
)");
|
||||
|
||||
// Create Installments table
|
||||
$db->exec("CREATE TABLE installments (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
sale_id INT NOT NULL,
|
||||
total_amount DECIMAL(15, 2) NOT NULL,
|
||||
paid_amount DECIMAL(15, 2) DEFAULT 0,
|
||||
monthly_payment DECIMAL(15, 2) NOT NULL,
|
||||
status ENUM('Active', 'Completed', 'Overdue') DEFAULT 'Active',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (sale_id) REFERENCES sales(id) ON DELETE CASCADE
|
||||
)");
|
||||
|
||||
// Create Activity Logs table
|
||||
$db->exec("CREATE TABLE activity_logs (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT,
|
||||
action VARCHAR(255) NOT NULL,
|
||||
ip_address VARCHAR(50),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
|
||||
)");
|
||||
|
||||
// Create Notifications table
|
||||
$db->exec("CREATE TABLE notifications (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
is_read BOOLEAN DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
)");
|
||||
|
||||
// Seed Branches
|
||||
@ -74,7 +149,7 @@ try {
|
||||
'Tesla' => ['Model S', 'Model X']
|
||||
];
|
||||
|
||||
$stmt = $db->prepare("INSERT INTO cars (vin, brand, model, year, price, mileage, transmission, fuel_type, branch_id, is_featured, image_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt = $db->prepare("INSERT INTO cars (vin, brand, model, year, price, mileage, transmission, fuel_type, branch_id, is_featured, image_url, installment_available) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
|
||||
for ($i = 1; $i <= 20; $i++) {
|
||||
$brand = $brands[array_rand($brands)];
|
||||
@ -84,13 +159,14 @@ try {
|
||||
$mileage = rand(0, 15000);
|
||||
$branch_id = rand(1, 4);
|
||||
$is_featured = ($i <= 8) ? 1 : 0; // 8 featured cars
|
||||
$installment_available = rand(0, 1);
|
||||
$image_url = "assets/images/cars/car{$i}.jpg";
|
||||
$vin = "VIN" . str_pad((string)$i, 10, "0", STR_PAD_LEFT);
|
||||
|
||||
$stmt->execute([
|
||||
$vin, $brand, $model, $year, $price, $mileage,
|
||||
'Automatic', rand(0,1) ? 'Gasoline' : 'Hybrid',
|
||||
$branch_id, $is_featured, $image_url
|
||||
$branch_id, $is_featured, $image_url, $installment_available
|
||||
]);
|
||||
}
|
||||
|
||||
@ -98,6 +174,9 @@ try {
|
||||
$stmt = $db->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)");
|
||||
$stmt->execute(['admin', password_hash('admin123', PASSWORD_DEFAULT), 'Super Admin']);
|
||||
|
||||
// Create flag file for automated setup
|
||||
file_put_contents(__DIR__ . '/db/setup_done.flag', date('Y-m-d H:i:s'));
|
||||
|
||||
echo "<h1>Setup Successful!</h1>";
|
||||
echo "<p>Database recreated and exactly 20 premium cars seeded.</p>";
|
||||
echo "<p><strong>Admin Credentials:</strong> admin / admin123</p>";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user