error('Manifest CloseAPI: Manifest, company and warehouse must be defined.'); $response = self::errorResponse('Manifest CloseAPI: Manifest, company and warehouse must be defined.'); } else { try { $records = self::findOpenManifestRecords($params); if ($records->isEmpty()) { Log::channel('dailyCron')->info('Manifest CloseAPI: No records found in ICM705M1.'); $response = self::errorResponse('Manifest CloseAPI: No records found in ICM705M1.'); } else { $response = self::submitManifest($records->first(), $params['SHUSER']); } } catch (\Throwable $exception) { Log::error($exception); $response = response()->json([ 'code' => '500', 'status' => 'Error', 'message' => 'Manifest Closing failed unexpectedly.', ], 500); } } return $response; } 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]); } }