Add vehicle plate order history route and data retrieval

Introduced a new route to fetch the last five orders for a vehicle using its plate. Implemented a corresponding method in the `orders_o` class to query order history based on vehicle plates, supporting dynamic entry limits. This enhances functionality for tracking vehicle-associated orders.
This commit is contained in:
Jepp9350
2025-01-07 11:48:27 +01:00
parent 56c81f084d
commit 1d03ee25ef
2 changed files with 55 additions and 0 deletions
+13
View File
@@ -217,4 +217,17 @@ class orders_o extends db
}
return false;
}
/**
* @param string $plate The vehicle plate
* @param int $entries The number of last entries to return (default 10)
* @return array The orders for the vehicle plate
*/
public function getOrderHistoryByVehiclePlate(string $plate, int $entries = 10): array
{
global $db;
$sql = "SELECT * FROM $this->table WHERE reg_1 = '$plate' OR reg_2 = '$plate' OR reg_3 = '$plate' ORDER BY id DESC LIMIT $entries";
$result = $db->query($sql);
return $db->fetch_all($result);
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace routes;
use classes\authentication;
use objects\logs_o;
use objects\orders_o;
use traits\route_t;
class vehiclePlateLastOrdersRoute
{
use route_t;
public function run(): void
{
$this->get('/department/vehicle/order/history', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('department_vehicle_order_last_five');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Make sure the vehicle plate is set
if (!(string)$this->fromRequest('plate')) {
$response->error('Plate parameter is required', 400);
}
// Log the incident
(new logs_o())->add('orders', 'global', 1, $user->id, 'FETCH_VEHICLE_ORDER_HISTORY', 'Successfully fetched vehicle order history');
// Return the list of departments
$response->success(
(new orders_o())->getOrderHistoryByVehiclePlate($this->fromRequest('plate'), 5)
);
} else {
// Log the incident
(new logs_o())->add('orders', 'global', 1, 0, 'FETCH_VEHICLE_ORDER_HISTORY', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
}
}