updates to M3 module

This commit is contained in:
Mark Gregory
2026-09-16 14:28:17 +01:00
parent 3410ab85fe
commit d1a83c9b09
164 changed files with 45562 additions and 273 deletions
@@ -0,0 +1,294 @@
<?php
namespace Modules\M3\Http\Controllers\API;
use Modules\M3\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use DB;
use Config;
use Str;
use Log;
use Http;
use Cache;
use DateTime;
use Smalot\PdfParser\Parser;
use File;
use Helper;
class ReplenAPI extends Controller
{
# M3Replenishement with URL Params
public function M3InventoryReportReplen(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->all());
// Log::info( $request->all() );
# Get The Calling User
$SHUSER = m3_user() !== '' ? Str::upper(m3_user()) : 'WebUser';
$MACPKR = isset($MACPKR) ? $MACPKR : '000000';
$SHSOURCE = 'M3InventoryReportReplen';
$M3Errors = '';
try {
# Get Json Contents
$urlParams = count($request->all())>0 ? $request->all() : '';
$CO = isset($urlParams["CO"]) ? $urlParams["CO"] : '';
$WHS = isset($urlParams["WHS"]) ? $urlParams["WHS"] : '';
$PARTNO = isset($urlParams["PARTNO"]) ? $urlParams["PARTNO"] : '';
$PKLN = isset($urlParams["PKLN"]) ? $urlParams["PKLN"] : '';
$REQPKWS = isset($urlParams["REQPKWS"]) ? $urlParams["REQPKWS"] : '';
$REQKPKUS = isset($urlParams["REQKPKUS"]) ? $urlParams["REQKPKUS"] : 'WebUser';
if($urlParams!=''){
$IncomingParams = ['CO'=> $CO,'WHS'=> $WHS,'PARTNO'=> $PARTNO,'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);
$PART = explode( ',',$PARTNO);
foreach( $PART as $key => $value){
$PARTS[] = ["PART"=> $value,"LOT"=>""];
}
// dump($PARTS);
$JSON = json_encode( ["WHS" => $WHS,"PARTS"=> $PARTS ] );
# Write to Logs : Params Sent To M3
$data = ['SHMACPKR'=>$MACPKR,'SHDT'=> date('Y-m-d'),'SHTIME'=> date('H:i:s') ,'SHERROR'=> '','SHINTBOX'=> '','SHTYPE'=> 'M3-Req-out' ,
'SHDATA'=>'Sent To M3-'.$JSON ,'SHUSER'=> $SHUSER,'SHSOURCE'=> $SHSOURCE,'SHCO'=>$CO,'SHWHS'=>$WHS ];
Helper::logToICM699M($data);
# Get Token
$tokenResponse = Helper::checkReceiveTokenV2();
$tokenData = $tokenResponse->getData();
// dd($IonToken->code);
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'))
->retry(3, 100)
->post($endPointURL);
$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);
# Array
$responseArray = json_decode($JsonSanytized,true);
// LOG::info( $responseArray);
$responseTMP = $responseArray['result'];
// LOG::info($responseTMP);
}
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 $value['Location']
$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['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['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['LLSPR10A01'] = $value['Allocation']; # Allocation
$SQLInsertPrep630M['LLSPR15A01'] = $value['AllocatedTo']; # AllocatedTo
$DescSanytized = Helper::sanityzeString($value['Description']);
// $DescSanytized = str_replace(['–','\u20131'], '-',$value['Description']);
$SQLInsertPrep630M['LLCMDESC'] = $DescSanytized; // Description
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 URL Parameters Sent','SHUSER'=> $SHUSER,'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'=> 'Curl Error: Communication with M3 Timed Out'.' 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'=> $SHUSER,'SHSOURCE'=> $SHSOURCE,'SHCO'=>$CO,'SHWHS'=>$WHS];
Helper::logToICM699M($data);
// Log::error($th);
return response()->json([
'code'=>'405',
'status'=>'error',
'message'=>'Catched Error: '.$th
]);
}
}
}
# End M3InventoryReportReplen
}