39 lines
1.5 KiB
PHP
39 lines
1.5 KiB
PHP
<?php
|
|
// transit/vehicle/update.php — تعديل بيانات مركبة
|
|
// POST: vehicle_id, [plate, make, model, year, color, capacity, vehicle_type, notes]
|
|
|
|
require_once __DIR__ . '/../../transit/connect_admin.php';
|
|
|
|
$vehicleId = filterRequest('vehicle_id', 'int');
|
|
if (!$vehicleId) jsonError('vehicle_id is required', 400);
|
|
|
|
$chk = $transit_con->prepare("SELECT id FROM transit_vehicles WHERE id=? AND org_id=? LIMIT 1");
|
|
$chk->execute([$vehicleId, $transit_org_id]);
|
|
if (!$chk->fetch()) jsonError('Vehicle not found or access denied', 404);
|
|
|
|
$plate = filterRequest('plate');
|
|
if ($plate) {
|
|
$plate = strtoupper($plate);
|
|
$dup = $transit_con->prepare("SELECT id FROM transit_vehicles WHERE plate=? AND id<>? LIMIT 1");
|
|
$dup->execute([$plate, $vehicleId]);
|
|
if ($dup->fetch()) jsonError('Vehicle plate already registered', 409);
|
|
}
|
|
|
|
$vType = filterRequest('vehicle_type');
|
|
if ($vType && !in_array($vType, ['bus','minibus','van','other'])) $vType = null;
|
|
|
|
$transit_con->prepare(
|
|
"UPDATE transit_vehicles
|
|
SET plate=COALESCE(?, plate), make=COALESCE(?, make), model=COALESCE(?, model),
|
|
year=COALESCE(?, year), color=COALESCE(?, color), capacity=COALESCE(?, capacity),
|
|
vehicle_type=COALESCE(?, vehicle_type), notes=COALESCE(?, notes)
|
|
WHERE id=?"
|
|
)->execute([
|
|
$plate, filterRequest('make'), filterRequest('model'),
|
|
filterRequest('year', 'int'), filterRequest('color'), filterRequest('capacity', 'int'),
|
|
$vType, filterRequest('notes'),
|
|
$vehicleId,
|
|
]);
|
|
|
|
jsonSuccess(['vehicle_id' => $vehicleId], 'Vehicle updated');
|