232 lines
6.2 KiB
PHP
232 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\GrMaster;
|
|
use App\Models\Plant;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Response;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Str;
|
|
|
|
class PdfController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function getPdf(Request $request)
|
|
{
|
|
// Validate input
|
|
// $request->validate([
|
|
// 'filename' => 'required|string',
|
|
// ]);
|
|
$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);
|
|
}
|
|
|
|
$filename = $request->header('process-order');
|
|
|
|
if (!$filename)
|
|
{
|
|
return response()->json(['error' => 'Missing file-name header'], 400);
|
|
}
|
|
|
|
$filename = basename($filename);
|
|
|
|
// Ensure the file has .pdf extension
|
|
if (!str_ends_with(strtolower($filename), '.pdf')) {
|
|
$filename .= '.pdf';
|
|
}
|
|
|
|
$filePath = "uploads/ProcessOrder/" . $filename;
|
|
|
|
if (!Storage::disk('local')->exists($filePath)) {
|
|
return response()->json(['error' => 'File not found'], 404);
|
|
}
|
|
|
|
$file = Storage::disk('local')->get($filePath);
|
|
$mimeType = Storage::disk('local')->mimeType($filePath);
|
|
|
|
return Response::make($file, 200, [
|
|
'Content-Type' => $mimeType,
|
|
'Content-Disposition' => 'inline; filename="' . $filename . '"',
|
|
]);
|
|
|
|
}
|
|
|
|
public function getGRPdf(Request $request)
|
|
{
|
|
// Validate input
|
|
// $request->validate([
|
|
// 'filename' => 'required|string',
|
|
// ]);
|
|
$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);
|
|
}
|
|
|
|
$filename = $request->header('gr-number');
|
|
|
|
if (!$filename)
|
|
{
|
|
return response()->json(['error' => 'Missing file-name header'], 400);
|
|
}
|
|
|
|
$filename = basename($filename);
|
|
|
|
// Ensure the file has .pdf extension
|
|
if (!str_ends_with(strtolower($filename), '.pdf')) {
|
|
$filename .= '.pdf';
|
|
}
|
|
|
|
$filePath = "uploads/GRNumber/" . $filename;
|
|
|
|
if (!Storage::disk('local')->exists($filePath)) {
|
|
return response()->json(['error' => 'File not found'], 404);
|
|
}
|
|
|
|
$file = Storage::disk('local')->get($filePath);
|
|
$mimeType = Storage::disk('local')->mimeType($filePath);
|
|
|
|
return Response::make($file, 200, [
|
|
'Content-Type' => $mimeType,
|
|
'Content-Disposition' => 'inline; filename="' . $filename . '"',
|
|
]);
|
|
|
|
}
|
|
|
|
public function getGRSerial(Request $request)
|
|
{
|
|
$expectedUser = env('API_AUTH_USER');
|
|
$expectedPw = env('API_AUTH_PW');
|
|
$header_auth = $request->header('Authorization');
|
|
$expectedToken = $expectedUser . ':' . $expectedPw;
|
|
|
|
if ("Bearer " . $expectedToken != $header_auth)
|
|
{
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid authorization token!'
|
|
], 403);
|
|
}
|
|
|
|
$plantCode = $request->header('plant-code');
|
|
|
|
$grNumber = $request->header('gr-number');
|
|
|
|
|
|
if (!$plantCode) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant Code value can't be empty"
|
|
], 404);
|
|
}
|
|
else if (!$grNumber)
|
|
{
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'GR Number is cannot be empty!'
|
|
], 403);
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCode)->first();
|
|
$plantId = $plant->id;
|
|
|
|
if (!$plant) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant Code '{$plantCode}' not found!"
|
|
], 404);
|
|
}
|
|
|
|
$grExist = GRMaster::where('gr_number', $grNumber)->first();
|
|
|
|
if (!$grExist) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'GR Number not found'
|
|
], 404);
|
|
}
|
|
|
|
$grExists = GRMaster::where('plant_id', $plantId)
|
|
->where('gr_number', $grNumber)
|
|
->first();
|
|
|
|
if (!$grExists) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'GR Number not found for this plant!'
|
|
], 404);
|
|
}
|
|
|
|
|
|
$serialNumbers = GrMaster::where('plant_id', $plantId)
|
|
->where('gr_number', $grNumber)
|
|
->pluck('serial_number')
|
|
->toArray();
|
|
|
|
if (empty($serialNumbers)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'No serial numbers found for the given GR number!'
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'serial_numbers' => $serialNumbers
|
|
], 200);
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
}
|