Add unit tests for vehicle search metadata alignment and extend API fixtures for vehicle and order item creation. Refactor booking and route handling logic for booked vehicle searches.

This commit is contained in:
Jeppe Bundgaard
2026-04-13 21:42:37 +02:00
parent d267fb0f09
commit dc79352f40
4 changed files with 188 additions and 50 deletions
+83 -33
View File
@@ -747,6 +747,7 @@ class vehiclesRoute
/** Get the different lists of registration numbers */
$options = ['limit' => 10];
$booked_filters = [...($department ? ['department' => $department] : [])];
$booking_lookup_filters = ['order_id' => null, 'deleted_at' => null, ...$booked_filters];
$verified_regs = self::getVerifiedRegs($search, $options);
$known_regs = self::getKnownRegs($search, $options);
$booked_regs = self::getBookedRegs($search, $options, $booked_filters);
@@ -782,7 +783,7 @@ class vehiclesRoute
// Limit the results to 10 items total, while keeping the relevance order
$vehicles = array_slice($relevant, 0, 10);
/** Format the relevance results */
$vehicles = array_map(function ($reg) use ($verified_regs, $known_regs, $booked_regs, $unknown_regs) {
$vehicles = array_map(function ($reg) use ($verified_regs, $known_regs, $booked_regs, $unknown_regs, $booking_lookup_filters) {
// Determine the status of the vehicle
$status = null;
$isVerified = in_array($reg, $verified_regs);
@@ -800,43 +801,18 @@ class vehiclesRoute
// Booking related variables
$booking_id = null;
$notes = null; // Booking notes
$booking_datetime = null;
// Set the status based on priority: booked > verified > known > unknown
if ($isBooked) {
$status = 'booked';
/**
* // Get the booking
* $booking = (new bookings_o())->getFieldsWhere([
* 'regNrTraekker' => $reg,
* 'status' => 'pending',
* ], ['id', 'customer_number', 'reference_number', 'notes']);
* // If there is no booking with the tractor reg, check the trailer reg
* if (!$booking) {
* $booking = (new bookings_o())->getFieldsWhere([
* 'regNrTrailer' => $reg,
* 'status' => 'pending',
* ], ['id', 'customer_number', 'reference_number', 'notes']);
* }
*/
// Get the booking
$booking = (new order_bookings_o())->getFieldsWhere([
'reg_1' => $reg,
'order_id' => null,
'deleted_at' => null,
], ['id', 'customer_number', 'reference', 'note']);
// If there is no booking with the tractor reg, check the trailer reg
if (!$booking) {
$booking = (new order_bookings_o())->getFieldsWhere([
'reg_2' => $reg,
'order_id' => null,
'deleted_at' => null,
], ['id', 'customer_number', 'reference', 'note']);
}
$booking = self::getPendingOrderBookingSummaryByPlate($reg, $booking_lookup_filters);
// Populate other variables
$booking_id = $booking ? (int)$booking[0]['id'] : null;
$customer_number = $booking ? (int)$booking[0]['customer_number'] : null;
$reference = $booking ? (string)$booking[0]['reference'] : null;
$notes = $booking ? (string)$booking[0]['note'] : null;
$booking_id = $booking ? (int)$booking['id'] : null;
$customer_number = $booking ? (int)$booking['customer_number'] : null;
$reference = $booking ? (string)$booking['reference'] : null;
$notes = $booking ? (string)$booking['note'] : null;
$booking_datetime = $booking ? (string)($booking['datetime'] ?? '') : null;
// If the booking has a customer number, check if the customer is barred
if ($customer_number && (new users_o())->isCustomerBarred((int)$customer_number)) {
$status = 'card';
@@ -897,6 +873,7 @@ class vehiclesRoute
'last_order_id' => $last_order_id,
'reference' => $reference,
'booking_id' => $booking_id,
'booking_datetime' => $booking_datetime,
'notes' => $notes,
'customer_name' => $customer_name,
'barred' => $status === 'card', // If the status is 'card', the customer is barred
@@ -994,6 +971,79 @@ class vehiclesRoute
));
}
private static function getPendingOrderBookingSummaryByPlate(string $reg, array $filters = []): ?array
{
$bookings_o = new order_bookings_o();
$fields = ['id', 'customer_number', 'reference', 'note', 'datetime'];
$results1 = $bookings_o->getFieldsWhere([
'reg_1' => $reg,
...$filters
], $fields) ?: [];
$results2 = $bookings_o->getFieldsWhere([
'reg_2' => $reg,
...$filters
], $fields) ?: [];
$matches_by_key = [];
foreach (array_merge($results1, $results2) as $booking) {
if (!is_array($booking)) {
continue;
}
$booking_id = isset($booking['id']) ? (int)$booking['id'] : 0;
$dedupe_key = $booking_id > 0
? 'booking:' . $booking_id
: implode('|', [
(string)($booking['reference'] ?? ''),
(string)($booking['note'] ?? ''),
(string)($booking['datetime'] ?? ''),
]);
$matches_by_key[$dedupe_key] = $booking;
}
if (empty($matches_by_key)) {
return null;
}
$matches = array_values($matches_by_key);
usort($matches, function (array $left, array $right): int {
$left_has_datetime = self::hasPendingOrderBookingSummaryDatetime($left);
$right_has_datetime = self::hasPendingOrderBookingSummaryDatetime($right);
if ($left_has_datetime !== $right_has_datetime) {
return $right_has_datetime <=> $left_has_datetime;
}
$timestamp_difference = self::getPendingOrderBookingSummaryTimestamp($left) <=> self::getPendingOrderBookingSummaryTimestamp($right);
if ($timestamp_difference !== 0) {
return $timestamp_difference;
}
return ((int)($left['id'] ?? 0)) <=> ((int)($right['id'] ?? 0));
});
return $matches[0] ?? null;
}
private static function hasPendingOrderBookingSummaryDatetime(array $booking): bool
{
return trim((string)($booking['datetime'] ?? '')) !== '';
}
private static function getPendingOrderBookingSummaryTimestamp(array $booking): int
{
$raw_value = (string)($booking['datetime'] ?? '');
if ($raw_value !== '') {
$parsed_value = strtotime($raw_value);
if ($parsed_value !== false) {
return $parsed_value;
}
}
$fallback_id = (int)($booking['id'] ?? PHP_INT_MAX);
return $fallback_id > 0 ? $fallback_id : PHP_INT_MAX;
}
private static function getUnknownRegs(string $search, array $options = ['limit' => null]): array
{
// Unknown vehicles are vehicles that has been scanned by the LPR system, but are not in any of the other lists