diff --git a/app/Http/Controllers/TestingPanelController.php b/app/Http/Controllers/TestingPanelController.php index ecbd571..4e59f66 100644 --- a/app/Http/Controllers/TestingPanelController.php +++ b/app/Http/Controllers/TestingPanelController.php @@ -10,7 +10,9 @@ 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; @@ -452,7 +454,7 @@ class TestingPanelController extends Controller // return response("Plant not found.", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant code '{$plantCode}' not found!", + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", ], 404); } @@ -552,7 +554,7 @@ class TestingPanelController extends Controller // return response("Plant not found.", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant code '{$plantCode}' not found!", + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", 'status_response' => (object) [], ], 404); } @@ -938,18 +940,26 @@ class TestingPanelController extends Controller $itemCode = $request->header('item-code'); // $description = $item ? $item->description : ''; - if ($plantCode == null || $plantCode == '') { - // return response("ERROR: Plant Name can't be empty", 400) - // ->header('Content-Type', 'text/plain'); + if ($plantCode == null || $plantCode == '' || ! $plantCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); - } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { + ], 404); + } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code found!', - ], 400); + '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', @@ -977,8 +987,8 @@ class TestingPanelController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Plant not found!', - ], 400); + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", + ], 404); } $plantId = $plant->id; @@ -1079,22 +1089,41 @@ class TestingPanelController extends Controller ], 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 == '') { - // return response("ERROR: Plant Name can't be empty", 400) - // ->header('Content-Type', 'text/plain'); + if ($plantCode == null || $plantCode == '' || ! $plantCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); - } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { + ], 404); + } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code found!', - ], 400); + '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', @@ -1122,13 +1151,34 @@ class TestingPanelController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Plant not found!', - ], 400); + '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) { @@ -1219,23 +1269,42 @@ class TestingPanelController extends Controller ], 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 == '') { - // return response("ERROR: Plant Name can't be empty", 400) - // ->header('Content-Type', 'text/plain'); + if ($plantCode == null || $plantCode == '' || ! $plantCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); - } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { + ], 404); + } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code found!', - ], 400); + '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', @@ -1288,19 +1357,40 @@ class TestingPanelController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant code : '$plantCode' not found!", - ], 400); + '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!", + 'status_description' => "Pump code '$pumpCode' not found in item master table!", ], 404); } @@ -1309,7 +1399,7 @@ class TestingPanelController extends Controller if (! $item) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump code : '$pumpCode' not found in item master table for the plant : '$plantName'!", + 'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!", ], 404); } @@ -1322,7 +1412,7 @@ class TestingPanelController extends Controller if (! $pumpTestingMaster) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump code : '$pumpCode' not found in pump testing master table!", + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table!", ], 404); } @@ -1331,7 +1421,7 @@ class TestingPanelController extends Controller if (! $pumpTestingMaster) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump code : '$pumpCode' not found in pump testing master table for the plant : '$plantName'!", + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table for the plant '$plantName'!", ], 404); } @@ -1342,7 +1432,7 @@ class TestingPanelController extends Controller if (! $pumpTestingEntry) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump serial number : '$pumpSerial' not found in pump testing entry table!", + 'status_description' => "Pump serial number '$pumpSerial' not found in pump testing entry table!", ], 404); } @@ -1351,7 +1441,7 @@ class TestingPanelController extends Controller 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'!", + 'status_description' => "Pump serial number '$pumpSerial' not found in pump testing entry table for the plant '$plantName'!", ], 404); } @@ -1360,7 +1450,7 @@ class TestingPanelController extends Controller 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'!", + 'status_description' => "Pump serial number '$pumpSerial' with Pump code '$pumpCode' not found in pump testing entry table for the plant '$plantName'!", ], 404); } @@ -1369,27 +1459,27 @@ class TestingPanelController extends Controller 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'!", + '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 ?? '', // $result['tested_date'] ?? '2026-07-16' - 'correction_head' => $pumpTestingEntry->correction_head ?? '', // $result['correction_head'] ?? '5.0', - 'pump_entries' => array_map(function ($entry) { + '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'] ?? '', - 'frequency' => $entry['frequency'] ?? '', - 'speed' => $entry['speed'] ?? '', - 'd_head' => $entry['head'] ?? '', - 'flow_reading' => $entry['flow_reading'] ?? '', - 'current' => $entry['current'] ?? '', - 'watt' => $entry['watt'] ?? '', - 'tested_by' => $entry['created_by'] ?? '', - 'created_datetime' => ! empty($entry['created_at']) ? Carbon::parse($entry['created_at'])->setTimezone('Asia/Kolkata')->format('Y-m-d H:i:s') : '', + '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 ?? []), ]; @@ -1397,7 +1487,7 @@ class TestingPanelController extends Controller return response()->json($output, 200); } - public function get_pump_result(Request $request) + public function storePumpEntry(Request $request) { $expectedUser = env('API_AUTH_USER'); $expectedPw = env('API_AUTH_PW'); @@ -1412,22 +1502,49 @@ class TestingPanelController extends Controller ], 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 == '') { - // return response("ERROR: Plant Name can't be empty", 400) - // ->header('Content-Type', 'text/plain'); + $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!", - ], 400); - } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { + ], 404); + } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code found!', - ], 400); + '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', @@ -1468,6 +1585,21 @@ class TestingPanelController extends Controller '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(); @@ -1475,19 +1607,40 @@ class TestingPanelController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Plant not found!', - ], 400); + '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!', + 'status_description' => "Pump code '$pumpCode' not found in item master table!", ], 404); } @@ -1496,142 +1649,764 @@ class TestingPanelController extends Controller if (! $item) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump code not found in item master table for the plant : '$plantName'!", + 'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!", ], 404); } $itemId = $item->id; - $description = $item ? $item->description : ''; + // $description = $item ? $item->description : ''; - $category = $item ? $item->category : ''; + $pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first(); - // $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); - // } - - $entries = $result['pump_entries'] ?? []; - - if (empty($entries)) { - $entries = [ - [ - 'sl_no' => '1', - 'voltage' => '415', - 'frequency' => '50.02', - 'speed' => '2976', - 'head' => '5', - 'flow_reading' => '18.75', - 'current' => '61.8', - 'watt' => '40730', - ], - [ - 'sl_no' => '2', - 'voltage' => '415', - 'frequency' => '50.05', - 'speed' => '2976', - 'head' => '100', - 'flow_reading' => '18.71', - 'current' => '72.7', - 'watt' => '47100', - ], - [ - 'sl_no' => '3', - 'voltage' => '415', - 'frequency' => '50.04', - 'speed' => '2975', - 'head' => '150', - 'flow_reading' => '17.65', - 'current' => '76.3', - 'watt' => '48000', - ], - [ - 'sl_no' => '4', - 'voltage' => '415', - 'frequency' => '50.02', - 'speed' => '2974', - 'head' => '200', - 'flow_reading' => '15.65', - 'current' => '74', - 'watt' => '47760', - ], - [ - 'sl_no' => '5', - 'voltage' => '415', - 'frequency' => '50.04', - 'speed' => '2977', - 'head' => '250', - 'flow_reading' => '12.51', - 'current' => '68.2', - 'watt' => '45240', - ], - [ - 'sl_no' => '6', - 'voltage' => '415', - 'frequency' => '50', - 'speed' => '2979', - 'head' => '270', - 'flow_reading' => '11.53', - 'current' => '65.5', - 'watt' => '43050', - ], - [ - 'sl_no' => '7', - 'voltage' => '415', - 'frequency' => '50.05', - 'speed' => '2983', - 'head' => '300', - 'flow_reading' => '8.65', - 'current' => '57.4', - 'watt' => '37460', - ], - [ - 'sl_no' => '8', - 'voltage' => '415', - 'frequency' => '50.03', - 'speed' => '2990', - 'head' => '325', - 'flow_reading' => '0', - 'current' => '35.8', - 'watt' => '21300', - ], - ]; + 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' => $result['tested_date'] ?? '2026-07-16', - 'test_type' => $result['test-type'] ?? 'Pump Performance Test', - 'correction_head' => $result['correction_head'] ?? '5.0', - 'pump_entries' => array_map(function ($entry) { + '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' => $entry['sl_no'] ?? '', - 'voltage' => $entry['voltage'] ?? '', - 'frequency' => $entry['frequency'] ?? '', - 'speed' => $entry['speed'] ?? '', - 'head' => $entry['head'] ?? '', - 'flow_reading' => $entry['flow_reading'] ?? '', - 'current' => $entry['current'] ?? '', - 'watt' => $entry['watt'] ?? '', + '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'), ]; - }, $entries ?? []), + }, $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 entries 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 entries 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 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); + } + } + } + /** * Update the specified resource in storage. */ diff --git a/routes/api.php b/routes/api.php index 5f35861..8272e6b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -158,8 +158,12 @@ Route::get('testing/pump-master/get', [TestingPanelController::class, 'get_pump_ Route::get('testing/pump-entry/get', [TestingPanelController::class, 'get_pump_entry']); +Route::post('testing/pump-entry/store-data', [TestingPanelController::class, 'storePumpEntry']); + Route::get('testing/pump-result/get', [TestingPanelController::class, 'get_pump_result']); +Route::post('testing/pump-result/store-data', [TestingPanelController::class, 'storePumpResult']); + // Testing panel Controller Route::get('testing/user/get-data', [UserController::class, 'get_user_data']);