m3 Security Fixes

This commit is contained in:
Mark Gregory
2026-09-17 10:31:34 +01:00
parent c9856d58c1
commit a094477840
6 changed files with 251 additions and 379 deletions
+82 -86
View File
@@ -2,9 +2,9 @@
namespace Modules\M3\Http\Controllers;
use Config;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Log;
use Storage;
use Str;
@@ -13,99 +13,95 @@ class WhsPerfMetrics extends Controller
{
public static function index(Request $request)
{
$context = self::defaultContext();
try {
$SHUSER = m3_user() !== '' ? Str::upper(trim(m3_user())) : '';
// dd($SHUSER);
if ($SHUSER != '') {
// Get User CO & WH from Table ICM428M
$ICM428M = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
->select('R428US', 'R428CO', 'R428WH', 'R428PL')
->where('R428US', '=', $SHUSER)
->first();
$R428US = isset($ICM428M->R428US) ? $ICM428M->R428US : ''; // User
$COMPANY = isset($ICM428M->R428CO) ? $ICM428M->R428CO : '110'; // Company
$PLANT = isset($ICM428M->R428PL) ? $ICM428M->R428PL : '144'; // Plant
$WHS = isset($ICM428M->R428WH) ? $ICM428M->R428WH : '322'; // WareHouse
}
// Warehouse Table
$IC670M = DB::connection(config('m3.connection', 'pgsql'))->table('ic670m')->get();
// Get URL PARAMS
$urlParams = count($request->all()) > 0 ? $request->all() : '';
// dump($urlParams);
$REPORTDATEDisp = isset($urlParams['REPORTDATEDisp']) ? \DateTime::createFromFormat('Y-m-d', trim($urlParams['REPORTDATEDisp']))->format('Ymd') : today()->format('Ymd');
$WHDDA = $COMPANY . $PLANT . $WHS;
// dump($WHDDA);
if ($REPORTDATEDisp != '' && $COMPANY != '' && $WHS != '' && $PLANT != '') {
// Case 729: Add Currency code A1SHCURR FROM ICM666M
$selectContent = "
SELECT A5MACPKR, A5ONO,
(SELECT A4REFORD FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) AS A4REFORD,
A5PART, A5RSVQTY, A5PACKED, A5RSVQTY - A5PACKED AS CALCULATED_COLUMN,
A5SLUNPR * A5PACKED AS ANOTHER_CALCULATED_COLUMN,
(SELECT A4SHSHNM FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) AS A4SHSHNM ,
SHDT, SHTIME,
CASE WHEN SHDT IS NULL THEN (SELECT SHDATA FROM ICM699M WHERE SHMACPKR=A5MACPKR AND SHTYPE='M3-Res-out' AND SHSOURCE='CloseShipmentCoChargeAPI' ORDER BY SHDT DESC, SHTIME DESC FETCH FIRST 1 ROWS ONLY) ELSE '' END AS REASON,
A1SHCURR,
CASE
WHEN (SELECT A4SHIPTYPE FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) = 'LTL'
THEN (SELECT A4RATETCP FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) * " . (100.00 - Config::get('arrays.misc.discountCarrierRateForCustomerPriceLTL')) / 100 . '
ELSE (SELECT A4RATETCP FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) * ' . (100.00 - Config::get('arrays.misc.discountCarrierRateForCustomerPriceParcel')) / 100 . "
END AS FREIGHT
FROM ICM688M2
LEFT JOIN ICM699M ON SHMACPKR=A5MACPKR AND SHTYPE='M3-Res-out' AND SHDATA LIKE 'Success%' AND SHSOURCE='CloseShipmentCoChargeAPI'
LEFT JOIN ICM666M ON A1DELNO=A5MACPKR AND A1ONO=A5ONO AND A1PKLN=A5BRADLN
WHERE A5MACPKR IN (
SELECT DISTINCT AYMACPKR
FROM ICM695M1
WHERE AYSTATUS IN ('Z','z') AND AYAPR8N002 = $REPORTDATEDisp
)
AND A5CO=$COMPANY AND A5WHS=$WHS AND A5PLANT=$PLANT";
// dump($selectContent);
$ICM688M2 = DB::connection(config('m3.connection', 'pgsql'))
->select($selectContent);
// dd($ICM688M2);
} else {
$ICM688M2 = collect();
}
// Get List of Files
$allReportsUrls = [];
$files = Storage::disk('publicLabels')->allFiles('WhsPerf/' . $WHS);
// $whsPerfFile = 'WhsPerf/'.$WHS.'/WhsPerfMetrics_'.$DateToday.'-'.$WHS.'.xlsx';
foreach ($files as $file) {
// Get the file URL (if URL is configured in filesystems.php)
$url = Storage::disk('publicLabels')->url($file);
$allReportsUrls[$file] = $url;
}
// dd($allReportsUrls);
} catch (\Throwable $th) {
Log::error($th);
$context = array_merge($context, self::loadUserContext());
$context['IC670M'] = self::loadWarehouses();
$context['WHDDA'] = $context['COMPANY'] . $context['PLANT'] . $context['WHS'];
$context['allReportsUrls'] = self::loadReportUrls($context['WHS']);
} catch (\Throwable $exception) {
Log::error('Unable to load warehouse performance metrics.', [
'exception' => $exception,
]);
}
return view('m3::liberty-theme.pages.WhsPerfMetrics', compact('IC670M', 'WHDDA', 'COMPANY', 'PLANT', 'WHS', 'allReportsUrls'));
return view('m3::liberty-theme.pages.WhsPerfMetrics', $context);
}
// Download File
public static function downloadFiles(Request $request)
{
// dd($request->all());
$fileName = $request->filename;
if (Storage::disk('publicLabels')->exists($fileName)) {
// File exists, retrieve content or URL
$fileUrl = Storage::disk('publicLabels')->url($fileName);
$fileNameOnly = basename($fileName);
$validated = $request->validate([
'filename' => ['required', 'string'],
]);
// Download the file
return Storage::disk('publicLabels')->download($fileName, $fileNameOnly);
} else {
// Download the file (use code below)
$fileName = ltrim($validated['filename'], '/');
$disk = Storage::disk('publicLabels');
if (! $disk->exists($fileName)) {
abort(404, 'Warehouse performance file not found.');
}
return $disk->download($fileName, basename($fileName));
}
private static function defaultContext(): array
{
return [
'IC670M' => collect(),
'WHDDA' => '',
'COMPANY' => '',
'PLANT' => '',
'WHS' => '',
'allReportsUrls' => [],
];
}
private static function loadUserContext(): array
{
$connection = config('m3.connection', 'pgsql');
$user = Str::upper(trim((string) m3_user()));
if ($user === '' || ! Schema::connection($connection)->hasTable('icm428m')) {
return ['COMPANY' => '', 'PLANT' => '', 'WHS' => ''];
}
$profile = DB::connection($connection)->table('icm428m')
->select('R428CO', 'R428PL', 'R428WH')
->where('R428US', $user)
->first();
return [
'COMPANY' => trim((string) ($profile->R428CO ?? '')),
'PLANT' => trim((string) ($profile->R428PL ?? '')),
'WHS' => trim((string) ($profile->R428WH ?? '')),
];
}
private static function loadWarehouses()
{
$connection = config('m3.connection', 'pgsql');
if (! Schema::connection($connection)->hasTable('ic670m')) {
return collect();
}
return DB::connection($connection)->table('ic670m')->get();
}
private static function loadReportUrls(string $warehouse): array
{
if ($warehouse === '') {
return [];
}
$disk = Storage::disk('publicLabels');
$urls = [];
foreach ($disk->allFiles('WhsPerf/' . $warehouse) as $file) {
$urls[$file] = $disk->url($file);
}
return $urls;
}
}