Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 11s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 13s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 21s
Laravel Pint / pint (pull_request) Successful in 5m36s
Laravel Larastan / larastan (pull_request) Failing after 9m10s
2426 lines
108 KiB
PHP
2426 lines
108 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\BeforeTestReading;
|
|
use App\Models\Item;
|
|
use App\Models\LeakTestReading;
|
|
use App\Models\Machine;
|
|
use App\Models\MotorTestingMaster;
|
|
use App\Models\Plant;
|
|
use App\Models\PumpTestingEntry;
|
|
use App\Models\PumpTestingMaster;
|
|
use App\Models\PumpTestingResult;
|
|
use App\Models\TestingPanelReading;
|
|
use App\Models\User;
|
|
use App\Models\WorkGroupMaster;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Str;
|
|
|
|
class TestingPanelController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created readings in storage.
|
|
*/
|
|
public function storeReadings(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);
|
|
}
|
|
|
|
$data = $request->all();
|
|
|
|
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' => "Plant code can't be empty!",
|
|
], 404);
|
|
} elseif (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!',
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $data['plant_code'])->first();
|
|
if (! $plant) {
|
|
// return response("Plant not found.", 400)->header('Content-Type', 'text/plain');
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Plant not found!',
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
$plantName = $plant->name;
|
|
|
|
if ($data['line_name'] == null || $data['line_name'] == '') {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Group work center can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($data['line_name']) < 0) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid group work center found!',
|
|
], 404);
|
|
}
|
|
|
|
$gWorkCenter = WorkGroupMaster::where('name', $data['line_name'])->first();
|
|
if (! $gWorkCenter) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Group work center not found!',
|
|
], 404);
|
|
}
|
|
|
|
$gWorkCenter = WorkGroupMaster::where('name', $data['line_name'])->where('plant_id', $plantId)->first();
|
|
if (! $gWorkCenter) {
|
|
// 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' => "Group work center not found for the specified plant : '{$data['plant_code']}'!",
|
|
], 404);
|
|
}
|
|
|
|
$gWorkCenterId = $gWorkCenter->id;
|
|
|
|
if ($data['machine_name'] == null || $data['machine_name'] == '') {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Work center can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($data['machine_name']) < 0) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid work center found!',
|
|
], 404);
|
|
}
|
|
|
|
$machine = Machine::where('work_center', $data['machine_name'])->first();
|
|
if (! $machine) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Work center not found!',
|
|
], 404);
|
|
}
|
|
|
|
$machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->first();
|
|
if (! $machine) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Work center not found for the specified plant : '{$data['plant_code']}'!",
|
|
], 404);
|
|
}
|
|
|
|
$machine = Machine::where('work_center', $data['machine_name'])->where('work_group_master_id', $gWorkCenterId)->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' => "Work center not found for the specified Group work center : '{$data['line_name']}'!",
|
|
], 404);
|
|
|
|
}
|
|
|
|
$machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->where('work_group_master_id', $gWorkCenterId)->first();
|
|
if (! $machine) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Work center not found for the specified Plant : '{$data['plant_code']}' and Group work center : '{$data['line_name']}'!",
|
|
], 404);
|
|
}
|
|
|
|
$lineId = $machine->line_id;
|
|
$machineId = $machine->id;
|
|
|
|
try {
|
|
$insertedSerials = [];
|
|
$missedItemCodes = [];
|
|
$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 == '' || $code == null) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code can't be empty!",
|
|
], 404);
|
|
}
|
|
|
|
// Collect duplicates
|
|
if (isset($itemCodeCounts[$code])) {
|
|
$itemCodeCounts[$code]++;
|
|
// Only add to duplicates array once
|
|
if ($itemCodeCounts[$code] == 2) {
|
|
$duplicateItemCodes[] = $code;
|
|
}
|
|
} else {
|
|
$itemCodeCounts[$code] = 1;
|
|
}
|
|
|
|
$motorTestingMaster = MotorTestingMaster::whereHas('item', function ($query) use ($item) {
|
|
$query->where('code', $item['item_code']);
|
|
})->where('plant_id', $plantId)->first();
|
|
|
|
if (! $motorTestingMaster) {
|
|
$missedItemCodes[] = $item['item_code'];
|
|
}
|
|
|
|
if (! empty($item['serial_numbers']) && is_array($item['serial_numbers'])) {
|
|
foreach ($item['serial_numbers'] as $serial) {
|
|
$existSnoCount[] = $serial['serial_number'];
|
|
}
|
|
}
|
|
}
|
|
|
|
// If any duplicates found, return error
|
|
if (! empty($duplicateItemCodes)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Duplicate item codes found in request: '.implode(', ', $duplicateItemCodes),
|
|
], 404);
|
|
}
|
|
|
|
$uniqueInvalidCodes = array_unique($missedItemCodes);
|
|
|
|
if (! empty($uniqueInvalidCodes)) {
|
|
|
|
// return response("Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in motor testing master for the specified plant {$plantName}", 400)
|
|
// ->header('Content-Type', 'text/plain');
|
|
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Item codes : '.implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : '{$plantName}'!",
|
|
], 404);
|
|
}
|
|
|
|
$insertedSnoCount = [];
|
|
|
|
foreach ($data['item_codes'] as $item) {
|
|
|
|
$motorTestingMaster = MotorTestingMaster::whereHas('item', callback: function ($query) use ($item) {
|
|
$query->where('code', $item['item_code']);
|
|
})->where('plant_id', $plantId)->first();
|
|
|
|
$motorTestingMasterId = $motorTestingMaster->id;
|
|
|
|
if (! empty($item['serial_numbers']) && is_array($item['serial_numbers'])) {
|
|
foreach ($item['serial_numbers'] as $serial) {
|
|
// For update_count calculation
|
|
$updateCount = [
|
|
'plant_id' => $plantId,
|
|
'line_id' => $lineId,
|
|
'machine_id' => $machineId,
|
|
'motor_testing_master_id' => $motorTestingMasterId,
|
|
'serial_number' => $serial['serial_number'] ?? null,
|
|
'rework_count' => $serial['rework_count'] ?? 0,
|
|
];
|
|
|
|
// // Find the current max update_count for this composite key //updated_at
|
|
// // $maxUpdateCount = \App\Models\TestingPanelReading::where($updateCount)->max('update_count');
|
|
// $maxUpdateCount = TestingPanelReading::where($updateCount)->orderByDesc('update_count')->select('update_count')->first();
|
|
// // ->select(TestingPanelReading::raw('MAX(CAST(update_count AS INTEGER)) AS max_update_count'))->value('max_update_count');
|
|
// // ->value('update_count')
|
|
|
|
// First, get the maximum length
|
|
$maxLength = TestingPanelReading::where($updateCount)->selectRaw('MAX(LENGTH(update_count)) as max_length')->value('max_length');
|
|
|
|
// Then, get all records with that length
|
|
$lastUpdateCount = TestingPanelReading::where($updateCount)->whereRaw('LENGTH(update_count) = ?', [$maxLength])->orderByDesc('update_count')->select('update_count')->first(); // ->get();
|
|
|
|
$newUpdateCount = ($lastUpdateCount == null || $lastUpdateCount == '') ? 0 : (int) $lastUpdateCount?->update_count + 1; // $maxUpdateCount?->update_count
|
|
|
|
$updateCountString = (string) $newUpdateCount;
|
|
|
|
$row = [
|
|
'plant_id' => $plantId,
|
|
'line_id' => $lineId,
|
|
'machine_id' => $machineId,
|
|
'motor_testing_master_id' => $motorTestingMasterId,
|
|
'serial_number' => $serial['serial_number'] ?? null,
|
|
'winded_serial_number' => $serial['winded_serial_number'] ?? null,
|
|
'output' => $serial['output'] ?? null,
|
|
'before_fr_volt' => $serial['before_fr_volt'] ?? null,
|
|
'before_fr_cur' => $serial['before_fr_cur'] ?? null,
|
|
'before_fr_pow' => $serial['before_fr_pow'] ?? null,
|
|
'before_fr_res_ry' => $serial['before_fr_res_ry'] ?? null,
|
|
'before_fr_res_yb' => $serial['before_fr_res_yb'] ?? null,
|
|
'before_fr_res_br' => $serial['before_fr_res_br'] ?? null,
|
|
'before_fr_ir' => $serial['before_fr_ir'] ?? null,
|
|
'before_fr_ir_r' => $serial['before_fr_ir_r'] ?? null,
|
|
'before_fr_ir_y' => $serial['before_fr_ir_y'] ?? null,
|
|
'before_fr_ir_b' => $serial['before_fr_ir_b'] ?? null,
|
|
'before_fr_freq' => $serial['before_fr_freq'] ?? null,
|
|
'before_fr_speed' => $serial['before_fr_speed'] ?? null,
|
|
'after_fr_vol' => $serial['after_fr_vol'] ?? null,
|
|
'after_fr_cur' => $serial['after_fr_cur'] ?? null,
|
|
'after_fr_pow' => $serial['after_fr_pow'] ?? null,
|
|
'after_fr_ir_hot' => $serial['after_fr_ir_hot'] ?? null,
|
|
'after_fr_ir_hot_r' => $serial['after_fr_ir_hot_r'] ?? null,
|
|
'after_fr_ir_hot_y' => $serial['after_fr_ir_hot_y'] ?? null,
|
|
'after_fr_ir_hot_b' => $serial['after_fr_ir_hot_b'] ?? null,
|
|
'after_fr_ir_cool' => $serial['after_fr_ir_cool'] ?? null,
|
|
'after_fr_ir_cool_r' => $serial['after_fr_ir_cool_r'] ?? null,
|
|
'after_fr_ir_cool_y' => $serial['after_fr_ir_cool_y'] ?? null,
|
|
'after_fr_ir_cool_b' => $serial['after_fr_ir_cool_b'] ?? null,
|
|
'after_fr_freq' => $serial['after_fr_freq'] ?? null,
|
|
'after_fr_speed' => $serial['after_fr_speed'] ?? null,
|
|
'after_fr_leak_cur' => $serial['after_fr_leak_cur'] ?? null,
|
|
'locked_rt_volt' => $serial['locked_rt_volt'] ?? null,
|
|
'locked_rt_cur' => $serial['locked_rt_cur'] ?? null,
|
|
'locked_rt_pow' => $serial['locked_rt_pow'] ?? null,
|
|
'no_load_pickup_volt' => $serial['no_load_pickup_volt'] ?? null,
|
|
'room_temperature' => $serial['room_temperature'] ?? null,
|
|
'hv_test' => $serial['hv_test'] ?? null,
|
|
'batch_number' => $serial['batch_number'] ?? null,
|
|
'batch_count' => $serial['batch_count'] ?? 0,
|
|
'result' => $serial['result'] ?? null,
|
|
'remark' => $serial['remark'] ?? null,
|
|
'rework_count' => $serial['rework_count'] ?? 0,
|
|
'output_flag' => $serial['output_flag'] ?? 0,
|
|
'tested_by' => $serial['tested_by'] ?? null,
|
|
'updated_by' => $serial['updated_by'] ?? null,
|
|
'created_at' => $serial['created_at'] ?? null,
|
|
'updated_at' => $serial['updated_at'] ?? $serial['created_at'],
|
|
'scanned_at' => $serial['scanned_at'] ?? null,
|
|
'update_count' => $updateCountString,
|
|
];
|
|
|
|
// Insert the new record
|
|
TestingPanelReading::create($row);
|
|
$insertedSerials[] = $serial['serial_number'] ?? '[unknown]';
|
|
$insertedSnoCount[] = $serial['serial_number'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! empty($insertedSerials)) {
|
|
if (count($existSnoCount) == count($insertedSnoCount)) {
|
|
// $messages[] = "Inserted serial numbers are: " . implode(', ', $insertedSerials);
|
|
return response()->json([
|
|
'status_code' => 'SUCCESS',
|
|
'status_description' => 'Inserted serial numbers are: '.implode(', ', $insertedSerials),
|
|
], 200);
|
|
} else {
|
|
$missingSno = array_diff($existSnoCount, $insertedSnoCount);
|
|
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Missed serial numbers are: '.implode(', ', $missingSno),
|
|
], 404);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// return response($e->getMessage(), 500)->header('Content-Type', 'text/plain');
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Store testing panel readings internal server error : '.$e?->getCode(),
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Store a newly created leak test status in storage.
|
|
*/
|
|
public function storeLeakTestStatus(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);
|
|
}
|
|
|
|
$data = $request->all();
|
|
|
|
$plantCode = $data['plant_code'] ?? '';
|
|
$itemCode = $data['item_code'] ?? '';
|
|
$serialNumber = $data['serial_number'] ?? '';
|
|
$testStatus = $data['test_status'] ?? 'F';
|
|
|
|
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code can't be empty!",
|
|
], 404);
|
|
} elseif (! is_numeric($plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' should contain only numeric values!",
|
|
], 404);
|
|
} elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
|
], 404);
|
|
}
|
|
|
|
if ($itemCode == null || $itemCode == '' || ! $itemCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($itemCode) < 6) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code '{$itemCode}' should contain minimum 6 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($itemCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code '{$itemCode}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $itemCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code '{$itemCode}' should not begin with '0'!",
|
|
], 404);
|
|
}
|
|
|
|
if ($serialNumber == null || $serialNumber == '' || ! $serialNumber) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Serial number can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($serialNumber) < 9) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Serial number '{$serialNumber}' should contain minimum 9 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($serialNumber)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Serial number '{$serialNumber}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $serialNumber)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Serial number '{$serialNumber}' should not begin with '0' or letter!",
|
|
], 404);
|
|
}
|
|
|
|
if ($testStatus == null || $testStatus == '') {
|
|
$testStatus = 'F';
|
|
} elseif (Str::contains($testStatus, 'P', ignoreCase: true)) {
|
|
$testStatus = 'P';
|
|
} elseif (Str::contains($testStatus, 'F', ignoreCase: true)) {
|
|
$testStatus = 'F';
|
|
} else {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Leak test status must be 'P' or 'F'!",
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCode)->first();
|
|
if (! $plant) {
|
|
// return response("Plant not found.", 400)->header('Content-Type', 'text/plain');
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' not found in plant table!",
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
|
|
try {
|
|
LeakTestReading::create([
|
|
'plant_id' => $plantId,
|
|
'item_code' => $itemCode,
|
|
'serial_number' => $serialNumber,
|
|
'test_status' => $testStatus,
|
|
]);
|
|
|
|
$existing = LeakTestReading::where('serial_number', $serialNumber)->where('item_code', $itemCode)->where('test_status', $testStatus)->where('plant_id', $plantId)->latest()->first();
|
|
|
|
if ($existing) {
|
|
return response()->json([
|
|
'status_code' => 'SUCCESS',
|
|
'status_description' => 'Successfully updated leak test status.',
|
|
], 200);
|
|
} else {
|
|
return response()->json([
|
|
'status_code' => 'SUCCESS',
|
|
'status_description' => 'Failed to update leak test status!',
|
|
], 200);
|
|
}
|
|
} catch (\Exception $e) {
|
|
// return response($e->getMessage(), 500)->header('Content-Type', 'text/plain');
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Store leak test status internal server error : '.$e?->getCode(),
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get leak test status.
|
|
*/
|
|
public function getLeakTestStatus(Request $request)
|
|
{
|
|
$expectedUser = env('API_AUTH_USER');
|
|
$expectedPw = env('API_AUTH_PW');
|
|
$header_auth = $request->header('Authorization');
|
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
|
|
|
// $data = $request->all();
|
|
if ('Bearer '.$expectedToken != $header_auth) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid authorization token!',
|
|
'status_response' => (object) [],
|
|
], 403);
|
|
}
|
|
|
|
$plantCode = trim($request->header('plant-code')) ?? null;
|
|
// $serialNumbers = $request->header('serial-numbers') ?? null;
|
|
$data = $request->all();
|
|
$serialNumbers = $data['serial_numbers'] ?? '';
|
|
// $serialNumbers = $request->input('serial_numbers', []);
|
|
|
|
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code can't be empty!",
|
|
'status_response' => (object) [],
|
|
], 404);
|
|
} elseif (! is_numeric($plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' should contain only numeric values!",
|
|
'status_response' => (object) [],
|
|
], 404);
|
|
} elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!",
|
|
'status_response' => (object) [],
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
|
'status_response' => (object) [],
|
|
], 404);
|
|
}
|
|
|
|
if (empty($serialNumbers) || ! is_array($serialNumbers) || count($serialNumbers) == 0) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Serial numbers is required and must be a non-empty array.',
|
|
'status_response' => (object) [],
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCode)->first();
|
|
if (! $plant) {
|
|
// return response("Plant not found.", 400)->header('Content-Type', 'text/plain');
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' not found in plant table!",
|
|
'status_response' => (object) [],
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
|
|
// $serialArray = array_map('trim', explode(',', $serialNumbers));
|
|
// // $serialNumber = 'SNO-'.Str::random(5).'-'.time(); // Default random serial number if not provided
|
|
|
|
$serialArray = $serialNumbers;
|
|
|
|
// Remove null, empty string, or falsy values from $serialArray
|
|
// $serialArray = array_filter($serialArray, function ($serial) {
|
|
// return $serial != null && $serial != '' && $serial;
|
|
// });
|
|
// Remove null/empty/falsy and reindex the array
|
|
$serialArray = array_values(array_filter($serialNumbers, function ($serial) {
|
|
return $serial != null && $serial != '' && is_string($serial) && trim($serial) != '';
|
|
}));
|
|
|
|
$serialStatus = [];
|
|
$validSerials = [];
|
|
$hasFail = false;
|
|
foreach ($serialArray as $serialNumber) {
|
|
if ($serialNumber == null || $serialNumber == '' || ! $serialNumber) {
|
|
$serialStatus[$serialNumber] = "Serial number can't be empty!";
|
|
$hasFail = true;
|
|
// $serialStatus[$serialNumber] = 'I';
|
|
} elseif (Str::length($serialNumber) < 9) {
|
|
$serialStatus[$serialNumber] = 'Should contain minimum 9 digits!';
|
|
$hasFail = true;
|
|
// $serialStatus[$serialNumber] = 'I';
|
|
} elseif (! ctype_alnum($serialNumber)) {
|
|
$serialStatus[$serialNumber] = 'Should contain only alpha-numeric values!';
|
|
$hasFail = true;
|
|
// $serialStatus[$serialNumber] = 'I';
|
|
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $serialNumber)) {
|
|
$serialStatus[$serialNumber] = "Should not begin with '0' or letter!";
|
|
$hasFail = true;
|
|
// $serialStatus[$serialNumber] = 'I';
|
|
} else {
|
|
$validSerials[$serialNumber] = 'Not Found';
|
|
}
|
|
}
|
|
|
|
// $key = array_search('26021040180982', $serialArray);
|
|
// if ($key !== false) {
|
|
// unset($serialArray[$key]);
|
|
// }
|
|
|
|
if ($validSerials == null || empty($validSerials)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Has pending leak test!',
|
|
'status_response' => [$serialStatus],
|
|
], 404);
|
|
} else {
|
|
$serialArray = $validSerials; // nonExistingSerials
|
|
}
|
|
|
|
// Check which serial numbers exist in leak_test_readings table
|
|
$existingSerials = [];
|
|
|
|
$existingSerials = LeakTestReading::where('plant_id', $plantId)->whereIn('serial_number', array_keys($serialArray))
|
|
->pluck('test_status', 'serial_number')
|
|
->toArray();
|
|
|
|
if ($existingSerials == null || empty($existingSerials)) {
|
|
$mergedResult = $serialStatus + $serialArray; // array_merge($serialStatus, $serialArray);
|
|
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Has pending leak test!',
|
|
'status_response' => [$mergedResult],
|
|
], 404);
|
|
}
|
|
|
|
$hasFail = (! $hasFail) ? in_array('F', $existingSerials) : $hasFail;
|
|
// Map 'F' to 'Fail' and 'P' to 'Pass'
|
|
$existingSerials = array_map(function ($status) {
|
|
return $status === 'F' ? 'Fail' : ($status === 'P' ? 'Pass' : $status);
|
|
}, $existingSerials);
|
|
|
|
// Sort: Fail comes first, then Pass
|
|
uasort($existingSerials, function ($a, $b) {
|
|
// 'Fail' < 'Pass' so Fail comes first
|
|
return $a <=> $b;
|
|
});
|
|
|
|
// Get non-existing serial numbers (serials not in the database)
|
|
// $serialArray = array_diff($serialArray, array_keys($existingSerials));
|
|
$nonExistingSerials = array_diff_key($serialArray, $existingSerials);
|
|
|
|
if ($nonExistingSerials == null || empty($nonExistingSerials)) {
|
|
$mergedResult = $serialStatus + $existingSerials;
|
|
|
|
return response()->json([
|
|
'status_code' => $hasFail ? 'ERROR' : 'SUCCESS',
|
|
'status_description' => $hasFail ? 'Has pending leak test!' : 'Each serial number has passed the leak test!',
|
|
'status_response' => [$mergedResult],
|
|
], $hasFail ? 404 : 200);
|
|
} else {
|
|
$mergedResult = $serialStatus + $nonExistingSerials + $existingSerials; // nonExistingSerials
|
|
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Has pending leak test!',
|
|
'status_response' => [$mergedResult],
|
|
], 404);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Store a newly created before test status in storage.
|
|
*/
|
|
public function storeBeforeTestStatus(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);
|
|
}
|
|
|
|
$data = $request->all();
|
|
|
|
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' => "Plant code can't be empty!",
|
|
], 404);
|
|
} elseif (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!',
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $data['plant_code'])->first();
|
|
if (! $plant) {
|
|
// return response("Plant not found.", 400)->header('Content-Type', 'text/plain');
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Plant not found!',
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
$plantName = $plant->name;
|
|
|
|
if ($data['line_name'] == null || $data['line_name'] == '') {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Group work center can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($data['line_name']) < 0) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid group work center found!',
|
|
], 404);
|
|
}
|
|
|
|
$gWorkCenter = WorkGroupMaster::where('name', $data['line_name'])->first();
|
|
if (! $gWorkCenter) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Group work center not found!',
|
|
], 404);
|
|
}
|
|
|
|
$gWorkCenter = WorkGroupMaster::where('name', $data['line_name'])->where('plant_id', $plantId)->first();
|
|
if (! $gWorkCenter) {
|
|
// 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' => "Group work center not found for the specified plant : '{$data['plant_code']}'!",
|
|
], 404);
|
|
}
|
|
|
|
$gWorkCenterId = $gWorkCenter->id;
|
|
|
|
if ($data['machine_name'] == null || $data['machine_name'] == '') {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Work center can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($data['machine_name']) < 0) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid work center found!',
|
|
], 404);
|
|
}
|
|
|
|
$machine = Machine::where('work_center', $data['machine_name'])->first();
|
|
if (! $machine) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Work center not found!',
|
|
], 404);
|
|
}
|
|
|
|
$machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->first();
|
|
if (! $machine) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Work center not found for the specified plant : '{$data['plant_code']}'!",
|
|
], 404);
|
|
}
|
|
|
|
$machine = Machine::where('work_center', $data['machine_name'])->where('work_group_master_id', $gWorkCenterId)->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' => "Work center not found for the specified Group work center : '{$data['line_name']}'!",
|
|
], 404);
|
|
|
|
}
|
|
|
|
$machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->where('work_group_master_id', $gWorkCenterId)->first();
|
|
if (! $machine) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Work center not found for the specified Plant : '{$data['plant_code']}' and Group work center : '{$data['line_name']}'!",
|
|
], 404);
|
|
}
|
|
|
|
$lineId = $machine->line_id;
|
|
$machineId = $machine->id;
|
|
|
|
try {
|
|
$insertedSerials = [];
|
|
$missedItemCodes = [];
|
|
$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 == '' || $code == null) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code can't be empty!",
|
|
], 404);
|
|
}
|
|
|
|
// Collect duplicates
|
|
if (isset($itemCodeCounts[$code])) {
|
|
$itemCodeCounts[$code]++;
|
|
// Only add to duplicates array once
|
|
if ($itemCodeCounts[$code] == 2) {
|
|
$duplicateItemCodes[] = $code;
|
|
}
|
|
} else {
|
|
$itemCodeCounts[$code] = 1;
|
|
}
|
|
|
|
$motorTestingMaster = MotorTestingMaster::whereHas('item', function ($query) use ($item) {
|
|
$query->where('code', $item['item_code']);
|
|
})->where('plant_id', $plantId)->first();
|
|
|
|
if (! $motorTestingMaster) {
|
|
$missedItemCodes[] = $item['item_code'];
|
|
}
|
|
|
|
if (! empty($item['serial_numbers']) && is_array($item['serial_numbers'])) {
|
|
foreach ($item['serial_numbers'] as $serial) {
|
|
$existSnoCount[] = $serial['serial_number'];
|
|
}
|
|
}
|
|
}
|
|
|
|
// If any duplicates found, return error
|
|
if (! empty($duplicateItemCodes)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Duplicate item codes found in request: '.implode(', ', $duplicateItemCodes),
|
|
], 404);
|
|
}
|
|
|
|
$uniqueInvalidCodes = array_unique($missedItemCodes);
|
|
|
|
if (! empty($uniqueInvalidCodes)) {
|
|
|
|
// return response("Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in motor testing master for the specified plant {$plantName}", 400)
|
|
// ->header('Content-Type', 'text/plain');
|
|
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Item codes : '.implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : '{$plantName}'!",
|
|
], 404);
|
|
}
|
|
|
|
$insertedSnoCount = [];
|
|
|
|
foreach ($data['item_codes'] as $item) {
|
|
|
|
$motorTestingMaster = MotorTestingMaster::whereHas('item', callback: function ($query) use ($item) {
|
|
$query->where('code', $item['item_code']);
|
|
})->where('plant_id', $plantId)->first();
|
|
|
|
$motorTestingMasterId = $motorTestingMaster->id;
|
|
|
|
if (! empty($item['serial_numbers']) && is_array($item['serial_numbers'])) {
|
|
foreach ($item['serial_numbers'] as $serial) {
|
|
|
|
$row = [
|
|
'plant_id' => $plantId,
|
|
'line_id' => $lineId,
|
|
'machine_id' => $machineId,
|
|
'motor_testing_master_id' => $motorTestingMasterId,
|
|
'serial_number' => $serial['serial_number'] ?? null,
|
|
'before_fr_res_ry' => $serial['before_fr_res_ry'] ?? null,
|
|
'before_fr_res_yb' => $serial['before_fr_res_yb'] ?? null,
|
|
'before_fr_res_br' => $serial['before_fr_res_br'] ?? null,
|
|
'before_fr_ir' => $serial['before_fr_ir'] ?? null,
|
|
'tested_by' => (($serial['tested_by'] == 'jothi') ? 'Admin' : $serial['tested_by']) ?? null,
|
|
'scanned_at' => $serial['scanned_at'] ?? now(),
|
|
];
|
|
|
|
// Insert the new record
|
|
BeforeTestReading::create($row);
|
|
$insertedSerials[] = $serial['serial_number'] ?? '[unknown]';
|
|
$insertedSnoCount[] = $serial['serial_number'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! empty($insertedSerials)) {
|
|
if (count($existSnoCount) == count($insertedSnoCount)) {
|
|
return response()->json([
|
|
'status_code' => 'SUCCESS',
|
|
'status_description' => 'Inserted before test serial numbers are: '.implode(', ', $insertedSerials),
|
|
], 200);
|
|
} else {
|
|
$missingSno = array_diff($existSnoCount, $insertedSnoCount);
|
|
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Missed before test serial numbers are: '.implode(', ', $missingSno),
|
|
], 404);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// return response($e->getMessage(), 500)->header('Content-Type', 'text/plain');
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Store testing panel before test readings internal server error : '.$e?->getCode(),
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function get_motor_master(Request $request)
|
|
{
|
|
$expectedUser = env('API_AUTH_USER');
|
|
$expectedPw = env('API_AUTH_PW');
|
|
$header_auth = $request->header('Authorization');
|
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
|
|
|
// $data = $request->all();
|
|
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');
|
|
// $description = $item ? $item->description : '';
|
|
|
|
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code can't be empty!",
|
|
], 404);
|
|
} elseif (! is_numeric($plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' should contain only numeric values!",
|
|
], 404);
|
|
} elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
|
], 404);
|
|
} elseif ($itemCode == null || $itemCode == '' || ! $itemCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($itemCode) < 6) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code '{$itemCode}' should contain minimum 6 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($itemCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code '{$itemCode}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $itemCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code '{$itemCode}' should not begin with '0'!",
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCode)->first();
|
|
|
|
if (! $plant) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' not found in plant table!",
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
$plantName = $plant->name;
|
|
|
|
$item = Item::where('code', $itemCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Item code not found in item table!',
|
|
], 404);
|
|
}
|
|
|
|
$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 : '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$itemId = $item->id;
|
|
|
|
$description = $item ? $item->description : '';
|
|
|
|
$category = $item ? $item->category : '';
|
|
|
|
$motorTestingMaster = MotorTestingMaster::where('item_id', $itemId)->first();
|
|
|
|
if (! $motorTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Item code not found in motor testing master table!',
|
|
], 404);
|
|
}
|
|
|
|
$motorTestingMaster = MotorTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first();
|
|
|
|
if (! $motorTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Item code not found in motor testing master table for the plant : '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$output = [
|
|
'mot_subassembly_code' => $motorTestingMaster->subassembly_code ?? '',
|
|
'mot_model_name' => ($itemCode == '123456') ? 'SAMPLE TYPE' : $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);
|
|
}
|
|
|
|
public function get_pump_master(Request $request)
|
|
{
|
|
$expectedUser = env('API_AUTH_USER');
|
|
$expectedPw = env('API_AUTH_PW');
|
|
$header_auth = $request->header('Authorization');
|
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
|
|
|
// $data = $request->all();
|
|
if ('Bearer '.$expectedToken != $header_auth) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid authorization token!',
|
|
], 403);
|
|
}
|
|
|
|
$userName = $request->header('user-name');
|
|
|
|
if (! $userName || $userName == null || $userName == '') {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name can't be empty!",
|
|
], 404);
|
|
} elseif ($userName == 'jothi') {
|
|
$userName = 'Admin';
|
|
}
|
|
|
|
$plantCode = $request->header('plant-code');
|
|
$pumpCode = $request->header('pump-code');
|
|
// $description = $item ? $item->description : '';
|
|
|
|
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code can't be empty!",
|
|
], 404);
|
|
} elseif (! is_numeric($plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' should contain only numeric values!",
|
|
], 404);
|
|
} elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
|
], 404);
|
|
} elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($pumpCode) < 6) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain minimum 6 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should not begin with '0'!",
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCode)->first();
|
|
|
|
if (! $plant) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' not found in plant table!",
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
$plantName = $plant->name;
|
|
|
|
$user = User::where('name', $userName)->first();
|
|
|
|
$userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first();
|
|
|
|
if (! $user) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found in users table!",
|
|
], 403);
|
|
} elseif (! $userPlant && ! $user->hasRole('Super Admin')) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!",
|
|
], 403);
|
|
} elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User '{$userName}' does not have rights!",
|
|
], 403);
|
|
}
|
|
|
|
$item = Item::where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Pump code not found in item master table!',
|
|
], 404);
|
|
}
|
|
|
|
$item = Item::where('plant_id', $plantId)->where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code not found in item master table for the plant : '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$itemId = $item->id;
|
|
|
|
$description = $item ? $item->description : '';
|
|
|
|
// $category = $item ? $item->category : '';
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Pump code not found in pump testing master table!',
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code not found in pump testing master table for the plant : '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$output = [
|
|
'pump_type' => ($pumpCode == '123456') ? 'SAMPLE TYPE' : $description,
|
|
'head' => $pumpTestingMaster->head ?? '',
|
|
'head_type' => $pumpTestingMaster->head_type ?? '',
|
|
'discharge' => $pumpTestingMaster->discharge ?? '',
|
|
'discharge_type' => $pumpTestingMaster->discharge_type ?? '',
|
|
'motor_efficiency' => $pumpTestingMaster->motor_efficiency ?? '',
|
|
'pump_efficiency' => $pumpTestingMaster->pump_efficiency ?? '',
|
|
'speed' => $pumpTestingMaster->speed ?? '',
|
|
'frequency' => $pumpTestingMaster->frequency ?? '',
|
|
'i_max' => $pumpTestingMaster->i_max ?? '',
|
|
'p_input' => $pumpTestingMaster->p_input ?? '',
|
|
'delivery_size' => $pumpTestingMaster->delivery_size ?? '',
|
|
'no_of_stages' => $pumpTestingMaster->no_of_stages ?? '',
|
|
'size' => $pumpTestingMaster->size ?? '',
|
|
'maximum_head' => $pumpTestingMaster->maximum_head ?? '',
|
|
'motor_type' => $pumpTestingMaster->motor_type ?? '',
|
|
'motor_code' => $pumpTestingMaster->motor_code ?? '',
|
|
'kw_hp' => $pumpTestingMaster->kw_hp ?? '',
|
|
'connection_type' => $pumpTestingMaster->connection_type ?? '',
|
|
'voltage' => $pumpTestingMaster->voltage ?? '',
|
|
'phase' => $pumpTestingMaster->phase ?? '',
|
|
'oh_minimum' => $pumpTestingMaster->oh_minimum ?? '',
|
|
'oh_maximum' => $pumpTestingMaster->oh_maximum ?? '',
|
|
'current_minimum' => $pumpTestingMaster->current_minimum ?? '',
|
|
'current_maximum' => $pumpTestingMaster->current_maximum ?? '',
|
|
'class_of_insulation' => $pumpTestingMaster->class_of_insulation ?? '',
|
|
'testing_code' => $pumpTestingMaster->testing_code ?? '',
|
|
];
|
|
|
|
return response()->json($output, 200);
|
|
}
|
|
|
|
public function get_pump_entry(Request $request)
|
|
{
|
|
$expectedUser = env('API_AUTH_USER');
|
|
$expectedPw = env('API_AUTH_PW');
|
|
$header_auth = $request->header('Authorization');
|
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
|
|
|
// $data = $request->all();
|
|
if ('Bearer '.$expectedToken != $header_auth) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid authorization token!',
|
|
], 403);
|
|
}
|
|
|
|
$userName = $request->header('user-name');
|
|
|
|
if (! $userName || $userName == null || $userName == '') {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name can't be empty!",
|
|
], 404);
|
|
} elseif ($userName == 'jothi') {
|
|
$userName = 'Admin';
|
|
}
|
|
|
|
$plantCode = $request->header('plant-code');
|
|
$pumpCode = $request->header('pump-code');
|
|
$pumpSerial = $request->header('pump-serial-number');
|
|
$testedType = $request->header('tested-type') ?? 'Pump Performance Test';
|
|
|
|
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code can't be empty!",
|
|
], 404);
|
|
} elseif (! is_numeric($plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' should contain only numeric values!",
|
|
], 404);
|
|
} elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
|
], 404);
|
|
} elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($pumpCode) < 6) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain minimum 6 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should not begin with '0'!",
|
|
], 404);
|
|
} elseif ($pumpSerial == null || $pumpSerial == '' || ! $pumpSerial) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($pumpSerial) < 9) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should contain minimum 9 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($pumpSerial)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $pumpSerial)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should not begin with '0' or letter!",
|
|
], 404);
|
|
} elseif ($testedType == null || $testedType == '' || ! $testedType) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Tested type can't be empty!",
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCode)->first();
|
|
|
|
if (! $plant) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' not found in plant table!",
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
$plantName = $plant->name;
|
|
|
|
$user = User::where('name', $userName)->first();
|
|
|
|
$userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first();
|
|
|
|
if (! $user) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found in users table!",
|
|
], 403);
|
|
} elseif (! $userPlant && ! $user->hasRole('Super Admin')) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!",
|
|
], 403);
|
|
} elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User '{$userName}' does not have rights!",
|
|
], 403);
|
|
}
|
|
|
|
$item = Item::where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in item master table!",
|
|
], 404);
|
|
}
|
|
|
|
$item = Item::where('plant_id', $plantId)->where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$itemId = $item->id;
|
|
|
|
// $description = $item ? $item->description : '';
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in pump testing master table!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in pump testing master table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpMasterId = $pumpTestingMaster->id;
|
|
|
|
$pumpTestingEntry = PumpTestingEntry::where('pump_serial_number', $pumpSerial)->first();
|
|
|
|
if (! $pumpTestingEntry) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '$pumpSerial' not found in pump testing entry table!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingEntry = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->first();
|
|
|
|
if (! $pumpTestingEntry) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '$pumpSerial' not found in pump testing entry table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingEntry = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->first();
|
|
|
|
if (! $pumpTestingEntry) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '$pumpSerial' with Pump code '$pumpCode' not found in pump testing entry table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingEntry = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->first();
|
|
|
|
if (! $pumpTestingEntry) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '$pumpSerial' with Tested type '$testedType' not found in pump testing entry table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingEntries = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->get()->toArray();
|
|
|
|
$output = [
|
|
'tested_date' => $pumpTestingEntry->tested_date ?? now('Asia/Kolkata')->toDateString(), // $result['tested_date'] ?? '2026-07-16'
|
|
'correction_head' => $pumpTestingEntry->correction_head ?? '0', // $result['correction_head'] ?? '5.0',
|
|
'pump_entries' => array_map(function ($entry) use ($userName) {
|
|
return [
|
|
'sl_no' => $entry['sl_no'] ?? '',
|
|
'voltage' => $entry['voltage'] ?? '0',
|
|
'frequency' => $entry['frequency'] ?? '0',
|
|
'speed' => $entry['speed'] ?? '0',
|
|
'd_head' => $entry['head'] ?? '0',
|
|
'flow_reading' => $entry['flow_reading'] ?? '0',
|
|
'current' => $entry['current'] ?? '0',
|
|
'watt' => $entry['watt'] ?? '0',
|
|
'tested_by' => $entry['created_by'] ?? $userName,
|
|
'created_datetime' => ! empty($entry['created_at']) ? Carbon::parse($entry['created_at'])->setTimezone('Asia/Kolkata')->format('Y-m-d H:i:s') : now('Asia/Kolkata')->format('Y-m-d H:i:s'),
|
|
];
|
|
}, $pumpTestingEntries ?? []),
|
|
];
|
|
|
|
return response()->json($output, 200);
|
|
}
|
|
|
|
public function storePumpEntry(Request $request)
|
|
{
|
|
$expectedUser = env('API_AUTH_USER');
|
|
$expectedPw = env('API_AUTH_PW');
|
|
$header_auth = $request->header('Authorization');
|
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
|
|
|
// $data = $request->all();
|
|
if ('Bearer '.$expectedToken != $header_auth) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid authorization token!',
|
|
], 403);
|
|
}
|
|
|
|
$userName = $request->header('user-name');
|
|
|
|
if (! $userName || $userName == null || $userName == '') {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name can't be empty!",
|
|
], 404);
|
|
} elseif ($userName == 'jothi') {
|
|
$userName = 'Admin';
|
|
}
|
|
|
|
$plantCode = $request->header('plant-code');
|
|
$pumpCode = $request->header('pump-code');
|
|
$pumpSerial = $request->header('pump-serial-number');
|
|
$testedType = $request->header('tested-type') ?? 'Pump Performance Test';
|
|
|
|
$json = $request->input('data');
|
|
$data = json_decode($json, true);
|
|
|
|
$testedDate = $data['tested_date'] ?? now('Asia/Kolkata')->toDateString();
|
|
$correctionHead = $data['correction_head'] ?? '0';
|
|
$entries = $data['pump_entries'] ?? [];
|
|
|
|
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code can't be empty!",
|
|
], 404);
|
|
} elseif (! is_numeric($plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' should contain only numeric values!",
|
|
], 404);
|
|
} elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
|
], 404);
|
|
} elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($pumpCode) < 6) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain minimum 6 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should not begin with '0'!",
|
|
], 404);
|
|
} elseif ($pumpSerial == null || $pumpSerial == '' || ! $pumpSerial) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($pumpSerial) < 9) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should contain minimum 9 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($pumpSerial)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $pumpSerial)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should not begin with '0' or letter!",
|
|
], 404);
|
|
} elseif ($testedType == null || $testedType == '' || ! $testedType) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Tested type can't be empty!",
|
|
], 404);
|
|
} elseif ($testedDate == null || $testedDate == '' || ! $testedDate) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Tested date can't be empty!",
|
|
], 404);
|
|
} elseif ($correctionHead == null || $correctionHead == '' || ! $correctionHead) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Correction head can't be empty!",
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCode)->first();
|
|
|
|
if (! $plant) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' not found in plant table!",
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
$plantName = $plant->name;
|
|
|
|
$user = User::where('name', $userName)->first();
|
|
|
|
$userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first();
|
|
|
|
if (! $user) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found in users table!",
|
|
], 403);
|
|
} elseif (! $userPlant && ! $user->hasRole('Super Admin')) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!",
|
|
], 403);
|
|
} elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User '{$userName}' does not have rights!",
|
|
], 403);
|
|
}
|
|
|
|
$item = Item::where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in item master table!",
|
|
], 404);
|
|
}
|
|
|
|
$item = Item::where('plant_id', $plantId)->where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$itemId = $item->id;
|
|
|
|
// $description = $item ? $item->description : '';
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in pump testing master table!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in pump testing master table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpMasterId = $pumpTestingMaster->id;
|
|
|
|
$pumpTestingEntry = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->first();
|
|
|
|
if ($pumpTestingEntry) {
|
|
$existPumpCode = $pumpTestingEntry?->pumpTestingMasters?->item?->code;
|
|
if ($existPumpCode != $pumpCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Duplicate pump code found! Pump serial number '{$pumpSerial}' is already assigned for pump code '{$existPumpCode}'!",
|
|
], 404);
|
|
}
|
|
}
|
|
|
|
$pumpTestingEntry = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->first();
|
|
|
|
if ($pumpTestingEntry) {
|
|
// update logic
|
|
$cnt = 0;
|
|
$updatedRows = 0;
|
|
foreach ($entries as $entry) {
|
|
$sl_no = $entry['sl_no'] ?? null;
|
|
$voltage = $entry['voltage'] ?? '0';
|
|
$frequency = $entry['frequency'] ?? '0';
|
|
$speed = $entry['speed'] ?? '0';
|
|
$head = $entry['head'] ?? '0';
|
|
$flow_reading = $entry['flow_reading'] ?? '0';
|
|
$current = $entry['current'] ?? '0';
|
|
$watt = $entry['watt'] ?? '0';
|
|
$tested_by = $entry['tested_by'] ?? $userName;
|
|
// $pendingExists = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $sl_no)->first();
|
|
|
|
$cnt++;
|
|
if ($sl_no == 'Constant') {
|
|
for ($i = $cnt; $i <= 10; $i++) {
|
|
PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $i)->forceDelete();
|
|
}
|
|
}
|
|
|
|
$rowAffected = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $sl_no)
|
|
->update([
|
|
'tested_date' => $testedDate,
|
|
'correction_head' => $correctionHead,
|
|
'voltage' => $voltage,
|
|
'frequency' => $frequency,
|
|
'speed' => $speed,
|
|
'head' => $head,
|
|
'flow_reading' => $flow_reading,
|
|
'current' => $current,
|
|
'watt' => $watt,
|
|
'created_by' => $tested_by,
|
|
'updated_at' => now(),
|
|
'updated_by' => $userName,
|
|
]);
|
|
|
|
if ($rowAffected) {
|
|
$updatedRows++;
|
|
} else {
|
|
$updatedRows++;
|
|
PumpTestingEntry::create([
|
|
'plant_id' => $plantId,
|
|
'pump_testing_master_id' => $pumpMasterId,
|
|
'tested_date' => $testedDate,
|
|
'tested_type' => $testedType,
|
|
'pump_serial_number' => $pumpSerial,
|
|
'correction_head' => $correctionHead,
|
|
'sl_no' => $sl_no,
|
|
'voltage' => $voltage,
|
|
'frequency' => $frequency,
|
|
'speed' => $speed,
|
|
'head' => $head,
|
|
'flow_reading' => $flow_reading,
|
|
'current' => $current,
|
|
'watt' => $watt,
|
|
'created_by' => $tested_by,
|
|
'updated_at' => now(),
|
|
'updated_by' => $userName,
|
|
]);
|
|
}
|
|
}
|
|
|
|
if ($updatedRows == 0) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Failed to update exist pump testing entries for the pump serial number '{$pumpSerial}'.",
|
|
], 404);
|
|
} else {
|
|
return response()->json([
|
|
'status_code' => 'SUCCESS',
|
|
'status_description' => "Successfully updated exist pump testing entries for the pump serial number '{$pumpSerial}'.",
|
|
], 200);
|
|
}
|
|
} else {
|
|
// insert logic
|
|
$createdRows = 0;
|
|
foreach ($entries as $entry) {
|
|
$sl_no = $entry['sl_no'] ?? null;
|
|
$voltage = $entry['voltage'] ?? '0';
|
|
$frequency = $entry['frequency'] ?? '0';
|
|
$speed = $entry['speed'] ?? '0';
|
|
$head = $entry['head'] ?? '0';
|
|
$flow_reading = $entry['flow_reading'] ?? '0';
|
|
$current = $entry['current'] ?? '0';
|
|
$watt = $entry['watt'] ?? '0';
|
|
$tested_by = $entry['tested_by'] ?? $userName;
|
|
|
|
$createdRows++;
|
|
PumpTestingEntry::create([
|
|
'plant_id' => $plantId,
|
|
'pump_testing_master_id' => $pumpMasterId,
|
|
'tested_date' => $testedDate,
|
|
'tested_type' => $testedType,
|
|
'pump_serial_number' => $pumpSerial,
|
|
'correction_head' => $correctionHead,
|
|
'sl_no' => $sl_no,
|
|
'voltage' => $voltage,
|
|
'frequency' => $frequency,
|
|
'speed' => $speed,
|
|
'head' => $head,
|
|
'flow_reading' => $flow_reading,
|
|
'current' => $current,
|
|
'watt' => $watt,
|
|
'created_by' => $tested_by,
|
|
'updated_at' => now(),
|
|
'updated_by' => $userName,
|
|
]);
|
|
}
|
|
|
|
if ($createdRows == 0) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Failed to create new pump testing entries for the pump serial number '{$pumpSerial}'.",
|
|
], 404);
|
|
} else {
|
|
return response()->json([
|
|
'status_code' => 'SUCCESS',
|
|
'status_description' => "Successfully created new pump testing entries for the pump serial number '{$pumpSerial}'.",
|
|
], 200);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function get_pump_result(Request $request)
|
|
{
|
|
$expectedUser = env('API_AUTH_USER');
|
|
$expectedPw = env('API_AUTH_PW');
|
|
$header_auth = $request->header('Authorization');
|
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
|
|
|
// $data = $request->all();
|
|
if ('Bearer '.$expectedToken != $header_auth) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid authorization token!',
|
|
], 403);
|
|
}
|
|
|
|
$userName = $request->header('user-name');
|
|
|
|
if (! $userName || $userName == null || $userName == '') {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name can't be empty!",
|
|
], 404);
|
|
} elseif ($userName == 'jothi') {
|
|
$userName = 'Admin';
|
|
}
|
|
|
|
$plantCode = $request->header('plant-code');
|
|
$pumpCode = $request->header('pump-code');
|
|
$pumpSerial = $request->header('pump-serial-number');
|
|
$testedType = $request->header('tested-type') ?? 'Pump Performance Test';
|
|
|
|
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code can't be empty!",
|
|
], 404);
|
|
} elseif (! is_numeric($plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' should contain only numeric values!",
|
|
], 404);
|
|
} elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
|
], 404);
|
|
} elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($pumpCode) < 6) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain minimum 6 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should not begin with '0'!",
|
|
], 404);
|
|
} elseif ($pumpSerial == null || $pumpSerial == '' || ! $pumpSerial) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($pumpSerial) < 9) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should contain minimum 9 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($pumpSerial)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $pumpSerial)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should not begin with '0' or letter!",
|
|
], 404);
|
|
} elseif ($testedType == null || $testedType == '' || ! $testedType) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Tested type can't be empty!",
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCode)->first();
|
|
|
|
if (! $plant) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' not found in plant table!",
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
$plantName = $plant->name;
|
|
|
|
$user = User::where('name', $userName)->first();
|
|
|
|
$userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first();
|
|
|
|
if (! $user) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found in users table!",
|
|
], 403);
|
|
} elseif (! $userPlant && ! $user->hasRole('Super Admin')) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!",
|
|
], 403);
|
|
} elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User '{$userName}' does not have rights!",
|
|
], 403);
|
|
}
|
|
|
|
$item = Item::where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in item master table!",
|
|
], 404);
|
|
}
|
|
|
|
$item = Item::where('plant_id', $plantId)->where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$itemId = $item->id;
|
|
|
|
// $description = $item ? $item->description : '';
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in pump testing master table!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in pump testing master table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpMasterId = $pumpTestingMaster->id;
|
|
|
|
$pumpTestingResult = PumpTestingResult::where('pump_serial_number', $pumpSerial)->first();
|
|
|
|
if (! $pumpTestingResult) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '$pumpSerial' not found in pump testing result table!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->first();
|
|
|
|
if (! $pumpTestingResult) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '$pumpSerial' not found in pump testing result table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->first();
|
|
|
|
if (! $pumpTestingResult) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '$pumpSerial' with Pump code '$pumpCode' not found in pump testing result table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->first();
|
|
|
|
if (! $pumpTestingResult) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '$pumpSerial' with Tested type '$testedType' not found in pump testing result table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingResults = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->get()->toArray();
|
|
|
|
$output = [
|
|
'tested_date' => $pumpTestingResult->tested_date ?? now('Asia/Kolkata')->toDateString(), // $result['tested_date'] ?? '2026-07-16'
|
|
'correction_head' => $pumpTestingResult->correction_head ?? '0', // $result['correction_head'] ?? '5.0',
|
|
'pump_results' => array_map(function ($result) use ($userName) {
|
|
return [
|
|
'sl_no' => $result['sl_no'] ?? '',
|
|
'voltage' => $result['voltage'] ?? '0',
|
|
'frequency' => $result['frequency'] ?? '0',
|
|
'speed' => $result['speed'] ?? '0',
|
|
'd_head' => $result['head'] ?? '0',
|
|
'flow_reading' => $result['flow_reading'] ?? '0',
|
|
'current' => $result['current'] ?? '0',
|
|
'watt' => $result['watt'] ?? '0',
|
|
'tested_by' => $result['created_by'] ?? $userName,
|
|
'created_datetime' => ! empty($result['created_at']) ? Carbon::parse($result['created_at'])->setTimezone('Asia/Kolkata')->format('Y-m-d H:i:s') : now('Asia/Kolkata')->format('Y-m-d H:i:s'),
|
|
];
|
|
}, $pumpTestingResults ?? []),
|
|
];
|
|
|
|
return response()->json($output, 200);
|
|
}
|
|
|
|
public function storePumpResult(Request $request)
|
|
{
|
|
$expectedUser = env('API_AUTH_USER');
|
|
$expectedPw = env('API_AUTH_PW');
|
|
$header_auth = $request->header('Authorization');
|
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
|
|
|
// $data = $request->all();
|
|
if ('Bearer '.$expectedToken != $header_auth) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid authorization token!',
|
|
], 403);
|
|
}
|
|
|
|
$userName = $request->header('user-name');
|
|
|
|
if (! $userName || $userName == null || $userName == '') {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name can't be empty!",
|
|
], 404);
|
|
} elseif ($userName == 'jothi') {
|
|
$userName = 'Admin';
|
|
}
|
|
|
|
$plantCode = $request->header('plant-code');
|
|
$pumpCode = $request->header('pump-code');
|
|
$pumpSerial = $request->header('pump-serial-number');
|
|
$testedType = $request->header('tested-type') ?? 'Pump Performance Test';
|
|
|
|
$json = $request->input('data');
|
|
$data = json_decode($json, true);
|
|
|
|
$testedDate = $data['tested_date'] ?? now('Asia/Kolkata')->toDateString();
|
|
$correctionHead = $data['correction_head'] ?? '0';
|
|
$results = $data['pump_results'] ?? [];
|
|
|
|
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code can't be empty!",
|
|
], 404);
|
|
} elseif (! is_numeric($plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' should contain only numeric values!",
|
|
], 404);
|
|
} elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
|
], 404);
|
|
} elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($pumpCode) < 6) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain minimum 6 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $pumpCode)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '{$pumpCode}' should not begin with '0'!",
|
|
], 404);
|
|
} elseif ($pumpSerial == null || $pumpSerial == '' || ! $pumpSerial) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number can't be empty!",
|
|
], 404);
|
|
} elseif (Str::length($pumpSerial) < 9) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should contain minimum 9 digits!",
|
|
], 404);
|
|
} elseif (! ctype_alnum($pumpSerial)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should contain only alpha-numeric values!",
|
|
], 404);
|
|
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $pumpSerial)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump serial number '{$pumpSerial}' should not begin with '0' or letter!",
|
|
], 404);
|
|
} elseif ($testedType == null || $testedType == '' || ! $testedType) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Tested type can't be empty!",
|
|
], 404);
|
|
} elseif ($testedDate == null || $testedDate == '' || ! $testedDate) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Tested date can't be empty!",
|
|
], 404);
|
|
} elseif ($correctionHead == null || $correctionHead == '' || ! $correctionHead) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Correction head can't be empty!",
|
|
], 404);
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCode)->first();
|
|
|
|
if (! $plant) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code '{$plantCode}' not found in plant table!",
|
|
], 404);
|
|
}
|
|
|
|
$plantId = $plant->id;
|
|
$plantName = $plant->name;
|
|
|
|
$user = User::where('name', $userName)->first();
|
|
|
|
$userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first();
|
|
|
|
if (! $user) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found in users table!",
|
|
], 403);
|
|
} elseif (! $userPlant && ! $user->hasRole('Super Admin')) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!",
|
|
], 403);
|
|
} elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "User '{$userName}' does not have rights!",
|
|
], 403);
|
|
}
|
|
|
|
$item = Item::where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in item master table!",
|
|
], 404);
|
|
}
|
|
|
|
$item = Item::where('plant_id', $plantId)->where('code', $pumpCode)->first();
|
|
|
|
if (! $item) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$itemId = $item->id;
|
|
|
|
// $description = $item ? $item->description : '';
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in pump testing master table!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpTestingMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first();
|
|
|
|
if (! $pumpTestingMaster) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Pump code '$pumpCode' not found in pump testing master table for the plant '$plantName'!",
|
|
], 404);
|
|
}
|
|
|
|
$pumpMasterId = $pumpTestingMaster->id;
|
|
|
|
$pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->first();
|
|
|
|
if ($pumpTestingResult) {
|
|
$existPumpCode = $pumpTestingResult?->pumpTestingMasters?->item?->code;
|
|
if ($existPumpCode != $pumpCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Duplicate pump code found! Pump serial number '{$pumpSerial}' is already assigned for pump code '{$existPumpCode}'!",
|
|
], 404);
|
|
}
|
|
}
|
|
|
|
$pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->first();
|
|
|
|
if ($pumpTestingResult) {
|
|
// update logic
|
|
$cnt = 0;
|
|
$updatedRows = 0;
|
|
foreach ($results as $result) {
|
|
if (! $result) {
|
|
continue;
|
|
}
|
|
|
|
$sl_no = $result['sl_no'] ?? null;
|
|
$voltage = $result['voltage'] ?? '0';
|
|
$current = $result['current'] ?? '0';
|
|
$watt = $result['watt'] ?? '0';
|
|
$frequency = $result['frequency'] ?? '0';
|
|
$speed = $result['speed'] ?? '0';
|
|
$discharge = $result['discharge'] ?? '0';
|
|
$head = $result['head'] ?? '0';
|
|
$pump_output = $result['pump_output'] ?? '0';
|
|
$power_p2 = $result['power_p2'] ?? '0';
|
|
$overall_efficiency = $result['overall_efficiency'] ?? '0';
|
|
$pump_efficiency = $result['pump_efficiency'] ?? '0';
|
|
$tested_by = $result['tested_by'] ?? $userName;
|
|
// $pendingExists = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $sl_no)->first();
|
|
|
|
$cnt++;
|
|
|
|
$rowAffected = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $sl_no)
|
|
->update([
|
|
'tested_date' => $testedDate,
|
|
'correction_head' => $correctionHead,
|
|
'voltage' => $voltage,
|
|
'current' => $current,
|
|
'watt' => $watt,
|
|
'frequency' => $frequency,
|
|
'speed' => $speed,
|
|
'discharge' => $discharge,
|
|
'head' => $head,
|
|
'pump_output' => $pump_output,
|
|
'power_p2' => $power_p2,
|
|
'overall_efficiency' => $overall_efficiency,
|
|
'pump_efficiency' => $pump_efficiency,
|
|
'created_by' => $tested_by,
|
|
'updated_at' => now(),
|
|
'updated_by' => $userName,
|
|
]);
|
|
|
|
if ($rowAffected) {
|
|
$updatedRows++;
|
|
} else {
|
|
$updatedRows++;
|
|
PumpTestingResult::create([
|
|
'plant_id' => $plantId,
|
|
'pump_testing_master_id' => $pumpMasterId,
|
|
'tested_date' => $testedDate,
|
|
'tested_type' => $testedType,
|
|
'pump_serial_number' => $pumpSerial,
|
|
'correction_head' => $correctionHead,
|
|
'sl_no' => $sl_no,
|
|
'voltage' => $voltage,
|
|
'current' => $current,
|
|
'watt' => $watt,
|
|
'frequency' => $frequency,
|
|
'speed' => $speed,
|
|
'discharge' => $discharge,
|
|
'head' => $head,
|
|
'pump_output' => $pump_output,
|
|
'power_p2' => $power_p2,
|
|
'overall_efficiency' => $overall_efficiency,
|
|
'pump_efficiency' => $pump_efficiency,
|
|
'created_by' => $tested_by,
|
|
'updated_at' => now(),
|
|
'updated_by' => $userName,
|
|
]);
|
|
}
|
|
}
|
|
|
|
if ($updatedRows == 0) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Failed to update exist pump testing results for the pump serial number '{$pumpSerial}'.",
|
|
], 404);
|
|
} else {
|
|
for ($i = $cnt; $i <= 10; $i++) {
|
|
PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $i)->forceDelete();
|
|
}
|
|
|
|
return response()->json([
|
|
'status_code' => 'SUCCESS',
|
|
'status_description' => "Successfully updated exist pump testing results for the pump serial number '{$pumpSerial}'.",
|
|
], 200);
|
|
}
|
|
} else {
|
|
// insert logic
|
|
$createdRows = 0;
|
|
foreach ($results as $result) {
|
|
if (! $result) {
|
|
continue;
|
|
}
|
|
|
|
$sl_no = $result['sl_no'] ?? null;
|
|
$voltage = $result['voltage'] ?? '0';
|
|
$current = $result['current'] ?? '0';
|
|
$watt = $result['watt'] ?? '0';
|
|
$frequency = $result['frequency'] ?? '0';
|
|
$speed = $result['speed'] ?? '0';
|
|
$discharge = $result['discharge'] ?? '0';
|
|
$head = $result['head'] ?? '0';
|
|
$pump_output = $result['pump_output'] ?? '0';
|
|
$power_p2 = $result['power_p2'] ?? '0';
|
|
$overall_efficiency = $result['overall_efficiency'] ?? '0';
|
|
$pump_efficiency = $result['pump_efficiency'] ?? '0';
|
|
$tested_by = $result['tested_by'] ?? $userName;
|
|
|
|
$createdRows++;
|
|
PumpTestingResult::create([
|
|
'plant_id' => $plantId,
|
|
'pump_testing_master_id' => $pumpMasterId,
|
|
'tested_date' => $testedDate,
|
|
'tested_type' => $testedType,
|
|
'pump_serial_number' => $pumpSerial,
|
|
'correction_head' => $correctionHead,
|
|
'sl_no' => $sl_no,
|
|
'voltage' => $voltage,
|
|
'current' => $current,
|
|
'watt' => $watt,
|
|
'frequency' => $frequency,
|
|
'speed' => $speed,
|
|
'discharge' => $discharge,
|
|
'head' => $head,
|
|
'pump_output' => $pump_output,
|
|
'power_p2' => $power_p2,
|
|
'overall_efficiency' => $overall_efficiency,
|
|
'pump_efficiency' => $pump_efficiency,
|
|
'created_by' => $tested_by,
|
|
'updated_at' => now(),
|
|
'updated_by' => $userName,
|
|
]);
|
|
}
|
|
|
|
if ($createdRows == 0) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Failed to create new pump testing results for the pump serial number '{$pumpSerial}'.",
|
|
], 404);
|
|
} else {
|
|
return response()->json([
|
|
'status_code' => 'SUCCESS',
|
|
'status_description' => "Successfully created new pump testing results for the pump serial number '{$pumpSerial}'.",
|
|
], 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)
|
|
{
|
|
//
|
|
}
|
|
}
|