M3 Security Fixes for pass 28
This commit is contained in:
@@ -12,129 +12,62 @@ use Str;
|
|||||||
|
|
||||||
class CallCloseAPI extends Command
|
class CallCloseAPI extends Command
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* The name and signature of the console command.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $signature = 'command:CallCloseAPI {user?} {manifest?} {co?} {whs?} ';
|
protected $signature = 'command:CallCloseAPI {user?} {manifest?} {co?} {whs?} ';
|
||||||
|
|
||||||
protected $description = 'Call Close API';
|
protected $description = 'Call Close API';
|
||||||
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
parent::__construct();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
// $this->info('Start CallCloseAPI...');
|
self::configureTimeouts();
|
||||||
ini_set('max_execution_time', Config::get('arrays.PHPTimeOut'));
|
|
||||||
ini_set('max_input_time', Config::get('arrays.PHPTimeOut'));
|
|
||||||
ini_set('default_socket_timeout', Config::get('arrays.PHPTimeOut'));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get the Calling User
|
$manifest = (string) ($this->argument('manifest') ?? '');
|
||||||
$userId = $this->argument('user');
|
if ($manifest !== '') {
|
||||||
$SHUSER = isset($userId) ? $userId : (m3_user() !== '' ? Str::upper(trim(m3_user())) : '');
|
return $this->closeSpecificManifest($manifest, (string) ($this->argument('co') ?? ''), (string) ($this->argument('whs') ?? ''));
|
||||||
|
|
||||||
// Get the Manifest Number
|
|
||||||
$manifest = $this->argument('manifest');
|
|
||||||
$MANIFESTNum = isset($manifest) ? $manifest : '';
|
|
||||||
$co = $this->argument('co');
|
|
||||||
$APCO = isset($co) ? $co : '';
|
|
||||||
$whs = $this->argument('whs');
|
|
||||||
$APWHS = isset($whs) ? $whs : '';
|
|
||||||
|
|
||||||
// if ($SHUSER!='') {
|
|
||||||
// Log::channel('dailyCron')->info("Cron...Start CallCloseAPI...For Manifest#: ".$MANIFESTNum); // ".$SHUSER."
|
|
||||||
|
|
||||||
if ($MANIFESTNum == '') {
|
|
||||||
// Get Macpickers from ICM705M1
|
|
||||||
$ICM705M1 = DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
|
|
||||||
->select('APMANIFEST', 'APCO', 'APWHS')
|
|
||||||
->where('APSPARE2', '')
|
|
||||||
->where('APMANIFEST', '!=', '0')
|
|
||||||
->whereRaw('EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - APLDCLOZ)) / 60 > 15')
|
|
||||||
->orderBy('APMANIFEST', 'desc')
|
|
||||||
->get();
|
|
||||||
// dump($ICM705M1);
|
|
||||||
|
|
||||||
if (count($ICM705M1) > 0) {
|
|
||||||
// Loop the Results from $ICM705M1
|
|
||||||
foreach ($ICM705M1 as $key => $ICM705M1R) {
|
|
||||||
$MANIFESTNum = $ICM705M1R->APMANIFEST;
|
|
||||||
|
|
||||||
$USERCO = isset($ICM705M1R->APCO) ? $ICM705M1R->APCO : '';
|
|
||||||
$USERWH = isset($ICM705M1R->APWHS) ? $ICM705M1R->APWHS : '';
|
|
||||||
|
|
||||||
// Update ICM705M1 and set APSPARE2 to P
|
|
||||||
$ICM705M1U = DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
|
|
||||||
->where('APMANIFEST', $MANIFESTNum)
|
|
||||||
->where('APCO', $USERCO)
|
|
||||||
->where('APWHS', $USERWH)
|
|
||||||
->update(['APSPARE2' => 'P']);
|
|
||||||
|
|
||||||
$myRequest = new Request;
|
|
||||||
$myRequest->setMethod('POST');
|
|
||||||
$myRequest->request->add([
|
|
||||||
'WHS' => $USERWH,
|
|
||||||
'COMPANY' => $USERCO,
|
|
||||||
'TRANSTYPE' => 'M',
|
|
||||||
'MANIFEST' => $MANIFESTNum,
|
|
||||||
]);
|
|
||||||
$callCloseShipment = CloseShipment::CloseShipmentCoChargeAPI($myRequest);
|
|
||||||
$responseData = $callCloseShipment->getData();
|
|
||||||
$jsonData = json_encode($responseData);
|
|
||||||
Log::channel('dailyCron')->info('Close Shipment API Response for Manifest:' . (string) (int) $MANIFESTNum . '-' . $jsonData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Call Closing for a specific Manifest
|
$this->closePendingManifests();
|
||||||
else {
|
return self::SUCCESS;
|
||||||
// Get Macpickers from ICM705M1
|
|
||||||
$ICM705M1 = DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
|
|
||||||
->select('APMANIFEST', 'APCO', 'APWHS')
|
|
||||||
->where('APSPARE2', '')
|
|
||||||
->where('APCO', $APCO)
|
|
||||||
->where('APWHS', $APWHS)
|
|
||||||
->where('APMANIFEST', '=', $MANIFESTNum)
|
|
||||||
->first();
|
|
||||||
|
|
||||||
// Update ICM705M1 and set APSPARE2 to P
|
|
||||||
$ICM705M1U = DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
|
|
||||||
->where('APMANIFEST', $MANIFESTNum)
|
|
||||||
->where('APCO', $APCO)
|
|
||||||
->where('APWHS', $APWHS)
|
|
||||||
->update(['APSPARE2' => 'P']);
|
|
||||||
|
|
||||||
$myRequest = new Request;
|
|
||||||
$myRequest->setMethod('POST');
|
|
||||||
|
|
||||||
$myRequest->request->add([
|
|
||||||
'WHS' => $APWHS,
|
|
||||||
'COMPANY' => $APCO,
|
|
||||||
'TRANSTYPE' => 'M',
|
|
||||||
'MANIFEST' => $MANIFESTNum,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$callCloseShipment = CloseShipment::CloseShipmentCoChargeAPI($myRequest);
|
|
||||||
$responseData = $callCloseShipment->getData();
|
|
||||||
$jsonData = json_encode($responseData);
|
|
||||||
Log::channel('dailyCron')->info('Close Shipment API Response for Manifest:' . $MANIFESTNum . '-' . $jsonData);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
// }
|
|
||||||
// else{
|
|
||||||
// Log::channel('dailyCron')->error('A user must be defined...');
|
|
||||||
|
|
||||||
// $this->error('A user must be defined...');
|
|
||||||
// }
|
|
||||||
|
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
// Log::error($th);
|
|
||||||
Log::channel('dailyCron')->error($th);
|
Log::channel('dailyCron')->error($th);
|
||||||
|
return self::FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static 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 closePendingManifests(): void
|
||||||
|
{
|
||||||
|
$rows = DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
|
||||||
|
->select('APMANIFEST', 'APCO', 'APWHS')->where('APSPARE2', '')->where('APMANIFEST', '!=', '0')
|
||||||
|
->whereRaw('EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - APLDCLOZ)) / 60 > 15')->orderBy('APMANIFEST', 'desc')->get();
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$this->closeManifest((string) $row->APMANIFEST, (string) ($row->APCO ?? ''), (string) ($row->APWHS ?? ''));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function closeSpecificManifest(string $manifest, string $co, string $whs): int
|
||||||
|
{
|
||||||
|
$exists = DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
|
||||||
|
->where('APSPARE2', '')->where('APCO', $co)->where('APWHS', $whs)->where('APMANIFEST', $manifest)->exists();
|
||||||
|
if (! $exists) {
|
||||||
|
Log::channel('dailyCron')->warning('Manifest not available for close', compact('manifest', 'co', 'whs'));
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
$this->closeManifest($manifest, $co, $whs);
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function closeManifest(string $manifest, string $co, string $whs): void
|
||||||
|
{
|
||||||
|
DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
|
||||||
|
->where('APMANIFEST', $manifest)->where('APCO', $co)->where('APWHS', $whs)->update(['APSPARE2' => 'P']);
|
||||||
|
$request = Request::create('', 'POST', ['WHS' => $whs, 'COMPANY' => $co, 'TRANSTYPE' => 'M', 'MANIFEST' => $manifest]);
|
||||||
|
$response = CloseShipment::CloseShipmentCoChargeAPI($request);
|
||||||
|
$json = json_encode($response->getData());
|
||||||
|
Log::channel('dailyCron')->info('Close Shipment API Response for Manifest:' . $manifest . '-' . $json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,280 +4,116 @@ namespace Modules\M3\Http\Controllers\API;
|
|||||||
|
|
||||||
use Config;
|
use Config;
|
||||||
use DB;
|
use DB;
|
||||||
|
use Illuminate\Http\Client\Response as HttpResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Support\Facades\Http;
|
||||||
use Log;
|
|
||||||
use Modules\M3\Helpers\Helper;
|
use Modules\M3\Helpers\Helper;
|
||||||
use Modules\M3\Http\Controllers\Controller;
|
use Modules\M3\Http\Controllers\Controller;
|
||||||
use Str;
|
use Str;
|
||||||
|
|
||||||
class TwoShipAPIs extends Controller
|
class TwoShipAPIs extends Controller
|
||||||
{
|
{
|
||||||
// TwoShipUpdateHold with URL Params
|
private const SOURCE = 'TwoShipUpdateHold';
|
||||||
|
private const RESPONSE_TYPE = 'AggStopRsp';
|
||||||
|
|
||||||
public function TwoShipUpdateHold(Request $request)
|
public function TwoShipUpdateHold(Request $request)
|
||||||
{
|
{
|
||||||
ini_set('max_execution_time', Config::get('arrays.PHPTimeOut'));
|
$this->configureTimeouts();
|
||||||
ini_set('max_input_time', Config::get('arrays.PHPTimeOut'));
|
$user = m3_user() !== '' ? Str::upper(m3_user()) : 'WebUser';
|
||||||
ini_set('default_socket_timeout', Config::get('arrays.PHPTimeOut'));
|
$params = $request->all();
|
||||||
|
$whs = (string) ($params['WHS'] ?? '');
|
||||||
|
$co = (string) ($params['COMPANY'] ?? '');
|
||||||
|
$macpkr = (string) ($params['MACPKR'] ?? '');
|
||||||
|
|
||||||
// dd($request->all());
|
if ($params === []) {
|
||||||
|
$this->log($macpkr ?: '000000', $co, $whs, $user, 'X', 'No URL Parameters Sent');
|
||||||
// Log::info( $request->all() );
|
return response()->json(['code' => '405', 'status' => 'Error', 'message' => 'No URL Parameters Sent']);
|
||||||
// Get The Calling User
|
}
|
||||||
$SHUSER = m3_user() !== '' ? Str::upper(m3_user()) : 'WebUser';
|
|
||||||
|
|
||||||
$SHSOURCE = 'TwoShipUpdateHold';
|
|
||||||
$TwoShipErrors = '';
|
|
||||||
|
|
||||||
$urlParams = count($request->all()) > 0 ? $request->all() : '';
|
|
||||||
$WHS = isset($urlParams['WHS']) ? $urlParams['WHS'] : '';
|
|
||||||
$CO = isset($urlParams['COMPANY']) ? $urlParams['COMPANY'] : '';
|
|
||||||
$MACPKR = isset($urlParams['MACPKR']) ? $urlParams['MACPKR'] : '';
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if ($urlParams != '') {
|
$this->logRequest($macpkr, $co, $whs, $user);
|
||||||
$IncomingParams = ['CO' => $CO, 'WHS' => $WHS, 'MACPKR' => $MACPKR];
|
$orders = $this->aggregationOrders($macpkr, $co, $whs);
|
||||||
$IncomingParams = json_encode($IncomingParams);
|
$response = $this->callTwoShip($orders);
|
||||||
|
$error = $this->responseError($response);
|
||||||
// Write to Logs : Params In
|
if ($error !== '') {
|
||||||
$data = ['SHMACPKR' => $MACPKR,
|
$this->log($macpkr, $co, $whs, $user, 'X', $response->status() . '-' . $error);
|
||||||
'SHDT' => date('Y-m-d'),
|
return response()->json(['code' => '405', 'status' => 'error', 'message' => 'Status: ' . $response->status() . '-' . $error]);
|
||||||
'SHTIME' => date('H:i:s'),
|
|
||||||
'SHERROR' => '',
|
|
||||||
'SHINTBOX' => '',
|
|
||||||
'SHTYPE' => 'AggStopReq',
|
|
||||||
'SHDATA' => $IncomingParams,
|
|
||||||
'SHUSER' => $SHUSER,
|
|
||||||
'SHSOURCE' => $SHSOURCE,
|
|
||||||
'SHCO' => $CO,
|
|
||||||
'SHWHS' => $WHS,
|
|
||||||
];
|
|
||||||
Helper::logToICM699M($data);
|
|
||||||
|
|
||||||
// First Step : Get MACPKRs list if available
|
|
||||||
// select a4macpkr from icm688m1 where a4shpaggcd in (select a4shpaggcd from icm688m1 where a4macpkr=233709) and a4shpaggcd<>0 and a4concmplt<>'y' and a4co=410 and a4whs=111
|
|
||||||
$A4MACPKRSel = DB::connection(config('m3.connection', 'pgsql'))->select(
|
|
||||||
"select a4macpkr from icm688m1 where a4shpaggcd in (select a4shpaggcd from icm688m1 where a4macpkr={$MACPKR})
|
|
||||||
and a4shpaggcd<>0 and a4concmplt<>'y' and a4co='{$CO}' and a4whs='{$WHS}' ",
|
|
||||||
);
|
|
||||||
// dd($A4MACPKRSel);
|
|
||||||
|
|
||||||
if (count($A4MACPKRSel) > 0) {
|
|
||||||
foreach ($A4MACPKRSel as $key => $A4MACPKRR) {
|
|
||||||
$A4MACPKRArr[] = $A4MACPKRR->A4MACPKR;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$A4MACPKRArr[] = $MACPKR;
|
|
||||||
}
|
|
||||||
|
|
||||||
// dump($A4MACPKRArr);
|
|
||||||
|
|
||||||
// Call 2ship API with Params
|
|
||||||
$WS_key = Config::get('arrays.2Ship.TwoShip_Key');
|
|
||||||
$end_point = Config::get('arrays.2Ship.end_point');
|
|
||||||
$API = Config::get('arrays.2Ship.hold_update');
|
|
||||||
|
|
||||||
$ShipOnHoldOrder_url = $end_point . $API;
|
|
||||||
|
|
||||||
// Build Json Data
|
|
||||||
$data = [
|
|
||||||
'WS_Key' => $WS_key,
|
|
||||||
'OrderNumbers' => [implode(',', $A4MACPKRArr)],
|
|
||||||
// 'ShipmentId' => 3,
|
|
||||||
'FindBy' => 'ByMultipleOrderNumbers',
|
|
||||||
'DataToBeUpdated' => [
|
|
||||||
'UpdateAggregationSettings' => true,
|
|
||||||
'OrderAggregationSettingsInfo' => [
|
|
||||||
'NotConsideredAggregation' => true,
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$json_string = json_encode($data);
|
|
||||||
|
|
||||||
// dump($json_string);
|
|
||||||
|
|
||||||
// ######
|
|
||||||
// Initialise the cURL var
|
|
||||||
$ch = curl_init();
|
|
||||||
|
|
||||||
// Get the response from cURL
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_URL, $ShipOnHoldOrder_url);
|
|
||||||
curl_setopt($ch, CURLOPT_VERBOSE, false);
|
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
|
||||||
// When CURLOPT_SSL_VERIFYPEER is enabled (value=1), and the verification fails to prove that the certificate is authentic, the connection fails.
|
|
||||||
// When the option is zero, the peer certificate verification succeeds regardless.
|
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
|
||||||
// When CURLOPT_SSL_VERIFYHOST is 2, that certificate must indicate that the server is the server to which you meant to connect,
|
|
||||||
// or the connection fails. Simply put, it means it has to have the same name in the certificate as is in the URL you operate against.
|
|
||||||
|
|
||||||
// Set data content type
|
|
||||||
curl_setopt(
|
|
||||||
$ch,
|
|
||||||
CURLOPT_HTTPHEADER,
|
|
||||||
[
|
|
||||||
'Content-Type: application/json',
|
|
||||||
'Content-Length: ' . strlen($json_string),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
// Post data
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_string);
|
|
||||||
|
|
||||||
// Set timeout
|
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, Config::get('arrays.PHPTimeOut'));
|
|
||||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Config::get('arrays.PHPTimeOut'));
|
|
||||||
|
|
||||||
// if set true, cURL will follow redirects
|
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
||||||
|
|
||||||
// Execute the request
|
|
||||||
$response = curl_exec($ch);
|
|
||||||
|
|
||||||
// Verifying error
|
|
||||||
$info = curl_getinfo($ch);
|
|
||||||
|
|
||||||
// Get status
|
|
||||||
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
||||||
|
|
||||||
// Output the error code
|
|
||||||
// $CurlError= curl_error($ch);
|
|
||||||
$CurlError = str_replace("'", ' ', curl_error($ch));
|
|
||||||
|
|
||||||
// Close the session
|
|
||||||
unset($ch);
|
|
||||||
|
|
||||||
// dump($response);
|
|
||||||
// dump($status);
|
|
||||||
// dump($CurlError);
|
|
||||||
|
|
||||||
if ($status == '200' && $CurlError == '') {
|
|
||||||
$responseJSON = json_decode($response);
|
|
||||||
// dump($responseJSON);
|
|
||||||
if ($responseJSON->Success == false) {
|
|
||||||
$TwoShipErrors = '2Ship Error: ' . $responseJSON->GlobalError;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$TwoShipErrors = 'Error: No Response from 2Ship';
|
|
||||||
$TwoShipErrors .= ' -Curl Error: ' . $CurlError;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate response From 2Ship
|
|
||||||
// Error
|
|
||||||
if ($TwoShipErrors != '') {
|
|
||||||
// dump($TwoShipErrors);
|
|
||||||
|
|
||||||
// Write to Logs : Error
|
|
||||||
$data = ['SHMACPKR' => $MACPKR,
|
|
||||||
'SHDT' => date('Y-m-d'),
|
|
||||||
'SHTIME' => date('H:i:s'),
|
|
||||||
'SHERROR' => 'X',
|
|
||||||
'SHINTBOX' => '',
|
|
||||||
'SHTYPE' => 'AggStopRsp',
|
|
||||||
'SHDATA' => $status . '-' . $TwoShipErrors,
|
|
||||||
'SHUSER' => $SHUSER,
|
|
||||||
'SHSOURCE' => $SHSOURCE,
|
|
||||||
'SHCO' => $CO,
|
|
||||||
'SHWHS' => $WHS,
|
|
||||||
];
|
|
||||||
Helper::logToICM699M($data);
|
|
||||||
|
|
||||||
// Send Response
|
|
||||||
return response()->json([
|
|
||||||
'code' => '405',
|
|
||||||
'status' => 'error',
|
|
||||||
'message' => 'Status: ' . $status . '-' . $TwoShipErrors,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
// Success
|
|
||||||
else {
|
|
||||||
// Write to Logs : JSON
|
|
||||||
$data = ['SHMACPKR' => $MACPKR,
|
|
||||||
'SHDT' => date('Y-m-d'),
|
|
||||||
'SHTIME' => date('H:i:s'),
|
|
||||||
'SHERROR' => '',
|
|
||||||
'SHINTBOX' => '',
|
|
||||||
'SHTYPE' => 'AggStopRsp',
|
|
||||||
'SHDATA' => $response,
|
|
||||||
'SHUSER' => $SHUSER,
|
|
||||||
'SHSOURCE' => $SHSOURCE,
|
|
||||||
'SHCO' => $CO,
|
|
||||||
'SHWHS' => $WHS,
|
|
||||||
];
|
|
||||||
Helper::logToICM699M($data);
|
|
||||||
|
|
||||||
// Update ICM688M1 set a4concmplt to 'y'
|
|
||||||
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
|
|
||||||
->whereIn('A4MACPKR', $A4MACPKRArr)
|
|
||||||
->where('A4CO', $CO)
|
|
||||||
->where('A4WHS', $WHS)
|
|
||||||
->update(['A4CONCMPLT' => 'Y']);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send Response
|
|
||||||
return response()->json([
|
|
||||||
'code' => '200',
|
|
||||||
'status' => 'Success',
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
// Error : No URL params
|
|
||||||
else {
|
|
||||||
// Write to Logs Out : Error
|
|
||||||
$MACPKR = isset($MACPKR) ? $MACPKR : '000000';
|
|
||||||
$data = ['SHMACPKR' => $MACPKR,
|
|
||||||
'SHDT' => date('Y-m-d'),
|
|
||||||
'SHTIME' => date('H:i:s'),
|
|
||||||
'SHERROR' => 'X',
|
|
||||||
'SHINTBOX' => '',
|
|
||||||
'SHTYPE' => 'AggStopRsp',
|
|
||||||
'SHDATA' => 'No URL Parameters Sent',
|
|
||||||
'SHUSER' => $SHUSER,
|
|
||||||
'SHSOURCE' => $SHSOURCE,
|
|
||||||
'SHCO' => $CO,
|
|
||||||
'SHWHS' => $WHS,
|
|
||||||
];
|
|
||||||
Helper::logToICM699M($data);
|
|
||||||
|
|
||||||
// Return Error
|
$this->log($macpkr, $co, $whs, $user, '', $response->body());
|
||||||
return response()->json([
|
$this->markAggregationComplete($orders, $co, $whs);
|
||||||
'code' => '405',
|
return response()->json(['code' => '200', 'status' => 'Success']);
|
||||||
'status' => 'Error',
|
|
||||||
'message' => 'No URL Parameters Sent',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
// Write to Logs Out : Error
|
$message = str_contains($th->getMessage(), 'cURL error')
|
||||||
$getCurlErrors = str_contains($th->getMessage(), 'cURL error');
|
? 'Curl Error: Communication with 2Ship Timed Out.'
|
||||||
if ($getCurlErrors) {
|
: '2Ship communication failed.';
|
||||||
// dd($getCurlErrors);
|
$this->log($macpkr, $co, $whs, $user, 'X', $message);
|
||||||
// Write to Logs Out : Failed
|
return response()->json(['code' => '405', 'status' => 'error', 'message' => $message]);
|
||||||
$data = ['SHMACPKR' => $MACPKR,
|
|
||||||
'SHDT' => date('Y-m-d'),
|
|
||||||
'SHTIME' => date('H:i:s'),
|
|
||||||
'SHERROR' => 'X',
|
|
||||||
'SHINTBOX' => '',
|
|
||||||
'SHTYPE' => 'AggStopRsp',
|
|
||||||
'SHDATA' => 'Curl Error: Communication with 2Ship Timed Out' . ' 2Ship-JSN-out' . $IncomingParams,
|
|
||||||
'SHUSER' => $SHUSER,
|
|
||||||
'SHSOURCE' => $SHSOURCE,
|
|
||||||
'SHCO' => $CO,
|
|
||||||
'SHWHS' => $WHS,
|
|
||||||
];
|
|
||||||
Helper::logToICM699M($data);
|
|
||||||
|
|
||||||
return response()->json([
|
|
||||||
'code' => '405',
|
|
||||||
'status' => 'error',
|
|
||||||
'message' => 'Curl Error: Communication with 2Ship Timed Out.',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// End TwoShipUpdateHold
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ namespace Modules\M3\Http\Controllers;
|
|||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use DB;
|
use DB;
|
||||||
use Illuminate\Support\Facades\Http; // added this for curl call to v4
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
@@ -13,133 +13,128 @@ class userPickingStats5 extends Controller
|
|||||||
{
|
{
|
||||||
public static function index(Request $request)
|
public static function index(Request $request)
|
||||||
{
|
{
|
||||||
|
[$userArray, $co, $whs] = self::loadUsers();
|
||||||
|
[$starting, $ending, $days] = self::dateRange($request);
|
||||||
|
$user = $request->user;
|
||||||
|
$statistics = [];
|
||||||
|
$totals = self::emptyTotals();
|
||||||
|
|
||||||
// 618
|
if (isset($request->dates)) {
|
||||||
// Get Logged User CO & WH. Legacy M3 profile data may not yet exist in PostgreSQL.
|
[$statistics, $totals] = self::collectDailyStatistics($request, $starting, $days, $totals);
|
||||||
$co = '';
|
|
||||||
$whs = '';
|
|
||||||
$user_array = [];
|
|
||||||
try {
|
|
||||||
if (Schema::connection(config('m3.connection', 'pgsql'))->hasTable('icm428m')) {
|
|
||||||
$profile = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
|
|
||||||
->select('R428US', 'R428CO', 'R428WH')
|
|
||||||
->where('R428US', strtoupper((string) m3_user()))
|
|
||||||
->first();
|
|
||||||
$co = trim((string) ($profile->R428CO ?? ''));
|
|
||||||
$whs = trim((string) ($profile->R428WH ?? ''));
|
|
||||||
|
|
||||||
if ($co !== '' && $whs !== '') {
|
|
||||||
$rows = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
|
|
||||||
->selectRaw("R428US as userprf, (trim(R428FIRST) || ' ' || trim(R428FAMILY)) as name, R428EMPCDE as empno")
|
|
||||||
->where('R428EMPCDE', '!=', 0)
|
|
||||||
->where('R428CO', $co)
|
|
||||||
->where('R428WH', $whs)
|
|
||||||
->get();
|
|
||||||
foreach ($rows as $row) {
|
|
||||||
$user_array[] = [
|
|
||||||
'userprf' => trim((string) $row->userprf),
|
|
||||||
'name' => trim((string) $row->name),
|
|
||||||
'empno' => $row->empno,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
array_multisort(array_column($user_array, 'name'), SORT_ASC, $user_array);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
Log::warning('M3 picking user list unavailable', ['exception' => $e->getMessage()]);
|
|
||||||
$user_array = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by date
|
if ($request->format === 'json') {
|
||||||
|
return response()->json($statistics);
|
||||||
|
}
|
||||||
|
|
||||||
|
$statistic5 = $statistics;
|
||||||
|
$totals_array = $totals;
|
||||||
|
$user_array = $userArray;
|
||||||
|
|
||||||
|
return view('m3::liberty-theme.pages.userPickingStats5', compact(
|
||||||
|
'statistic5', 'totals_array', 'user_array', 'user', 'starting', 'ending', 'request'
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function loadUsers(): array
|
||||||
|
{
|
||||||
|
$users = [];
|
||||||
|
$co = '';
|
||||||
|
$whs = '';
|
||||||
|
|
||||||
|
try {
|
||||||
|
$connection = config('m3.connection', 'pgsql');
|
||||||
|
if (! Schema::connection($connection)->hasTable('icm428m')) {
|
||||||
|
return [$users, $co, $whs];
|
||||||
|
}
|
||||||
|
|
||||||
|
$profile = DB::connection($connection)->table('icm428m')
|
||||||
|
->select('R428CO', 'R428WH')
|
||||||
|
->where('R428US', strtoupper((string) m3_user()))
|
||||||
|
->first();
|
||||||
|
$co = trim((string) ($profile->R428CO ?? ''));
|
||||||
|
$whs = trim((string) ($profile->R428WH ?? ''));
|
||||||
|
if ($co === '' || $whs === '') {
|
||||||
|
return [$users, $co, $whs];
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows = DB::connection($connection)->table('icm428m')
|
||||||
|
->selectRaw("R428US as userprf, (trim(R428FIRST) || ' ' || trim(R428FAMILY)) as name, R428EMPCDE as empno")
|
||||||
|
->where('R428EMPCDE', '!=', 0)->where('R428CO', $co)->where('R428WH', $whs)->get();
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$users[] = ['userprf' => trim((string) $row->userprf), 'name' => trim((string) $row->name), 'empno' => $row->empno];
|
||||||
|
}
|
||||||
|
array_multisort(array_column($users, 'name'), SORT_ASC, $users);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Log::warning('M3 picking user list unavailable', ['exception' => $e->getMessage()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [$users, $co, $whs];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function dateRange(Request $request): array
|
||||||
|
{
|
||||||
if (isset($request->dates)) {
|
if (isset($request->dates)) {
|
||||||
$date = explode(' - ', $request->dates);
|
$dates = explode(' - ', (string) $request->dates);
|
||||||
$start = str_replace([' ', '-'], '', $date[0]);
|
$starting = $dates[0] ?? date('Y-m-d');
|
||||||
$end = str_replace([' ', '-'], '', $date[1]);
|
$ending = $dates[1] ?? $starting;
|
||||||
$starting = $date[0];
|
|
||||||
$ending = $date[1];
|
|
||||||
} else {
|
} else {
|
||||||
$start = date('Ymd', strtotime('-5 days'));
|
|
||||||
$end = date('Ymd');
|
|
||||||
$starting = date('Y-m-d', strtotime('-5 days'));
|
$starting = date('Y-m-d', strtotime('-5 days'));
|
||||||
// $starting = "2021-11-01";
|
|
||||||
$ending = date('Y-m-d');
|
$ending = date('Y-m-d');
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculate day difference between 2 dates
|
$days = (int) (new DateTime($starting))->diff(new DateTime($ending))->format('%a');
|
||||||
$str_date = new DateTime($starting);
|
return [$starting, $ending, $days];
|
||||||
$end_date = new DateTime($ending);
|
}
|
||||||
$interval = $str_date->diff($end_date);
|
|
||||||
$days = $interval->format('%a'); // retrieve number of days %a
|
|
||||||
// echo "diff=$days<br>";
|
|
||||||
|
|
||||||
$user = $request->user;
|
private static function emptyTotals(): array
|
||||||
|
{
|
||||||
|
return ['performance' => 0, 'blendedTargetLPH' => 0, 'actualLPH' => 0, 'grossHours' => 0];
|
||||||
|
}
|
||||||
|
|
||||||
// retrieve session user and pw, also the HTTP server host address and port
|
private static function collectDailyStatistics(Request $request, string $starting, int $days, array $totals): array
|
||||||
$username = $request->header('php-auth-user');
|
{
|
||||||
$pw = $request->header('php-auth-pw');
|
$statistics = [];
|
||||||
$http_host = $request->server('HTTP_HOST');
|
$count = 0;
|
||||||
$urlStats4 = "http://$http_host/userPickingStats4";
|
$url = 'http://' . $request->server('HTTP_HOST') . '/userPickingStats4';
|
||||||
|
|
||||||
// initialize arrays
|
for ($day = 0; $day <= $days; $day++) {
|
||||||
$statistic5 = $totals_array = [];
|
$date = date('Y-m-d', strtotime($starting . "+$day days"));
|
||||||
// initialize totals
|
$statistics[$day] = self::fetchStatistic($request, $url, $date);
|
||||||
$totals_array['performance'] = 0;
|
self::addTotals($totals, $statistics[$day]);
|
||||||
$totals_array['blendedTargetLPH'] = 0;
|
if (($statistics[$day]['performance'] ?? 0) > 0) {
|
||||||
$totals_array['actualLPH'] = 0;
|
$count++;
|
||||||
$totals_array['grossHours'] = 0;
|
|
||||||
$y = 0;
|
|
||||||
|
|
||||||
// loop for the days difference and passing proper date to v4 to get json encoded response
|
|
||||||
if (isset($request->dates)) {
|
|
||||||
for ($x = 0; $x <= $days; $x++) {
|
|
||||||
|
|
||||||
$next_date = date('Y-m-d', strtotime($starting . "+$x days"));
|
|
||||||
|
|
||||||
$parameters = ['dates' => $next_date, 'user' => $request->user, 'format' => 'json'];
|
|
||||||
// echo "<pre>"; print_r($parameters); echo "$urlStats4</pre>";
|
|
||||||
|
|
||||||
$response = Http::withBasicAuth($username, $pw)->get($urlStats4, $parameters);
|
|
||||||
// echo $response;
|
|
||||||
$statusCode = $response->status();
|
|
||||||
|
|
||||||
// echo "$statusCode json return=".$response->getBody()." <br>";
|
|
||||||
|
|
||||||
$statistic5[$x] = json_decode($response->getBody(), true);
|
|
||||||
|
|
||||||
$totals_array['performance'] += isset($statistic5[$x]['performance']) ? $statistic5[$x]['performance'] : 0;
|
|
||||||
$totals_array['blendedTargetLPH'] += isset($statistic5[$x]['blendedTargetLPH']) ? $statistic5[$x]['blendedTargetLPH'] : 0;
|
|
||||||
$totals_array['actualLPH'] += isset($statistic5[$x]['actualLPH']) ? $statistic5[$x]['actualLPH'] : 0;
|
|
||||||
$totals_array['grossHours'] += isset($statistic5[$x]['grossHours']) ? $statistic5[$x]['grossHours'] : 0;
|
|
||||||
|
|
||||||
if (! isset($statistic5[$x]['day'])) {
|
|
||||||
$statistic5[$x]['day'] = $next_date;
|
|
||||||
}
|
|
||||||
if (isset($statistic5[$x]['performance'])) {
|
|
||||||
if ($statistic5[$x]['performance'] > 0) {
|
|
||||||
$y++;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$statistic5[$x]['performance'] = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($y > 0) {
|
if ($count > 0) {
|
||||||
$totals_array['performance'] /= $y;
|
foreach (array_keys($totals) as $key) {
|
||||||
$totals_array['blendedTargetLPH'] /= $y;
|
$totals[$key] /= $count;
|
||||||
$totals_array['actualLPH'] /= $y;
|
}
|
||||||
$totals_array['grossHours'] /= $y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($request->format == 'json') {
|
return [$statistics, $totals];
|
||||||
echo '<pre>';
|
}
|
||||||
print_r($statistic5);
|
|
||||||
echo '</pre>';
|
|
||||||
} else {
|
|
||||||
// dd($statistic5);
|
|
||||||
// if($statistic5[1]!=null)
|
|
||||||
return view('m3::liberty-theme.pages.userPickingStats5', compact('statistic5', 'totals_array', 'user_array', 'user', 'starting', 'ending', 'request'));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private static function fetchStatistic(Request $request, string $url, string $date): array
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$response = Http::withBasicAuth((string) $request->header('php-auth-user'), (string) $request->header('php-auth-pw'))
|
||||||
|
->get($url, ['dates' => $date, 'user' => $request->user, 'format' => 'json']);
|
||||||
|
$data = $response->successful() ? (json_decode($response->body(), true) ?: []) : [];
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Log::warning('M3 picking statistics unavailable', ['date' => $date, 'exception' => $e->getMessage()]);
|
||||||
|
$data = [];
|
||||||
|
}
|
||||||
|
$data['day'] = $data['day'] ?? $date;
|
||||||
|
$data['performance'] = $data['performance'] ?? 0;
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function addTotals(array &$totals, array $statistic): void
|
||||||
|
{
|
||||||
|
foreach (array_keys($totals) as $key) {
|
||||||
|
$totals[$key] += (float) ($statistic[$key] ?? 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user