1
0
forked from poc/pds

Updated validation functionality and failure messages in controller files

This commit is contained in:
dhanabalan
2025-07-05 18:25:11 +05:30
parent 3eb9f64fca
commit de0de426f0
7 changed files with 218 additions and 340 deletions

View File

@@ -3,6 +3,8 @@
namespace App\Http\Controllers;
use App\Models\Item;
use App\Models\Line;
use App\Models\Machine;
use App\Models\MotorTestingMaster;
use App\Models\Plant;
use App\Models\TestingPanelReading;
@@ -14,6 +16,7 @@ use chillerlan\QRCode\QROptions;
use chillerlan\QRCode\Output\QROutputInterface;
use Mpdf\QrCode\Output;
use Mpdf\QrCode\QrCode;
use Str;
class TestingPanelController extends Controller
{
@@ -36,19 +39,31 @@ class TestingPanelController extends Controller
$header_auth = $request->header('Authorization');
$expectedToken = $expectedUser . ':' . $expectedPw;
if("Bearer " . $expectedToken !== $header_auth)
if ("Bearer " . $expectedToken != $header_auth)
{
return response("Unauthorized", 403)
->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Invalid authorization token!'
], 403);
}
$data = $request->all();
if ($data['plant_code'] == '' || !ctype_digit($data['plant_code'])) {
if ($data['plant_code'] == null || $data['plant_code'] == '')
{
// return response("ERROR: Please provide a valid plant code.", 400)
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Please provide a valid plant code.'
'status_description' => "Plant code can't be empty!"
], 400);
}
else if (Str::length($data['plant_code']) < 4 || !is_numeric($data['plant_code']) || !preg_match('/^[1-9]\d{3,}$/', $data['plant_code']))//!ctype_digit($data['plant_code'])
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Invalid plant code found!"
], 400);
}
@@ -57,88 +72,80 @@ class TestingPanelController extends Controller
//return response("Plant not found.", 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Plant not found.'
'status_description' => 'Plant not found!'
], 400);
}
$plantId = $plant->id;
$line = \App\Models\Line::where('name', $data['line_name'])->first();
$line = Line::where('group_work_center', $data['line_name'])->first();
if (!$line)
{
//return response("Line not found.", 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Line not found.'
'status_description' => 'Group work center not found!'
], 400);
}
$line = \App\Models\Line::where('name', $data['line_name'])
->where('plant_id', $plantId)
->first();
$line = Line::where('group_work_center', $data['line_name'])->where('plant_id', $plantId)->first();
if (!$line)
{
//return response( "Line not found for the specified plant : {$data['plant_code']}",400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Line not found for the specified plant : {$data['plant_code']}."
'status_description' => "Group work center not found for the specified plant : '{$data['plant_code']}'!"
], 400);
}
$lineId = $line->id;
$machine = \App\Models\Machine::where('name', $data['machine_name'])
->first();
$machine = Machine::where('work_center', $data['machine_name'])->first();
if (!$machine)
{
// return response('Machine not found', 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Machine not found.'
'status_description' => 'Work center not found!'
], 400);
}
$machine = \App\Models\Machine::where('name', $data['machine_name'])
->where('plant_id', $plantId)
->first();
$machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->first();
if (!$machine)
{
// return response("Machine not found for the specified plant : {$data['plant_code']}", 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Machine not found for the specified plant : {$data['plant_code']}."
'status_description' => "Work center not found for the specified plant : '{$data['plant_code']}'!"
], 400);
}
$machine = \App\Models\Machine::where('name', $data['machine_name'])
->where('line_id', $lineId)
->first();
$machine = Machine::where('work_center', $data['machine_name'])->where('line_id', $lineId)->first();
if (!$machine)
{
// return response("Machine not found for the specified line : {$data['line_name']}", 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Machine not found for the specified line : {$data['line_name']}."
'status_description' => "Work center not found for the specified Group work center : '{$data['line_name']}'!"
], 400);
}
$machine = \App\Models\Machine::where('name', $data['machine_name'])
->where('plant_id', $plantId)
->where('line_id', $lineId)
->first();
$machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->where('line_id', $lineId)->first();
if (!$machine) {
// return response('Machine not found for the specified plant and line', 400)
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Machine not found for the specified plant and line.'
'status_description' => 'Work center not found for the specified plant and group work center!'
], 400);
}
$machineId = $machine->id;
try
@@ -148,20 +155,18 @@ class TestingPanelController extends Controller
$duplicateItemCodes = [];
$existSnoCount = [];
if (!empty($data['item_codes']) && is_array($data['item_codes']))
{
foreach ($data['item_codes'] as $item)
{
$code = $item['item_code'] ?? null;
// Check if item_code is present
if ($code == '')
if ($code == '' || $code == null)
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Item code cannot be empty.'
'status_description' => "Item code can't be empty!"
], 400);
}
@@ -212,7 +217,7 @@ class TestingPanelController extends Controller
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : {$plant->name}"
'status_description' => "Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : '{$plant->name}'!"
], 400);
}
@@ -248,7 +253,7 @@ class TestingPanelController extends Controller
->select(TestingPanelReading::raw('MAX(CAST(update_count AS INTEGER)) AS max_update_count'))
->value('max_update_count');
$newUpdateCount = ($maxUpdateCount === null || $maxUpdateCount === '') ? 0 : $maxUpdateCount + 1;
$newUpdateCount = ($maxUpdateCount == null || $maxUpdateCount == '') ? 0 : $maxUpdateCount + 1;
$updateCountString = (string) $newUpdateCount;
@@ -328,27 +333,20 @@ class TestingPanelController extends Controller
}
else
{
// $missingSno = array_diff($existSnoCount,$insertedSnoCount);
// $messages[] = "Missed serial numbers are: " . implode(', ', $missingSno);
$missingSno = array_diff($existSnoCount,$insertedSnoCount);
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Missed serial numbers are: " . implode(', ', $missingSno)'
'status_description' => "Missed serial numbers are: " . implode(', ', $missingSno)
], 400);
}
}
// return response("" . implode("\n", $messages), 200)
// ->header('Content-Type', 'text/plain');
}
catch (\Exception $e)
{
// return response($e->getMessage(), 500)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Internal Sever Error : '.$e?->getCode()
'status_description' => 'Store testing panel readings internal server error : '.$e?->getCode()
], 500);
}
}
@@ -363,40 +361,34 @@ class TestingPanelController extends Controller
$header_auth = $request->header('Authorization');
$expectedToken = $expectedUser . ':' . $expectedPw;
if("Bearer " . $expectedToken !== $header_auth)
{
return response("Unauthorized", 403)
->header('Content-Type', 'text/plain');
}
//$data = $request->all();
if ("Bearer " . $expectedToken !== $header_auth)
if ("Bearer " . $expectedToken != $header_auth)
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Invalid authorization token'
'status_description' => 'Invalid authorization token!'
], 403);
}
$plantCode = $request->header('plant-code');
$itemCode = $request->header('item-code');
// $description = $item ? $item->description : '';
// $description = $item ? $item->description : '';
// Fetch item by code
$item = Item::where('code', $itemCode)->first();
// Get description or empty string if not found
$description = $item ? $item->description : '';
$category = $item ? $item->category : '';
if ($plantCode === null || $plantCode === '')
if ($plantCode == null || $plantCode == '')
{
// return response("ERROR: Plant Name can't be empty", 400)
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Plant Name cannot be empty"
'status_description' => "Plant code can't be empty!"
], 400);
}
else if (Str::length($plantCode) < 4 || !is_numeric($plantCode) || !preg_match('/^[1-9]\d{3,}$/', $plantCode))
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Invalid plant code found!"
], 400);
}
else if($itemCode == null || $itemCode == '')
@@ -405,7 +397,14 @@ class TestingPanelController extends Controller
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item Code cannot be empty"
'status_description' => "Item Code can't be empty!"
], 400);
}
else if(Str::length($itemCode) < 6 || !ctype_alnum($itemCode))
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Invalid item code found!"
], 400);
}
@@ -414,7 +413,7 @@ class TestingPanelController extends Controller
if (!$plant) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Plant not found"
'status_description' => "Plant not found!"
], 400);
}
@@ -426,59 +425,70 @@ class TestingPanelController extends Controller
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item Code not found in item table for the plant : $plant->name"
'status_description' => "Item Code not found in item table!"
], 404);
}
$motorTestingMaster = MotorTestingMaster::where('plant_id', $plant->id)
->where('item_id', $item->id)
->first();
$item = Item::where('plant_id', $plantId)->where('code', $itemCode)->first();
if (!$item)
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item Code not found in item table for the plant : '$plant->name'!"
], 404);
}
// Get description or empty string if not found
$description = $item ? $item->description : '';
$category = $item ? $item->category : '';
$motorTestingMaster = MotorTestingMaster::where('plant_id', $plantId)->where('item_id', $item->id)->first();
if (!$motorTestingMaster)
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item Code not found in master table for the plant : $plant->name"
'status_description' => "Item Code not found in motor testing master table for the plant : '$plant->name'!"
], 404);
}
$output = [
"mot_model_name" => $description ?? "",
"mot_non_isi_model" => $motorTestingMaster->isi_model ? "0" :"1",
"mot_phase" => $motorTestingMaster->phase ?? "",
"mot_hp" => $motorTestingMaster->hp ?? "",
"mot_kw" => $motorTestingMaster->kw ?? "",
"mot_volt" => $motorTestingMaster->volt ?? "",
"mot_cur" => $motorTestingMaster->current ?? "",
"mot_rpm" => $motorTestingMaster->rpm ?? "",
"mot_rate_torque_kg" => $motorTestingMaster->torque ?? "",
"mot_freq" => $motorTestingMaster->frequency ?? "",
"mot_conn" => $motorTestingMaster->connection ?? "",
"mot_ins_res_limit" => $motorTestingMaster->ins_res_limit ?? "",
"mot_ins_res_type" => $motorTestingMaster->ins_res_type ?? "",
"mot_category" => $category ?? "",
"mot_routine_test_time" => $motorTestingMaster->routine_test_time ?? "",
"mot_res_ry_ll" => $motorTestingMaster->res_ry_ll ?? "",
"mot_res_ry_ul" => $motorTestingMaster->res_ry_ul ?? "",
"mot_res_yb_ll" => $motorTestingMaster->res_yb_ll ?? "",
"mot_res_yb_ul" => $motorTestingMaster->res_yb_ul ?? "",
"mot_res_br_ll" => $motorTestingMaster->res_br_ll ?? "",
"mot_res_br_ul" => $motorTestingMaster->res_br_ul ?? "",
"mot_lock_volt_limit" => $motorTestingMaster->lock_volt_limit ?? "",
"mot_leak_cur_limit" => $motorTestingMaster->leak_cur_limit ?? "",
"mot_lock_cur_ll" => $motorTestingMaster->lock_cur_ll ?? "",
"mot_lock_cur_ul" => $motorTestingMaster->lock_cur_ul ?? "",
"mot_noload_cur_ll" => $motorTestingMaster->noload_cur_ll ?? "",
"mot_noload_cur_ul" => $motorTestingMaster->noload_cur_ul ?? "",
"mot_noload_pow_ll" => $motorTestingMaster->noload_pow_ll ?? "",
"mot_noload_pow_ul" => $motorTestingMaster->noload_pow_ul ?? "",
"mot_noload_spd_ll" => $motorTestingMaster->noload_spd_ll ?? "",
"mot_noload_spd_ul" => $motorTestingMaster->noload_spd_ul ?? "",
"mot_model_name" => $description,
"mot_non_isi_model" => $motorTestingMaster->isi_model ? "0" :"1",
"mot_phase" => $motorTestingMaster->phase ?? "",
"mot_hp" => $motorTestingMaster->hp ?? "",
"mot_kw" => $motorTestingMaster->kw ?? "",
"mot_volt" => $motorTestingMaster->volt ?? "",
"mot_cur" => $motorTestingMaster->current ?? "",
"mot_rpm" => $motorTestingMaster->rpm ?? "",
"mot_rate_torque_kg" => $motorTestingMaster->torque ?? "",
"mot_freq" => $motorTestingMaster->frequency ?? "",
"mot_conn" => $motorTestingMaster->connection ?? "",
"mot_ins_res_limit" => $motorTestingMaster->ins_res_limit ?? "",
"mot_ins_res_type" => $motorTestingMaster->ins_res_type ?? "",
"mot_category" => $category,
"mot_routine_test_time" => $motorTestingMaster->routine_test_time ?? "",
"mot_res_ry_ll" => $motorTestingMaster->res_ry_ll ?? "",
"mot_res_ry_ul" => $motorTestingMaster->res_ry_ul ?? "",
"mot_res_yb_ll" => $motorTestingMaster->res_yb_ll ?? "",
"mot_res_yb_ul" => $motorTestingMaster->res_yb_ul ?? "",
"mot_res_br_ll" => $motorTestingMaster->res_br_ll ?? "",
"mot_res_br_ul" => $motorTestingMaster->res_br_ul ?? "",
"mot_lock_volt_limit" => $motorTestingMaster->lock_volt_limit ?? "",
"mot_leak_cur_limit" => $motorTestingMaster->leak_cur_limit ?? "",
"mot_lock_cur_ll" => $motorTestingMaster->lock_cur_ll ?? "",
"mot_lock_cur_ul" => $motorTestingMaster->lock_cur_ul ?? "",
"mot_noload_cur_ll" => $motorTestingMaster->noload_cur_ll ?? "",
"mot_noload_cur_ul" => $motorTestingMaster->noload_cur_ul ?? "",
"mot_noload_pow_ll" => $motorTestingMaster->noload_pow_ll ?? "",
"mot_noload_pow_ul" => $motorTestingMaster->noload_pow_ul ?? "",
"mot_noload_spd_ll" => $motorTestingMaster->noload_spd_ll ?? "",
"mot_noload_spd_ul" => $motorTestingMaster->noload_spd_ul ?? "",
];
return response()->json($output, 200);
}
/**