From dafe1ca4580833fdc5484fb9f63dacbad7c1c3e8 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 12 Jul 2025 16:20:00 +0530 Subject: [PATCH] Added gurad names get api for guard dashboard --- .../ModuleGuardHourlyCountController.php | 173 ++++++++++++++++++ .../Controllers/ModuleGuardNameController.php | 124 +++++++++++++ routes/api.php | 6 + 3 files changed, 303 insertions(+) create mode 100644 app/Http/Controllers/ModuleGuardHourlyCountController.php create mode 100644 app/Http/Controllers/ModuleGuardNameController.php diff --git a/app/Http/Controllers/ModuleGuardHourlyCountController.php b/app/Http/Controllers/ModuleGuardHourlyCountController.php new file mode 100644 index 000000000..e9e651047 --- /dev/null +++ b/app/Http/Controllers/ModuleGuardHourlyCountController.php @@ -0,0 +1,173 @@ +header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $plantName = $request->header('plant-name'); + $dateHeader = $request->header('date'); // required: format Y-m-d + $guardIdHeader = $request->header('guard-name'); // required: guard_name_id + $timeHeader = $request->header('time-range'); // required: "08:00 - 10:00" + + if (!$plantName || !$dateHeader || !$guardIdHeader || !$timeHeader) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Missing one or more headers: plant-name, date, guard-name, time-range' + ], 404); + } + + $plant = Plant::where('name', $plantName)->first(); + if (!$plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant '{$plantName}' not found!" + ], 404); + } + + $selectedPlant = $plant->id; + //$selectedDate = $dateHeader; + $selectedDate = Carbon::createFromFormat('d-m-Y', $dateHeader)->format('Y-m-d'); + // $selectedGuardId = $guardIdHeader; + $guard = GuardName::where('name', $guardIdHeader)->first(); + + if (!$guard) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Guard '{$guardIdHeader}' not found!" + ], 404); + } + + $selectedGuardId = $guard->id; + + [$startTime, $endTime] = explode(' - ', $timeHeader); + // $startFormatted = Carbon::parse("{$selectedDate} {$startTime}"); + // $endFormatted = Carbon::parse("{$selectedDate} {$endTime}"); + + // $startDateTime = $startFormatted->format('H:i:s'); + // $endDateTime = $endFormatted->format('H:i:s'); + try + { + [$startTime, $endTime] = explode(' - ', $timeHeader); + $startDateTime = Carbon::parse("{$selectedDate} {$startTime}"); + $endDateTime = Carbon::parse("{$selectedDate} {$endTime}"); + } + catch (\Exception $e) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid time-range format. Use HH:MM:SS - HH:MM:SS" + ], 400); + } + + $patrols = GuardPatrolEntry::where('plant_id', $selectedPlant) + ->where('guard_name_id', $selectedGuardId) + ->whereBetween('patrol_time', [$startDateTime, $endDateTime]) + ->orderBy('patrol_time') + ->get(); + + if ($patrols->count() < 2) { + return response()->json([ + 'status_code' => 'SUCCESS', + 'labels' => [], + 'datasets' => [] + ]); + } + + $checkPointTimes = CheckPointTime::where('plant_id', $selectedPlant) + ->orderBy('sequence_number') + ->get(['sequence_number', 'min_cushioning', 'max_cushioning', 'check_point1_id', 'check_point2_id']); + + $expectedSequence = []; + foreach ($checkPointTimes as $row) { + $expectedSequence[] = $row->check_point1_id; + } + if ($checkPointTimes->isNotEmpty()) { + $expectedSequence[] = $checkPointTimes->last()->check_point2_id; + } + + $intervals = []; + $labels = []; + $currentSeqIndex = 0; + + for ($i = 0; $i < $patrols->count() - 1; $i++) { + $current = Carbon::parse($patrols[$i]->patrol_time); + $next = Carbon::parse($patrols[$i + 1]->patrol_time); + $interval = round($next->floatDiffInRealSeconds($current, true) / 60, 2); + + $intervals[] = $interval; + $labels[] = "Seq " . ($i + 1); + } + + return response()->json([ + 'status_code' => 'SUCCESS', + 'labels' => $labels, + 'datasets' => [ + [ + 'label' => 'Interval (minutes)', + 'data' => array_map(fn($val) => $val ?: "", $intervals), + ] + ] + ]); + } + + + /** + * 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) + { + // + } +} diff --git a/app/Http/Controllers/ModuleGuardNameController.php b/app/Http/Controllers/ModuleGuardNameController.php new file mode 100644 index 000000000..403ce8ea2 --- /dev/null +++ b/app/Http/Controllers/ModuleGuardNameController.php @@ -0,0 +1,124 @@ +header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $plantName = $request->header('plant-name'); + $dateHeader = $request->header('date'); + + if (!$plantName) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant value can't be empty" + ], 404); + } + else if (!$dateHeader) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Date value can't be empty" + ], 404); + } + + $plant = Plant::where('name', $plantName)->first(); + if (!$plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant '{$plantName}' not found!" + ], 404); + } + + $selectedPlant = $plant->id; + + try + { + $selectedDate = Carbon::createFromFormat('d-m-Y', $dateHeader)->format('Y-m-d'); + } + catch (\Exception $e) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid date format. Expected format: d-m-Y" + ], 400); + } + //$selectedDate = Carbon::createFromFormat('d-m-Y', $dateHeader)->format('Y-m-d'); + + $guardNames = GuardPatrolEntry::join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id') + ->where('guard_patrol_entries.plant_id', $selectedPlant) + ->whereDate('guard_patrol_entries.patrol_time', $selectedDate) + ->select('guard_names.name') + ->distinct() + ->orderBy('guard_names.name') + ->pluck('name'); + + if ($guardNames->isEmpty()) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Guard names not found for the selected plant and date.' + ], 404); + } + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => $guardNames, + ]); + } + + /** + * 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) + { + // + } +} diff --git a/routes/api.php b/routes/api.php index 2ad77d873..aec3ed2e7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -7,6 +7,8 @@ use App\Http\Controllers\ModuleFGLineController; use App\Http\Controllers\ModuleFilterController; use App\Http\Controllers\ModuleFilterDataController; use App\Http\Controllers\ModuleGuardDayCountController; +use App\Http\Controllers\ModuleGuardHourlyCountController; +use App\Http\Controllers\ModuleGuardNameController; use App\Http\Controllers\ModuleInvoiceDataController; use App\Http\Controllers\ModuleInvoiceQuantityController; use App\Http\Controllers\ModuleInvoiceTypeController; @@ -106,3 +108,7 @@ Route::get('get/module-invoice-quantity/data', [ModuleInvoiceQuantityController: //Guard Dashboard Controller Route::get('get/module-guard-day/data', [ModuleGuardDayCountController::class, 'get_guardDay_countData']); + +Route::get('get/module-guard-hourly/data', [ModuleGuardHourlyCountController::class, 'get_guardDay_hourlyData']); + +Route::get('get/module-guard-name/data', [ModuleGuardNameController::class, 'get_guard_name_Data']);