diff --git a/app/controllers/RefuelController.php b/app/controllers/RefuelController.php index b7b2151..59bd00d 100644 --- a/app/controllers/RefuelController.php +++ b/app/controllers/RefuelController.php @@ -2,8 +2,84 @@ class RefuelController extends Controller { public function create() { - $vehicle = new Vehicle(); - $vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']); - $this->view('refuel/create', ['title' => "New refuel record", 'vehicles' => $vehicles]); + if($_SERVER['REQUEST_METHOD'] === 'GET'){ + $vehicle = new Vehicle(); + $vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']); + $this->view('refuel/create', [ + 'title' => "New refuel record", + 'vehicles' => $vehicles, + ]); + return; + } + + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $vehicle_id = $_POST['vehicle'] ?? ''; + $fuel_type = $_POST['fuel_type'] ?? ''; + $liters = $_POST['liters'] ?? ''; + $price_per_liter = $_POST['price_per_liter'] ?? ''; + $total_price = $_POST['total_price'] ?? ''; + $note = $_POST['note'] ?? ''; + + $validator = new Validator(); + $validator->required('vehicle', $vehicle_id); + $validator->required('fuel_type', $fuel_type); + $validator->required('liters', $liters); + $validator->required('price_per_liter', $price_per_liter); + $validator->required('total_price', $total_price); + $validator->number('liters', $liters); + $validator->number('price_per_liter', $price_per_liter); + $validator->number('total_price', $total_price); + + if (round($liters * $price_per_liter, 2) != $total_price) { + $validator->setErrors(["total_price" => "Price calculation is wrong"]); + } + + if($note == "") $note = NULL; + + if (!$validator->passes()) { + $vehicle = new Vehicle(); + $vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']); + $this->view('refuel/create', [ + 'error' => 'Please correct the errors below.', + 'validationErrors' => $validator->errors() ?: [], + 'vehicles' => $vehicles, + 'title' => 'New refuel record', + ]); + return; + } + + $record = new Refuel(); + $result = $record->create([ + 'user_id' => $_SESSION['user']['id'], + 'vehicle_id' => $vehicle_id, + 'fuel_type' => $fuel_type, + 'note' => $note, + 'liters' => $liters, + 'price_per_liter' => $price_per_liter, + 'total_price' => $total_price, + ]); + + if ($result === true) { + $this->redirect('/'); + } else { + $vehicle = new Vehicle(); + $vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']); + $this->view('refuel/create', [ + 'title' => 'New refuel record', + 'error' => $result, + 'validationErrors' => [], + 'vehicles' => $vehicles, + ]); + } + return; + } + } + + public function edit() { + // Edit refuel record (to be implemented later) + } + + public function delete() { + // Delete refuel record (to be implemented later) } } diff --git a/app/controllers/VehicleController.php b/app/controllers/VehicleController.php index d2897ca..0ea430e 100644 --- a/app/controllers/VehicleController.php +++ b/app/controllers/VehicleController.php @@ -22,7 +22,7 @@ class VehicleController extends Controller { if($note == "") $note = NULL; if (!$validator->passes()) { - $this->view('vehicle/create', [ + $this->view('vehicles/create', [ 'error' => 'Please correct the errors below.', 'validationErrors' => $validator->errors() ?: [], ]); diff --git a/app/models/Refuel.php b/app/models/Refuel.php index 51b5c1a..e5cbc47 100644 --- a/app/models/Refuel.php +++ b/app/models/Refuel.php @@ -1,5 +1,37 @@ db = Database::getInstance()->getConnection(); + } + + public function create($data) { + try{ + $stmt = $this->db->prepare(" + INSERT INTO refueling_records (user_id, vehicle_id, fuel_type, note, liters, price_per_liter, total_price, created_at) + VALUES (?, ?, ?, ?, ?, ?, ?, NOW()) + "); + + $stmt->bind_param( + "iissddd", + $data['user_id'], + $data['vehicle_id'], + $data['fuel_type'], + $data['note'], + $data['liters'], + $data['price_per_liter'], + $data['total_price'], + ); + + if ($stmt->execute()) { + return true; + } else { + return "Error: " . $stmt->error; + } + } catch(mysqli_sql_exception $e) { + return $e->getMessage(); + } + } } diff --git a/app/views/layouts/base.php b/app/views/layouts/base.php index 80fd9a6..00a9225 100644 --- a/app/views/layouts/base.php +++ b/app/views/layouts/base.php @@ -6,7 +6,7 @@ - Fuel Stats | <?= $data['title'] ?> + Fuel Stats<?= isset($data['title']) ? " | " . $data['title'] : "" ?> @@ -17,7 +17,7 @@
home - +
diff --git a/app/views/layouts/noheader.php b/app/views/layouts/noheader.php index 9047b6c..5967711 100644 --- a/app/views/layouts/noheader.php +++ b/app/views/layouts/noheader.php @@ -6,7 +6,7 @@ - Fuel Stats | <?= $data['title'] ?> + Fuel Stats<?= isset($data['title']) ? " | " . $data['title'] : "" ?> diff --git a/app/views/refuel/create.php b/app/views/refuel/create.php index 1514216..699a4ac 100644 --- a/app/views/refuel/create.php +++ b/app/views/refuel/create.php @@ -13,9 +13,12 @@ + get('validationErrors')['vehicle'])): ?> + get('validationErrors')['vehicle'] ?> + + get('validationErrors')['fuel_type'])): ?> + get('validationErrors')['fuel_type'] ?> + - + + get('validationErrors')['liters'])): ?> + get('validationErrors')['liters'] ?> + - + + + get('validationErrors')['price_per_liter'])): ?> + get('validationErrors')['price_per_liter'] ?> + - - - - - + + + get('validationErrors')['total_price'])): ?> + get('validationErrors')['total_price'] ?> + + get('validationErrors')['note'])): ?> + get('validationErrors')['note'] ?> + diff --git a/core/Database.php b/core/Database.php index 11e437f..c1d51ae 100644 --- a/core/Database.php +++ b/core/Database.php @@ -91,13 +91,16 @@ class Database { $refuelingTableQuery = " CREATE TABLE IF NOT EXISTS refueling_records ( id INT AUTO_INCREMENT PRIMARY KEY, + user_id INT NOT NULL, vehicle_id INT NOT NULL, fuel_type ENUM('Diesel', 'Gasoline 95', 'Gasoline 98', 'Premium Diesel', 'Premium Gasoline 95', 'Premium Gasoline 98', 'Other') NOT NULL, - liters DECIMAL(10, 2) NOT NULL, - price_per_liter DECIMAL(10, 2) NOT NULL, - total_price DECIMAL(10, 2) NOT NULL, + note VARCHAR(150) NULL, + liters DOUBLE(10, 2) NOT NULL, + price_per_liter DOUBLE(10, 2) NOT NULL, + total_price DOUBLE(10, 2) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (vehicle_id) REFERENCES vehicles(id) ON DELETE CASCADE + FOREIGN KEY (vehicle_id) REFERENCES vehicles(id) ON DELETE CASCADE, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ) ENGINE=InnoDB; "; diff --git a/core/Validator.php b/core/Validator.php index ed5bdc5..38b992d 100644 --- a/core/Validator.php +++ b/core/Validator.php @@ -12,6 +12,15 @@ class Validator { } } + /** + * Check if a field contains numbers + */ + public function number($field, $value, $message = null) { + if(!is_numeric($value)) { + $this->errors[$field] = $message ?? "$field must be an number"; + } + } + /** * Check if a field meets minimum length */ @@ -46,6 +55,10 @@ class Validator { return $this->errors; } + public function setErrors($errors) { + $this->errors = $errors; + } + /** * Check if validation passed */