Merge pull request 'Added POST API logs to view the structure in command prompt' (#10) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s
Reviewed-on: #10
This commit was merged in pull request #10.
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Models\Plant;
|
|||||||
use App\Models\ProcessOrder;
|
use App\Models\ProcessOrder;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Response;
|
use Illuminate\Support\Facades\Response;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Str;
|
use Str;
|
||||||
@@ -21,55 +22,51 @@ class PdfController extends Controller
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateGR(Request $request)
|
public function updateGR(Request $request)
|
||||||
{
|
{
|
||||||
$expectedUser = env('API_AUTH_USER');
|
$expectedUser = env('API_AUTH_USER');
|
||||||
$expectedPw = env('API_AUTH_PW');
|
$expectedPw = env('API_AUTH_PW');
|
||||||
|
|
||||||
$header_auth = $request->header('Authorization');
|
$header_auth = $request->header('Authorization');
|
||||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
||||||
|
|
||||||
if ("Bearer " . $expectedToken != $header_auth)
|
if ('Bearer '.$expectedToken != $header_auth) {
|
||||||
{
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Invalid authorization token!'
|
'status_description' => 'Invalid authorization token!',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $request->all();
|
$data = $request->all();
|
||||||
|
|
||||||
if ($data['plant_code'] == null || $data['plant_code'] == '')
|
if ($data['plant_code'] == null || $data['plant_code'] == '') {
|
||||||
{
|
|
||||||
// return response("ERROR: Please provide a valid plant code.", 400)
|
// return response("ERROR: Please provide a valid plant code.", 400)
|
||||||
// ->header('Content-Type', 'text/plain');
|
// ->header('Content-Type', 'text/plain');
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Plant code can't be empty!"
|
'status_description' => "Plant code can't be empty!",
|
||||||
], 400);
|
], 400);
|
||||||
}
|
} 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'])
|
||||||
else if (Str::length($data['plant_code']) < 4 || !is_numeric($data['plant_code']) || !preg_match('/^[1-9]\d{3,}$/', $data['plant_code']))//!ctype_digit($data['plant_code'])
|
|
||||||
{
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Invalid plant code found!"
|
'status_description' => 'Invalid plant code found!',
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plant = Plant::where('code', $data['plant_code'])->first();
|
$plant = Plant::where('code', $data['plant_code'])->first();
|
||||||
if (!$plant) {
|
if (! $plant) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Plant not found!'
|
'status_description' => 'Plant not found!',
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plantId = $plant->id;
|
$plantId = $plant->id;
|
||||||
if ($data['gr_number'] == null || $data['gr_number'] == '')
|
if ($data['gr_number'] == null || $data['gr_number'] == '') {
|
||||||
{
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "GR Number can't be empty!"
|
'status_description' => "GR Number can't be empty!",
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,10 +74,10 @@ class PdfController extends Controller
|
|||||||
->where('gr_number', $data['gr_number'])
|
->where('gr_number', $data['gr_number'])
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$grExists) {
|
if (! $grExists) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "GR Number {$data['gr_number']} not found for plant {$data['plant_code']}!"
|
'status_description' => "GR Number {$data['gr_number']} not found for plant {$data['plant_code']}!",
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,17 +86,17 @@ class PdfController extends Controller
|
|||||||
if ($scannedBy == '' || $scannedBy == null) {
|
if ($scannedBy == '' || $scannedBy == null) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Scanned by can't be empty!"
|
'status_description' => "Scanned by can't be empty!",
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = User::where('name', $data['scanned_by'])
|
$user = User::where('name', $data['scanned_by'])
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$user) {
|
if (! $user) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "'{$data['scanned_by']}' user not found!"
|
'status_description' => "'{$data['scanned_by']}' user not found!",
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,13 +108,13 @@ class PdfController extends Controller
|
|||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Duplicate item codes found in request!',
|
'status_description' => 'Duplicate item codes found in request!',
|
||||||
'duplicate_item_codes' => array_values($duplicateItemCodes)
|
'duplicate_item_codes' => array_values($duplicateItemCodes),
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$allSerials = [];
|
$allSerials = [];
|
||||||
foreach ($data['item_codes'] as $item) {
|
foreach ($data['item_codes'] as $item) {
|
||||||
if (!isset($item['serial_numbers']) || !is_array($item['serial_numbers'])) {
|
if (! isset($item['serial_numbers']) || ! is_array($item['serial_numbers'])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
foreach ($item['serial_numbers'] as $serial) {
|
foreach ($item['serial_numbers'] as $serial) {
|
||||||
@@ -131,7 +128,7 @@ class PdfController extends Controller
|
|||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Duplicate serial numbers found in request!',
|
'status_description' => 'Duplicate serial numbers found in request!',
|
||||||
'duplicate_serial_numbers' => array_values($duplicateSerials)
|
'duplicate_serial_numbers' => array_values($duplicateSerials),
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,12 +138,12 @@ class PdfController extends Controller
|
|||||||
$itemCode = $item['item_code'] ?? null;
|
$itemCode = $item['item_code'] ?? null;
|
||||||
|
|
||||||
// Skip if item code is missing
|
// Skip if item code is missing
|
||||||
if (!$itemCode) {
|
if (! $itemCode) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if item code is less than 6 digits or not numeric
|
// Check if item code is less than 6 digits or not numeric
|
||||||
if (strlen($itemCode) < 6 || !ctype_digit($itemCode)) {
|
if (strlen($itemCode) < 6 || ! ctype_digit($itemCode)) {
|
||||||
$invalidLengthItemCodes[] = $itemCode;
|
$invalidLengthItemCodes[] = $itemCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -154,32 +151,32 @@ class PdfController extends Controller
|
|||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Some item codes are invalid: must be at least 6 digits!',
|
'status_description' => 'Some item codes are invalid: must be at least 6 digits!',
|
||||||
'invalid_item_codes' => array_values($invalidLengthItemCodes)
|
'invalid_item_codes' => array_values($invalidLengthItemCodes),
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$invalidItemCodes = [];
|
$invalidItemCodes = [];
|
||||||
$invalidPlantItems = [];
|
$invalidPlantItems = [];
|
||||||
|
|
||||||
foreach ($data['item_codes'] as $item)
|
foreach ($data['item_codes'] as $item) {
|
||||||
{
|
|
||||||
$itemCode = $item['item_code'] ?? null;
|
$itemCode = $item['item_code'] ?? null;
|
||||||
|
|
||||||
if (!$itemCode) {
|
if (! $itemCode) {
|
||||||
$invalidItemCodes[] = "(missing)";
|
$invalidItemCodes[] = '(missing)';
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$itemObj = Item::where('code', $itemCode)->first();
|
$itemObj = Item::where('code', $itemCode)->first();
|
||||||
if (!$itemObj) {
|
if (! $itemObj) {
|
||||||
$invalidItemCodes[] = $itemCode;
|
$invalidItemCodes[] = $itemCode;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$itemPlant = Item::where('plant_id', $plantId)
|
$itemPlant = Item::where('plant_id', $plantId)
|
||||||
->where('code', $itemCode)->first();
|
->where('code', $itemCode)->first();
|
||||||
if (!$itemPlant) {
|
if (! $itemPlant) {
|
||||||
$invalidPlantItems[] = $itemCode;
|
$invalidPlantItems[] = $itemCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -189,7 +186,7 @@ class PdfController extends Controller
|
|||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Some item codes are invalid!',
|
'status_description' => 'Some item codes are invalid!',
|
||||||
'not_found_items' => array_values($invalidItemCodes),
|
'not_found_items' => array_values($invalidItemCodes),
|
||||||
'not_in_plant' => array_values($invalidPlantItems)
|
'not_in_plant' => array_values($invalidPlantItems),
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,7 +201,7 @@ class PdfController extends Controller
|
|||||||
->where('code', $itemCode)
|
->where('code', $itemCode)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$itemObj) {
|
if (! $itemObj) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,14 +225,13 @@ class PdfController extends Controller
|
|||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Some serial numbers not found in DB for given plant, GR number, and item!',
|
'status_description' => 'Some serial numbers not found in DB for given plant, GR number, and item!',
|
||||||
'missing_serials' => $missingSerialsByItem
|
'missing_serials' => $missingSerialsByItem,
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$alreadyCompleted = [];
|
$alreadyCompleted = [];
|
||||||
|
|
||||||
foreach ($data['item_codes'] as $item)
|
foreach ($data['item_codes'] as $item) {
|
||||||
{
|
|
||||||
$itemCode = $item['item_code'];
|
$itemCode = $item['item_code'];
|
||||||
$serialNumbers = $item['serial_numbers'];
|
$serialNumbers = $item['serial_numbers'];
|
||||||
|
|
||||||
@@ -248,7 +244,7 @@ class PdfController extends Controller
|
|||||||
->where('serial_number', $serial)
|
->where('serial_number', $serial)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$gr) {
|
if (! $gr) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,11 +254,11 @@ class PdfController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($alreadyCompleted)) {
|
if (! empty($alreadyCompleted)) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Below serial numbers are already completed',
|
'status_description' => 'Below serial numbers are already completed',
|
||||||
'serial_numbers' => $alreadyCompleted
|
'serial_numbers' => $alreadyCompleted,
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
// {
|
// {
|
||||||
@@ -286,7 +282,6 @@ class PdfController extends Controller
|
|||||||
// ], 400);
|
// ], 400);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
// $itemId = $itemObj->id;
|
// $itemId = $itemObj->id;
|
||||||
|
|
||||||
// // Update all serial numbers for this item
|
// // Update all serial numbers for this item
|
||||||
@@ -297,9 +292,7 @@ class PdfController extends Controller
|
|||||||
// ->update(['created_by' => $scannedBy]);
|
// ->update(['created_by' => $scannedBy]);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
foreach ($data['item_codes'] as $item) {
|
||||||
foreach ($data['item_codes'] as $item)
|
|
||||||
{
|
|
||||||
$itemCode = $item['item_code'];
|
$itemCode = $item['item_code'];
|
||||||
$serialNumbers = $item['serial_numbers'];
|
$serialNumbers = $item['serial_numbers'];
|
||||||
|
|
||||||
@@ -309,12 +302,12 @@ class PdfController extends Controller
|
|||||||
->where('item_id', $itemId)
|
->where('item_id', $itemId)
|
||||||
->where('gr_number', $data['gr_number'])
|
->where('gr_number', $data['gr_number'])
|
||||||
->whereIn('serial_number', $serialNumbers)
|
->whereIn('serial_number', $serialNumbers)
|
||||||
->update(['created_by' => $scannedBy, 'status' => 'Completed',]);
|
->update(['created_by' => $scannedBy, 'status' => 'Completed']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'SUCCESS',
|
'status_code' => 'SUCCESS',
|
||||||
'status_description' => 'Serial numbers updated successfully!'
|
'status_description' => 'Serial numbers updated successfully!',
|
||||||
], 200);
|
], 200);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -324,40 +317,38 @@ class PdfController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function getPdf(Request $request)
|
public function getPdf(Request $request)
|
||||||
{
|
{
|
||||||
// Validate input
|
// Validate input
|
||||||
// $request->validate([
|
// $request->validate([
|
||||||
// 'filename' => 'required|string',
|
// 'filename' => 'required|string',
|
||||||
// ]);
|
// ]);
|
||||||
$expectedUser = env('API_AUTH_USER');
|
$expectedUser = env('API_AUTH_USER');
|
||||||
$expectedPw = env('API_AUTH_PW');
|
$expectedPw = env('API_AUTH_PW');
|
||||||
$header_auth = $request->header('Authorization');
|
$header_auth = $request->header('Authorization');
|
||||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
||||||
|
|
||||||
if ("Bearer " . $expectedToken != $header_auth)
|
if ('Bearer '.$expectedToken != $header_auth) {
|
||||||
{
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Invalid authorization token!'
|
'status_description' => 'Invalid authorization token!',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$filename = $request->header('process-order');
|
$filename = $request->header('process-order');
|
||||||
|
|
||||||
if (!$filename)
|
if (! $filename) {
|
||||||
{
|
|
||||||
return response()->json(['error' => 'Missing file-name header'], 400);
|
return response()->json(['error' => 'Missing file-name header'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$filename = basename($filename);
|
$filename = basename($filename);
|
||||||
|
|
||||||
// Ensure the file has .pdf extension
|
// Ensure the file has .pdf extension
|
||||||
if (!str_ends_with(strtolower($filename), '.pdf')) {
|
if (! str_ends_with(strtolower($filename), '.pdf')) {
|
||||||
$filename .= '.pdf';
|
$filename .= '.pdf';
|
||||||
}
|
}
|
||||||
|
|
||||||
$filePath = "uploads/ProcessOrder/" . $filename;
|
$filePath = 'uploads/ProcessOrder/'.$filename;
|
||||||
|
|
||||||
if (!Storage::disk('local')->exists($filePath)) {
|
if (! Storage::disk('local')->exists($filePath)) {
|
||||||
return response()->json(['error' => 'File not found'], 404);
|
return response()->json(['error' => 'File not found'], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,55 +357,53 @@ class PdfController extends Controller
|
|||||||
|
|
||||||
return Response::make($file, 200, [
|
return Response::make($file, 200, [
|
||||||
'Content-Type' => $mimeType,
|
'Content-Type' => $mimeType,
|
||||||
'Content-Disposition' => 'inline; filename="' . $filename . '"',
|
'Content-Disposition' => 'inline; filename="'.$filename.'"',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getGRPdf(Request $request)
|
public function getGRPdf(Request $request)
|
||||||
{
|
{
|
||||||
// Validate input
|
// Validate input
|
||||||
// $request->validate([
|
// $request->validate([
|
||||||
// 'filename' => 'required|string',
|
// 'filename' => 'required|string',
|
||||||
// ]);
|
// ]);
|
||||||
$expectedUser = env('API_AUTH_USER');
|
$expectedUser = env('API_AUTH_USER');
|
||||||
$expectedPw = env('API_AUTH_PW');
|
$expectedPw = env('API_AUTH_PW');
|
||||||
$header_auth = $request->header('Authorization');
|
$header_auth = $request->header('Authorization');
|
||||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
||||||
|
|
||||||
if ("Bearer " . $expectedToken != $header_auth)
|
if ('Bearer '.$expectedToken != $header_auth) {
|
||||||
{
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Invalid authorization token!'
|
'status_description' => 'Invalid authorization token!',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$filename = $request->header('gr-number');
|
$filename = $request->header('gr-number');
|
||||||
|
|
||||||
if (!$filename)
|
if (! $filename) {
|
||||||
{
|
return response()->json([
|
||||||
return response()->json([
|
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Gr Number cannot be empty!'
|
'status_description' => 'Gr Number cannot be empty!',
|
||||||
], 403);
|
], 403);
|
||||||
//return response()->json(['error' => 'Missing file-name header'], 400);
|
// return response()->json(['error' => 'Missing file-name header'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$filename = basename($filename);
|
$filename = basename($filename);
|
||||||
|
|
||||||
// Ensure the file has .pdf extension
|
// Ensure the file has .pdf extension
|
||||||
if (!str_ends_with(strtolower($filename), '.pdf')) {
|
if (! str_ends_with(strtolower($filename), '.pdf')) {
|
||||||
$filename .= '.pdf';
|
$filename .= '.pdf';
|
||||||
}
|
}
|
||||||
|
|
||||||
$filePath = "uploads/GRNumber/" . $filename;
|
$filePath = 'uploads/GRNumber/'.$filename;
|
||||||
|
|
||||||
if (!Storage::disk('local')->exists($filePath)) {
|
if (! Storage::disk('local')->exists($filePath)) {
|
||||||
//return response()->json(['error' => 'File not found'], 404);
|
// return response()->json(['error' => 'File not found'], 404);
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Pdf File not found for the provided GrNumber!'
|
'status_description' => 'Pdf File not found for the provided GrNumber!',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -423,7 +412,7 @@ class PdfController extends Controller
|
|||||||
|
|
||||||
return Response::make($file, 200, [
|
return Response::make($file, 200, [
|
||||||
'Content-Type' => $mimeType,
|
'Content-Type' => $mimeType,
|
||||||
'Content-Disposition' => 'inline; filename="' . $filename . '"',
|
'Content-Disposition' => 'inline; filename="'.$filename.'"',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -431,75 +420,71 @@ class PdfController extends Controller
|
|||||||
public function getGRSerial(Request $request)
|
public function getGRSerial(Request $request)
|
||||||
{
|
{
|
||||||
$expectedUser = env('API_AUTH_USER');
|
$expectedUser = env('API_AUTH_USER');
|
||||||
$expectedPw = env('API_AUTH_PW');
|
$expectedPw = env('API_AUTH_PW');
|
||||||
$header_auth = $request->header('Authorization');
|
$header_auth = $request->header('Authorization');
|
||||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
||||||
|
|
||||||
if ("Bearer " . $expectedToken != $header_auth)
|
if ('Bearer '.$expectedToken != $header_auth) {
|
||||||
{
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Invalid authorization token!'
|
'status_description' => 'Invalid authorization token!',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plantCode = $request->header('plant-code');
|
$plantCode = $request->header('plant-code');
|
||||||
|
|
||||||
$grNumber = $request->header('gr-number');
|
$grNumber = $request->header('gr-number');
|
||||||
|
|
||||||
|
if (! $plantCode) {
|
||||||
if (!$plantCode) {
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Plant Code value can't be empty"
|
'status_description' => "Plant Code value can't be empty",
|
||||||
], 404);
|
], 404);
|
||||||
}
|
} elseif (! $grNumber) {
|
||||||
else if (!$grNumber)
|
return response()->json([
|
||||||
{
|
|
||||||
return response()->json([
|
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'GR Number cannot be empty!'
|
'status_description' => 'GR Number cannot be empty!',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plant = Plant::where('code', $plantCode)->first();
|
$plant = Plant::where('code', $plantCode)->first();
|
||||||
$plantId = $plant->id;
|
$plantId = $plant->id;
|
||||||
|
|
||||||
if (!$plant) {
|
if (! $plant) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Plant Code '{$plantCode}' not found!"
|
'status_description' => "Plant Code '{$plantCode}' not found!",
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$grExist = GRMaster::where('gr_number', $grNumber)->first();
|
$grExist = GRMaster::where('gr_number', $grNumber)->first();
|
||||||
|
|
||||||
if (!$grExist) {
|
if (! $grExist) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'GR Number not found'
|
'status_description' => 'GR Number not found',
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$grExists = GRMaster::where('plant_id', $plantId)
|
$grExists = GRMaster::where('plant_id', $plantId)
|
||||||
->where('gr_number', $grNumber)
|
->where('gr_number', $grNumber)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$grExists) {
|
if (! $grExists) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'GR Number not found for this plant!'
|
'status_description' => 'GR Number not found for this plant!',
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$grRecords = GrMaster::where('plant_id', $plantId)
|
$grRecords = GrMaster::where('plant_id', $plantId)
|
||||||
->where('gr_number', $grNumber)
|
->where('gr_number', $grNumber)
|
||||||
->get(['serial_number', 'item_id']);
|
->get(['serial_number', 'item_id']);
|
||||||
|
|
||||||
if (empty($grRecords )) {
|
if (empty($grRecords)) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'No serial numbers found for the given GR number!'
|
'status_description' => 'No serial numbers found for the given GR number!',
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -518,95 +503,90 @@ class PdfController extends Controller
|
|||||||
// ], 200);
|
// ], 200);
|
||||||
$itemIds = $grRecords->pluck('item_id')->unique()->filter();
|
$itemIds = $grRecords->pluck('item_id')->unique()->filter();
|
||||||
|
|
||||||
$items = Item::whereIn('id', $itemIds)->pluck('code', 'id');
|
$items = Item::whereIn('id', $itemIds)->pluck('code', 'id');
|
||||||
|
|
||||||
$result = $grRecords->groupBy('item_id')->map(function ($group, $itemId) use ($items) {
|
$result = $grRecords->groupBy('item_id')->map(function ($group, $itemId) use ($items) {
|
||||||
return [
|
return [
|
||||||
'item_code' => $items[$itemId] ?? null,
|
'item_code' => $items[$itemId] ?? null,
|
||||||
'serial_numbers' => $group->pluck('serial_number')->toArray()
|
'serial_numbers' => $group->pluck('serial_number')->toArray(),
|
||||||
];
|
];
|
||||||
})->values(); // remove keys
|
})->values(); // remove keys
|
||||||
|
|
||||||
return response()->json($result, 200);
|
return response()->json($result, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getProcessOrderData (Request $request)
|
public function getProcessOrderData(Request $request)
|
||||||
{
|
{
|
||||||
$expectedUser = env('API_AUTH_USER');
|
$expectedUser = env('API_AUTH_USER');
|
||||||
$expectedPw = env('API_AUTH_PW');
|
$expectedPw = env('API_AUTH_PW');
|
||||||
$header_auth = $request->header('Authorization');
|
$header_auth = $request->header('Authorization');
|
||||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
$expectedToken = $expectedUser.':'.$expectedPw;
|
||||||
|
|
||||||
if ("Bearer " . $expectedToken != $header_auth)
|
if ('Bearer '.$expectedToken != $header_auth) {
|
||||||
{
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Invalid authorization token!'
|
'status_description' => 'Invalid authorization token!',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plantCode = $request->header('plant-code');
|
$plantCode = $request->header('plant-code');
|
||||||
|
|
||||||
$processOrder = $request->header('process-order');
|
$processOrder = $request->header('process-order');
|
||||||
|
|
||||||
|
if (! $plantCode) {
|
||||||
if (!$plantCode) {
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Plant Code value can't be empty"
|
'status_description' => "Plant Code value can't be empty",
|
||||||
], 404);
|
], 404);
|
||||||
}
|
} elseif (! $processOrder) {
|
||||||
else if (!$processOrder)
|
return response()->json([
|
||||||
{
|
|
||||||
return response()->json([
|
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Process Order cannot be empty!'
|
'status_description' => 'Process Order cannot be empty!',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plant = Plant::where('code', $plantCode)->first();
|
$plant = Plant::where('code', $plantCode)->first();
|
||||||
$plantId = $plant->id;
|
$plantId = $plant->id;
|
||||||
|
|
||||||
if (!$plant) {
|
if (! $plant) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Plant Code '{$plantCode}' not found!"
|
'status_description' => "Plant Code '{$plantCode}' not found!",
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$processOrderExist = ProcessOrder::where('process_order', $processOrder)->first();
|
$processOrderExist = ProcessOrder::where('process_order', $processOrder)->first();
|
||||||
|
|
||||||
if (!$processOrderExist) {
|
if (! $processOrderExist) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Process order not found'
|
'status_description' => 'Process order not found',
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$proOrdAgPlant = ProcessOrder::where('plant_id', $plantId)
|
$proOrdAgPlant = ProcessOrder::where('plant_id', $plantId)
|
||||||
->where('process_order', $processOrder)
|
->where('process_order', $processOrder)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$proOrdAgPlant) {
|
if (! $proOrdAgPlant) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Process order not found for this plant!'
|
'status_description' => 'Process order not found for this plant!',
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$item = $proOrdAgPlant->item;
|
$item = $proOrdAgPlant->item;
|
||||||
|
|
||||||
$processOrderRecords = ProcessOrder::with('item')
|
$processOrderRecords = ProcessOrder::with('item')
|
||||||
->where('plant_id', $plant->id)
|
->where('plant_id', $plant->id)
|
||||||
->where('process_order', $processOrder)
|
->where('process_order', $processOrder)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
$lastRecord = ProcessOrder::with('item')
|
$lastRecord = ProcessOrder::with('item')
|
||||||
->where('plant_id', $plant->id)
|
->where('plant_id', $plant->id)
|
||||||
->where('process_order', $processOrder)
|
->where('process_order', $processOrder)
|
||||||
->orderBy('id', 'desc')
|
->orderBy('id', 'desc')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$totalReceivedQty = $processOrderRecords->sum('received_quantity');
|
$totalReceivedQty = $processOrderRecords->sum('received_quantity');
|
||||||
// $lastRecord = $processOrderRecords->first();
|
// $lastRecord = $processOrderRecords->first();
|
||||||
@@ -615,7 +595,7 @@ class PdfController extends Controller
|
|||||||
if ($totalReceivedQty == $proOrdAgPlant->order_quantity) {
|
if ($totalReceivedQty == $proOrdAgPlant->order_quantity) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' has already reached its order quantity."
|
'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' has already reached its order quantity.",
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -627,66 +607,67 @@ class PdfController extends Controller
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'item_code' => $item?->code ?? "",
|
'item_code' => $item?->code ?? '',
|
||||||
'description' => $item?->description ?? "",
|
'description' => $item?->description ?? '',
|
||||||
// 'coil_number' => $proOrdAgPlant->coil_number ?? "",
|
// 'coil_number' => $proOrdAgPlant->coil_number ?? "",
|
||||||
// 'order_quantity' => (string)$proOrdAgPlant->order_quantity ?? "",
|
// 'order_quantity' => (string)$proOrdAgPlant->order_quantity ?? "",
|
||||||
'coil_number' => $lastRecord->coil_number ?? "",
|
'coil_number' => $lastRecord->coil_number ?? '',
|
||||||
'order_quantity' => (string)$lastRecord->order_quantity ?? "",
|
'order_quantity' => (string) $lastRecord->order_quantity ?? '',
|
||||||
'received_quantity' => (string)$totalReceivedQty ?? "",
|
'received_quantity' => (string) $totalReceivedQty ?? '',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function storeProcessOrderData(Request $request)
|
public function storeProcessOrderData(Request $request)
|
||||||
{
|
{
|
||||||
$expectedUser = env('API_AUTH_USER');
|
$expectedUser = env('API_AUTH_USER');
|
||||||
$expectedPw = env('API_AUTH_PW');
|
$expectedPw = env('API_AUTH_PW');
|
||||||
$headerAuth = $request->header('Authorization');
|
$headerAuth = $request->header('Authorization');
|
||||||
$expectedToken = "Bearer " . $expectedUser . ':' . $expectedPw;
|
$expectedToken = 'Bearer '.$expectedUser.':'.$expectedPw;
|
||||||
|
|
||||||
if ($headerAuth !== $expectedToken) {
|
if ($headerAuth !== $expectedToken) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Invalid authorization token!'
|
'status_description' => 'Invalid authorization token!',
|
||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plantCode = $request->header('plant-code');
|
Log::info('POST : Process Orders API called', ['request_data' => $request->all()]);
|
||||||
|
|
||||||
|
$plantCode = $request->header('plant-code');
|
||||||
$processOrder = $request->header('process-order');
|
$processOrder = $request->header('process-order');
|
||||||
|
|
||||||
if (!$plantCode || !$processOrder) {
|
if (! $plantCode || ! $processOrder) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'plant-code and process-order are required'
|
'status_description' => 'plant-code and process-order are required',
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plant = Plant::where('code', $plantCode)->first();
|
$plant = Plant::where('code', $plantCode)->first();
|
||||||
if (!$plant) {
|
if (! $plant) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Invalid plant code'
|
'status_description' => 'Invalid plant code',
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plantId = $plant->id;
|
$plantId = $plant->id;
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'item_code' => 'nullable|integer',
|
'item_code' => 'nullable|integer',
|
||||||
'coil_number' => 'nullable|string',
|
'coil_number' => 'nullable|string',
|
||||||
'order_quantity' => 'nullable|integer',
|
'order_quantity' => 'nullable|integer',
|
||||||
'received_quantity' => 'nullable|numeric',
|
'received_quantity' => 'nullable|numeric',
|
||||||
'created_by' => 'nullable|string',
|
'created_by' => 'nullable|string',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$item = Item::where('code', $validated['item_code'])
|
$item = Item::where('code', $validated['item_code'])
|
||||||
->where('plant_id', $plantId)
|
->where('plant_id', $plantId)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$item)
|
if (! $item) {
|
||||||
{
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Invalid item_code for this plant'
|
'message' => 'Invalid item_code for this plant',
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -694,14 +675,14 @@ class PdfController extends Controller
|
|||||||
|
|
||||||
$user = User::where('name', $createdBy)->first();
|
$user = User::where('name', $createdBy)->first();
|
||||||
|
|
||||||
if (!$user) {
|
if (! $user) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "User '{$createdBy}' not found"
|
'status_description' => "User '{$createdBy}' not found",
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$existing = ProcessOrder::where('plant_id', $plant->id)
|
$existing = ProcessOrder::where('plant_id', $plant->id)
|
||||||
->where('process_order', $processOrder)
|
->where('process_order', $processOrder)
|
||||||
->where('item_id', '!=', $item->id)
|
->where('item_id', '!=', $item->id)
|
||||||
->first();
|
->first();
|
||||||
@@ -709,7 +690,7 @@ class PdfController extends Controller
|
|||||||
if ($existing) {
|
if ($existing) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' already has item_code '{$existing->item->code}'"
|
'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' already has item_code '{$existing->item->code}'",
|
||||||
], 409);
|
], 409);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -719,7 +700,7 @@ class PdfController extends Controller
|
|||||||
->sum('received_quantity');
|
->sum('received_quantity');
|
||||||
|
|
||||||
$newReceived = $validated['received_quantity'] ?? 0;
|
$newReceived = $validated['received_quantity'] ?? 0;
|
||||||
$orderQty = $validated['order_quantity'] ?? 0;
|
$orderQty = $validated['order_quantity'] ?? 0;
|
||||||
|
|
||||||
if ($orderQty == 0) {
|
if ($orderQty == 0) {
|
||||||
$orderQty = ProcessOrder::where('plant_id', $plant->id)
|
$orderQty = ProcessOrder::where('plant_id', $plant->id)
|
||||||
@@ -732,41 +713,37 @@ class PdfController extends Controller
|
|||||||
|
|
||||||
if ($total > $orderQty) {
|
if ($total > $orderQty) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Received quantity cannot exceed order quantity.
|
'status_description' => "Received quantity cannot exceed order quantity.
|
||||||
Order Qty = {$orderQty}, Already Received = {$alreadyReceived},
|
Order Qty = {$orderQty}, Already Received = {$alreadyReceived},Trying to Insert = {$newReceived}",
|
||||||
Trying to Insert = {$newReceived}"
|
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
ProcessOrder::Create(
|
ProcessOrder::Create(
|
||||||
[
|
[
|
||||||
'plant_id' => $plant->id,
|
'plant_id' => $plant->id,
|
||||||
'process_order' => $processOrder,
|
'process_order' => $processOrder,
|
||||||
'item_id' => $item->id,
|
'item_id' => $item->id,
|
||||||
'coil_number' => $validated['coil_number'] ?? "",
|
'coil_number' => $validated['coil_number'] ?? '',
|
||||||
'order_quantity' => $validated['order_quantity'] ?? 0,
|
'order_quantity' => $validated['order_quantity'] ?? 0,
|
||||||
'received_quantity' => $validated['received_quantity'] ?? 0,
|
'received_quantity' => $validated['received_quantity'] ?? 0,
|
||||||
'created_by' => $validated['created_by'] ?? "",
|
'created_by' => $validated['created_by'] ?? '',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'SUCCESS',
|
'status_code' => 'SUCCESS',
|
||||||
'status_description' => 'Record Inserted Successfully'
|
'status_description' => 'Record Inserted Successfully',
|
||||||
]);
|
]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => $e->getMessage()
|
'status_description' => $e->getMessage(),
|
||||||
], 500);
|
], 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the specified resource.
|
* Display the specified resource.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user