diff --git a/app/controllers/DashboardController.php b/app/controllers/DashboardController.php index 2066317..9a097ad 100644 --- a/app/controllers/DashboardController.php +++ b/app/controllers/DashboardController.php @@ -4,9 +4,35 @@ class DashboardController extends Controller { $vehicle = new Vehicle(); $vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']); + $default_car = $vehicle->getDefaultVehicle($_SESSION['user']['id']); + + $refuel = new Refuel(); + $data = [ + "date" => [], + "price" => [], + ]; + $raw_data = $refuel->latest_data($default_car['id'], 5); + foreach($raw_data as $one) { + array_push($data['date'], date('d. m.', strtotime($one['created_at']))); + array_push($data['price'], $one['price_per_liter']); + } + + $latest_record = [ + 'name', + 'liters', + 'price_per_liter', + 'total_price', + 'created_at' + ]; + + $latest_record = $refuel->latest_one($_SESSION['user']['id'])[0]; + $this->view('dashboard/index', [ 'title' => 'Dashboard', 'vehicles' => $vehicles, + 'date_price_data' => $data, + 'default_car' => $default_car, + 'latest_record' => $latest_record, ]); } diff --git a/app/models/Refuel.php b/app/models/Refuel.php index e5cbc47..5029936 100644 --- a/app/models/Refuel.php +++ b/app/models/Refuel.php @@ -34,4 +34,59 @@ class Refuel { return $e->getMessage(); } } + + public function latest_data($vehicle_id, $record_count) { + try { + $stmt = $this->db->prepare(" + SELECT `liters`, `price_per_liter`, `total_price`, `created_at` + FROM `refueling_records` + WHERE `vehicle_id` = ? + ORDER BY created_at DESC + LIMIT ?; + "); + + $stmt->bind_param("ii", $vehicle_id, $record_count); + if ($stmt->execute()) { + $result = $stmt->get_result(); + $data = $result->fetch_all(MYSQLI_ASSOC); + $stmt->close(); + return array_reverse($data); + } else { + return "Error: " . $stmt->error; + } + } catch (mysqli_sql_exception $e) { + return $e->getMessage(); + } + } + + public function latest_one($user_id, $record_count = 1) { + try { + $stmt = $this->db->prepare(" + SELECT + `r`.`vehicle_id`, + `v`.`name` AS `vehicle_name`, + `r`.`liters`, + `r`.`price_per_liter`, + `r`.`total_price`, + `r`.`created_at` + FROM `refueling_records` AS `r` + JOIN `vehicles` AS `v` ON `r`.`vehicle_id` = `v`.`id` + WHERE `r`.`user_id` = ? + ORDER BY `r`.`created_at` DESC + LIMIT ?; + "); + + $stmt->bind_param("ii", $user_id, $record_count); + if ($stmt->execute()) { + $result = $stmt->get_result(); + $data = $result->fetch_all(MYSQLI_ASSOC); + $stmt->close(); + return array_reverse($data); + } else { + return "Error: " . $stmt->error; + } + } catch (mysqli_sql_exception $e) { + return $e->getMessage(); + } + } } diff --git a/app/models/Vehicle.php b/app/models/Vehicle.php index 42dd2f1..3832345 100644 --- a/app/models/Vehicle.php +++ b/app/models/Vehicle.php @@ -33,9 +33,9 @@ class Vehicle { } } - public function getVehiclesByUser($userId) { + public function getVehiclesByUser($user_id) { $stmt = $this->db->prepare("SELECT id, name, registration_plate, fuel_type, note, created_at FROM vehicles WHERE user_id = ?"); - $stmt->bind_param("i", $userId); + $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); @@ -46,4 +46,18 @@ class Vehicle { return $vehicles; } + + public function getDefaultVehicle($user_id) { + $stmt = $this->db->prepare(" + SELECT id, name, registration_plate, fuel_type, note, is_default + FROM vehicles + WHERE user_id = ? AND is_default = TRUE + LIMIT 1 + "); + $stmt->bind_param("i", $user_id); + $stmt->execute(); + $result = $stmt->get_result(); + + return $result->fetch_assoc(); + } } diff --git a/app/views/dashboard/index.php b/app/views/dashboard/index.php index 559b53e..4d91474 100644 --- a/app/views/dashboard/index.php +++ b/app/views/dashboard/index.php @@ -7,41 +7,56 @@ List all vehicles
Frequency
-Reward points
+= $data['latest_record']['vehicle_name'] ?>
+ Liters: += $data['latest_record']['liters'] ?> liters
+ Price per liter: += $data['latest_record']['price_per_liter'] ?>,-/liter
+ Total price: += $data['latest_record']['total_price'] ?>,-
Frequency
-Reward points
-Frequency
-Reward points
-= $data['default_car']['name'] ?>
+ Registration plate += $data['default_car']['registration_plate'] ?>
+ Fuel type += $data['default_car']['fuel_type'] ?>
+ Note += $data['default_car']['note'] ?>
You're current streak is 123 days
-Good job!
+= $data['default_car']['name'] . " | " . $data['default_car']['registration_plate']?>
+