From b153a53f4bb7981bae264b20554f3c39aa2990df Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 11 Jul 2025 15:51:09 +0530 Subject: [PATCH] Added logic for all lines in modulefilterdataController and changed logic in module controller --- app/Http/Controllers/ModuleController.php | 14 +- .../ModuleFilterDataController.php | 188 +++++++++++++++--- 2 files changed, 166 insertions(+), 36 deletions(-) diff --git a/app/Http/Controllers/ModuleController.php b/app/Http/Controllers/ModuleController.php index b6e392fe7..f766e1eab 100644 --- a/app/Http/Controllers/ModuleController.php +++ b/app/Http/Controllers/ModuleController.php @@ -71,14 +71,20 @@ class ModuleController extends Controller ->get() ->unique('module_name') ->pluck('module_name') - ->values(); // reset array keys + ->values(); + + if ($uniqueModules->isEmpty()) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Module names not found' + ], 404); + } return response()->json([ 'status_code' => 'SUCCESS', - 'modules' => $uniqueModules - ]); - + 'status_description' => $uniqueModules + ], 200); } /** diff --git a/app/Http/Controllers/ModuleFilterDataController.php b/app/Http/Controllers/ModuleFilterDataController.php index 9416933fd..27feaab84 100644 --- a/app/Http/Controllers/ModuleFilterDataController.php +++ b/app/Http/Controllers/ModuleFilterDataController.php @@ -17,6 +17,99 @@ class ModuleFilterDataController extends Controller // } + // public function get_moduleFilterData(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()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => 'Invalid authorization token!' + // ], 403); + // } + + // $moduleName = $request->header('module-name'); + // $chartName = $request->header('chart-name'); + // $plantName = $request->header('plant-name'); + // $lineName = $request->header('line-name'); + // $filterName = $request->header('filter-name'); + + // $requiredHeaders = [ + // 'module-name', + // 'chart-name', + // 'plant-name', + // 'line-name', + // 'filter-name', + // ]; + + // $missingHeaders = []; + + // foreach ($requiredHeaders as $header) { + // if (empty($request->header($header))) { + // $missingHeaders[] = $header; + // } + // } + + // if (!empty($missingHeaders)) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => 'Missing required headers: ' . implode(', ', $missingHeaders) + // ], 400); + // } + + // // Fetch plant ID + // $plant = Plant::where('name', $plantName)->first(); + // if (!$plant) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => "Plant '{$plantName}' not found!" + // ], 404); + // } + + // // Fetch line ID + // $line = Line::where('name', $lineName)->where('plant_id', $plant->id)->first(); + // if (!$line) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => "Line '{$lineName}' not found for plant '{$plantName}'" + // ], 404); + // } + + // $activeFilter = strtolower(trim($request->header('filter-name'))); + + // if ($activeFilter == 'yesterday') { + // $startDate = now()->subDay()->setTime(8, 0, 0); + // $endDate = now()->setTime(8, 0, 0); + // } + // elseif ($activeFilter == 'this week') { + // $startDate = now()->startOfWeek()->setTime(8, 0, 0); + // $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); + // } + // elseif ($activeFilter == 'this month') { + // $startDate = now()->startOfMonth(); + // $endDate = now()->endOfMonth(); + // } + // else { + // $startDate = now()->setTime(8, 0, 0); + // $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + // } + + // $rowCount = ProductionQuantity::where('plant_id', $plant->id) + // ->where('line_id', $line->id) + // ->whereBetween('created_at', [$startDate, $endDate]) + // ->count(); + + // return response()->json([ + // 'status_code' => 'SUCCESS', + // 'status_description' => $rowCount + // ], 200); + // } + public function get_moduleFilterData(Request $request) { $expectedUser = env('API_AUTH_USER'); @@ -25,20 +118,13 @@ class ModuleFilterDataController extends Controller $header_auth = $request->header('Authorization'); $expectedToken = $expectedUser . ':' . $expectedPw; - if ("Bearer " . $expectedToken != $header_auth) - { + if ("Bearer " . $expectedToken != $header_auth) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid authorization token!' ], 403); } - $moduleName = $request->header('module-name'); - $chartName = $request->header('chart-name'); - $plantName = $request->header('plant-name'); - $lineName = $request->header('line-name'); - $filterName = $request->header('filter-name'); - $requiredHeaders = [ 'module-name', 'chart-name', @@ -62,7 +148,20 @@ class ModuleFilterDataController extends Controller ], 400); } - // Fetch plant ID + $validFilters = ['Today', 'Yesterday', 'This Week', 'This Month']; + $filterHeader = $request->header('filter-name'); + + if (!in_array($filterHeader, $validFilters)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid filter-name value! Accepted values are: " . implode(', ', $validFilters) + ], 400); + } + + $plantName = $request->header('plant-name'); + $lineName = $request->header('line-name'); + $filterName = strtolower(trim($request->header('filter-name'))); + $plant = Plant::where('name', $plantName)->first(); if (!$plant) { return response()->json([ @@ -71,7 +170,50 @@ class ModuleFilterDataController extends Controller ], 404); } - // Fetch line ID + // Set date filter range + if ($filterName === 'yesterday') { + $startDate = now()->subDay()->setTime(8, 0, 0); + $endDate = now()->setTime(8, 0, 0); + } elseif ($filterName === 'this week') { + $startDate = now()->startOfWeek()->setTime(8, 0, 0); + $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); + } elseif ($filterName === 'this month') { + $startDate = now()->startOfMonth(); + $endDate = now()->endOfMonth(); + } else { + $startDate = now()->setTime(8, 0, 0); + $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + } + + // Handle All Lines + if (strtolower(trim($lineName)) == 'all lines') { + $lines = Line::where('plant_id', $plant->id)->pluck('id', 'name'); + + if ($lines->isEmpty()) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "No lines found for plant '{$plantName}'" + ], 404); + } + + $lineCounts = []; + + foreach ($lines as $name => $id) { + $count = ProductionQuantity::where('plant_id', $plant->id) + ->where('line_id', $id) + ->whereBetween('created_at', [$startDate, $endDate]) + ->count(); + + $lineCounts[$name] = $count; + } + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => $lineCounts + ], 200); + } + + // Handle single line $line = Line::where('name', $lineName)->where('plant_id', $plant->id)->first(); if (!$line) { return response()->json([ @@ -80,29 +222,10 @@ class ModuleFilterDataController extends Controller ], 404); } - $activeFilter = strtolower(trim($request->header('filter-name'))); - - if ($activeFilter == 'yesterday') { - $startDate = now()->subDay()->setTime(8, 0, 0); - $endDate = now()->setTime(8, 0, 0); - } - elseif ($activeFilter == 'this_week') { - $startDate = now()->startOfWeek()->setTime(8, 0, 0); - $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); - } - elseif ($activeFilter == 'this_month') { - $startDate = now()->startOfMonth(); - $endDate = now()->endOfMonth(); - } - else { // today or default - $startDate = now()->setTime(8, 0, 0); - $endDate = now()->copy()->addDay()->setTime(8, 0, 0); - } - $rowCount = ProductionQuantity::where('plant_id', $plant->id) - ->where('line_id', $line->id) - ->whereBetween('created_at', [$startDate, $endDate]) - ->count(); + ->where('line_id', $line->id) + ->whereBetween('created_at', [$startDate, $endDate]) + ->count(); return response()->json([ 'status_code' => 'SUCCESS', @@ -110,6 +233,7 @@ class ModuleFilterDataController extends Controller ], 200); } + /** * Store a newly created resource in storage. */