header('Authorization'); $expectedToken = $expectedUser.':'.$expectedPw; if ('Bearer '.$expectedToken != $header_auth) { return response('ERROR: Unauthorized', 403) ->header('Content-Type', 'text/plain'); } $plantCode = $request->header('plant-code'); if(!$plantCode){ return response()->json([ 'status' => 'ERROR', 'status_description' => "Plant code can't be empty!", ], 404); } if (!ctype_digit((string) $plantCode)) { return response()->json([ 'status' => 'ERROR', 'status_description' => "plant code must be $plantCode a numeric value", ], 404); } $plantCod = Plant::where('code', $plantCode)->first(); if(!$plantCod){ return response()->json([ 'status' => 'ERROR', 'status_description' => "Plant code $plantCode not found!", ], 404); } $plantId = $plantCod->id; // $data = $request->all(); $data = $request->json()->all(); $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(!empty($results)){ return response()->json($results, 404); } 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(), ] ); } if($record){ return response()->json([ 'status' => 'SUCCESS', 'status_description' => 'Vehicle entry inserted successfully' ], 200); } else { return response()->json([ 'status' => 'ERROR', 'status_description' => 'Failed to insert record in the table vehicle entry' ], 404); } } /** * Store a newly created resource in storage. */ public function store(Request $request) { // } /** * Display the specified resource. */ public function show(string $id) { // } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { // } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } }