json()->all(); $warehouse = trim((string) ($input['WHS'] ?? '')); $payload = [ 'WHS' => $warehouse, 'PART' => trim((string) ($input['PART'] ?? '')), 'LOT' => trim((string) ($input['LOT'] ?? '')), ]; $tokenResponse = Helper::checkReceiveTokenV2(); $tokenData = $tokenResponse->getData(); if ((string) ($tokenData->code ?? '') !== '200') { self::logInventoryResponse( self::ZERO_PAD, (string) ($tokenData->message ?? ''), $user, $source, $warehouse ); return $tokenResponse; } return self::sendInventoryRequest( (string) ($tokenData->token ?? ''), $payload, $user, $source, $warehouse ); } catch (\Throwable $exception) { return self::inventoryExceptionResponse($exception, $payload, $user, $source, $warehouse); } } private static function sendInventoryRequest( string $token, array $payload, string $user, string $source, string $warehouse ) { if ($token === '') { return response()->json([ 'code' => '401', 'status' => 'error', 'message' => 'M3 authentication token was not returned.', ], 401); } $response = Http::withHeaders([ 'accept' => self::JSON_CONTENT_TYPE, 'Content-Type' => self::JSON_CONTENT_TYPE, ]) ->withToken($token) ->timeout(Config::get('arrays.curlTimeOut')) ->post(env('ENDPOINTURL') . 'APIFLOWS/Sentinel/M3InventoryReport?ionapiRespStyle=sync', $payload); $statusCode = $response->getStatusCode(); $sessionId = $response->hasHeader('x-transaction-id') ? '-sessionID:' . $response->header('x-transaction-id') : '-sessionID'; if ($statusCode !== Response::HTTP_OK) { self::logInventoryResponse( self::ZERO_PAD, $statusCode . self::NO_M3_RESPONSE . $sessionId, $user, $source, $warehouse, true ); return response()->json([ 'code' => '405', 'status' => 'error', 'message' => self::NO_M3_RESPONSE, ], 502); } $sanitized = Helper::sanityzeString($response->body()); self::logInventoryResponse( self::ZERO_PAD, $statusCode . $sanitized . '-' . json_encode($payload) . $sessionId, $user, $source, $warehouse ); return response()->json([ 'code' => $statusCode, 'status' => 'Success', 'message' => json_decode($sanitized), ]); } private static function inventoryExceptionResponse( \Throwable $exception, array $payload, string $user, string $source, string $warehouse ) { $isCurlError = str_contains($exception->getMessage(), 'cURL error'); $message = $isCurlError ? 'Curl Error: Communication with M3 Timed Out.' : 'M3 inventory request failed.'; self::logInventoryResponse( self::ZERO_PAD, $isCurlError ? $message : mb_substr($exception->getMessage(), 0, 300), $user, $source, $warehouse, true ); Log::error('M3 inventory request failed.', [ 'exception' => $exception, 'payload' => $payload, ]); return response()->json([ 'code' => '405', 'status' => 'error', 'message' => $message, ], 502); } private static function logInventoryResponse( string $deliveryNumber, string $message, string $user, string $source, string $warehouse, bool $isError = false ): void { Helper::logToICM699M([ 'SHMACPKR' => $deliveryNumber, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => $isError ? 'X' : '', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out', 'SHDATA' => $message, 'SHUSER' => $user, 'SHSOURCE' => $source, 'SHCO' => '', 'SHWHS' => $warehouse, ]); } // End M3InventoryReport // M3InventoryReportList with URL Params public function M3InventoryReportListURL(Request $request) { 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')); // Set API Name $SHSOURCE = 'M3InventoryReportListURL'; // Get The Calling User $SHUSER = m3_user() !== '' ? Str::upper(m3_user()) : 'WebUser'; $M3Errors = ''; try { // Get Json Contents $urlParams = count($request->all()) > 0 ? $request->all() : ''; $REQPKWS = isset($urlParams['REQPKWS']) ? $urlParams['REQPKWS'] : ''; $REQKPKUS = isset($urlParams['REQKPKUS']) ? $urlParams['REQKPKUS'] : 'WebUser'; $CO = isset($urlParams['CO']) ? $urlParams['CO'] : ''; $WHS = isset($urlParams['WHS']) ? $urlParams['WHS'] : ''; $MACPKR = isset($urlParams['MACPKR']) ? $urlParams['MACPKR'] : self::ZERO_PAD; // Delivery Number $PKLN = isset($urlParams['PKLN']) ? $urlParams['PKLN'] : ''; if ($urlParams != '') { $IncomingParams = ['CO' => $CO, 'WHS' => $WHS, 'MACPKR' => $MACPKR, 'PKLN' => $PKLN, 'REQPKWS' => $REQPKWS, 'REQKPKUS' => $REQKPKUS]; $IncomingParams = json_encode($IncomingParams); // Write to Logs : Params In $data = ['SHMACPKR' => $MACPKR, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => '', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Req-out', 'SHDATA' => $IncomingParams, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE, 'SHCO' => $CO, 'SHWHS' => $WHS]; Helper::logToICM699M($data); // Get Parts And Lots from ICM688M2 $ICM688M2SLTMP = DB::connection(config('m3.connection', 'pgsql'))->table('icm688m2') ->select('A5WHS', 'A5PART', 'A5LOTNUM', 'A5SENTPKR') ->distinct() ->where('A5CO', $CO) ->where('A5WHS', $WHS) ->where('A5MACPKR', $MACPKR); // Get Pack Lines if ($PKLN != '') { $ICM688M2SLTMP->where('A5PKLN', $PKLN); } $ICM688M2SL = $ICM688M2SLTMP->get(); // dd($ICM688M2SL); if (count($ICM688M2SL) > 0) { // Update ICM688M1 : Add Timestamp $ICM688M1TS = now(); DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1') ->where('A4MACPKR', $MACPKR) ->update(['A4INRFSHTS' => $ICM688M1TS]); $PICKERARR = []; foreach ($ICM688M2SL as $key => $ICM688M2) { $PART = trim($ICM688M2->A5PART); $LOT = trim($ICM688M2->A5LOTNUM); $PARTS[] = ['PART' => $PART, 'LOT' => $LOT]; $PICKERARR[$PART] = $ICM688M2->A5SENTPKR; } // Log::info($PARTS); // dump($PARTS); $JSON = json_encode(['WHS' => $WHS, 'PARTS' => $PARTS]); // Log::info($JSON); // Get Token $tokenResponse = Helper::checkReceiveTokenV2(); $tokenData = $tokenResponse->getData(); if ($tokenData->code == '200') { $IonToken = isset($tokenData->token) ? $tokenData->token : ''; } else { $SHDATA = isset($tokenData->message) ? $tokenData->message : ''; // Write to Logs : Error $data = ['SHMACPKR' => $MACPKR, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out', 'SHDATA' => $SHDATA, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE, 'SHCO' => $CO, 'SHWHS' => $WHS]; Helper::logToICM699M($data); return $tokenResponse; } // Call ionapi with Params $endPointURL = env('ENDPOINTURL') . 'APIFLOWS/Sentinel/M3InventoryReportList?ionapiRespStyle=sync'; $token = $IonToken; $response = Http::withHeaders([ 'accept' => self::JSON_CONTENT_TYPE, 'Content-Type' => self::JSON_CONTENT_TYPE, ]) ->withToken($token) ->withBody($JSON, self::JSON_CONTENT_TYPE) ->timeout(Config::get('arrays.curlTimeOut')) ->post($endPointURL); $M3SessionId = $response->hasHeader('x-transaction-id') ? '-sessionID:' . $response->header('x-transaction-id') : '-sessionID'; $responseStatusCode = $response->getStatusCode(); if ($responseStatusCode == '200') { // Check M3 Response for Errors $M3Errors = ''; $responseJSON = $response->getBody()->getContents(); $JsonSanytized = Helper::sanityzeString($responseJSON); // Array $responseArray = json_decode($JsonSanytized, true); $responseTMP = $responseArray['result']; } else { $M3Errors = self::NO_M3_RESPONSE; } // Validate response From M3 if ($M3Errors != '') { // Write to Logs : Error $data = ['SHMACPKR' => $MACPKR, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out', 'SHDATA' => $responseStatusCode . '-' . $M3Errors . $M3SessionId, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE, 'SHCO' => $CO, 'SHWHS' => $WHS]; Helper::logToICM699M($data); // Send Response return response()->json([ 'code' => '405', 'status' => 'error', 'message' => self::NO_M3_RESPONSE, ]); } else { // Write to Logs : JSON $data = ['SHMACPKR' => $MACPKR, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => '', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out', 'SHDATA' => $responseStatusCode . '-' . $JsonSanytized . $M3SessionId, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE, 'SHCO' => $CO, 'SHWHS' => $WHS]; Helper::logToICM699M($data); } $dataToWrite2 = []; foreach ($responseTMP as $key => $value) { // dump($value); $dataToWrite = []; foreach ($value as $key2 => $content) { if ($key2 != 'Locations') { $dataToWrite[$key2] = $content; } else { foreach ($content as $key3 => $content2) { // dump($content2); $dataToWrite2[] = array_merge($dataToWrite, $content2); } } } } // LOG::info($dataToWrite2); // Delete Old Records from IC630M foreach ($dataToWrite2 as $key => $value) { $ICM630MDeleteSQL = DB::connection(config('m3.connection', 'pgsql'))->table('ic630m') ->where('LLCO', $CO) ->where('LLWHNO', $WHS) ->where('LLPPN', trim($value['PartNumber'])) ->delete(); } foreach ($dataToWrite2 as $key => $value) { // Check if Location exist in DE640M ("LVCO", "WSELOC", "VLOCID") $DE640MSQLSEL = DB::connection(config('m3.connection', 'pgsql'))->table('de640m') ->where('LVCO', $CO) ->where('WSELOC', $value['Warehouse']) ->where('VLOCID', $value['Location']) ->first(); // dd($DE640MSQLSEL); $LocationExist = isset($DE640MSQLSEL->VLOCID) ? trim($DE640MSQLSEL->VLOCID) : ''; $WHExist = isset($DE640MSQLSEL->WSELOC) ? trim($DE640MSQLSEL->WSELOC) : ''; $COExist = isset($DE640MSQLSEL->LVCO) ? trim($DE640MSQLSEL->LVCO) : ''; // dd($LocationExist .': '.$WHExist.': '.$COExist); // if No records Founds insert New Record if ($LocationExist == '' && $WHExist == '' && $COExist == '') { $SQLInsertPrep640M['LVCO'] = $CO; // Company $SQLInsertPrep640M['WSELOC'] = $value['Warehouse']; // $SQLInsertPrep640M['LVDELNO'] = $MACPKR; # M3 Delivery note number DELETED // $SQLInsertPrep640M['LVUSD3'] = $value['']; # Pallet Size : Not Found in M3 JSON $SQLInsertPrep640M['VLOCID'] = $value['Location']; // Stock location $SQLInsertPrep640M['LVSEQN'] = $value['LocationSeq']; $SQLInsertPrep640M['LVUSD2'] = $value['LocationType']; $SQLInsertPrep640M['LVZONE'] = $value['Zone']; // Warehouse zone $SQLInsertPrep640M['LVMAINTTS'] = now(); // Last maintained time stamp DB::connection(config('m3.connection', 'pgsql'))->table('de640m')->insert($SQLInsertPrep640M); } // Update else { $SQLUpdatePrep640M['LVSEQN'] = $value['LocationSeq']; $SQLUpdatePrep640M['LVUSD2'] = $value['LocationType']; $SQLUpdatePrep640M['LVZONE'] = $value['Zone']; $SQLUpdatePrep640M['LVMAINTTS'] = now(); DB::connection(config('m3.connection', 'pgsql'))->table('de640m') ->where('LVCO', $CO) ->where('WSELOC', $value['Warehouse']) ->where('VLOCID', $value['Location']) ->update($SQLUpdatePrep640M); } // Insert New Records to IC630M $SQLInsertPrep630M = []; $SQLInsertPrep630M['LLCO'] = $CO; // Company $SQLInsertPrep630M['LLWHNO'] = $value['Warehouse']; // Warehouse // $SQLInsertPrep630M['LLDELNO'] = $MACPKR; # M3 Delivery note number DELETED $SQLInsertPrep630M['LLPPN'] = $value['PartNumber']; // Part Number $SQLInsertPrep630M['LLBALANCE'] = $value['Quantity']; // Quantity $SQLInsertPrep630M['LLSTLC'] = $value['Location']; // Stock location $SQLInsertPrep630M['LLLTNO'] = $value['LotNumber']; // Lot Number $SQLInsertPrep630M['LLMAINTTS'] = now(); // Request time timestamp $SQLInsertPrep630M['LLIZLCF'] = trim($value['LotNumber']) != '' ? '1' : ''; // Lot control flag $SQLInsertPrep630M['LLCMDESC'] = $value['Description']; // Description $SQLInsertPrep630M['LLSPR10A01'] = isset($value['Allocation']) ? $value['Allocation'] : ''; // Allocation $SQLInsertPrep630M['LLSPR15A01'] = isset($value['AllocatedTo']) ? $value['AllocatedTo'] : ''; // AllocatedTo DB::connection(config('m3.connection', 'pgsql'))->table('ic630m')->insert($SQLInsertPrep630M); } // dump($SQLInsertPrep); // Send Response return response()->json([ 'code' => '200', 'status' => 'Success', ]); } else { // Write to Logs Out : Error $MACPKR = isset($MACPKR) ? $MACPKR : self::ZERO_PAD; $data = ['SHMACPKR' => $MACPKR, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out', 'SHDATA' => 'No Records Found in ICM688M2', 'SHUSER' => $REQKPKUS, 'SHSOURCE' => $SHSOURCE, 'SHCO' => $CO, 'SHWHS' => $WHS]; Helper::logToICM699M($data); // Return Error return response()->json([ 'code' => '405', 'status' => 'Error', 'message' => 'No Records Found in ICM688M2', ]); } } else { // Write to Logs Out : Error $MACPKR = isset($MACPKR) ? $MACPKR : self::ZERO_PAD; $data = ['SHMACPKR' => $MACPKR, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out', 'SHDATA' => 'No URL Parameters Sent', 'SHUSER' => $REQKPKUS, 'SHSOURCE' => $SHSOURCE, 'SHCO' => $CO, 'SHWHS' => $WHS]; Helper::logToICM699M($data); // Return Error return response()->json([ 'code' => '405', 'status' => 'Error', 'message' => 'No URL Parameters Sent', ]); } } catch (\Throwable $th) { // Write to Logs Out : Error $MACPKR = isset($MACPKR) ? $MACPKR : self::ZERO_PAD; $getCurlErrors = str_contains($th->getMessage(), 'cURL error'); if ($getCurlErrors) { // dd($getCurlErrors); // Write to Logs Out : Failed $data = ['SHMACPKR' => $MACPKR, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out', 'SHDATA' => $th->getMessage() . ' M3-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 M3 Timed Out.', ]); } else { // Set SHERROR To Z if Duplicate Key Detected $th // Read The substring from SHDATA after M3-JSN-out $SHERROR = 'X'; $word = 'Duplicate key value specified'; $position = strpos($th, $word); if ($position !== false) { $SHERROR = 'Z'; } $data = ['SHMACPKR' => $MACPKR, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => $SHERROR, 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out', 'SHDATA' => 'An Error Occured-' . substr($th, 0, 300), 'SHUSER' => $REQKPKUS, 'SHSOURCE' => $SHSOURCE, 'SHCO' => $CO, 'SHWHS' => $WHS]; Helper::logToICM699M($data); Log::error($th); return response()->json([ 'code' => '405', 'status' => 'error', 'message' => 'Catched Error: ' . $th, ]); } } } // End M3InventoryReportListURL }