Added logic for all lines in modulefilterdataController and changed logic in module controller
This commit is contained in:
@@ -71,14 +71,20 @@ class ModuleController extends Controller
|
|||||||
->get()
|
->get()
|
||||||
->unique('module_name')
|
->unique('module_name')
|
||||||
->pluck('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([
|
return response()->json([
|
||||||
'status_code' => 'SUCCESS',
|
'status_code' => 'SUCCESS',
|
||||||
'modules' => $uniqueModules
|
'status_description' => $uniqueModules
|
||||||
]);
|
], 200);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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)
|
public function get_moduleFilterData(Request $request)
|
||||||
{
|
{
|
||||||
$expectedUser = env('API_AUTH_USER');
|
$expectedUser = env('API_AUTH_USER');
|
||||||
@@ -25,20 +118,13 @@ class ModuleFilterDataController extends Controller
|
|||||||
$header_auth = $request->header('Authorization');
|
$header_auth = $request->header('Authorization');
|
||||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
$expectedToken = $expectedUser . ':' . $expectedPw;
|
||||||
|
|
||||||
if ("Bearer " . $expectedToken != $header_auth)
|
if ("Bearer " . $expectedToken != $header_auth) {
|
||||||
{
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Invalid authorization token!'
|
'status_description' => 'Invalid authorization token!'
|
||||||
], 403);
|
], 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 = [
|
$requiredHeaders = [
|
||||||
'module-name',
|
'module-name',
|
||||||
'chart-name',
|
'chart-name',
|
||||||
@@ -62,7 +148,20 @@ class ModuleFilterDataController extends Controller
|
|||||||
], 400);
|
], 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();
|
$plant = Plant::where('name', $plantName)->first();
|
||||||
if (!$plant) {
|
if (!$plant) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
@@ -71,7 +170,50 @@ class ModuleFilterDataController extends Controller
|
|||||||
], 404);
|
], 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();
|
$line = Line::where('name', $lineName)->where('plant_id', $plant->id)->first();
|
||||||
if (!$line) {
|
if (!$line) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
@@ -80,29 +222,10 @@ class ModuleFilterDataController extends Controller
|
|||||||
], 404);
|
], 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)
|
$rowCount = ProductionQuantity::where('plant_id', $plant->id)
|
||||||
->where('line_id', $line->id)
|
->where('line_id', $line->id)
|
||||||
->whereBetween('created_at', [$startDate, $endDate])
|
->whereBetween('created_at', [$startDate, $endDate])
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'SUCCESS',
|
'status_code' => 'SUCCESS',
|
||||||
@@ -110,6 +233,7 @@ class ModuleFilterDataController extends Controller
|
|||||||
], 200);
|
], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a newly created resource in storage.
|
* Store a newly created resource in storage.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user