108 lines
2.9 KiB
PHP
Executable File
108 lines
2.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\M3\Http\Controllers;
|
|
|
|
use DB;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Log;
|
|
use Storage;
|
|
use Str;
|
|
|
|
class WhsPerfMetrics extends Controller
|
|
{
|
|
public static function index(Request $request)
|
|
{
|
|
$context = self::defaultContext();
|
|
|
|
try {
|
|
$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', $context);
|
|
}
|
|
|
|
public static function downloadFiles(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'filename' => ['required', 'string'],
|
|
]);
|
|
|
|
$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;
|
|
}
|
|
}
|