365 lines
16 KiB
PHP
365 lines
16 KiB
PHP
<?php
|
|
|
|
namespace Modules\M3\Http\Controllers;
|
|
|
|
use Config;
|
|
use Datatables;
|
|
use DB;
|
|
use Excel;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\M3\Services\SpreadsheetExport;
|
|
use Str;
|
|
|
|
class M3ErrorsDispController extends Controller
|
|
{
|
|
public static function index(Request $request)
|
|
{
|
|
// dump($request->all());
|
|
$macpkr = isset($request->macpkr) ? trim($request->macpkr) : '';
|
|
$shsource = isset($request->shsource) ? trim($request->shsource) : '';
|
|
$showerrors = isset($request->showerrors) ? trim($request->showerrors) : 'N';
|
|
$skipdates = isset($request->skipdates) ? trim($request->skipdates) : 'N';
|
|
|
|
// Get users from ICM428M
|
|
$connection = config('m3.connection', 'pgsql');
|
|
$ICM428M = Schema::connection($connection)->hasTable('icm428m')
|
|
? DB::connection($connection)->table('icm428m')->select('R428US', 'R428FIRST', 'R428FAMILY')->get()
|
|
: collect();
|
|
$ICM428MCOL = collect($ICM428M);
|
|
$ICM428MCOL->filter(function ($value) {
|
|
return $value->R428US = trim($value->R428US);
|
|
});
|
|
$ICM428MCOL->filter(function ($value) {
|
|
return $value->R428FIRST = trim($value->R428FIRST);
|
|
});
|
|
$ICM428MCOL->filter(function ($value) {
|
|
return $value->R428FAMILY = trim($value->R428FAMILY);
|
|
});
|
|
|
|
return view('m3::liberty-theme.pages.M3ErrorsDisplay', compact('macpkr', 'shsource', 'showerrors', 'skipdates', 'ICM428MCOL'));
|
|
}
|
|
|
|
public function datatables(Request $request)
|
|
{
|
|
$this->configureDataTableRuntime();
|
|
$filters = $this->dataTableFilters($request);
|
|
$connection = config('m3.connection', 'pgsql');
|
|
|
|
if (! Schema::connection($connection)->hasTable('icm699m')) {
|
|
return Datatables::of(collect())->addIndexColumn()->make(true);
|
|
}
|
|
|
|
$profile = $this->findM3Profile($connection, $filters['SHUSER']);
|
|
$query = $this->buildM3LogQuery($connection);
|
|
$this->applyM3LogFilters($query, $filters, $profile);
|
|
|
|
return Datatables::of($query)->addIndexColumn()->make(true);
|
|
}
|
|
|
|
private function configureDataTableRuntime(): void
|
|
{
|
|
$timeout = (string) Config::get('arrays.PHPTimeOut');
|
|
ini_set('max_execution_time', $timeout);
|
|
ini_set('max_input_time', $timeout);
|
|
ini_set('default_socket_timeout', $timeout);
|
|
ini_set('memory_limit', '1024M');
|
|
}
|
|
|
|
private function dataTableFilters(Request $request): array
|
|
{
|
|
return [
|
|
'SHMACPKR' => trim((string) $request->input('SHMACPKR', '')),
|
|
'SHSOURCE' => trim((string) $request->input('SHSOURCE', '')),
|
|
'STARTDATE' => trim((string) $request->input('STARTDATE', today()->subDays(1)->format('Y-m-d'))),
|
|
'ENDDATE' => trim((string) $request->input('ENDDATE', today()->format('Y-m-d'))),
|
|
'SHOWERRORS' => trim((string) $request->input('SHOWERRORS', 'N')),
|
|
'SHUSER' => m3_user() !== '' ? Str::upper(trim(m3_user())) : '',
|
|
'SKIPDATES' => trim((string) $request->input('SKIPDATES', 'N')),
|
|
];
|
|
}
|
|
|
|
private function findM3Profile(string $connection, string $user): ?object
|
|
{
|
|
if ($user === '' || ! Schema::connection($connection)->hasTable('icm428m')) {
|
|
return null;
|
|
}
|
|
|
|
return DB::connection($connection)->table('icm428m')
|
|
->select('R428US', 'R428CO', 'R428WH')
|
|
->where('R428US', $user)
|
|
->first();
|
|
}
|
|
|
|
private function buildM3LogQuery(string $connection)
|
|
{
|
|
$query = DB::connection($connection)->table('icm699m')
|
|
->select('ICM699M.*', DB::raw("ICM699M.SHDT || ' ' || ICM699M.SHTIME AS SHDATETIME"))
|
|
->distinct();
|
|
|
|
if (Schema::connection($connection)->hasTable('icm428m')) {
|
|
$query->addSelect('ICM428M.R428FIRST', 'ICM428M.R428FAMILY')
|
|
->leftJoin('ICM428M', 'ICM699M.SHUSER', '=', 'ICM428M.R428US');
|
|
}
|
|
if (Schema::connection($connection)->hasTable('icm688m1')) {
|
|
$query->addSelect('ICM688M1.A4ONO', 'ICM688M1.A4SHSHNM')
|
|
->leftJoin('ICM688M1', 'ICM699M.SHMACPKR', '=', 'ICM688M1.A4MACPKR');
|
|
}
|
|
if (Schema::connection($connection)->hasTable('icm666m')) {
|
|
$query->addSelect('ICM666M.A1ONO', 'ICM666M.A1SHSHNM')
|
|
->leftJoin('ICM666M', 'ICM699M.SHMACPKR', '=', 'ICM666M.A1DELNO');
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
private function applyM3LogFilters($query, array $filters, ?object $profile): void
|
|
{
|
|
if ($profile !== null) {
|
|
$query->whereIn('SHCO', [trim((string) ($profile->R428CO ?? '')), ''])
|
|
->whereIn('SHWHS', [trim((string) ($profile->R428WH ?? '')), '']);
|
|
} else {
|
|
$query->whereNull('SHCO')->whereNull('SHWHS');
|
|
}
|
|
|
|
foreach (['SHMACPKR', 'SHSOURCE'] as $field) {
|
|
if ($filters[$field] !== '') {
|
|
$query->where($field, $filters[$field]);
|
|
}
|
|
}
|
|
|
|
$query->where('SHSOURCE', '!=', 'PdfStorageAPI');
|
|
if ($filters['SHMACPKR'] === '' && $filters['SKIPDATES'] === 'N') {
|
|
$query->whereBetween('SHDT', [$filters['STARTDATE'], $filters['ENDDATE']]);
|
|
}
|
|
if ($filters['SHOWERRORS'] === 'Y') {
|
|
$query->where('SHERROR', 'X');
|
|
}
|
|
$query->where('SHTYPE', 'like', '%M3%');
|
|
}
|
|
|
|
public static function update699M(Request $request)
|
|
{
|
|
// dump($request->all());
|
|
$SHMACPKR = $request->SHMACPKR != null ? trim($request->SHMACPKR) : '';
|
|
$SHSOURCE = $request->SHSOURCE != null ? trim($request->SHSOURCE) : '';
|
|
$SHCO = $request->SHCO != null ? trim($request->SHCO) : '';
|
|
$SHWHS = $request->SHWHS != null ? trim($request->SHWHS) : '';
|
|
$SHDT = $request->SHDT != null ? trim($request->SHDT) : '';
|
|
$SHTIME = $request->SHTIME != null ? trim($request->SHTIME) : '';
|
|
$SHTYPE = $request->SHTYPE != null ? trim($request->SHTYPE) : '';
|
|
$SHACKNOTE = $request->SHACKNOTE != null ? trim(substr($request->SHACKNOTE, 0, 99)) : '';
|
|
$SHUSER = m3_user() !== '' ? Str::upper(m3_user()) : '';
|
|
$CLOSESIM = $request->CLOSESIM != null ? trim($request->CLOSESIM) : 'N';
|
|
|
|
if ($CLOSESIM == 'N') {
|
|
$ICM699MU = DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
|
|
->where('SHMACPKR', $SHMACPKR)
|
|
->where('SHDT', $SHDT)
|
|
->where('SHTIME', $SHTIME)
|
|
->where('SHCO', $SHCO)
|
|
->where('SHWHS', $SHWHS)
|
|
->where('SHSOURCE', $SHSOURCE)
|
|
->where('SHERROR', 'X')
|
|
->where('SHTYPE', $SHTYPE)
|
|
->update(['SHERROR' => 'Z', 'SHACKUSER' => $SHUSER, 'SHACKDATE' => date('Y-m-d'), 'SHACKTIME' => date('H:i:s'), 'SHACKNOTE' => $SHACKNOTE]);
|
|
|
|
} else {
|
|
$ICM699MU = DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
|
|
->where('SHCO', $SHCO)
|
|
->where('SHWHS', $SHWHS)
|
|
->where('SHSOURCE', $SHSOURCE)
|
|
->where('SHERROR', 'X')
|
|
->where('SHTYPE', $SHTYPE)
|
|
->update(['SHERROR' => 'Z', 'SHACKUSER' => $SHUSER, 'SHACKDATE' => date('Y-m-d'), 'SHACKTIME' => date('H:i:s'), 'SHACKNOTE' => $SHACKNOTE]);
|
|
}
|
|
|
|
if ($SHSOURCE == 'CloseShipmentCoChargeAPI') {
|
|
// Update ICM688M1 : Change A4PICKSTS
|
|
DB::connection(config('m3.connection', 'pgsql'))->table('icm688m1')
|
|
->where('A4MACPKR', $SHMACPKR)
|
|
->where('A4CO', $SHCO)
|
|
->where('A4WHS', $SHWHS)
|
|
->update(['A4PICKSTS' => 'a']);
|
|
}
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
// Export To Excel
|
|
public static function exporttoExcel(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'));
|
|
ini_set('memory_limit', '1024M');
|
|
|
|
try {
|
|
// dd($request->all());
|
|
$SHMACPKR = $request->SHMACPKR != null ? trim($request->SHMACPKR) : '';
|
|
$SHSOURCE = $request->SHSOURCE != null ? trim($request->SHSOURCE) : '';
|
|
$STARTDATE = $request->STARTDATE != null ? trim($request->STARTDATE) : today()->subDays(1)->format('Y-m-d');
|
|
$ENDDATE = $request->ENDDATE != null ? trim($request->ENDDATE) : today()->format('Y-m-d');
|
|
$SHOWERRORS = $request->SHOWERRORS != null ? trim($request->SHOWERRORS) : 'N';
|
|
$SHUSER = m3_user() !== '' ? Str::upper(trim(m3_user())) : '';
|
|
$SKIPDATES = $request->SKIPDATES != null ? trim($request->SKIPDATES) : 'N';
|
|
// Get Logged User CO & WH
|
|
// Get users from ICM428M
|
|
$ICM428M = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
|
|
->select('R428US', 'R428CO', 'R428WH')
|
|
->where('R428US', '=', $SHUSER)
|
|
->first();
|
|
|
|
$R428US = isset($ICM428M->R428US) ? $ICM428M->R428US : '';
|
|
$USERCO = isset($ICM428M->R428CO) ? $ICM428M->R428CO : '';
|
|
$USERWH = isset($ICM428M->R428WH) ? $ICM428M->R428WH : '';
|
|
|
|
// Get users from ICM428M
|
|
$ICM428MAll = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
|
|
->select('R428US', 'R428FIRST', 'R428FAMILY')
|
|
->get();
|
|
$ICM428MCOL = collect($ICM428MAll);
|
|
$ICM428MCOL->filter(function ($value) {
|
|
return $value->R428US = trim($value->R428US);
|
|
});
|
|
$ICM428MCOL->filter(function ($value) {
|
|
return $value->R428FIRST = trim($value->R428FIRST);
|
|
});
|
|
$ICM428MCOL->filter(function ($value) {
|
|
return $value->R428FAMILY = trim($value->R428FAMILY);
|
|
});
|
|
|
|
// Merge date and time fields
|
|
$mergedDateTime = DB::raw(" ICM699M.SHDT || ' ' || ICM699M.SHTIME AS SHDATETIME");
|
|
|
|
$ICM699M = DB::connection(config('m3.connection', 'pgsql'))->table('icm699m')
|
|
->select(
|
|
DB::raw('COALESCE(ICM688M1.A4ONO, ICM666M.A1ONO) AS DELIVERY_NUM '),
|
|
'ICM699M.SHMACPKR',
|
|
DB::raw('COALESCE(ICM688M1.A4SHSHNM, ICM666M.A1SHSHNM) AS CUSTOMER '),
|
|
$mergedDateTime,
|
|
DB::raw("CASE WHEN ICM699M.SHERROR !='Z' THEN TRIM(ICM699M.SHERROR) ELSE 'Closed' END AS ERROR"),
|
|
'ICM699M.SHTYPE',
|
|
'ICM699M.SHSOURCE',
|
|
DB::raw("CASE WHEN ICM428M.R428FIRST IS NOT NULL THEN TRIM(ICM428M.R428FIRST) ||' '|| TRIM(ICM428M.R428FAMILY) ELSE TRIM(ICM699M.SHUSER) END AS USER"),
|
|
'ICM699M.SHCODE',
|
|
'ICM699M.SHCO',
|
|
'ICM699M.SHWHS',
|
|
DB::raw(" ICM699M.SHACKDATE ||' '||ICM699M.SHACKTIME AS ACKDATE"),
|
|
'ICM699M.SHACKUSER',
|
|
'ICM699M.SHINTBOX',
|
|
'ICM699M.SHACKNOTE',
|
|
DB::raw('SUBSTR(ICM699M.SHDATA, 1, 200)'),
|
|
)
|
|
->distinct()
|
|
->leftJoin('ICM428M', 'ICM699M.SHUSER', '=', 'ICM428M.R428US')
|
|
->leftJoin('ICM688M1', 'ICM699M.SHMACPKR', '=', 'ICM688M1.A4MACPKR')
|
|
->leftJoin('ICM666M', 'ICM699M.SHMACPKR', '=', 'ICM666M.A1DELNO');
|
|
|
|
// Get Records Count only
|
|
$ICM699MC = DB::connection(config('m3.connection', 'pgsql'))->table('icm699m');
|
|
|
|
if ($R428US != '') {
|
|
$ICM699M->whereIn('SHCO', [$USERCO, '']);
|
|
$ICM699M->whereIn('SHWHS', [$USERWH, '']);
|
|
|
|
$ICM699MC->whereIn('SHCO', [$USERCO, '']);
|
|
$ICM699MC->whereIn('SHWHS', [$USERWH, '']);
|
|
} else {
|
|
$ICM699M->where('SHCO', null);
|
|
$ICM699M->where('SHWHS', null);
|
|
|
|
$ICM699MC->where('SHCO', null);
|
|
$ICM699MC->where('SHWHS', null);
|
|
}
|
|
|
|
if ($SHMACPKR != '') {
|
|
$ICM699M->where('SHMACPKR', $SHMACPKR);
|
|
|
|
$ICM699MC->where('SHMACPKR', $SHMACPKR);
|
|
}
|
|
|
|
if ($SHSOURCE != '') {
|
|
$ICM699M->where('SHSOURCE', $SHSOURCE);
|
|
|
|
$ICM699MC->where('SHSOURCE', $SHSOURCE);
|
|
}
|
|
// Disable PDFStorage Search
|
|
$ICM699M->where('SHSOURCE', '!=', 'PdfStorageAPI');
|
|
|
|
$ICM699MC->where('SHSOURCE', '!=', 'PdfStorageAPI');
|
|
|
|
if ($SHMACPKR == '' && $SKIPDATES == 'N') {
|
|
$ICM699M->whereBetween('SHDT', [$STARTDATE, $ENDDATE]);
|
|
|
|
$ICM699MC->whereBetween('SHDT', [$STARTDATE, $ENDDATE]);
|
|
}
|
|
|
|
if ($SHOWERRORS == 'Y') {
|
|
$ICM699M->where('SHERROR', 'X');
|
|
|
|
$ICM699MC->where('SHERROR', 'X');
|
|
}
|
|
|
|
// Get only M3 Logs
|
|
$ICM699M->where('SHTYPE', 'like', '%M3%');
|
|
|
|
$ICM699MC->where('SHTYPE', 'like', '%M3%');
|
|
|
|
// Count records before exporting to excel : Max 20K records are allowed
|
|
$ICM699MCount = $ICM699MC->count();
|
|
// dd($ICM699MCount);
|
|
// Set Max allowed records
|
|
$maxRecords = Config::get('arrays.maxRecDL');
|
|
if ($ICM699MCount > $maxRecords) {
|
|
return redirect()->back()->withInput()->with('message', 'Maximum allowed records for Excel Download is: ' . number_format($maxRecords) .
|
|
' current records: ' . number_format($ICM699MCount) . ' , change your search criteria.');
|
|
}
|
|
|
|
// Select Records and export them to Excel
|
|
$ICM699MSel = $ICM699M->get();
|
|
$ICM699MCol = collect($ICM699MSel);
|
|
|
|
// Get Transaction Types
|
|
$transxTypes = Config::get('arrays.transxTypes');
|
|
$ICM699MCol->filter(function ($value) use ($transxTypes) {
|
|
return $value->SHSOURCE = isset($transxTypes[trim($value->SHSOURCE)]) ? $transxTypes[trim($value->SHSOURCE)] : $value->SHSOURCE;
|
|
});
|
|
|
|
// Get User Names
|
|
$ICM699MCol->filter(function ($value) use ($ICM428MCOL) {
|
|
$UserTemp = $ICM428MCOL->firstWhere('R428US', trim($value->SHACKUSER));
|
|
$SHACKUSER = isset($UserTemp->R428FIRST) ? $UserTemp->R428FIRST . ' ' . $UserTemp->R428FAMILY : trim($value->SHACKUSER);
|
|
|
|
return $value->SHACKUSER = $SHACKUSER;
|
|
});
|
|
|
|
// Prepend Header To Excel
|
|
$newItem = ['SHMACPKR' => 'Delivery Note',
|
|
'DELIVERY_NUM' => 'Order #',
|
|
'CUSTOMER' => 'Customer',
|
|
'SHDATETIME' => 'Date',
|
|
'ERROR' => 'Error',
|
|
'SHTYPE' => 'Request Type',
|
|
'SHSOURCE' => 'Source of transaction',
|
|
'USER' => 'User',
|
|
'SHCODE' => 'Request or Response Data',
|
|
'SHCO' => 'Division',
|
|
'SHWHS' => 'Warehouse',
|
|
'ACKDATE' => 'Acknowledge/Retry Date',
|
|
'SHACKUSER' => 'Acknowledged/Retried by User',
|
|
'SHINTBOX' => 'Internal Box Number',
|
|
'SHACKNOTE' => 'Note',
|
|
'SHDATA' => 'Error description',
|
|
];
|
|
$ICM699MCol->prepend($newItem);
|
|
// dd($ICM699MCol);
|
|
|
|
// Download Excel File
|
|
return app(SpreadsheetExport::class)->download($ICM699MCol, 'transactionLog_' . $STARTDATE . '-' . $ENDDATE . '.xlsx');
|
|
} catch (\Throwable $th) {
|
|
Log::error($th);
|
|
}
|
|
}
|
|
}
|