env('ENDPOINTURLSSO') . env('URLTOKEN'), CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => 'grant_type=' . env('GRANT_TYPE') . '&password=' . env('PASSWORD') . '&username=' . env('USERNAME') . '&client_secret=' . env('CLIENT_SECRET') . '&client_id=' . env('CLIENT_ID'), CURLOPT_HTTPHEADER => [ 'Content-Type: application/x-www-form-urlencoded', 'charset:UTF-8', 'Accept: application/json', ], ]); $response = curl_exec($curl); // dd($response); $responseJ = json_decode($response); if ($response === false) { return response()->json([ 'code' => '400', 'status' => 'Failed', 'message' => 'Token Verification Error...' . curl_error($curl), ]); } elseif (isset($responseJ->access_token)) { $IonToken = $responseJ->access_token; // Log::info("New Token: " . $IonToken); Cache::put('token', $IonToken, now()->addMinutes(59)); } else { // dd($response); return response()->json([ 'code' => '400', 'status' => 'Failed', 'message' => 'Token Verification Error...M3 response:' . $response, ]); } curl_close($curl); } return $IonToken; // return response()->json([ // 'code' => '200', // 'token' => $IonToken // ]); } // Check M3 API Token V2 public static function checkReceiveTokenV2() { ini_set('max_execution_time', Config::get('arrays.PHPTimeOut')); ini_set('default_socket_timeout', Config::get('arrays.PHPTimeOut')); // Clear Cache // Cache::forget('token'); // Check if cached Token is still Valid $token = Cache::get('token'); if (isset($token)) { $IonToken = $token; // Log::info("Cached Token: ".$IonToken); } else { // ## CURL // ## Generate Token $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => env('ENDPOINTURLSSO') . env('URLTOKEN'), CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => 'grant_type=' . env('GRANT_TYPE') . '&password=' . env('PASSWORD') . '&username=' . env('USERNAME') . '&client_secret=' . env('CLIENT_SECRET') . '&client_id=' . env('CLIENT_ID'), CURLOPT_HTTPHEADER => [ 'Content-Type: application/x-www-form-urlencoded', 'charset:UTF-8', 'Accept: application/json', ], ]); $response = curl_exec($curl); // dd($response); $responseJ = json_decode($response); curl_close($curl); if ($response === false) { // Clear Cache Cache::forget('token'); // Return Error return response()->json([ 'code' => '400', 'status' => 'Failed', 'message' => 'Token Verification Error...' . curl_error($curl), ]); } elseif (isset($responseJ->access_token)) { $IonToken = $responseJ->access_token; // Log::info("New Token: " . $IonToken); Cache::put('token', $IonToken, now()->addMinutes(59)); } else { // Clear Cache Cache::forget('token'); return response()->json([ 'code' => '400', 'status' => 'Failed', 'message' => 'Token Verification Error...M3 response:' . $response, ]); } } // return $IonToken; return response()->json([ 'code' => '200', 'token' => $IonToken, ]); } // Read M3 JSON and Analyse it for Errors public static function checkM3Errors($responseResult) { // dd($responseResult); // M3 Response Types $M3TypesResponse = ['CloseShipment', 'Move', 'COCharge', 'Replenish', 'MoveDock']; // CloseShipment Types And Contents $M3CloseShipmentResp = ['AddPackage', 'PickingListClose', 'TrailerNo', 'TrackingNo', 'ContentLabel', 'DeliveryNote', 'ShipLabel']; // 'ContentLabel', 'DeliveryNote','ShipLabel' $ErrorMessages = ''; $typesErrors = 0; foreach ($M3TypesResponse as $key => $Type) { // dump($responseResult->$Type); if ($Type == 'CloseShipment') { // dump($Type); $CloseShipmentErr = 0; $M3RespClose = $responseResult->$Type; foreach ($M3CloseShipmentResp as $key => $closeType) { $ErrorMessage = $M3RespClose->$closeType->ErrorMessage; $ErrorMessage = $ErrorMessage == '' ? '' : $ErrorMessage; $Result = $M3RespClose->$closeType->Result; $ResultT = $M3RespClose->$closeType->Result; $Result = $Result == 'N/A' || $Result == 'OK' ? '' : $Result; if ($ErrorMessage != '' || $Result != '') { $ErrorMessages .= $closeType . '-' . $Result . '-' . $ErrorMessage . ' '; } // dump($closeType);// dump($ErrorMessage);// dump($Result); if ($ResultT == 'N/A') { $CloseShipmentErr++; } } } elseif ($Type == 'MoveDock') { $MoveDockRes = isset($responseResult->$Type->Move) ? $responseResult->$Type->Move : ''; $ErrorMessage = isset($MoveDockRes->ErrorMessage) ? $MoveDockRes->ErrorMessage : ''; $ErrorMessage = $ErrorMessage == '' ? '' : $ErrorMessage; $Result = isset($MoveDockRes->Result) ? $MoveDockRes->Result : ''; $ResultT = $Result; $Result = $Result == 'N/A' || $Result == 'OK' ? '' : $Result; if ($ErrorMessage != '' || $Result != '') { $ErrorMessages .= $Type . '-' . $Result . '-' . $ErrorMessage . ' '; } // dump($Type);// dump($ErrorMessage);// dump($Result);// dump($responseResult->$Type); if ($ResultT == 'N/A') { $typesErrors++; } } else { $ErrorMessage = isset($responseResult->$Type->ErrorMessage) ? $responseResult->$Type->ErrorMessage : ''; $ErrorMessage = $ErrorMessage == '' ? '' : $ErrorMessage; $Result = isset($responseResult->$Type->Result) ? $responseResult->$Type->Result : ''; $ResultT = $Result; $Result = $Result == 'N/A' || $Result == 'OK' ? '' : $Result; if ($ErrorMessage != '' || $Result != '') { $ErrorMessages .= $Type . '-' . $Result . '-' . $ErrorMessage . ' '; } // dump($Type); // dump($ErrorMessage); // dump($Result); // dump($responseResult->$Type); if ($ResultT == 'N/A') { $typesErrors++; } } } // dd($ErrorMessages); if ($CloseShipmentErr == count($M3CloseShipmentResp) && $typesErrors == count($M3TypesResponse) - 1) { $ErrorMessages .= '-M3 did not call any method or type '; } return $ErrorMessages; } // Log All Success And Error to ICM699M public static function logToICM699M($data) { ini_set('max_execution_time', Config::get('arrays.PHPTimeOut')); $insertSQL = DB::connection(config('m3.connection', 'pgsql'))->table('ICM699M')->insert($data); // dump($data); // dump($insertSQL); } // Get Substring in string between 2 Substrings public static function getSubstringBetween($string, $start, $end) { $startPos = strpos($string, $start); if ($startPos === false) { return ''; } $endPos = strpos($string, $end, $startPos + strlen($start)); if ($endPos === false) { return ''; } $substring = substr($string, $startPos + strlen($start), $endPos - ($startPos + strlen($start))); return $substring; } public static function checkLength($JSONL, $DBL, $TYPE) { // Check type $JSONType = gettype($JSONL); switch ($JSONType) { case 'string': $JSONTypeConv = 'string'; break; case 'double': $JSONTypeConv = 'number'; break; case 'integer': $JSONTypeConv = 'number'; break; default: $JSONTypeConv = 'null'; break; } $TYPE = $TYPE == 'integer' || $TYPE == 'DEC' ? 'number' : $TYPE; // Log::info( $JSONL .' L: '. strlen($JSONL) .' DBL: '. $DBL .' Type:'.$JSONType.':'.$TYPE ); // Check Json Field should be < DB Field $LengthOK[413] = (strlen($JSONL) <= $DBL) ? true : false; // Empty Fields $LengthOK[411] = true; // !empty($JSONL) ? true : false ; // Wrong Types $LengthOK[417] = true; // $JSONTypeConv == $TYPE ? true : false ; // Log::info($LengthOK); return $LengthOK; } public static function sanityzeString($StrTosanityze) { $badChars = Config::get('arrays.BadChars'); $goodString = trim($StrTosanityze); $goodString = str_replace("'", '', $goodString); $goodString = str_replace("\u{f0e0}", '', $goodString); $goodString = preg_replace('/\\\\u201[0-9A-F]+/i', '', $goodString); $goodString = str_replace($badChars, '', $goodString); $goodString = iconv('UTF-8', 'ISO-8859-1//IGNORE', $goodString); $goodString = iconv('ISO-8859-1', 'UTF-8', $goodString); // MAK : New Automatic Detection for encoding and conversion // Detect the encoding // $encoding = mb_detect_encoding($goodString, mb_list_encodings(), true); // Log::info($goodString); // If encoding is detected, convert the string // if ($encoding !== false) { // // Log::info($encoding); // // Log::info($goodString); // $goodString = mb_convert_encoding($goodString, "UTF-8", $encoding); // } else { // $goodString = ""; // } // dump($goodString); return $goodString; } // Remove Duplicate SubString public static function removeDuplicateSubstrings($str) { $result = ''; $seenPhrases = []; $phrases = explode(' ', $str); foreach ($phrases as $phrase) { if (! in_array($phrase, $seenPhrases)) { $result .= $phrase . ' '; $seenPhrases[] = $phrase; } } return trim($result); } public static function getUserCOWHSPLT() { $defaults = [ 'UserCO' => '', 'UserWHS' => '', 'UserPL' => '', ]; $loggedUser = strtoupper(trim((string) m3_user())); if ($loggedUser === '') { return $defaults; } try { if (! Schema::connection(config('m3.connection', 'pgsql'))->hasTable('icm428m')) { return $defaults; } $user = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m') ->select('R428US', 'R428ADMUSR', 'R428PWRUSR', 'R428CO', 'R428PL', 'R428WH', 'R42811A', 'R42821A') ->where('R428US', $loggedUser) ->first(); if (! $user) { return $defaults; } return [ 'UserCO' => trim((string) ($user->R428CO ?? '')), 'UserWHS' => trim((string) ($user->R428WH ?? '')), 'UserPL' => trim((string) ($user->R428PL ?? '')), ]; } catch (\Throwable $e) { \Illuminate\Support\Facades\Log::warning('M3 user context unavailable', ['exception' => $e->getMessage()]); return $defaults; } } }