Files
WMS/Modules/M3/Http/Controllers/API/M3InventoryReport.php
T
2026-09-16 17:16:25 +01:00

440 lines
22 KiB
PHP

<?php
namespace Modules\M3\Http\Controllers\API;
use Config;
use DB;
use Http;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Json;
use Log;
use Modules\M3\Helpers\Helper;
use Modules\M3\Http\Controllers\Controller;
use Str;
class M3InventoryReport extends Controller
{
// M3InventoryReport for Single Part
public function M3InventoryReport(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'));
// dd($request->json()->all());
// Get the Calling User
$SHUSER = m3_user() !== '' ? Str::upper(m3_user()) : 'WEBUSER';
// Set SHSOURCE
$SHSOURCE = 'M3InventoryReport';
$M3Errors = '';
$DELIVERYNO = isset($DELIVERYNO) ? $DELIVERYNO : '000000';
try {
// Get Json Contents
$jsonFile = count($request->json()->all()) > 0 ? $request->json()->all() : '';
if ($jsonFile != '') {
$WHS = isset($jsonFile['WHS']) ? $jsonFile['WHS'] : '';
$PART = isset($jsonFile['PART']) ? $jsonFile['PART'] : '';
$LOT = isset($jsonFile['LOT']) ? $jsonFile['LOT'] : '';
} else {
// code...
}
// 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' => '000000', 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => '', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out',
'SHDATA' => $SHDATA, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE, 'SHCO' => '', 'SHWHS' => $WHS];
Helper::logToICM699M($data);
return $tokenResponse;
}
// Call ionapi with Params
$endPointURL = env('ENDPOINTURL') . 'APIFLOWS/Sentinel/M3InventoryReport?ionapiRespStyle=sync';
$token = $IonToken;
$JSON = [
'WHS' => $WHS,
'PART' => $PART,
'LOT' => $LOT,
];
$response = Http::withHeaders([
'accept' => 'application/json',
'Content-Type' => 'application/json',
])
->withToken($token)
->timeout(Config::get('arrays.curlTimeOut'))
->post($endPointURL, $JSON);
$responseStatusCode = $response->getStatusCode();
$M3SessionId = $response->hasHeader('x-transaction-id') ? '-sessionID:' . $response->header('x-transaction-id') : '-sessionID';
if ($responseStatusCode == '200') {
// Check M3 Response for Errors
$M3Errors = '';
$responseJSON = $response->getBody()->getContents();
$JsonSanytized = Helper::sanityzeString($responseJSON);
} else {
$M3Errors = 'Error: No Response from M3';
}
if ($M3Errors != '') {
// Write to Logs : Error
$data = ['SHMACPKR' => $DELIVERYNO, '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' => '', 'SHWHS' => $WHS];
Helper::logToICM699M($data);
// Send Response
return response()->json([
'code' => '405',
'status' => 'error',
'message' => $M3Errors,
]);
} else {
// Write to Logs Out : Success
$data = ['SHMACPKR' => $DELIVERYNO, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => '', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out',
'SHDATA' => $responseStatusCode . $JsonSanytized . '-' . json_encode($JSON) . $M3SessionId, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE,
'SHCO' => '', 'SHWHS' => $WHS];
Helper::logToICM699M($data);
return response()->json([
'code' => $responseStatusCode,
'status' => 'Success',
'message' => json_decode($JsonSanytized),
]);
}
} catch (\Throwable $th) {
// Log::error($th);
$getCurlErrors = str_contains($th->getMessage(), 'cURL error');
if ($getCurlErrors) {
// Write to Logs Out : Failed
$data = ['SHMACPKR' => $DELIVERYNO, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out',
'SHDATA' => 'Curl Error: Communication with M3 Timed Out' . ' M3-JSN-out' . $JSON, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE, 'SHCO' => '', 'SHWHS' => $WHS];
Helper::logToICM699M($data);
return response()->json([
'code' => '405',
'status' => 'error',
'message' => 'Curl Error: Communication with M3 Timed Out.',
]);
} else {
// Write to Logs Out : Error
$data = ['SHMACPKR' => $DELIVERYNO, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out',
'SHDATA' => 'An Error Occured - ' . substr($th, 0, 300), 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE];
Helper::logToICM699M($data);
return response()->json([
'code' => '405',
'status' => 'error',
'message' => 'Catched Error: ' . $th,
]);
}
}
}
// 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'] : '000000'; // 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' => 'application/json',
'Content-Type' => 'application/json',
])
->withToken($token)
->withBody($JSON, 'application/json')
->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 = 'Error: No Response from M3';
}
// 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' => 'Error: No Response from M3',
]);
} 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 : '000000';
$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 : '000000';
$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 : '000000';
$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
}