Files
WMS/Modules/M3/Http/Controllers/M3CallCloseAPIController.php
T
2026-09-21 10:14:57 +01:00

113 lines
3.6 KiB
PHP
Executable File

<?php
namespace Modules\M3\Http\Controllers;
use Artisan;
use Config;
use DB;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Log;
class M3CallCloseAPIController extends Controller
{
public static function index(Request $request): JsonResponse
{
self::configureTimeouts();
$params = self::validatedParams($request);
if ($params === null) {
Log::channel('dailyCron')->error('Manifest CloseAPI: Manifest, company and warehouse must be defined.');
return self::errorResponse('Manifest CloseAPI: Manifest, company and warehouse must be defined.');
}
try {
$records = self::findOpenManifestRecords($params);
if ($records->isEmpty()) {
Log::channel('dailyCron')->info('Manifest CloseAPI: No records found in ICM705M1.');
return self::errorResponse('Manifest CloseAPI: No records found in ICM705M1.');
}
return self::submitManifest($records->first(), $params['SHUSER']);
} catch (\Throwable $exception) {
Log::error($exception);
return response()->json([
'code' => '500',
'status' => 'Error',
'message' => 'Manifest Closing failed unexpectedly.',
], 500);
}
}
private static function configureTimeouts(): void
{
$timeout = (string) Config::get('arrays.PHPTimeOut');
ini_set('max_execution_time', $timeout);
ini_set('max_input_time', $timeout);
ini_set('default_socket_timeout', $timeout);
}
private static function validatedParams(Request $request): ?array
{
$params = [
'SHUSER' => trim((string) $request->input('SHUSER', '')),
'MANIFEST' => trim((string) $request->input('MANIFEST', '')),
'APCO' => trim((string) $request->input('APCO', '')),
'APWHS' => trim((string) $request->input('APWHS', '')),
];
if ($params['MANIFEST'] === '' || $params['APCO'] === '' || $params['APWHS'] === '') {
return null;
}
return $params;
}
private static function findOpenManifestRecords(array $params)
{
$connection = config('m3.connection', 'pgsql');
if (! Schema::connection($connection)->hasTable('icm705m1')) {
return collect();
}
return DB::connection($connection)->table('icm705m1')
->select('APMANIFEST', 'APCO', 'APWHS')
->where('APSPARE2', '')
->where('APCO', $params['APCO'])
->where('APWHS', $params['APWHS'])
->where('APMANIFEST', $params['MANIFEST'])
->whereRaw('EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - APLDCLOZ)) / 60 > 15')
->orderBy('APMANIFEST')
->get();
}
private static function submitManifest(object $record, string $user): JsonResponse
{
$exitCode = Artisan::call('command:CallCloseAPI', [
'user' => $user,
'manifest' => $record->APMANIFEST ?? '',
'co' => $record->APCO ?? '',
'whs' => $record->APWHS ?? '',
]);
if ($exitCode === 1) {
return response()->json([
'code' => '200',
'status' => 'Success',
'message' => 'Manifest Closing submitted',
]);
}
return self::errorResponse('Manifest Closing Failed');
}
private static function errorResponse(string $message): JsonResponse
{
return response()->json(['code' => '206', 'status' => 'Error', 'message' => $message]);
}
}