diff --git a/bunks.php b/bunks.php new file mode 100644 index 0000000..3f01e38 --- /dev/null +++ b/bunks.php @@ -0,0 +1,206 @@ +prepare("INSERT INTO bunks (name, owner, contact, location) VALUES (?, ?, ?, ?)"); + $stmt->execute([$name, $owner, $contact, $location]); + $_SESSION['notification'] = ['text' => 'Bunk added successfully!', 'type' => 'success']; + } catch (PDOException $e) { + $_SESSION['notification'] = ['text' => 'Error adding bunk: ' . $e->getMessage(), 'type' => 'danger']; + } + } else { + $_SESSION['notification'] = ['text' => 'Bunk name is required.', 'type' => 'warning']; + } + header("Location: bunks.php"); + exit; +} + +if (isset($_SESSION['notification'])) { + $notification = $_SESSION['notification']; + unset($_SESSION['notification']); +} + +try { + $db = db(); + $stmt = $db->query("SELECT id, name, owner, contact, location, created_at FROM bunks ORDER BY created_at DESC"); + $bunks = $stmt->fetchAll(); +} catch (PDOException $e) { + $bunks = []; + $notification = ['text' => 'Error fetching bunks: ' . $e->getMessage(), 'type' => 'danger']; +} +?> + + + + + + Bunk Management + + + + + + + + + + + + + + + + +
+
+

Bunk Management

+ +
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameOwnerContactLocationCreatedActions
No bunks found. Add one to get started!
+ + +
+
+
+
+
+ + + + + +
+ +
+ + + + + diff --git a/db/config.php b/db/config.php index bb98f7d..580299f 100644 --- a/db/config.php +++ b/db/config.php @@ -1,17 +1,31 @@ PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, - ]); - } - return $pdo; + static $pdo; + if ($pdo) { + return $pdo; + } + + $host = getenv('DB_HOST') ?: '127.0.0.1'; + $port = getenv('DB_PORT') ?: '3306'; + $dbname = getenv('DB_NAME') ?: 'app'; + $user = getenv('DB_USER') ?: 'app'; + $pass = getenv('DB_PASS') ?: 'app'; + + $dsn = "mysql:host={$host};port={$port};dbname={$dbname};charset=utf8mb4"; + + $options = [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_EMULATE_PREPARES => false, + ]; + + try { + $pdo = new PDO($dsn, $user, $pass, $options); + return $pdo; + } catch (PDOException $e) { + throw new PDOException($e->getMessage(), (int)$e->getCode()); + } } diff --git a/db/migrations/001_initial_schema.sql b/db/migrations/001_initial_schema.sql new file mode 100644 index 0000000..1ccdb29 --- /dev/null +++ b/db/migrations/001_initial_schema.sql @@ -0,0 +1,149 @@ +-- Bunks +CREATE TABLE IF NOT EXISTS bunks ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + owner VARCHAR(255), + contact VARCHAR(50), + location TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); +-- Users and Roles +CREATE TABLE IF NOT EXISTS users ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(100) NOT NULL, + email VARCHAR(100) NOT NULL UNIQUE, + password VARCHAR(255) NOT NULL, + role ENUM('Superadmin', 'Manager', 'Attendant', 'Accountant') NOT NULL, + bunk_id INT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (bunk_id) REFERENCES bunks(id) ON DELETE SET NULL +); +-- Fuel Types +CREATE TABLE IF NOT EXISTS fuel_types ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(50) NOT NULL UNIQUE, + unit VARCHAR(20) DEFAULT 'Litre' +); +-- Tanks +CREATE TABLE IF NOT EXISTS tanks ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(100) NOT NULL, + bunk_id INT NOT NULL, + fuel_type_id INT NOT NULL, + capacity DECIMAL(10,2) NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (bunk_id) REFERENCES bunks(id) ON DELETE CASCADE, + FOREIGN KEY (fuel_type_id) REFERENCES fuel_types(id) +); +-- Pumps +CREATE TABLE IF NOT EXISTS pumps ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(100) NOT NULL, + bunk_id INT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (bunk_id) REFERENCES bunks(id) ON DELETE CASCADE +); +-- Nozzles (each nozzle belongs to a pump and draws from a tank) +CREATE TABLE IF NOT EXISTS nozzles ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(100) NOT NULL, + pump_id INT NOT NULL, + tank_id INT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (pump_id) REFERENCES pumps(id) ON DELETE CASCADE, + FOREIGN KEY (tank_id) REFERENCES tanks(id) ON DELETE CASCADE +); +-- Price History (fuel pricing) +CREATE TABLE IF NOT EXISTS price_history ( + id INT AUTO_INCREMENT PRIMARY KEY, + fuel_type_id INT NOT NULL, + bunk_id INT NOT NULL, + price DECIMAL(10,2) NOT NULL, + date DATE NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY bunk_fuel_date (bunk_id, fuel_type_id, date), + FOREIGN KEY (fuel_type_id) REFERENCES fuel_types(id), + FOREIGN KEY (bunk_id) REFERENCES bunks(id) ON DELETE CASCADE +); +-- Tank Readings (daily, per tank, unique per date) +CREATE TABLE IF NOT EXISTS tank_readings ( + id INT AUTO_INCREMENT PRIMARY KEY, + tank_id INT NOT NULL, + date DATE NOT NULL, + opening DECIMAL(10,2) NOT NULL, + receipts DECIMAL(10,2) DEFAULT 0, + closing DECIMAL(10,2) DEFAULT 0, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY tank_date_unique (tank_id, date), + FOREIGN KEY (tank_id) REFERENCES tanks(id) ON DELETE CASCADE +); +-- Daily Sales (per nozzle) +CREATE TABLE IF NOT EXISTS day_sales ( + id INT AUTO_INCREMENT PRIMARY KEY, + nozzle_id INT NOT NULL, + bunk_id INT NOT NULL, + date DATE NOT NULL, + opening_reading DECIMAL(10,2) NOT NULL, + closing_reading DECIMAL(10,2) NOT NULL, + testing DECIMAL(10,2) DEFAULT 0, + net_sale DECIMAL(10,2) GENERATED ALWAYS AS (closing_reading - opening_reading - testing) STORED, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY nozzle_date_unique (nozzle_id, date), + FOREIGN KEY (nozzle_id) REFERENCES nozzles(id) ON DELETE CASCADE, + FOREIGN KEY (bunk_id) REFERENCES bunks(id) ON DELETE CASCADE +); +-- Fuel Purchases / Receipts +CREATE TABLE IF NOT EXISTS fuel_receipts ( + id INT AUTO_INCREMENT PRIMARY KEY, + bunk_id INT NOT NULL, + supplier VARCHAR(255), + invoice_number VARCHAR(100), + fuel_type_id INT NOT NULL, + quantity DECIMAL(10,2) NOT NULL, + rate DECIMAL(10,2) NOT NULL, + amount DECIMAL(10,2) NOT NULL, + date DATE NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (bunk_id) REFERENCES bunks(id) ON DELETE CASCADE, + FOREIGN KEY (fuel_type_id) REFERENCES fuel_types(id) +); +-- Credit Customers +CREATE TABLE IF NOT EXISTS credit_customers ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + contact VARCHAR(50), + bunk_id INT NOT NULL, + credit_limit DECIMAL(10,2) DEFAULT 0, + outstanding_balance DECIMAL(10,2) DEFAULT 0, + FOREIGN KEY (bunk_id) REFERENCES bunks(id) ON DELETE CASCADE +); +-- Credit Sales +CREATE TABLE IF NOT EXISTS credit_sales ( + id INT AUTO_INCREMENT PRIMARY KEY, + bunk_id INT NOT NULL, + customer_id INT NOT NULL, + date DATE NOT NULL, + fuel_type_id INT NOT NULL, + quantity DECIMAL(10,2) NOT NULL, + rate DECIMAL(10,2) NOT NULL, + amount DECIMAL(10,2) NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (bunk_id) REFERENCES bunks(id) ON DELETE CASCADE, + FOREIGN KEY (customer_id) REFERENCES credit_customers(id), + FOREIGN KEY (fuel_type_id) REFERENCES fuel_types(id) +); +-- Expenses +CREATE TABLE IF NOT EXISTS expenses ( + id INT AUTO_INCREMENT PRIMARY KEY, + bunk_id INT NOT NULL, + category VARCHAR(100) NOT NULL, + amount DECIMAL(10,2) NOT NULL, + description TEXT, + payment_mode VARCHAR(50), + date DATE NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (bunk_id) REFERENCES bunks(id) ON DELETE CASCADE +); +-- Add a Superadmin User +INSERT IGNORE INTO users (name, email, password, role) VALUES +('Superadmin', 'admin@example.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'Superadmin'); -- password = "password" diff --git a/db/setup.php b/db/setup.php new file mode 100644 index 0000000..f772ea5 --- /dev/null +++ b/db/setup.php @@ -0,0 +1,11 @@ +exec($sql); + echo "Database schema created successfully."; +} catch (PDOException $e) { + die("Database setup failed: " . $e->getMessage()); +} diff --git a/index.php b/index.php index 7205f3d..3908f1e 100644 --- a/index.php +++ b/index.php @@ -1,150 +1,92 @@ - + - - - New Style - - - - - - - - - - - - - - - - - - - + + + Bunk Admin Dashboard + + + + + + + + + + + -
-
-

Analyzing your requirements and generating your website…

-
- Loading… -
-

AI is collecting your requirements and applying the first changes.

-

This page will update automatically as the plan is implemented.

-

Runtime: PHP — UTC

+ + + +
+

Dashboard

+
+
+
+
+ +
Bunk Management
+

Add, edit, and manage your fuel bunks.

+ Go to Bunks +
+
+
+
+
+
+ +
User Management
+

Manage users and roles (coming soon).

+ Go to Users +
+
+
+
+
+
+ +
Sales Reports
+

View daily and monthly sales data (coming soon).

+ Go to Reports +
+
+
+
-
- + + - + \ No newline at end of file