Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exit from the parking #269

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions app/controllers/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,13 @@ public function enterExitParking($land_ID = null){
$data = [
'id' => $_POST['id']
];


}
$data = [
'id' => $land_ID
];

// Check parking status of the driver
$parking_status = $this->driverModel->checkParkingStatus($data['id']);

else{
$data = [
'id' => $land_ID
];
}

$other_data['notification_count'] = 0;

Expand All @@ -144,33 +141,44 @@ public function enterExitParking($land_ID = null){

$lands = $this->landModel->viewLand($land_ID);

$this->view('driver/viewParking', $lands, $other_data);
}

// Check parking status of the driver
$parking_status = $this->driverModel->checkParkingStatus($data['id']);

// ------------------------ Scan QR Code ------------------------
public function scanQRCode(){
$vehicles = $this->driverModel->viewVehicles();
if($parking_status){
// If the driver is already parked, then exit the parking
$this->driverModel->exitParking($data);
redirect('driver/index/'.$land_ID);
}
else{
// If the driver is not parked, then select the vehicle type
$this->view('driver/enterExitParking/vehicleTypeSelection', $data, $other_data);
}
}

$other_data['notification_count'] = 0;
// Enter parking
public function enterParking(){
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

if ($other_data['notification_count'] < 10)
$other_data['notification_count'] = '0'.$other_data['notification_count'];
$land_ID = $_POST['id'];
$vehicle_type = $_POST['vehicle_type'];

$this->view('driver/scanQRCode', $vehicles, $other_data);
$this->driverModel->enterParking($land_ID, $vehicle_type);
redirect('driver/index/'.$land_ID);
}
}

// ------------------------ Enter Parking ------------------------
public function enterParking(){

// ------------------------ Scan QR Code ------------------------
public function scanQRCode(){
$vehicles = $this->driverModel->viewVehicles();

$other_data['notification_count'] = 0;

if ($other_data['notification_count'] < 10)
$other_data['notification_count'] = '0'.$other_data['notification_count'];

$this->view('driver/enterParking', $vehicles, $other_data);
$this->view('driver/scanQRCode', $vehicles, $other_data);
}

// ------------------------ Start Time ------------------------
Expand Down
100 changes: 99 additions & 1 deletion app/models/DriverModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,110 @@ public function updateVehicle($data): bool
// ------------------------- Parking Functionalities -------------------------
// Check parking status
public function checkParkingStatus($landID){
$this->db->query('SELECT * FROM driver_land WHERE landID = :landID and driverID = :driverID');
$this->db->query('SELECT * FROM driver_land WHERE landID = :landID and driverID = :driverID and status = 1');
$this->db->bind(':landID', $landID);
$this->db->bind(':driverID', $_SESSION['user_id']);

$row = $this->db->single();

if ($this->db->rowCount() == 0)
return 0;

return $row->status;
}

// Enter parking
public function enterParking($land_ID, $vehicle_type){
$this->db->query('INSERT INTO driver_land (landID, driverID, status, vehicleType, startTime) VALUES (:landID, :driverID, :status, :vehivleType, :startTime)');
$this->db->bind(':landID', $land_ID);
$this->db->bind(':driverID', $_SESSION['user_id']);
$this->db->bind(':status', 1);
$this->db->bind(':vehivleType', $vehicle_type);
$this->db->bind(':startTime', date("Y-m-d H:i:s"));

if ($this->db->execute()){
return true;
}
else {
return false;
}
}

// Exit parking
public function exitParking($data){
$this->db->query('UPDATE driver_land SET endTime = :endTime WHERE landID = :landID and driverID = :driverID');
$this->db->bind(':landID', $data['id']);
$this->db->bind(':driverID', $_SESSION['user_id']);
$this->db->bind(':endTime', date("Y-m-d H:i:s"));

if ($this->db->execute() && $this->updateCost($data)){
return true;
}
else {
return false;
}
}

// Update cost
public function updateCost($data){
// get the start time, end time and type of the vehicle
$this->db->query('SELECT * FROM driver_land WHERE landID = :landID AND driverID = :driverID AND status = 1');
$this->db->bind(':landID', $data['id']);
$this->db->bind(':driverID', $_SESSION['user_id']);
$row = $this->db->single();

$start_time = $row->startTime;
$end_time = $row->endTime;
$type = $row->vehicleType;

// Convert the start time and end time to DateTime objects
$datetime1 = new DateTime($start_time);
$datetime2 = new DateTime($end_time);

// Get the difference between the two DateTime objects
$interval = $datetime1->diff($datetime2);

// Get the total minutes
$total_minutes = ($interval->days * 24 * 60) + ($interval->h * 60) + $interval->i;

// Get the slot price of the given vehicle type
$this->db->query('SELECT * FROM land WHERE id = :id');
$this->db->bind(':id', $data['id']);
$row = $this->db->single();

// Calculate the slot price according to the vehicle type
if($type == 'car'){
$slot_price = $row->car;
}
else if($type == 'bike'){
$slot_price = $row->bike;
}
else if($type == 'threeWheel'){
$slot_price = $row->threeWheel;
}

// Calculate the cost
if($total_minutes % 60 == 0 OR $total_minutes % 60 < 10){
$cost = intval($total_minutes / 60) * $slot_price;
}
else if($total_minutes % 60 < 30){
$cost = intval($total_minutes / 60) * $slot_price + 0.5 * $slot_price;
}
else{
$cost = intval($total_minutes / 60) * $slot_price + $slot_price;
}

// Update the cost and status of the transaction
$this->db->query('UPDATE driver_land SET status = 0, cost = :cost WHERE landID = :landID AND driverID = :driverID AND status = 1;');
$this->db->bind(':landID', $data['id']);
$this->db->bind(':driverID', $_SESSION['user_id']);
$this->db->bind(':cost', $cost);

if ($this->db->execute()){
return true;
}
else {
return false;
}
}
}
37 changes: 37 additions & 0 deletions app/views/driver/enterExitParking/vehicleTypeSelection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php require APPROOT.'/views/inc/header.php'; ?>
<!-- TOP NAVIGATION -->
<?php require APPROOT.'/views/inc/components/topnavbar.php'; ?>

<!-- SIDE NAVIGATION -->
<?php
$section = 'dashboard';
require APPROOT.'/views/inc/components/sidenavbar.php';
?>

<main class="page-container">
<section class="section" id="main">
<div class="container">
<div class="vehicle-type-selection">
<form action="<?php echo URLROOT?>/driver/enterParking" method="post">
<input type="text" name="id" value="<?php echo $data['id'] ?>" hidden>
<input type="text" name="vehicle_type" value="car" hidden>
<input type="submit" value="Car">
</form>

<form action="<?php echo URLROOT?>/driver/enterParking" method="post">
<input type="text" name="id" value="<?php echo $data['id'] ?>" hidden>
<input type="text" name="vehicle_type" value="bike" hidden>
<input type="submit" value="Bike">
</form>

<form action="<?php echo URLROOT?>/driver/enterParking" method="post">
<input type="text" name="id" value="<?php echo $data['id'] ?>" hidden>
<input type="text" name="vehicle_type" value="threeWheel" hidden>
<input type="submit" value="threeWheel">
</form>
</div>
</div>
</section>
</main>

<?php require APPROOT.'/views/inc/footer.php'; ?>
2 changes: 1 addition & 1 deletion app/views/driver/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<td>
<form action="<?php echo URLROOT?>/driver/enterExitParking" method="post" class="enter_exit_to_parking" id="enter_exit_to_parking">
<input type="hidden" id="id" name="id" value="">
<input type="submit" value="View Prices" class="btn btn-primary" style="background-color: #fcd426; border-radius: 10px; padding: 10px 20px 10px 20px">
<input type="submit" value="Enter" class="btn btn-primary" style="background-color: #fcd426; border-radius: 10px; padding: 10px 20px 10px 20px">
</form>
</td>
</tr>
Expand Down