Files
WMS/Modules/M3/Http/Controllers/API/CloseShipment.php
T
2026-09-21 14:27:26 +01:00

1051 lines
56 KiB
PHP
Executable File

<?php
namespace Modules\M3\Http\Controllers\API;
use Config;
use DB;
use File;
use Http;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Log;
use Modules\M3\Helpers\Helper;
use Modules\M3\Http\Controllers\Controller;
use Modules\M3\Http\Controllers\GetContentsController;
use Str;
class CloseShipment extends Controller
{
private const ZERO_PAD = '000000';
private const JSON_CONTENT_TYPE = 'application/json';
private const NULL_M3_RESPONSE = 'M3 API responded with null value';
// CloseShipmentCoChargeAPI : with URL PARAMS COCharge
public static function CloseShipmentCoChargeAPI(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'));
// dump($request->all());
$nODEBUG = true; // ALWAYS set TRUE in Production
$pACKAGES = [];
$jSONCoCharge = [];
$jSONCS = [];
// Get the Calling User
$sHUSER = m3_user() !== '' ? Str::upper(m3_user()) : 'WebUser';
$sHSOURCE = 'CloseShipmentCoChargeAPI';
try {
// Get URL PARAMS
$urlParams = count($request->all()) > 0 ? $request->all() : '';
$wHS = isset($urlParams['WHS']) ? $urlParams['WHS'] : '';
$cOMPANY = isset($urlParams['COMPANY']) ? $urlParams['COMPANY'] : '';
$mANIFEST = isset($urlParams['MANIFEST']) ? $urlParams['MANIFEST'] : '';
$tRANSTYPE = isset($urlParams['TRANSTYPE']) ? $urlParams['TRANSTYPE'] : 'M'; // M , P , Z
$tRANSTYPE = $tRANSTYPE != '' ? $tRANSTYPE : 'M'; // M , P , Z
$sHDT = isset($urlParams['SHDT']) ? $urlParams['SHDT'] : '';
$sHTIME = isset($urlParams['SHTIME']) ? $urlParams['SHTIME'] : '';
$sHTYPE = isset($urlParams['SHTYPE']) ? $urlParams['SHTYPE'] : '';
$allErrMsgs = '';
$lineAck = [];
// PDF File PATH
$pdfFilePath = Config::get('arrays.PdfPath');
$dELIVERYNO = $mANIFEST != '' ? $mANIFEST : self::ZERO_PAD;
if ($urlParams != '') {
// Write to Logs : JSON
$jSONIN = json_encode([
'WHS' => $wHS,
'COMPANY' => $cOMPANY,
'MANIFEST-MACPKR' => $mANIFEST,
'TRANSTYPE' => $tRANSTYPE,
]);
$data = [
'SHMACPKR' => $dELIVERYNO,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => '',
'SHINTBOX' => '',
'SHTYPE' => 'M3-Req-out',
'SHDATA' => $jSONIN,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
// 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 : '';
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
// 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' => $sHDATA,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
return $tokenResponse;
}
// Set Endpoint URL
$endPointURL = env('ENDPOINTURL') . 'APIFLOWS/Sentinel/TransferAPI?ionapiRespStyle=sync';
$token = $ionToken;
// Read from ICM695M1
if ($tRANSTYPE == 'M') {
$iCM695M1PREP = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1')
->select('AYCO', 'AYWHS', 'AYMACPKR', 'AYRLNO', 'AYAPRF1551')
->distinct()
->where('AYCO', $cOMPANY)
->where('AYAPRF1551', $mANIFEST)
->where('AYWHS', $wHS)
->orderBy('AYMACPKR');
} elseif ($tRANSTYPE == 'P') {
$iCM695M1PREP = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1')
->select('AYCO', 'AYWHS', 'AYMACPKR', 'AYRLNO', 'AYAPRF1551')
->distinct()
->where('AYCO', $cOMPANY)
->where('AYMACPKR', $mANIFEST)
->where('AYWHS', $wHS)
->orderBy('AYMACPKR');
// Check Date & Time Before Updating ICM699M
if ($sHDT != '' && $sHTIME != '' && $sHTYPE != '' && $nODEBUG) {
DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
->where('SHMACPKR', $mANIFEST)
->where('SHDT', $sHDT)
->where('SHTIME', $sHTIME)
->where('SHCO', $cOMPANY)
->where('SHWHS', $wHS)
->where('SHSOURCE', $sHSOURCE)
->where('SHERROR', 'X')
->where('SHTYPE', $sHTYPE)
->update(['SHERROR' => 'Z', 'SHACKUSER' => $sHUSER, 'SHACKDATE' => date('Y-m-d'), 'SHACKTIME' => date('H:i:s')]);
}
// Get Acknowledged Record for M3-QtyChg PackSyst4tray
$iCM699MQtyChg = DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
->select()
->where('SHMACPKR', $mANIFEST)
->where('SHCO', $cOMPANY)
->where('SHWHS', $wHS)
->where('SHSOURCE', 'PackSyst4tray')
->where('SHERROR', 'Z')
->where('SHTYPE', 'M3-QtyChg')
->get();
foreach ($iCM699MQtyChg as $ICM699MQtyChgR) {
$string = $ICM699MQtyChgR->SHDATA;
$start = 'PickerLine:';
$end = ' -';
$lineAck[] = Helper::getSubstringBetween($string, $start, $end);
}
} elseif ($tRANSTYPE == 'Z') {
$iCM695M1PREP = DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
->select()
->distinct()
->where('A4CO', $cOMPANY)
->where('A4WHS', $wHS)
->where('A4MACPKR', $mANIFEST)
->orderBy('A4MACPKR');
}
$iCM695M1TMP = $iCM695M1PREP->get();
// PFC if MACKPKR already Closed : Case 671
$iCM695M1TMPCOL = collect($iCM695M1TMP);
if ($tRANSTYPE == 'Z') {
$iCM695M1TMPCOLF = $iCM695M1TMPCOL->map(function ($post) {
return trim($post->A4MACPKR);
});
} else {
$iCM695M1TMPCOLF = $iCM695M1TMPCOL->map(function ($post) {
return trim($post->AYMACPKR);
});
}
$iCM695M1TMPCOLFA = $iCM695M1TMPCOLF->toArray();
$iCM699MSuccess = DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
->select('SHMACPKR')
->whereIn('SHMACPKR', $iCM695M1TMPCOLFA)
->where('SHCO', $cOMPANY)
->where('SHWHS', $wHS)
->where('SHSOURCE', 'CloseShipmentCoChargeAPI')
->where('SHERROR', '')
->where('SHTYPE', 'M3-Res-out')
->where('SHDATA', 'like', '%Success%')
->get();
$iCM699MSuccessCol = collect($iCM699MSuccess);
$iCM699MSuccessColF = $iCM699MSuccessCol->map(function ($post) {
return $post->SHMACPKR;
});
$iCM699MSuccessColFAr = $iCM699MSuccessColF->toArray();
if ($tRANSTYPE == 'Z') {
// Use the reject method to remove items based on the condition
$iCM695M1TMPF = $iCM695M1TMP->reject(function ($post) use ($iCM699MSuccessColFAr) {
return in_array($post->A4MACPKR, $iCM699MSuccessColFAr);
});
} else {
// Use the reject method to remove items based on the condition
$iCM695M1TMPF = $iCM695M1TMP->reject(function ($post) use ($iCM699MSuccessColFAr) {
return in_array($post->AYMACPKR, $iCM699MSuccessColFAr);
});
}
$iCM695M1TMP = $iCM695M1TMPF;
$freightTermsTMP = DB::connection(config('m3.connection', 'pgsql'))->table('icm321m')
->selectRaw('TRIM(ARCODE) as FREIGHTTERMS,ARCHARGE as BILLABLE')
->get();
// Close Shipment for Manifest or Single MacPKR
if (count($iCM695M1TMP) > 0 && ($tRANSTYPE == 'M' || $tRANSTYPE == 'P')) {
// Loop the Results from $ICM595M1TMP
foreach ($iCM695M1TMP as $ICM695M1R) {
$pACKAGES = [];
$aYWHS = trim($ICM695M1R->AYWHS);
$aYCO = trim($ICM695M1R->AYCO);
$aYMACPKR = trim($ICM695M1R->AYMACPKR);
$aYRLNO = trim($ICM695M1R->AYRLNO);
$mANIFEST = trim($ICM695M1R->AYAPRF1551);
// Reset Errors after each Loop
$errorMsgs = '';
// Set Endflag for Short pick after each Loop
$endFlag = 0;
// Precheck All Qty are Equal before Proceeding to Close
// Get Parts And Lots from ICM688M2
$iCM688M2SL = DB::connection(config('m3.connection', 'pgsql'))->table('icm688m2')
->select('A5PART', 'A5PACKED', 'A5PICKED', 'A5PKLN', 'A5RSVQTY')
->distinct()
->where('A5CO', $aYCO)
->where('A5WHS', $aYWHS)
->where('A5MACPKR', $aYMACPKR)
->where('A5RLNO', $aYRLNO)
->orderBy('A5PART')
->get();
// dump($iCM688M2SL);
// Get PACK/Dock from ICM799M
// $ICM799MSEL = DB::connection(config('m3.connection', 'pgsql'))->table('icm799m')
// ->groupBy('IMTOLOC')
// ->select('IMTOLOC',DB::raw('SUM(IMQTY) as IMQTYTOT') )
// ->where('IMDIV',$aYCO)
// ->where('IMWHS',$aYWHS)
// ->where('IMDELNO',$aYMACPKR)
// ->where('IMLINE',$aYRLNO)
// ->whereIn('IMTOLOC',['DOCK','PACK'])
// ->get();
// PFC : Check that Picked / Packed are all Equal
foreach ($iCM688M2SL as $ICM688M2SLR) {
$a5PICKED = isset($ICM688M2SLR->A5PICKED) ? $ICM688M2SLR->A5PICKED : 0;
$a5RSVQTY = isset($ICM688M2SLR->A5RSVQTY) ? $ICM688M2SLR->A5RSVQTY : 0;
$a5ShortShipQty = (int) $a5RSVQTY - (int) $a5PICKED;
// Set EndFLag=1 for short picks
if ($a5ShortShipQty > 0) {
$endFlag = 1;
}
}
// Get TRAILERNUMBER SELECT APTRUCKID FROM ICM705M1 WHERE APCO=110 AND APWHS=321 AND APMANIFEST=22
$tRAILERNUMBERS = DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
->select('APTRUCKID', 'APTRACKNO', 'APMANIFEST')
->where('APCO', $cOMPANY)
->where('APMANIFEST', $mANIFEST)
->where('APWHS', $wHS)
->first();
$tRAILERNUMBER = isset($tRAILERNUMBERS->APTRUCKID) ? trim($tRAILERNUMBERS->APTRUCKID) : '';
$pRONUMBER = isset($tRAILERNUMBERS->APTRACKNO) ? trim($tRAILERNUMBERS->APTRACKNO) : '';
$iCM695M1JOINTMP = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1')
->selectRAW('AYMACPKR,AYINTBOX,AYHEIGHT,AYWIDTH,AYDEPTH,AYTYPE,AYLNWGHT,AYID,AYSPR1A01,TRIM(AYSPR15A01) AS SCAC , TRIM(AYSPR30A01) AS TRACKING,AYTRUCK AS AGGREGATION,A4RATETCP AS FREIGHT, A4OFSTRM AS FREIGHTTERMS,A4RATEM3,A4CLOSEM3, A4SHIPTYPE, A4REFORD,A4ONO,A4SHIPVIA,A42SSVCNAM,A4SHIPVIA1')
->leftJoin('ICM688M1', function ($join) {
$join->on('AYCO', '=', 'A4CO');
$join->on('AYWHS', '=', 'A4WHS');
$join->on('AYMACPKR', '=', 'A4MACPKR');
})
->where('AYMACPKR', $aYMACPKR)
->where('AYCO', $aYCO)
->where('AYWHS', $aYWHS)
->orderBy('AYINTBOX')
->get();
foreach ($iCM695M1JOINTMP as $ICM695M1JOINR) {
$a4SHIPVIA = isset($ICM695M1JOINR->A4SHIPVIA) ? trim(str_replace('&', '_', $ICM695M1JOINR->A4SHIPVIA)) : '';
$sHIPTYPE = trim($ICM695M1JOINR->A4SHIPTYPE);
$a42SSVCNAM = trim($ICM695M1JOINR->A42SSVCNAM);
$a4SHIPVIA1 = trim($ICM695M1JOINR->A4SHIPVIA1);
$fREIGHTTERMS = trim($ICM695M1JOINR->FREIGHTTERMS);
$a4RATEM3 = trim($ICM695M1JOINR->A4RATEM3);
$fREIGHT = trim($ICM695M1JOINR->FREIGHT);
$a4REFORD = trim($ICM695M1JOINR->A4REFORD);
$a4ONO = trim($ICM695M1JOINR->A4ONO);
$aYMACPKR = trim($ICM695M1JOINR->AYMACPKR);
$aYINTBOX = trim($ICM695M1JOINR->AYINTBOX);
$tRACKING = trim($ICM695M1JOINR->TRACKING);
$sCAC = trim($ICM695M1JOINR->SCAC);
$cARRIER = '';
$aYTYPE = trim($ICM695M1JOINR->AYID) != '' ? trim($ICM695M1JOINR->AYID) : '';
$aYLNWGHT = trim($ICM695M1JOINR->AYLNWGHT);
$aYHEIGHT = trim($ICM695M1JOINR->AYHEIGHT);
$aYWIDTH = trim($ICM695M1JOINR->AYWIDTH);
$aYDEPTH = trim($ICM695M1JOINR->AYDEPTH);
$bILLOFLADING = isset($ICM695M1JOINR->AYSPR1A01) ? trim($ICM695M1JOINR->AYSPR1A01) : '';
$tRANSPORTMODE = $a4SHIPVIA;
$sERVICETYPE = $a4SHIPVIA;
// Get Carrier Name from ICM160M
if ($sCAC != '') {
$sCACSub = substr($sCAC, 0, 4);
$iCM160M = DB::connection(config('m3.connection', 'pgsql'))->table('icm160m')
->select('CANAM')
->where('CANUM', $sCACSub)
->where('CACO', $cOMPANY)
->first();
$cARRIER = isset($iCM160M->CANAM) ? trim(str_replace('&', '_', $iCM160M->CANAM)) : '';
}
// PFC Check BOL for LTL
if ($sHIPTYPE == 'LTL') {
$errorMsgs = $bILLOFLADING == '' || $sCAC == '' || $tRACKING == '' ?
$errorMsgs . " Missing one or more of tracking number, BOL, or carrier. Please enter this data manually in 'invoicing' screen and retry this API call;" : $errorMsgs . '';
} else {
$errorMsgs = $sCAC == '' || $tRACKING == '' ?
$errorMsgs . " Missing one or more of tracking number, BOL, or carrier. Please enter this data manually in 'invoicing' screen and retry this API call; " : $errorMsgs . '';
}
if ($pRONUMBER == '' && $sCAC == 'RETR') {
$errorMsgs = $errorMsgs . ' Missing PARS info for this RETRans order. Please contact MPBA to ask them to enter the value for you, and then you can retry; ';
}
// MAK : PFC Check for GS1-Label A1SPR1A01 in ICM666M and ICM695M3
$gS1Label = '';
// First Check if GS1 Label is required in ICM666M
$iCM666MSQLSel = DB::connection(config('m3.connection', 'pgsql'))->table('icm666m')
->select('A1SPR1A01')
->where('A1CO', $aYCO)
->where('A1WHS', $aYWHS)
->where('A1DELNO', $aYMACPKR)
->first();
$gS1LabelExist = isset($iCM666MSQLSel->A1SPR1A01) ? trim($iCM666MSQLSel->A1SPR1A01) : 0;
// Case #566: BOL = PRONUMBER For SCAC=='RETR'
if ($sCAC == 'RETR') {
$bILLOFLADING = $pRONUMBER;
}
// Get Commodities
$myRequest = new Request;
$myRequest->setMethod('POST');
$myRequest->request->add([
'MACPKR' => $aYMACPKR,
]);
// Call processShipment
$controller = new GetContentsController;
$response = $controller->processShipment($myRequest);
$responseData = $response->getData();
$error = isset($responseData->error) ? $responseData->error : '';
if ($error == '') {
// $commodities = json_encode($responseData->commodities);
// Get Commodities
$commoditiesTmp = isset($responseData->commodities) ? $responseData->commodities : '';
if ($commoditiesTmp != '') {
$commodities = $commoditiesTmp;
} else {
$commodities = '';
$errorMsgs = $errorMsgs . ' Could not find Commodities; ';
}
} else {
$commodities = '';
$errorMsgs = $errorMsgs . ' Could not find Commodities; ';
}
// End Commodities
// Check ICM699M for SHERROR=X
$iCM699MS = DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
->select('SHMACPKR')
->where('SHMACPKR', $aYMACPKR)
->where('SHCO', $aYCO)
->where('SHWHS', $aYWHS)
->whereIn('SHSOURCE', ['TransferAPIURL', 'CloseShipmentCoChargeAPI', 'PackSyst4tray'])
->where('SHERROR', 'X')
->whereIn('SHTYPE', ['M3-Res-out', 'M3-QtyChg'])
->first();
$alreadySentToM3 = isset($iCM699MS->SHMACPKR) ? trim($iCM699MS->SHMACPKR) : '';
if ($alreadySentToM3 != '') {
// Add Message
$errorMsgs = $errorMsgs . ' There are previous M3 transmissions in error for this delivery note that have not been addressed; ';
}
if ($aYTYPE == '') {
// Get AETYPE SELECT APTRUCKID FROM ICM705M1 WHERE APCO=110 AND APWHS=321 AND APMANIFEST=22
$iCM693MS = DB::connection(config('m3.connection', 'pgsql'))->table('icm693m')
->select('AEID')
->where('AESPR1A01', 'D')
->where('AETYPE', trim($ICM695M1JOINR->AYTYPE))
->first();
$aYTYPE = isset($iCM693MS->AEID) ? trim($iCM693MS->AEID) : '';
}
$currentFreightTerms = $freightTermsTMP->where('FREIGHTTERMS', $fREIGHTTERMS)->first();
$bILLABLE = isset($currentFreightTerms->BILLABLE) ? trim($currentFreightTerms->BILLABLE) : 'N';
// If billable=Y and a4ratetcp(FREIGHT)>0 and a4ratem3=blank, then call the freight API CoCharge
if ($bILLABLE == 'Y' && $fREIGHT > 0 && $a4RATEM3 == '') {
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
if ($sHIPTYPE == 'LTL') {
$shippingToBill = $fREIGHT * (100.00 - Config::get('arrays.misc.discountCarrierRateForCustomerPriceLTL')) / 100;
} else {
$shippingToBill = $fREIGHT * (100.00 - Config::get('arrays.misc.discountCarrierRateForCustomerPriceParcel')) / 100;
}
// Construct Json for COCharge
$jSONCoCharge = [
'QUANTITY' => round($shippingToBill, 2), // $fREIGHT with modified Params
'ORDERCATEGORY' => $a4REFORD,
'CONUMBER' => $a4ONO,
];
} else {
$jSONCoCharge = [];
}
// If billable=Y and a4ratetcp(FREIGHT)=0 and a4ratem3=blank, then we need to:
// Write a record to ICM699M to indicate that we are not calling the rate API because the $ are 0
if ($bILLABLE == 'Y' && $fREIGHT == 0 && $a4RATEM3 == '') {
// Add Message
$errorMsgs = $errorMsgs . ' Please manually add a freight charge for this Prepaid and Add order; ';
}
// Close Shipment
// If ( (billable=Y and a4RATEM3 (from array in memory)="X") OR (billable=N) ) and a4closeM3="" then call the close API
// Set PDF URLs for Labels
$carrierLabel = '';
$contentLabel = '';
$docTypes = ['ContentLabel', 'CarrierLabel'];
foreach ($docTypes as $key => $value) {
switch ($value) {
case 'CarrierLabel':
$dOCTYPE = 'CarrierLabel';
$dOCNUMT = $aYINTBOX;
$pDFKey = $value . '/' . $value . '_' . $aYMACPKR . '_' . $dOCNUMT . '_' . $cOMPANY . '_' . $wHS;
$pdfFileName = $pdfFilePath . $pDFKey . '.pdf';
if (File::exists($pdfFileName)) {
$carrierLabel = route('m3.M3GetPDFFromCache.index', ['MACPKR' => $aYMACPKR, 'DOCTYPE' => $dOCTYPE]);
}
break;
case 'ContentLabel':
$dOCTYPE = 'ContentLabel';
$dOCNUMT = $aYINTBOX;
$pDFKey = $value . '/' . $value . '_' . $aYMACPKR . '_' . $dOCNUMT . '_' . $cOMPANY . '_' . $wHS;
$pdfFileName = $pdfFilePath . $pDFKey . '.pdf';
if (File::exists($pdfFileName)) {
$contentLabel = route('m3.M3GetPDFFromCache.index', ['MACPKR' => $aYMACPKR, 'DOCTYPE' => $dOCTYPE]);
}
break;
default:
break;
}
}
// Second Check GS1 Label existence in ICM695M3
if ($gS1LabelExist == 1) {
$iCM695M3SQLSel = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m3')
->select('A2GS1LABEL')
->where('A2CO', $aYCO)
->where('A2WHS', $aYWHS)
// ->where('A2MACPKR',$aYMACPKR)
->where('A2INTBOX', $aYINTBOX)
->first();
$gS1Label = isset($iCM695M3SQLSel->A2GS1LABEL) ? trim($iCM695M3SQLSel->A2GS1LABEL) : '';
if ($gS1Label == '') {
$errorMsgs = $errorMsgs . ' Missing G1-128 Label. Please contact MPBA to ask them to enter the value for you, and then you can retry; ';
}
}
// Get Packages Array
$pACKAGES[] = [
'TRACKINGID' => $tRACKING,
'BUNDLES' => $aYTYPE,
'WEIGHT' => $aYLNWGHT,
'HEIGHT' => $aYHEIGHT,
'WIDTH' => $aYWIDTH,
'LENGTH' => $aYDEPTH,
'SSCC' => $gS1Label,
'SHIPLABELDOC' => $carrierLabel,
'CONTENTLABELDOC' => $contentLabel,
];
}
// Check Packages array before proceeding with CloseShipment
if (! empty($pACKAGES)) {
// Get Packlist
$packingList = '';
$dOCTYPE = 'PackingList';
$dOCNUMT = $aYMACPKR;
$pDFKey = $dOCTYPE . '/' . $dOCTYPE . '_' . $aYMACPKR . '_' . $dOCNUMT . '_' . $cOMPANY . '_' . $wHS;
$pdfFileName = $pdfFilePath . $pDFKey . '.pdf';
if (File::exists($pdfFileName)) {
$packingList = route('m3.M3GetPDFFromCache.index', ['MACPKR' => $aYMACPKR, 'DOCTYPE' => $dOCTYPE]);
}
// Add Cocharge
if (count($jSONCoCharge) > 0) {
// Construct Json for CloseShipment With PDF Files
$tempClose = [
'DIVI' => $aYCO,
'WHS' => $aYWHS,
'DELIVERYNO' => $aYMACPKR,
'PICKLISTSUFFIX' => $aYRLNO,
'TYPE' => 'CloseShipment',
'SCAC' => $sCAC,
'CARRIER' => $cARRIER,
'TRANSPORTMODE' => $tRANSPORTMODE,
'SERVICETYPE' => $sERVICETYPE,
'TRACKINGNUMBER' => $tRACKING,
'PRONUMBER' => $pRONUMBER,
'BILLOFLADING' => $bILLOFLADING,
'TRAILERNUMBER' => $tRAILERNUMBER,
'PACKAGES' => $pACKAGES,
'DELIVERYNOTEDOC' => $packingList,
'shipType' => $sHIPTYPE,
'carrierServiceDescription' => $a42SSVCNAM,
'modeOfTransport' => $a4SHIPVIA1,
'COMMODITIES' => $commodities,
'ENDFLAG' => $endFlag,
];
$jSONCoChargeCS = array_merge($jSONCoCharge, $tempClose);
$jSONCS = json_encode($jSONCoChargeCS);
} else {
// Construct Json for CloseShipment With PDF Files
$jSONCS = json_encode([
'DIVI' => $aYCO,
'WHS' => $aYWHS,
'DELIVERYNO' => $aYMACPKR,
'PICKLISTSUFFIX' => $aYRLNO,
'TYPE' => 'CloseShipment',
'SCAC' => $sCAC,
'CARRIER' => $cARRIER,
'TRANSPORTMODE' => $tRANSPORTMODE,
'SERVICETYPE' => $sERVICETYPE,
'TRACKINGNUMBER' => $tRACKING,
'PRONUMBER' => $pRONUMBER,
'BILLOFLADING' => $bILLOFLADING,
'TRAILERNUMBER' => $tRAILERNUMBER,
'PACKAGES' => $pACKAGES,
'DELIVERYNOTEDOC' => $packingList,
'ServiceType' => $sHIPTYPE,
'ServiceLevelCode' => $a42SSVCNAM,
'TransportationMode' => $a4SHIPVIA1,
'COMMODITIES' => $commodities,
'ENDFLAG' => $endFlag,
]);
}
// No Errors Proceed in Calling M3 CLose API
if ($errorMsgs == '') {
// Write to Logs Out : Send JSON Request To M3
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
// dump($dELIVERYNO);
// MAK : Log JSON Manifest # and MACKPKR #
// Log::channel('dailyCron')->info('1 >>> JSON Sent for Manifest: '.$mANIFEST.' ,Macpicker: '.$aYMACPKR);
Log::channel('dailyCron')->info('1 >>> JSON Sent for Manifest: ' . (string) (int) $mANIFEST . ' ,Macpicker: ' . $aYMACPKR);
$data = [
'SHMACPKR' => $dELIVERYNO,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => '',
'SHINTBOX' => '',
'SHTYPE' => 'M3-JSN-out',
'SHDATA' => $jSONCS,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
$response = Http::withHeaders([
'accept' => self::JSON_CONTENT_TYPE,
'Content-Type' => self::JSON_CONTENT_TYPE,
'Connection' => 'close',
])
->withToken($token)
->withBody($jSONCS, self::JSON_CONTENT_TYPE)
->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';
$responseJSON = $response->getBody()->getContents();
$jsonSanytized = Helper::sanityzeString($responseJSON);
$responseContents = json_decode($jsonSanytized);
// MAK : Add response Time
// $M3ResponseTimeTmp = $response->hasHeader('x-response-time') ? ltrim($response->header('x-response-time'),"-") : '';
// $M3ResponseTimeTmp2 = $M3ResponseTimeTmp /1000;
// $dateTime = new DateTime();
// $dateTime->setTimestamp($M3ResponseTimeTmp2);
// // dump( $dateTime->format('d/m/Y H:i:s.v') );
// $M3ResponseTime = $M3ResponseTimeTmp != '' ? '-M3ResponseTime:' . $dateTime->format('d/m/Y H:i:s.v') : '-M3ResponseTime';
// // dump($M3ResponseTime);
// $transferStats = $response->transferStats;
// $M3transferTime = $transferStats->getTransferTime()!=null ? "-M3transferTime:".$transferStats->getTransferTime() : "-M3transferTime";
// dd($M3transferTime);
// Log M3 response
Log::channel('dailyCron')->info('2 >>> Close API M3 Responded...');
if ($responseContents != null && $responseStatusCode == '200') {
$responseMsg = $responseContents->result->CloseShipment;
$m3APIRESULT = $responseMsg->PickingListClose->Result;
// Check M3 Response for Errors
$responseResult = $responseContents->result;
$m3Errors = Helper::checkM3Errors($responseResult);
// If you get a successful response, update database file ICM688M1/A4RATEm3=X
// Update ICM688M1 : Change A4RATEM3
if (count($jSONCoCharge) > 0 && $m3APIRESULT != 'Error') {
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
->where('A4MACPKR', $aYMACPKR)
->where('A4CO', $aYCO)
->where('A4WHS', $aYWHS)
->update(['A4RATEM3' => 'X', 'A4PICKSTS' => 'A']);
} elseif (count($jSONCoCharge) == 0 && $m3APIRESULT != 'Error') {
// Update ICM688M1 : Change A4RATEM3
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
->where('A4MACPKR', $aYMACPKR)
->where('A4CO', $aYCO)
->where('A4WHS', $aYWHS)
->update(['A4PICKSTS' => 'A']);
}
} else {
$m3APIRESULT = 'Error';
$responseMsg = self::NULL_M3_RESPONSE;
$responseResult = '';
$m3Errors = $responseStatusCode;
}
// If you get a successful response
if ($m3APIRESULT != 'Error') {
// Write to Logs Out : Success
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
$data = [
'SHMACPKR' => $dELIVERYNO,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => '',
'SHINTBOX' => '',
'SHTYPE' => 'M3-Res-out',
'SHDATA' => 'Success...' . json_encode($responseResult) . $m3SessionId,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
}
// If you get a failure, write a record to ICM699M to indicate that the CLOSE API call failed
else {
// Update ICM688M1 : Change A4PICKSTS
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
->where('A4MACPKR', $aYMACPKR)
->where('A4CO', $aYCO)
->where('A4WHS', $aYWHS)
->update(['A4PICKSTS' => 'a']);
// Write to Logs Out : Error
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
$data = [
'SHMACPKR' => $dELIVERYNO,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => 'X',
'SHINTBOX' => '',
'SHTYPE' => 'M3-Res-out',
'SHDATA' => 'Important-Error: ' . $m3Errors . json_encode($responseMsg) . $m3SessionId,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
// Add Message
$errorMsgs = $errorMsgs . $m3Errors . ' ';
}
}
// Errors found Log to ICM699M and update A4PICKSTS
else {
// Write to Logs : Error
// Clear Duplicate messages
$errorMsgsC = Helper::removeDuplicateSubstrings($errorMsgs);
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
$data = [
'SHMACPKR' => $dELIVERYNO,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => 'X',
'SHINTBOX' => '',
'SHTYPE' => 'M3-Res-out',
'SHDATA' => $errorMsgsC,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
}
}
$allErrMsgs = $allErrMsgs . $errorMsgs;
}
// Update ICM699M : Look for CommTCP_read and set SHERROR to Z Case # 580
$sHACKNOTE = 'Sentinel automatically closed the error (false positive CommTCP_read)';
DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
->where('SHMACPKR', $aYMACPKR)
->where('SHCO', $cOMPANY)
->where('SHWHS', $wHS)
->where('SHSOURCE', $sHSOURCE)
->where('SHERROR', 'X')
->where('SHTYPE', 'M3-General')
->where('SHDATA', 'like', '%CommTCP_read%')
->update(['SHERROR' => 'Z', 'SHACKUSER' => $sHUSER, 'SHACKDATE' => date('Y-m-d'), 'SHACKTIME' => date('H:i:s'), 'SHACKNOTE' => $sHACKNOTE]);
// Update ICM705M1 to say that we have processed this manifest (set APSPARE2='X' )
if ($tRANSTYPE == 'M') {
DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
->where('APMANIFEST', $mANIFEST)
->where('APCO', $cOMPANY)
->where('APWHS', $wHS)
->update(['APSPARE2' => 'X']);
}
// Send Response
if ($allErrMsgs == '') {
return response()->json([
'code' => '200',
'status' => 'Success',
'message' => 'All Operations Successful',
]);
} else {
$allErrMsgsC = Helper::removeDuplicateSubstrings($allErrMsgs);
return response()->json([
'code' => '206',
'status' => 'Success With Warnings',
'message' => $allErrMsgsC . '; Check Transaction Logs for Errors or Warnings.',
]);
}
}
// Close Shipment for Zero Picks
elseif (count($iCM695M1TMP) > 0 && $tRANSTYPE == 'Z') {
$errorMsgs = '';
foreach ($iCM695M1TMP as $key => $ICM695M1JOINR) {
$sHIPTYPE = trim($ICM695M1JOINR->A4SHIPTYPE);
$fREIGHTTERMS = trim($ICM695M1JOINR->A4OFSTRM);
$a4RATEM3 = trim($ICM695M1JOINR->A4RATEM3);
$fREIGHT = trim($ICM695M1JOINR->A4RATETCP);
$a4REFORD = trim($ICM695M1JOINR->A4REFORD);
$a4ONO = trim($ICM695M1JOINR->A4ONO);
$aYMACPKR = trim($ICM695M1JOINR->A4MACPKR);
$aYWHS = trim($ICM695M1JOINR->A4WHS);
$aYCO = trim($ICM695M1JOINR->A4CO);
// Construct Json for CloseShipment Without PDF Files
$jSONCS = json_encode([
'DIVI' => $aYCO,
'WHS' => $aYWHS,
'DELIVERYNO' => $aYMACPKR,
'TYPE' => 'CloseShipment',
'PICKLISTSUFFIX' => '1',
'ENDFLAG' => '1',
]);
// Write to Logs Out : JSON Request To M3
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
$data = [
'SHMACPKR' => $dELIVERYNO,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => '',
'SHINTBOX' => '',
'SHTYPE' => 'M3-JSN-out',
'SHDATA' => $jSONCS,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
$response = Http::withHeaders([
'accept' => self::JSON_CONTENT_TYPE,
'Content-Type' => self::JSON_CONTENT_TYPE,
'Connection' => 'close',
])
->withToken($token)
->withBody($jSONCS, self::JSON_CONTENT_TYPE)
->timeout(Config::get('arrays.curlTimeOut'))
->retry(2, 100)
->post($endPointURL);
$responseContents = json_decode($response->getBody()->getContents());
$m3SessionId = $response->hasHeader('x-transaction-id') ? '-sessionID:' . $response->header('x-transaction-id') : '-sessionID';
$m3Errors = '';
if ($responseContents != null) {
$responseMsg = $responseContents->result->CloseShipment;
$m3APIRESULT = $responseMsg->PickingListClose->Result;
// Check M3 Response for Errors
$responseResult = $responseContents->result;
$m3Errors = Helper::checkM3Errors($responseResult);
// If you get a successful response, update database file ICM688M1/A4PICKSTS=A
if ($m3APIRESULT != 'Error') {
// Update ICM688M1 : Change A4RATEM3
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
->where('A4MACPKR', $aYMACPKR)
->where('A4CO', $aYCO)
->where('A4WHS', $aYWHS)
->where('A4PICKSTS', '!=', 'X')
->update(['A4PICKSTS' => 'A']);
}
} else {
$m3APIRESULT = 'Error';
$responseMsg = self::NULL_M3_RESPONSE;
}
// If you get a successful response
if ($m3APIRESULT != 'Error') {
// Write to Logs Out : Success
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
$data = [
'SHMACPKR' => $dELIVERYNO,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => '',
'SHINTBOX' => '',
'SHTYPE' => 'M3-Res-out',
'SHDATA' => 'Success' . $m3SessionId,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
}
// If you get a failure, write a record to ICM699M to indicate that the CLOSE API call failed
else {
// Update ICM688M1 : Change A4PICKSTS
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
->where('A4MACPKR', $aYMACPKR)
->where('A4CO', $aYCO)
->where('A4WHS', $aYWHS)
->where('A4PICKSTS', '!=', 'X')
->update(['A4PICKSTS' => 'a']);
// Write to Logs Out : Error
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
$data = [
'SHMACPKR' => $dELIVERYNO,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => 'X',
'SHINTBOX' => '',
'SHTYPE' => 'M3-Res-out',
'SHDATA' => 'Important-Error: ' . $m3Errors . json_encode($responseMsg) . $m3SessionId,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
// Add Message
$errorMsgs = $errorMsgs . $m3Errors . '-';
}
// Update ICM699M : Look for CommTCP_read and set SHERROR to Z Case # 580
$sHACKNOTE = 'Sentinel automatically closed the error (false positive CommTCP_read)';
DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
->where('SHMACPKR', $aYMACPKR)
->where('SHCO', $cOMPANY)
->where('SHWHS', $wHS)
->where('SHSOURCE', $sHSOURCE)
->where('SHERROR', 'X')
->where('SHTYPE', 'M3-General')
->where('SHDATA', 'like', '%CommTCP_read%')
->update(['SHERROR' => 'Z', 'SHACKUSER' => $sHUSER, 'SHACKDATE' => date('Y-m-d'), 'SHACKTIME' => date('H:i:s'), 'SHACKNOTE' => $sHACKNOTE]);
}
// Send Response
if ($errorMsgs == '') {
return response()->json([
'code' => '200',
'status' => 'Success',
'message' => 'All Operations Successful',
]);
} else {
return response()->json([
'code' => '206',
'status' => 'Success With Warnings',
'message' => $errorMsgs . '; Check The Logs for Errors or Warnings.',
]);
}
}
// ## End Zero Pick
else {
$dELIVERYNO = isset($mANIFEST) ? $mANIFEST : self::ZERO_PAD;
// # Set SHDATA Message
$sHDATAMSG = 'No records found in ICM695M1,or URL parameters incomplete';
// count($iCM695M1TMP)==0 ? 'MacPkr already closed' :
$sHERROR = 'X';
// count($iCM695M1TMP)==0 ? 'Z' :
// Write to Logs Out : Error
$data = [
'SHMACPKR' => $dELIVERYNO,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => $sHERROR,
'SHINTBOX' => '',
'SHTYPE' => 'M3-Req-out',
'SHDATA' => $sHDATAMSG,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
// Return Error
return response()->json([
'code' => '405',
'status' => 'Error',
'message' => 'No records found in ICM695M1,or URL parameters incomplete For MacPkr/Manifest:' . $dELIVERYNO,
]);
}
} else {
// Write to Logs Out : Error
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
$data = [
'SHMACPKR' => $dELIVERYNO,
'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' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
// Return Error
return response()->json([
'code' => '405',
'status' => 'Error',
'message' => 'No URL Parameters Sent',
]);
}
} catch (\Throwable $th) {
$getCurlErrors = str_contains($th->getMessage(), 'cURL error');
if ($getCurlErrors) {
// dd($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' . $jSONIN,
'SHUSER' => $sHUSER,
'SHSOURCE' => $sHSOURCE,
'SHCO' => $cOMPANY,
'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
$dELIVERYNO = isset($aYMACPKR) ? $aYMACPKR : self::ZERO_PAD;
$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,
'SHCO' => $cOMPANY,
'SHWHS' => $wHS,
];
Helper::logToICM699M($data);
Log::error($th);
return response()->json([
'code' => '405',
'status' => 'error',
'message' => 'Catched Error: ' . $th,
]);
}
}
}
// End CloseShipmentCoChargeAPI
}