diff --git a/app/Http/Controllers/ObdController.php b/app/Http/Controllers/ObdController.php new file mode 100644 index 000000000..1d2b21496 --- /dev/null +++ b/app/Http/Controllers/ObdController.php @@ -0,0 +1,258 @@ +header('Authorization'); + + $expectedToken = $expectedUser . ':' . $expectedPw; + + if("Bearer " . $expectedToken !== $header_auth) + { + return response("ERROR: Unauthorized", 403) + ->header('Content-Type', 'text/plain'); + } + + try + { + $data = $request->all(); + // Validate required fields + $missing = []; + if (empty($data['plant_name'])) $missing[] = 'plant_name'; + if (empty($data['obd_number'])) $missing[] = 'obd_number'; + if (empty($data['line_numbers'])) $missing[] = 'line_numbers'; + + if (!empty($missing)) + { + $message = "ERROR: Missing required field(s): " . implode(', ', $missing); + return response($message, 400)->header('Content-Type', 'text/plain'); + } + + // Lookup plant_id by plant_name + $plantId = Plant::where('name', $data['plant_name'])->value('id'); + if (!$plantId) { + return response("ERROR: Plant '" . $data['plant_name'] . "' not found", 404) + ->header('Content-Type', 'text/plain'); + } + + $missingLines = []; + $updated = 0; + + foreach ($data['line_numbers'] as $line) + { + if (empty($line['line_number'])) continue; + + $exists = WeightValidation::where('plant_id', $plantId) + ->where('obd_number', $data['obd_number']) + ->where('line_number', $line['line_number']) + ->exists(); + + if (!$exists) + { + $missingLines[] = $line['line_number']; + continue; + } + + $count = WeightValidation::where([ + 'plant_id' => $plantId, + 'obd_number' => $data['obd_number'], + 'line_number'=> $line['line_number'], + ]) + ->update([ + 'vehicle_number' => $line['vehicle_number'] ?? null, + 'bundle_number' => $line['bundle_number'] ?? null, + 'picked_weight' => $line['picked_weight'] ?? null, + 'scanned_by' => $line['scanned_by'] ?? null, + 'updated_at' => now(), + ]); + + $updated += $count; + } + + if (!empty($missingLines)) { + $message = "ERROR: Line(s) " . implode(', ', $missingLines) . " not found for Plant ID '" . $plantId . "' and OBD Number: '" . $data['obd_number'] . "'"; + return response($message, 404)->header('Content-Type', 'text/plain'); + } else { + $message = "SUCCESS: OBD Number '" . $data['obd_number'] . "' updated successfully"; + return response($message, 200)->header('Content-Type', 'text/plain'); + } + } + catch (\Exception $e) + { + return response("ERROR: Server error", 500)->header('Content-Type', 'text/plain'); + } + } + + /** + * Display the specified resource. + */ + + // public function get(Request $request) + // { + // $expectedUser = env('API_AUTH_USER'); + // $expectedPw = env('API_AUTH_PW'); + + // $header_auth = $request->header('Authorization'); + + // $expectedToken = $expectedUser . ':' . $expectedPw; + + // if ("Bearer " . $expectedToken !== $header_auth) { + // return response("ERROR: Unauthorized", 403) + // ->header('Content-Type', 'text/plain'); + // } + + // $plantName = $request->header('Plant_Name'); + // $obdNumber = $request->header('obd_number'); + + // if (empty($plantName) || empty($obdNumber)) { + // return response("ERROR: Missing required headers (Plant_Name, OBD_Number)", 400) + // ->header('Content-Type', 'text/plain'); + // } + + // // Fetch the plant record by name + // $plant = Plant::where('name', $plantName)->value('id'); + + // if (!$plant) + // { + // return response("ERROR: Plant not found", 400) + // ->header('Content-Type', 'text/plain'); + // } + + // $plantId = $plant->id; + + // $records = WeightValidation::where('plant_id', $plantId) + // ->where('obd_number', $obdNumber) + // ->get(); + + // if ($records->isEmpty()) { + // return response("ERROR: No records found", 404)->header('Content-Type', 'text/plain'); + // } + + // $itemIds = $records->pluck('item_id')->unique(); + // $itemCodes = Item::whereIn('id', $itemIds) + // ->select('id', 'code', 'description') + // ->get() + // ->keyBy('id'); + + // $ObdResponseStructure = [ + // 'OBD_Number' => [ + // [ + // 'OBD_Number' => $obdNumber, + // 'Line_Numbers' => $records->map(function ($item) use ($itemCodes) { + // $itemInfo = $itemCodes[$item->item_id] ?? null; + // return [ + // 'Line' => $item->line_number, + // 'Material_Code' => $itemInfo->code ?? "", + // 'Material_Description' => $itemInfo->description ?? "", + // // 'Quantity' => $item->picked_weight ?? "", + // 'Batch_Number' => $item->batch_number ?? "", + // 'Heat_Number' => $item->heat_number ?? "", + // ]; + // })->toArray() + // ] + // ] + // ]; + // return response()->json($ObdResponseStructure); + // } + + public function get(Request $request) + { + $expectedUser = env('API_AUTH_USER'); + $expectedPw = env('API_AUTH_PW'); + $header_auth = $request->header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken !== $header_auth) { + return response("ERROR: Unauthorized", 403) + ->header('Content-Type', 'text/plain'); + } + + $plantName = $request->header('plant_name'); // use lowercase + $obdNumber = $request->header('obd_number'); + + if (empty($plantName) || empty($obdNumber)) { + return response("ERROR: Missing required headers (plant_name, obd_number)", 400) + ->header('Content-Type', 'text/plain'); + } + + // Fetch the plant id by name + $plantId = Plant::where('name', $plantName)->value('id'); + + if (!$plantId) { + return response("ERROR: Plant not found", 400) + ->header('Content-Type', 'text/plain'); + } + + $records = WeightValidation::where('plant_id', $plantId) + ->where('obd_number', $obdNumber) + ->get(); + + if ($records->isEmpty()) { + return response("ERROR: No records found", 404)->header('Content-Type', 'text/plain'); + } + + $itemIds = $records->pluck('item_id')->unique(); + $itemCodes = Item::whereIn('id', $itemIds) + ->select('id', 'code', 'description') + ->get() + ->keyBy('id'); + + $ObdResponseStructure = [ + 'OBD_Number' => [ + [ + 'OBD_Number' => $obdNumber, + 'Line_Numbers' => $records->map(function ($item) use ($itemCodes) { + $itemInfo = $itemCodes[$item->item_id] ?? null; + return [ + 'Line' => $item->line_number, + 'Material_Code' => $itemInfo->code ?? "", + 'Material_Description' => $itemInfo->description ?? "", + 'Batch_Number' => $item->batch_number ?? "", + 'Heat_Number' => $item->heat_number ?? "", + ]; + })->toArray() + ] + ] + ]; + return response()->json($ObdResponseStructure); + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/routes/api.php b/routes/api.php new file mode 100644 index 000000000..4184f007b --- /dev/null +++ b/routes/api.php @@ -0,0 +1,41 @@ +json([ +// 'message' => 'User updated successfully', +// 'data' => $request->all() +// ]); +// }); + + +// Route::middleware('auth.basic')->post('/user/update', function (Request $request) { +// return response()->json([ +// 'message' => 'Authenticated via Basic Auth', +// 'user' => $request->user(), +// 'data' => $request->all() +// ]); +// }); + + + +Route::post('obd/store-data', [ObdController::class, 'store']) + ->withoutMiddleware(VerifyCsrfToken::class); + +Route::get('obd/store-data/get', [ObdController::class, 'get']);