Files
pds/app/Http/Controllers/PlantController.php
dhanabalan d4186d11c9
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 16s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 27s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 27s
Laravel Pint / pint (pull_request) Successful in 4m37s
Laravel Larastan / larastan (pull_request) Failing after 6m4s
Added leak test line validation in plant controller
2026-11-03 10:50:05 +05:30

267 lines
8.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Item;
use App\Models\Line;
use App\Models\Plant;
use Illuminate\Http\Request;
class PlantController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function get_all_data(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);
}
$plants = Plant::with('company')->orderBy('code')->get();
$plantsData = $plants->map(function ($plant) {
return [
'company' => $plant->company ? $plant->company->name : '', // Get company name
'plant_code' => (string) $plant->code,
'plant_name' => $plant->name,
'plant_warehouse_number' => (string) $plant->warehouse_number,
'plant_address' => $plant->address,
];
});
return response()->json([
'plants' => $plantsData,
]);
}
public function getItemCode(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);
}
$plantCode = $request->header('plant-code');
$itemCode = $request->header('item-code');
if (! $plantCode) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Plant Code value can't be empty",
], 404);
} elseif (! $itemCode) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Item code cannot be empty!',
], 404);
}
$plant = Plant::where('code', $plantCode)->first();
$plantId = $plant->id;
if (! $plant) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Plant Code '{$plantCode}' not found!",
], 404);
}
$itemCodeExist = Item::where('code', $itemCode)->first();
if (! $itemCodeExist) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Item code not found',
], 404);
}
$itemCodePlantExist = Item::where('code', $itemCode)->where('plant_id', $plantId)->first();
if (! $itemCodePlantExist) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item code not found against plant code '$plantCode'",
], 404);
}
if (empty(trim($itemCodePlantExist->description ?? ''))) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Description is empty in Item Master against item code '$itemCode'.",
], 404);
}
return response()->json([
'status_code' => 'SUCCESS',
'status_description' => [
'description' => $itemCodePlantExist->description,
],
], 200);
}
public function getItemLine(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);
}
$plantCode = $request->header('plant-code');
$lineName = $request->header('line-name');
$itemCode = $request->header('item-code');
if (! $plantCode) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Plant Code value can't be empty",
], 404);
}
elseif (! $lineName) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Line Name cannot be empty!',
], 404);
} elseif (! $itemCode) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Item code cannot be empty!',
], 404);
}
$plant = Plant::where('code', $plantCode)->first();
$plantId = $plant->id;
if (! $plant) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Plant Code '{$plantCode}' not found!",
], 404);
}
$itemCodeExist = Item::where('code', $itemCode)->first();
if (! $itemCodeExist) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Item code not found',
], 404);
}
$itemCodePlantExist = Item::where('code', $itemCode)->where('plant_id', $plantId)->first();
if (! $itemCodePlantExist) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item code not found against plant code '$plantCode'",
], 404);
}
$lineNameExist = Line::where('name', $lineName)->first();
if (! $lineNameExist) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Line Name '$lineName' not found",
], 404);
}
$lineNamePlantExist = Line::where('plant_id', $plantId)->where('name', $lineName)->first();
if (! $lineNamePlantExist) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Line Name '$lineName' not found against plant code '$plantCode'",
], 404);
}
$itemCodeLineExist = Item::where('code', $itemCode)
->where('plant_id', $plantId)
->whereNotNull('line_id')
->where('line_id', '!=', $lineNamePlantExist->id)
->first();
if ($itemCodeLineExist) {
$mappedLine = Line::find($itemCodeLineExist->line_id);
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item code '$itemCode' is already mapped to line '{$mappedLine->name}'",
], 404);
}
$itemCodeLineMapped = Item::where('code', $itemCode)
->where('plant_id', $plantId)
->where('line_id', $lineNamePlantExist->id)
->first();
if (! $itemCodeLineMapped) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Not Ok'
], 200);
}
else{
return response()->json([
'status_code' => 'SUCCESS',
'status_description' => 'Ok',
], 200);
}
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}