Allow vehicle type to be unset and improve related checks

Updated logic to allow vehicle type to be set to 0 and handle related subscription behavior. Added safeguards to prevent setting subscriptions when type is unset. Introduced `isPlateSeenBefore` in `orders_o` and enhanced `plateScansRoute` to include `seen_before` data.
This commit is contained in:
Jepp9350
2025-05-06 16:36:24 +02:00
parent 84b6d4de3e
commit e3d67acd07
4 changed files with 44 additions and 14 deletions
@@ -80,7 +80,7 @@ class customer_vehicles_o extends db
'id',
]);
return array_map(function ($addon) {
return (new customer_vehicles_addons_o())->select($addon['id']);
return (new customer_vehicles_addons_o())->select((int)$addon['id']);
}, $addons);
}
+12
View File
@@ -561,4 +561,16 @@ class orders_o extends db
}
return $order_items;
}
public function isPlateSeenBefore(string $reg_1): bool
{
// Check if the plate has been seen before
$count = self::getFieldsWhere([
'reg_1' => $reg_1,
'deleted_at' => null,
], [
'reg_1',
]);
return (bool)$count;
}
}
@@ -5,6 +5,7 @@ namespace routes;
use classes\authentication;
use objects\customer_vehicles_o;
use objects\logs_o;
use objects\orders_o;
use objects\plate_scans_o;
use objects\users_o;
use traits\route_t;
@@ -85,6 +86,9 @@ class plateScansRoute
$number_plate_scans = (new plate_scans_o());
$result = $number_plate_scans->listObjectsWithPaginationIfSet(
function ($scan) {
$tmp_scan_seen_before = [
'seen_before' => (new orders_o())->isPlateSeenBefore((string)$scan['plate']),
];
$tmp_scan_customer = [
'barred' => false,
'customer_number' => null,
@@ -101,6 +105,7 @@ class plateScansRoute
return [
...$scan,
...$tmp_scan_customer,
...$tmp_scan_seen_before,
];
},
$number_plate_scans->forceRestrictFilters(
+26 -13
View File
@@ -226,20 +226,28 @@ class vehiclesRoute
$type = (int)self::getParameter('type');
// Make sure the type is an integer
self::requireType($type, self::type_int());
self::requireMinValue($type, 1);
// Make sure the type is a valid type
$products_o = new products_o();
$products_o->select((int)$type);
if (!$products_o->exists() || !$products_o->subscription_allowed->value()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'EDIT_VEHICLE', 'Invalid type');
// Return an error
$response->error('Invalid type', 400);
self::requireMinValue($type, 0);
// Make sure the type is a valid type (If it isn't 0)
if ($type === 0) {
// Set the type to null
$vehicle->type->set(0);
// Turn off the subscription
$vehicle->wash_subscription->set(0);
return;
} else {
$products_o = new products_o();
$products_o->select((int)$type);
if (!$products_o->exists() || !$products_o->subscription_allowed->value()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'EDIT_VEHICLE', 'Invalid type');
// Return an error
$response->error('Invalid type', 400);
}
// Set the type
$vehicle->type->set(
(int)$type
);
}
// Set the type
$vehicle->type->set(
(int)$type
);
}
if (self::isParametersSet(['reg'])) {
$reg = (string)self::getParameter('reg');
@@ -252,6 +260,11 @@ class vehiclesRoute
if (self::isParametersSet(['wash_subscription'])) {
$subscription = (bool)self::getParameter('wash_subscription');
self::requireType($subscription, self::type_bool());
// Check if the type is a valid type (If it isn't 0)
if ((int)$vehicle->type->value() === 0 && $subscription) {
// Set the subscription to null
$response->error('Unable to set subscription, type is not set', 400);
}
// Set the wash subscription
$vehicle->wash_subscription->set($subscription ? 1 : 0);
}