diff --git a/admin/index.php b/admin/index.php new file mode 100644 index 0000000..36e826f --- /dev/null +++ b/admin/index.php @@ -0,0 +1,80 @@ +query("SELECT COUNT(*) FROM users")->fetchColumn(); +$carsCount = $pdo->query("SELECT COUNT(*) FROM cars")->fetchColumn(); +$branchesCount = $pdo->query("SELECT COUNT(*) FROM branches")->fetchColumn(); +$salesCount = $pdo->query("SELECT COUNT(*) FROM sales WHERE status = 'Completed'")->fetchColumn(); +$revenue = $pdo->query("SELECT SUM(final_price) FROM sales WHERE status = 'Completed'")->fetchColumn() ?: 0; +$activeContracts = $pdo->query("SELECT COUNT(*) FROM installments WHERE status = 'Active'")->fetchColumn(); + +// Recent Activity +$activities = $pdo->query("SELECT * FROM activity_logs ORDER BY created_at DESC LIMIT 5")->fetchAll(); +?> + + + +
+
+
+
Total Users
+
+
+
+
Branches
+
+
+
+
Total Inventory
+
+
+
$
+
Total Revenue
+
+
+
+
Completed Sales
+
+
+
+
Active Installments
+
+
+ +
+
+

System Activity Log

+ +
    + +
  • + + +
  • + +
+ +

No recent activity logged.

+ +
+ +
+

Quick Actions

+ +
+
+ + diff --git a/admin/reports.php b/admin/reports.php new file mode 100644 index 0000000..f86007a --- /dev/null +++ b/admin/reports.php @@ -0,0 +1,12 @@ + + +
+ +

Enterprise Reporting

+

Financial reports, dealer performance, and inventory turnover analytics will be available here.

+
+ diff --git a/admin/sales.php b/admin/sales.php new file mode 100644 index 0000000..b0ee5f4 --- /dev/null +++ b/admin/sales.php @@ -0,0 +1,12 @@ + + +
+ +

Sales Records System

+

No sales recorded yet. Once sales are made, they will appear here along with installment tracking.

+
+ diff --git a/admin/users.php b/admin/users.php new file mode 100644 index 0000000..497b45d --- /dev/null +++ b/admin/users.php @@ -0,0 +1,86 @@ +prepare("DELETE FROM users WHERE id = ?"); + $stmt->execute([$id]); + echo "
User Deleted
"; + } +} + +if (isset($_POST['update_role'])) { + $id = $_POST['user_id']; + $role = $_POST['role']; + if ($id != $_SESSION['user_id']) { // Prevent changing own role to something lower accidentally + $stmt = $pdo->prepare("UPDATE users SET role = ? WHERE id = ?"); + $stmt->execute([$role, $id]); + echo "
User Role Updated
"; + } +} + +$stmt = $pdo->query("SELECT * FROM users ORDER BY created_at DESC"); +$users = $stmt->fetchAll(); +?> + + + +
+ + + + + + + + + + + + + + + + + + + + + +
IDUsernameRoleJoinedActions
# +
+ +
+ + + +
+ + + +
+ +
+ +
+ + +
+ +
+
+ + diff --git a/buyer/index.php b/buyer/index.php new file mode 100644 index 0000000..d99e289 --- /dev/null +++ b/buyer/index.php @@ -0,0 +1,63 @@ +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 = ? +"); +$installments->execute([$userId]); +$myInstallments = $installments->fetchAll(); +?> + +
+ + +
+

My Installment Plans

+ + + + + + + + + + + + + + + + + + + + + + +
VehicleTotalPaidMonthlyStatus
$$$ + + + +
+ +

You have no active installment plans.

+ Browse Cars + +
+
+ + diff --git a/check_db.php b/check_db.php new file mode 100644 index 0000000..fc53361 --- /dev/null +++ b/check_db.php @@ -0,0 +1,32 @@ +query("SHOW TABLES LIKE 'users'"); + if ($stmt->rowCount() > 0) { + echo "Table 'users' exists.\n"; + } else { + echo "Table 'users' does not exist.\n"; + // Create table + $sql = "CREATE TABLE `users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `username` varchar(100) NOT NULL, + `email` varchar(100) NOT NULL UNIQUE, + `password` varchar(255) NOT NULL, + `role` enum('Guest','Customer','Dealer','Employee','Manager','Admin','Super Admin') DEFAULT 'Customer', + `created_at` timestamp NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`), + UNIQUE KEY `username` (`username`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"; + $pdo->exec($sql); + echo "Table 'users' created.\n"; + + // Seed admin user + $password = password_hash('admin123', PASSWORD_DEFAULT); + $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, ?)"); + $stmt->execute(['admin', 'admin@example.com', $password, 'Admin']); + echo "Admin user created (user: admin, pass: admin123).\n"; + } +} catch (PDOException $e) { + echo "Error: " . $e->getMessage(); +} diff --git a/db/migrations/001_enterprise_schema.sql b/db/migrations/001_enterprise_schema.sql new file mode 100644 index 0000000..26ff311 --- /dev/null +++ b/db/migrations/001_enterprise_schema.sql @@ -0,0 +1,72 @@ +-- Branches Table +CREATE TABLE IF NOT EXISTS `branches` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `name` VARCHAR(100) NOT NULL, + `location` VARCHAR(255) NOT NULL, + `phone` VARCHAR(20), + `email` VARCHAR(100), + `manager_id` INT, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +-- Car Images Table (Multiple Images) +CREATE TABLE IF NOT EXISTS `car_images` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `car_id` INT NOT NULL, + `image_url` VARCHAR(255) NOT NULL, + `is_primary` TINYINT(1) DEFAULT 0, + FOREIGN KEY (`car_id`) REFERENCES `cars`(`id`) ON DELETE CASCADE +); + +-- Sales Table +CREATE TABLE IF NOT EXISTS `sales` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `car_id` INT NOT NULL, + `buyer_id` INT NOT NULL, + `seller_id` INT, -- Dealer or Admin who sold it + `sale_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + `final_price` DECIMAL(15, 2) NOT NULL, + `payment_method` ENUM('Cash', 'Installment') DEFAULT 'Cash', + `status` ENUM('Pending', 'Completed', 'Cancelled') DEFAULT 'Pending', + FOREIGN KEY (`car_id`) REFERENCES `cars`(`id`), + FOREIGN KEY (`buyer_id`) REFERENCES `users`(`id`) +); + +-- Installments Table +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.00, + `monthly_payment` DECIMAL(15, 2) NOT NULL, + `due_date` DATE, + `status` ENUM('Active', 'Completed', 'Defaulted') DEFAULT 'Active', + FOREIGN KEY (`sale_id`) REFERENCES `sales`(`id`) ON DELETE CASCADE +); + +-- Activity Logs +CREATE TABLE IF NOT EXISTS `activity_logs` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `user_id` INT, + `action` VARCHAR(255) NOT NULL, + `details` TEXT, + `ip_address` VARCHAR(45), + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +-- Notifications +CREATE TABLE IF NOT EXISTS `notifications` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `user_id` INT NOT NULL, + `message` TEXT NOT NULL, + `is_read` TINYINT(1) DEFAULT 0, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE +); + +-- Update Cars Table +ALTER TABLE `cars` ADD COLUMN IF NOT EXISTS `dealer_id` INT DEFAULT NULL; +ALTER TABLE `cars` ADD COLUMN IF NOT EXISTS `installment_available` TINYINT(1) DEFAULT 0; + +-- Update Users Table (Ensure role column is correct - strictly speaking it already exists but this is safe) +-- ALTER TABLE `users` MODIFY COLUMN `role` ENUM('Guest','Customer','Dealer','Employee','Manager','Admin','Super Admin') DEFAULT 'Customer'; diff --git a/dealer/index.php b/dealer/index.php new file mode 100644 index 0000000..8143484 --- /dev/null +++ b/dealer/index.php @@ -0,0 +1,44 @@ +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->execute([$dealerId]); +$mySalesCount = $mySales->fetchColumn(); +?> + +
+ + +
+
+
+
My Inventory
+
+
+
+
My Sales
+
+
+ +
+

Manage Inventory

+

You can add new cars to your branch inventory here.

+ Go to Inventory Management + +
+
+ + diff --git a/includes/auth.php b/includes/auth.php new file mode 100644 index 0000000..33f8422 --- /dev/null +++ b/includes/auth.php @@ -0,0 +1,51 @@ +prepare("SELECT * FROM users WHERE username = ? OR email = ?"); + $stmt->execute([$username, $username]); + $user = $stmt->fetch(); + + if ($user && password_verify($password, $user['password'])) { + $_SESSION['user_id'] = $user['id']; + $_SESSION['username'] = $user['username']; + $_SESSION['role'] = $user['role']; + return true; + } + return false; +} + +function logout() { + session_destroy(); + header('Location: /login.php'); + exit; +} diff --git a/includes/header.php b/includes/header.php index 1d604b4..d9ad75b 100644 --- a/includes/header.php +++ b/includes/header.php @@ -1,6 +1,7 @@ >Work
  • >About
  • >Contact Us
  • -
  • Admin
  • + + +
  • Dashboard
  • + +
  • Logout
  • + +
  • Login
  • + diff --git a/includes/role_middleware.php b/includes/role_middleware.php new file mode 100644 index 0000000..275d9a4 --- /dev/null +++ b/includes/role_middleware.php @@ -0,0 +1,43 @@ +prepare("INSERT INTO activity_logs (user_id, action, ip_address) VALUES (?, 'Login', ?)") + ->execute([$_SESSION['user_id'], $_SERVER['REMOTE_ADDR']]); + } catch (Exception $e) { /* Ignore logging error */ } + + switch ($role) { + case 'Admin': + case 'Super Admin': + case 'Manager': + header('Location: /admin/index.php'); + break; + case 'Dealer': + header('Location: /dealer/index.php'); + break; + case 'Customer': + case 'Buyer': + header('Location: /buyer/index.php'); + break; + default: + header('Location: /index.php'); + } + exit; + } else { + $error = "Invalid username or password"; + } +} +?> + + + + + + Login - Car Market + + + + + + + diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..46aa490 --- /dev/null +++ b/logout.php @@ -0,0 +1,3 @@ +prepare("SELECT id FROM users WHERE username = ? OR email = ?"); + $stmt->execute([$username, $email]); + if ($stmt->fetch()) { + $error = "Username or email already exists"; + } else { + // Register user + $hash = password_hash($password, PASSWORD_DEFAULT); + $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, 'Customer')"); + try { + $stmt->execute([$username, $email, $hash]); + $success = "Registration successful!"; + } catch (PDOException $e) { + $error = "Registration failed: " . $e->getMessage(); + } + } + } +} +?> + + + + + + Register - Car Market + + + + +
    +

    Create Account

    + +
    + + +
    +

    Proceed to Login

    + +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    + + +
    + + +
    + +
    + +