134 lines
5.3 KiB
PHP
134 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\M3\Http\Controllers\API;
|
|
|
|
use Config;
|
|
use DB;
|
|
use Http;
|
|
use Modules\M3\Helpers\Helper;
|
|
|
|
final class CloseShipmentZeroPickProcessor
|
|
{
|
|
private const SOURCE = 'CloseShipmentCoChargeAPI';
|
|
private const JSON_CONTENT_TYPE = 'application/json';
|
|
private const NULL_M3_RESPONSE = 'M3 API responded with null value';
|
|
|
|
public function __construct(private readonly array $context, private readonly string $token)
|
|
{
|
|
}
|
|
|
|
public function process($shipments)
|
|
{
|
|
$errors = '';
|
|
foreach ($shipments as $row) {
|
|
$shipment = $this->shipmentContext($row);
|
|
$errors .= $this->closeShipment($shipment);
|
|
$this->closeFalsePositiveErrors($shipment);
|
|
}
|
|
|
|
return $this->resultResponse($errors);
|
|
}
|
|
|
|
private function shipmentContext(object $row): array
|
|
{
|
|
return [
|
|
'delivery' => trim($row->A4MACPKR),
|
|
'whs' => trim($row->A4WHS),
|
|
'company' => trim($row->A4CO),
|
|
];
|
|
}
|
|
|
|
private function closeShipment(array $shipment): string
|
|
{
|
|
$json = json_encode([
|
|
'DIVI' => $shipment['company'], 'WHS' => $shipment['whs'],
|
|
'DELIVERYNO' => $shipment['delivery'], 'TYPE' => 'CloseShipment',
|
|
'PICKLISTSUFFIX' => '1', 'ENDFLAG' => '1',
|
|
]);
|
|
$this->log($shipment, 'M3-JSN-out', $json);
|
|
$response = Http::withHeaders([
|
|
'accept' => self::JSON_CONTENT_TYPE, 'Content-Type' => self::JSON_CONTENT_TYPE, 'Connection' => 'close',
|
|
])->withToken($this->token)->withBody($json, self::JSON_CONTENT_TYPE)
|
|
->timeout(Config::get('arrays.curlTimeOut'))->retry(2, 100)
|
|
->post(env('ENDPOINTURL') . 'APIFLOWS/Sentinel/TransferAPI?ionapiRespStyle=sync');
|
|
$result = $this->parseResponse($response, $shipment);
|
|
|
|
if ($result['success']) {
|
|
$this->log($shipment, 'M3-Res-out', 'Success' . $result['session']);
|
|
|
|
return '';
|
|
}
|
|
|
|
$this->markFailed($shipment);
|
|
$this->log($shipment, 'M3-Res-out', 'Important-Error: ' . $result['errors'] . json_encode($result['message']) . $result['session'], 'X');
|
|
|
|
return $result['errors'] . '-';
|
|
}
|
|
|
|
private function parseResponse($response, array $shipment): array
|
|
{
|
|
$contents = json_decode($response->getBody()->getContents());
|
|
$session = $response->hasHeader('x-transaction-id') ? '-sessionID:' . $response->header('x-transaction-id') : '-sessionID';
|
|
if ($contents === null) {
|
|
return ['success' => false, 'message' => self::NULL_M3_RESPONSE, 'errors' => '', 'session' => $session];
|
|
}
|
|
|
|
$message = $contents->result->CloseShipment;
|
|
$apiResult = $message->PickingListClose->Result;
|
|
$errors = Helper::checkM3Errors($contents->result);
|
|
if ($apiResult !== 'Error') {
|
|
$this->markSuccessful($shipment);
|
|
}
|
|
|
|
return ['success' => $apiResult !== 'Error', 'message' => $message, 'errors' => $errors, 'session' => $session];
|
|
}
|
|
|
|
private function markSuccessful(array $shipment): void
|
|
{
|
|
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
|
|
->where('A4MACPKR', $shipment['delivery'])->where('A4CO', $shipment['company'])
|
|
->where('A4WHS', $shipment['whs'])->where('A4PICKSTS', '!=', 'X')->update(['A4PICKSTS' => 'A']);
|
|
}
|
|
|
|
private function markFailed(array $shipment): void
|
|
{
|
|
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
|
|
->where('A4MACPKR', $shipment['delivery'])->where('A4CO', $shipment['company'])
|
|
->where('A4WHS', $shipment['whs'])->where('A4PICKSTS', '!=', 'X')->update(['A4PICKSTS' => 'a']);
|
|
}
|
|
|
|
private function closeFalsePositiveErrors(array $shipment): void
|
|
{
|
|
DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
|
|
->where('SHMACPKR', $shipment['delivery'])->where('SHCO', $shipment['company'])
|
|
->where('SHWHS', $shipment['whs'])->where('SHSOURCE', self::SOURCE)->where('SHERROR', 'X')
|
|
->where('SHTYPE', 'M3-General')->where('SHDATA', 'like', '%CommTCP_read%')
|
|
->update([
|
|
'SHERROR' => 'Z', 'SHACKUSER' => $this->context['user'], 'SHACKDATE' => date('Y-m-d'),
|
|
'SHACKTIME' => date('H:i:s'), 'SHACKNOTE' => 'Sentinel automatically closed the error (false positive CommTCP_read)',
|
|
]);
|
|
}
|
|
|
|
private function resultResponse(string $errors)
|
|
{
|
|
if ($errors === '') {
|
|
return response()->json(['code' => '200', 'status' => 'Success', 'message' => 'All Operations Successful']);
|
|
}
|
|
|
|
return response()->json([
|
|
'code' => '206', 'status' => 'Success With Warnings',
|
|
'message' => $errors . '; Check The Logs for Errors or Warnings.',
|
|
]);
|
|
}
|
|
|
|
private function log(array $shipment, string $type, string $message, string $error = ''): void
|
|
{
|
|
Helper::logToICM699M([
|
|
'SHMACPKR' => $shipment['delivery'], 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'),
|
|
'SHERROR' => $error, 'SHINTBOX' => '', 'SHTYPE' => $type, 'SHDATA' => $message,
|
|
'SHUSER' => $this->context['user'], 'SHSOURCE' => self::SOURCE,
|
|
'SHCO' => $shipment['company'], 'SHWHS' => $shipment['whs'],
|
|
]);
|
|
}
|
|
}
|