173 lines
6.1 KiB
PHP
Executable File
173 lines
6.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\M3\Http\Controllers;
|
|
|
|
use Config;
|
|
use DB;
|
|
use File;
|
|
use Illuminate\Http\Request;
|
|
use Str;
|
|
|
|
class M3GetPDFFromCacheController extends Controller
|
|
{
|
|
public static function index(Request $request)
|
|
{
|
|
$state = self::initialState();
|
|
|
|
try {
|
|
self::loadUserContext($state);
|
|
self::loadDocuments($request, $state);
|
|
} catch (\Throwable $throwable) {
|
|
report($throwable);
|
|
}
|
|
|
|
return view('m3::liberty-theme.pages.M3GetPDF', self::viewData($state));
|
|
}
|
|
|
|
private static function initialState(): array
|
|
{
|
|
return [
|
|
'aggmsg' => '', 'IC670M' => collect(), 'PDFArray' => [], 'carrier' => [],
|
|
'trackingno' => [], 'ICM688M1' => [], 'AYMACPKR' => '', 'WHDDA' => '',
|
|
'COMPANY' => '', 'PLANT' => '', 'WHS' => '',
|
|
];
|
|
}
|
|
|
|
private static function loadUserContext(array &$state): void
|
|
{
|
|
$user = m3User() !== '' ? Str::upper(trim(m3User())) : '';
|
|
if ($user !== '') {
|
|
$profile = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
|
|
->select('R428US', 'R428CO', 'R428WH', 'R428PL')->where('R428US', $user)->first();
|
|
$state['COMPANY'] = $profile->R428CO ?? '';
|
|
$state['PLANT'] = $profile->R428PL ?? '';
|
|
$state['WHS'] = $profile->R428WH ?? '';
|
|
}
|
|
|
|
$state['WHDDA'] = $state['COMPANY'] . $state['PLANT'] . $state['WHS'];
|
|
$state['IC670M'] = DB::connection(config('m3.connection', 'pgsql'))->table('ic670m')->get();
|
|
}
|
|
|
|
private static function loadDocuments(Request $request, array &$state): void
|
|
{
|
|
$params = $request->all();
|
|
if ($params === []) {
|
|
return;
|
|
}
|
|
|
|
$state['AYMACPKR'] = trim((string) ($params['MACPKR'] ?? ''));
|
|
$documentType = trim((string) ($params['DOCTYPE'] ?? ''));
|
|
$documentTypes = $documentType !== '' ? [$documentType] : ['PackingList', 'ContentLabel', 'CarrierLabel', 'GSLabel'];
|
|
$aggregation = self::aggregationNumber($state);
|
|
$rows = self::documentRows($state, $aggregation);
|
|
if ($rows->isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
$keys = self::cacheKeys($rows, $documentTypes, $state);
|
|
$state['PDFArray'] = self::readCachedPdfs($keys);
|
|
}
|
|
|
|
private static function aggregationNumber(array &$state): string
|
|
{
|
|
$row = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1')
|
|
->select('AYTRUCK')->where('AYCO', $state['COMPANY'])->where('AYWHS', $state['WHS'])
|
|
->where('AYMACPKR', $state['AYMACPKR'])->first();
|
|
$aggregation = trim((string) ($row->AYTRUCK ?? ''));
|
|
if ($aggregation !== '' && $aggregation !== '0') {
|
|
$state['aggmsg'] = "This delivery note is part of aggregation $aggregation ";
|
|
$state['ICM688M1'] = DB::connection(config('m3.connection', 'pgsql'))
|
|
->table('icm688m1')->select('A4MACPKR')->where('A4SHPAGGCD', $aggregation)->get()->all();
|
|
}
|
|
|
|
return $aggregation;
|
|
}
|
|
|
|
private static function documentRows(array $state, string $aggregation)
|
|
{
|
|
$query = DB::connection(config('m3.connection', 'pgsql'))->table('icm695m1')
|
|
->select('AYCO', 'AYWHS', 'AYMACPKR', 'AYINTBOX', 'AYSPR15A01', 'AYSPR30A01', 'AYTRUCK')
|
|
->distinct()->where('AYCO', $state['COMPANY'])->where('AYWHS', $state['WHS']);
|
|
|
|
$aggregation !== '' && $aggregation !== '0'
|
|
? $query->where('AYTRUCK', $aggregation)
|
|
: $query->where('AYMACPKR', $state['AYMACPKR']);
|
|
|
|
return $query->orderBy('AYMACPKR')->get();
|
|
}
|
|
|
|
private static function cacheKeys(iterable $rows, array $documentTypes, array &$state): array
|
|
{
|
|
$keys = [];
|
|
foreach ($rows as $row) {
|
|
self::appendRowKeys($keys, $row, $documentTypes, $state);
|
|
}
|
|
|
|
return array_values(array_unique($keys));
|
|
}
|
|
|
|
private static function appendRowKeys(array &$keys, object $row, array $documentTypes, array &$state): void
|
|
{
|
|
$warehouse = trim((string) $row->AYWHS);
|
|
$company = trim((string) $row->AYCO);
|
|
$picker = trim((string) $row->AYMACPKR);
|
|
$documentNumber = trim((string) $row->AYINTBOX);
|
|
$state['WHS'] = $warehouse;
|
|
$state['COMPANY'] = $company;
|
|
$state['AYMACPKR'] = $picker;
|
|
$state['carrier'][] = trim((string) $row->AYSPR15A01);
|
|
$state['trackingno'][] = trim((string) $row->AYSPR30A01);
|
|
|
|
if ($picker === '' || $company === '' || $warehouse === '') {
|
|
return;
|
|
}
|
|
|
|
$contentLabel = '';
|
|
foreach ($documentTypes as $type) {
|
|
[$key, $extraKey, $contentLabel] = self::documentKey($type, $picker, $documentNumber, $company, $warehouse, $contentLabel);
|
|
if ($extraKey !== null) {
|
|
$keys[] = $extraKey;
|
|
}
|
|
$keys[] = $key;
|
|
}
|
|
}
|
|
|
|
private static function documentKey(string $type, string $picker, string $number, string $company, string $warehouse, string $contentLabel): array
|
|
{
|
|
$documentNumber = $number;
|
|
$extraKey = null;
|
|
if ($type === 'ContentLabel') {
|
|
$contentLabel = is_numeric($number) ? '' : 'LTL';
|
|
} elseif ($type === 'PackingList') {
|
|
$documentNumber = $picker;
|
|
} elseif ($type === 'CarrierLabel' && $contentLabel === 'LTL') {
|
|
$extraKey = "$type/{$type}_{$picker}_{$picker}_{$company}_{$warehouse}";
|
|
}
|
|
|
|
$key = $type === 'GSLabel'
|
|
? "$type/{$type}_{$picker}_{$company}_{$warehouse}_{$number}"
|
|
: "$type/{$type}_{$picker}_{$documentNumber}_{$company}_{$warehouse}";
|
|
|
|
return [$key, $extraKey, $contentLabel];
|
|
}
|
|
|
|
private static function readCachedPdfs(array $keys): array
|
|
{
|
|
$result = [];
|
|
$path = (string) Config::get('arrays.PdfPath');
|
|
foreach ($keys as $key) {
|
|
$fileName = $path . $key . '.pdf';
|
|
if (File::exists($fileName)) {
|
|
$result[] = base64_encode(File::get($fileName));
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private static function viewData(array $state): array
|
|
{
|
|
return $state;
|
|
}
|
|
}
|