Improve invoice fetching and add vehicle plate lookup API
Refactored the invoice retrieval to handle pagination for both drafts and booked invoices, ensuring all results are fetched when exceeding the limit. Added a new route for vehicle plate lookup, allowing retrieval of order history for a specified vehicle plate. Implemented the corresponding database query method in `orders_o`.
This commit is contained in:
@@ -289,4 +289,17 @@ class orders_o extends db
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the order history for a vehicle plate
|
||||
* @param string $plate The vehicle plate
|
||||
* @return array The order history
|
||||
*/
|
||||
public function get_vehicle_order_history(string $plate): array
|
||||
{
|
||||
global $db;
|
||||
$sql = "SELECT * FROM $this->table WHERE reg_1 = '$plate' OR reg_2 = '$plate' OR reg_3 = '$plate'";
|
||||
$result = $db->query($sql);
|
||||
return $db->fetch_all($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use objects\logs_o;
|
||||
use objects\orders_o;
|
||||
use traits\route_t;
|
||||
|
||||
class vehiclePlateLookupRoute
|
||||
{
|
||||
use route_t;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$this->get('/department/license-plate/lookup', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('department_license_plate_lookup');
|
||||
// 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);
|
||||
}
|
||||
// Return the vehicle order history
|
||||
$response->success((new orders_o())->get_vehicle_order_history($this->fromRequest('plate')));
|
||||
} 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -280,22 +280,56 @@ class economic_s
|
||||
|
||||
// Get the economic class
|
||||
$economic = new economic();
|
||||
$drafts = $economic->invoices->drafts->get([
|
||||
'(date$gte:' . $start . '$and:date$lte:' . $end . ')' => '', // To allow for time filtering, we need to add the time frame to the query
|
||||
],
|
||||
[
|
||||
'skipPages' => 0,
|
||||
'pageSize' => 1000, // Since the limit is 1000, we need to set the page size to 1000.
|
||||
]
|
||||
)->collection;
|
||||
$booked = $economic->invoices->booked->get([
|
||||
'(date$gte:' . $start . '$and:date$lte:' . $end . ')' => '', // To allow for time filtering, we need to add the time frame to the query
|
||||
],
|
||||
[
|
||||
'skipPages' => 0,
|
||||
'pageSize' => 1000, // Since the limit is 1000, we need to set the page size to 1000.
|
||||
]
|
||||
)->collection;
|
||||
$repeat['drafts'] = [
|
||||
'invoiceCount' => 0,
|
||||
'repeat' => true,
|
||||
'pageCount' => 0,
|
||||
];
|
||||
$drafts = [];
|
||||
while ($repeat['drafts']['repeat']) {
|
||||
$repeat['drafts']['repeat'] = false;
|
||||
// Merge the draft collection, since the limit is 1000, we need to get all the drafts
|
||||
$drafts_tmp = $economic->invoices->drafts->get([
|
||||
'(date$gte:' . $start . '$and:date$lte:' . $end . ')' => '', // To allow for time filtering, we need to add the time frame to the query
|
||||
],
|
||||
[
|
||||
'skipPages' => $repeat['drafts']['pageCount'],
|
||||
'pageSize' => 1000, // Since the limit is 1000, we need to set the page size to 1000.
|
||||
]
|
||||
)->collection;
|
||||
$drafts = array_merge($drafts, $drafts_tmp);
|
||||
|
||||
// If the count of the drafts is equal to the limit, we need to repeat the process
|
||||
if (count($drafts_tmp) == 1000) {
|
||||
$repeat['drafts']['repeat'] = true;
|
||||
$repeat['drafts']['pageCount']++;
|
||||
}
|
||||
}
|
||||
$repeat['booked'] = [
|
||||
'invoiceCount' => 0,
|
||||
'repeat' => true,
|
||||
'pageCount' => 0,
|
||||
];
|
||||
$booked = [];
|
||||
while ($repeat['booked']['repeat']) {
|
||||
$repeat['booked']['repeat'] = false;
|
||||
// Merge the booked collection, since the limit is 1000, we need to get all the booked
|
||||
$booked_tmp = $economic->invoices->booked->get([
|
||||
'(date$gte:' . $start . '$and:date$lte:' . $end . ')' => '', // To allow for time filtering, we need to add the time frame to the query
|
||||
],
|
||||
[
|
||||
'skipPages' => $repeat['booked']['invoiceCount'],
|
||||
'pageSize' => 1000, // Since the limit is 1000, we need to set the page size to 1000.
|
||||
]
|
||||
)->collection;
|
||||
$booked = array_merge($booked, $booked_tmp);
|
||||
|
||||
// If the count of the booked is equal to the limit, we need to repeat the process
|
||||
if (count($booked_tmp) == 1000) {
|
||||
$repeat['booked']['repeat'] = true;
|
||||
$repeat['booked']['pageCount']++;
|
||||
}
|
||||
}
|
||||
$drafts_total = 0;
|
||||
$booked_total = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user