128 lines
5.2 KiB
PHP
Executable File
128 lines
5.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\M3\Http\Controllers\API;
|
|
|
|
use Config;
|
|
use DB;
|
|
use Illuminate\Http\Client\Response as HttpResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Modules\M3\Helpers\Helper;
|
|
use Modules\M3\Http\Controllers\Controller;
|
|
use Str;
|
|
|
|
class TwoShipAPIs extends Controller
|
|
{
|
|
private const SOURCE = 'TwoShipUpdateHold';
|
|
|
|
private const RESPONSE_TYPE = 'AggStopRsp';
|
|
|
|
public function TwoShipUpdateHold(Request $request)
|
|
{
|
|
$this->configureTimeouts();
|
|
$user = m3_user() !== '' ? Str::upper(m3_user()) : 'WebUser';
|
|
$params = $request->all();
|
|
$whs = (string) ($params['WHS'] ?? '');
|
|
$co = (string) ($params['COMPANY'] ?? '');
|
|
$macpkr = (string) ($params['MACPKR'] ?? '');
|
|
|
|
if ($params === []) {
|
|
$this->log($macpkr ?: '000000', $co, $whs, $user, 'X', 'No URL Parameters Sent');
|
|
|
|
return response()->json(['code' => '405', 'status' => 'Error', 'message' => 'No URL Parameters Sent']);
|
|
}
|
|
|
|
try {
|
|
$this->logRequest($macpkr, $co, $whs, $user);
|
|
$orders = $this->aggregationOrders($macpkr, $co, $whs);
|
|
$response = $this->callTwoShip($orders);
|
|
$error = $this->responseError($response);
|
|
if ($error !== '') {
|
|
$this->log($macpkr, $co, $whs, $user, 'X', $response->status() . '-' . $error);
|
|
|
|
return response()->json(['code' => '405', 'status' => 'error', 'message' => 'Status: ' . $response->status() . '-' . $error]);
|
|
}
|
|
|
|
$this->log($macpkr, $co, $whs, $user, '', $response->body());
|
|
$this->markAggregationComplete($orders, $co, $whs);
|
|
|
|
return response()->json(['code' => '200', 'status' => 'Success']);
|
|
} catch (\Throwable $th) {
|
|
$message = str_contains($th->getMessage(), 'cURL error')
|
|
? 'Curl Error: Communication with 2Ship Timed Out.'
|
|
: '2Ship communication failed.';
|
|
$this->log($macpkr, $co, $whs, $user, 'X', $message);
|
|
|
|
return response()->json(['code' => '405', 'status' => 'error', 'message' => $message]);
|
|
}
|
|
}
|
|
|
|
private function configureTimeouts(): void
|
|
{
|
|
$timeout = Config::get('arrays.PHPTimeOut');
|
|
ini_set('max_execution_time', $timeout);
|
|
ini_set('max_input_time', $timeout);
|
|
ini_set('default_socket_timeout', $timeout);
|
|
}
|
|
|
|
private function logRequest(string $macpkr, string $co, string $whs, string $user): void
|
|
{
|
|
$data = json_encode(['CO' => $co, 'WHS' => $whs, 'MACPKR' => $macpkr]);
|
|
Helper::logToICM699M($this->logData($macpkr, $co, $whs, $user, '', (string) $data, 'AggStopReq'));
|
|
}
|
|
|
|
private function aggregationOrders(string $macpkr, string $co, string $whs): array
|
|
{
|
|
$aggregation = DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
|
|
->where('A4MACPKR', $macpkr)->value('A4SHPAGGCD');
|
|
if (empty($aggregation) || (string) $aggregation === '0') {
|
|
return [$macpkr];
|
|
}
|
|
$orders = DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
|
|
->where('A4SHPAGGCD', $aggregation)->where('A4SHPAGGCD', '<>', 0)->where('A4CONCMPLT', '<>', 'y')
|
|
->where('A4CO', $co)->where('A4WHS', $whs)->pluck('A4MACPKR')->all();
|
|
|
|
return $orders !== [] ? $orders : [$macpkr];
|
|
}
|
|
|
|
private function callTwoShip(array $orders): HttpResponse
|
|
{
|
|
$payload = [
|
|
'WS_Key' => Config::get('arrays.2Ship.TwoShip_Key'),
|
|
'OrderNumbers' => [implode(',', $orders)],
|
|
'FindBy' => 'ByMultipleOrderNumbers',
|
|
'DataToBeUpdated' => ['UpdateAggregationSettings' => true, 'OrderAggregationSettingsInfo' => ['NotConsideredAggregation' => true]],
|
|
];
|
|
|
|
return Http::acceptJson()->asJson()->timeout((int) Config::get('arrays.PHPTimeOut'))
|
|
->post(Config::get('arrays.2Ship.end_point') . Config::get('arrays.2Ship.hold_update'), $payload);
|
|
}
|
|
|
|
private function responseError(HttpResponse $response): string
|
|
{
|
|
if (! $response->successful()) {
|
|
return 'Error: No Response from 2Ship';
|
|
}
|
|
$body = $response->json();
|
|
|
|
return ($body['Success'] ?? true) === false ? '2Ship Error: ' . ($body['GlobalError'] ?? 'Unknown error') : '';
|
|
}
|
|
|
|
private function markAggregationComplete(array $orders, string $co, string $whs): void
|
|
{
|
|
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
|
|
->whereIn('A4MACPKR', $orders)->where('A4CO', $co)->where('A4WHS', $whs)->update(['A4CONCMPLT' => 'Y']);
|
|
}
|
|
|
|
private function log(string $macpkr, string $co, string $whs, string $user, string $error, string $message): void
|
|
{
|
|
Helper::logToICM699M($this->logData($macpkr, $co, $whs, $user, $error, $message, self::RESPONSE_TYPE));
|
|
}
|
|
|
|
private function logData(string $macpkr, string $co, string $whs, string $user, string $error, string $message, string $type): array
|
|
{
|
|
return ['SHMACPKR' => $macpkr, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => $error,
|
|
'SHINTBOX' => '', 'SHTYPE' => $type, 'SHDATA' => $message, 'SHUSER' => $user, 'SHSOURCE' => self::SOURCE, 'SHCO' => $co, 'SHWHS' => $whs];
|
|
}
|
|
}
|