From 8c0af96b940ccec3b0dffebf77b56759d0a9540d Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 11 May 2026 16:27:41 +0530 Subject: [PATCH] Added uuid logic in vehicle controller --- app/Http/Controllers/VehicleController.php | 199 ++++++++++++--------- 1 file changed, 112 insertions(+), 87 deletions(-) diff --git a/app/Http/Controllers/VehicleController.php b/app/Http/Controllers/VehicleController.php index 9ecd9af..3004ce4 100644 --- a/app/Http/Controllers/VehicleController.php +++ b/app/Http/Controllers/VehicleController.php @@ -57,101 +57,126 @@ class VehicleController extends Controller $plantId = $plantCod->id; - $data = $request->all(); + // $data = $request->all(); - $vehicleNo = $data['vehicle_number'] ?? ''; - $entryTimeRaw = $data['entry_time'] ?? ''; - $exitTimeRaw = $data['exit_time'] ?? ''; - $duration = $data['duration'] ?? ''; - $type = $data['type'] ?? ''; + $data = $request->json()->all(); - if(!$vehicleNo) - { - return response()->json([ - 'status' => 'ERROR', - 'status_description' => "Vehicle number cant't be empty!", - ], 404); - } - else if (strlen($vehicleNo) < 8) { - return response()->json([ - 'status' => 'ERROR', - 'status_description' => "vehicle number '$vehicleNo' must be at least 8 characters long", - ], 404); - } - else if(!$entryTimeRaw) - { - return response()->json([ - 'status' => 'ERROR', - 'status_description' => "Entry time cant't be empty!", - ], 404); - } - // else if(!$exitTimeRaw) - // { - // return response()->json([ - // 'status' => 'ERROR', - // 'status_description' => "Exit time cant't be empty!", - // ], 404); - // } - // else if(!$duration) - // { - // return response()->json([ - // 'status' => 'ERROR', - // 'status_description' => "Duration cant't be empty!", - // ], 404); - // } - else if(!$type) - { - return response()->json([ - 'status' => 'ERROR', - 'status_description' => "type cant't be empty!", - ], 404); + $results = []; + + foreach ($data as $item) { + + $uuid = $item['uuid'] ?? ''; + $vehicleNo = trim($item['vehicle_number'] ?? ''); + $entryTimeRaw = $item['entry_time'] ?? ''; + $exitTimeRaw = $item['exit_time'] ?? ''; + $duration = $item['duration'] ?? ''; + $type = $item['type'] ?? ''; + + // vehicle number validation + if (!$vehicleNo) { + + $results[] = [ + 'uuid' => $uuid, + 'status_code' => 'ERROR', + 'status_description' => "Vehicle number can't be empty!" + ]; + + continue; + } + + if (strlen($vehicleNo) < 8) { + + $results[] = [ + 'uuid' => $uuid, + 'status_code' => 'ERROR', + 'status_description' => "Vehicle number '$vehicleNo' must be at least 8 characters long" + ]; + + continue; + } + + // entry time validation + if (!$entryTimeRaw) { + + $results[] = [ + 'uuid' => $uuid, + 'status_code' => 'ERROR', + 'status_description' => "Entry time can't be empty!" + ]; + + continue; + } + + // type validation + if (!$type) { + + $results[] = [ + 'uuid' => $uuid, + 'status_code' => 'ERROR', + 'status_description' => "Type can't be empty!" + ]; + + continue; + } + + // duration validation + if ($duration && !preg_match('/^\d{2}:\d{2}:\d{2}$/', $duration)) { + + $results[] = [ + 'uuid' => $uuid, + 'status_code' => 'ERROR', + 'status_description' => "Invalid duration format $duration" + ]; + + continue; + } } - if ($entryTimeRaw && !Carbon::hasFormat($entryTimeRaw, 'd/m/Y h:i:s A')) { - return response()->json([ - 'status' => 'ERROR', - 'status_description' => "Invalid Entry time format $entryTimeRaw. Expected dd/mm/yyyy hh:mm:ss AM/PM", - ], 404); - } - else if ($exitTimeRaw && !Carbon::hasFormat($exitTimeRaw, 'd/m/Y h:i:s A')) { - return response()->json([ - 'status' => 'ERROR', - 'status_description' => "Invalid Exit time format $exitTimeRaw. Expected dd/mm/yyyy hh:mm:ss AM/PM", - ], 404); - } - else if ($duration && !preg_match('/^\d{2}:\d{2}:\d{2}$/', $duration)) { - return response()->json([ - 'status' => 'ERROR', - 'status_description' => "Invalid duration format $duration. Expected HH:MM:SS", - ], 404); + if(!empty($results)){ + return response()->json($results, 404); } - if(!empty($entryTimeRaw)){ - $entryTime = $entryTimeRaw - ? Carbon::createFromFormat('d/m/Y h:i:s A', $entryTimeRaw) - : null; + foreach ($data as $item) { + + $uuid = $item['uuid'] ?? ''; + $vehicleNo = trim($item['vehicle_number'] ?? ''); + $entryTimeRaw = $item['entry_time'] ?? ''; + $exitTimeRaw = $item['exit_time'] ?? ''; + $duration = $item['duration'] ?? ''; + $type = $item['type'] ?? ''; + + if(!empty($entryTimeRaw)){ + $entryTime = $entryTimeRaw + ? Carbon::createFromFormat('d/m/Y h:i:s A', $entryTimeRaw) + : null; + } + + $exitTime = null; + + if(!empty($exitTimeRaw)){ + + $exitTime = $exitTimeRaw + ? Carbon::createFromFormat('d/m/Y h:i:s A', $exitTimeRaw) + : null; + } + + $record = VehicleEntry::updateOrInsert( + [ + 'uuid' => $uuid + ], + [ + 'plant_id' => $plantId, + 'vehicle_number' => $vehicleNo, + 'entry_time' => $entryTime, + 'exit_time' => $exitTime ?? null, + 'duration' => $duration ?: null, + 'type' => $type, + 'updated_at' => now(), + 'created_at' => now(), + ] + ); } - $exitTime = null; - - if(!empty($exitTimeRaw)){ - - $exitTime = $exitTimeRaw - ? Carbon::createFromFormat('d/m/Y h:i:s A', $exitTimeRaw) - : null; - } - - $record = VehicleEntry::insert([ - 'plant_id' => $plantId, - 'vehicle_number' => $vehicleNo, - 'entry_time' => $entryTime, - 'exit_time' => $exitTime ?? null, - 'duration' => $duration ?: null, - 'type' => $type, - 'created_at' => now(), - 'updated_at' => now(), - ]); - if($record){ return response()->json([ 'status' => 'SUCCESS', -- 2.49.1