From a9378cd309a1683905cc9e31824ec70a983b0022 Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Thu, 17 Sep 2026 11:15:23 +0100 Subject: [PATCH] M3 Security Fixes for scan command --- Modules/M3/Console/WHSMetricsSaveXls.php | 230 ++++++++--------- .../Controllers/BoxPackToPalletController.php | 237 ++++++++---------- .../Controllers/M3ErrorsDispController.php | 78 +++--- 3 files changed, 233 insertions(+), 312 deletions(-) diff --git a/Modules/M3/Console/WHSMetricsSaveXls.php b/Modules/M3/Console/WHSMetricsSaveXls.php index 9b416165..3b8ea174 100644 --- a/Modules/M3/Console/WHSMetricsSaveXls.php +++ b/Modules/M3/Console/WHSMetricsSaveXls.php @@ -41,139 +41,111 @@ class WHSMetricsSaveXls extends Command * @return int */ public function handle() + { + $this->configureRuntime(); + + try { + Log::channel('dailyCron')->info('Cron...Start Save WHS Metrics to Excel'); + $company = '110'; + $warehouse = '321'; + $plant = '143'; + $file = 'WhsPerf/' . $warehouse . '/WhsPerfMetrics_' . date('Y-m-d') . '-' . $warehouse . '.xlsx'; + + Log::channel('dailyCron')->info('>>> File Name: ' . $file); + $this->info('Generate Today WHS Performance Report...'); + + [$summary, $details] = $this->reportCollections($company, $warehouse, $plant); + $this->storeReport($summary, $details, $file); + + Log::channel('dailyCron')->info('Cron...End Save Today WHS Performance Report to Excel'); + $this->info('Cron...End Save Today WHS Performance Report to Excel'); + } catch (\Throwable $th) { + Log::channel('dailyCron')->error($th); + } + + return self::SUCCESS; + } + + private function configureRuntime(): void { ini_set('max_execution_time', Config::get('arrays.PHPTimeOutCL')); ini_set('max_input_time', Config::get('arrays.PHPTimeOutCL')); ini_set('default_socket_timeout', Config::get('arrays.PHPTimeOutCL')); ini_set('memory_limit', '1024M'); - - try { - Log::channel('dailyCron')->info('Cron...Start Save WHS Metrics to Excel'); - - // Get the Calling User - $userId = $this->argument('user'); - $SHUSER = isset($userId) ? $userId : (m3_user() !== '' ? Str::upper(trim(m3_user())) : ''); - - $CO = $this->argument('co'); - $COMPANY = '110'; - $PWHS = $this->argument('whs'); - $WHS = '321'; - $PPL = $this->argument('pl'); - $PLANT = '143'; - - $date = new \DateTime; - $DateToday = $date->format('Y-m-d'); - - // Define File Name - $whsPerfFile = 'WhsPerf/' . $WHS . '/WhsPerfMetrics_' . $DateToday . '-' . $WHS . '.xlsx'; - Log::channel('dailyCron')->info('>>> File Name:'); - Log::channel('dailyCron')->info($whsPerfFile); - - $this->info('Generate Today WHS Performance Report...'); - - if ($COMPANY != '' && $WHS != '' && $PLANT != '') { - // Get Header Content - $ICM700MHeadSel = DB::connection(config('m3.connection', 'pgsql')) - ->select(" - WITH TEMPFILE AS ( - SELECT A.*,A4TPCKWGHT,A4NOPICLNS,A4PICKSTS, - CASE WHEN UPPER(A4PICKSTS)<>'A' THEN 'Open' ELSE 'Closed' END AS STATUS, - CASE WHEN A4TPCKWGHT>16000 THEN 'FTL' ELSE A4SHIPTYPE END AS A4SHIPTYPE, - (SELECT SUM(A5RSVQTY*A5SLUNPR) FROM ICM688M2 WHERE A4MACPKR=A5MACPKR) AS DOLLARS - FROM ICM700M A LEFT JOIN ICM688M1 B ON A4MACPKR=A3MACPKR - WHERE A3MSMTDATE= (SELECT MAX(A3MSMTDATE) FROM ICM700M) AND A3CO='$COMPANY' AND A3WHS='$WHS' AND A3PLT='$PLANT' - ) - SELECT A4SHIPTYPE,STATUS,SOURCE,COUNT(*) AS PICKERS, SUM(A4TPCKWGHT) AS WEIGHT,SUM(A4NOPICLNS) AS LINES,SUM(DOLLARS) AS DOLLARS - FROM TEMPFILE - GROUP BY A4SHIPTYPE,STATUS,SOURCE - ORDER BY 1,3,2 - "); - - $newItem = ['A4SHIPTYPE' => 'Method', 'STATUS' => 'Status', 'SOURCE' => 'Source', 'PICKERS' => 'Pickers', - 'WEIGHT' => 'Weight', 'LINES' => 'Lines', 'DOLLARS' => 'Dollars']; - - // Trim All values - $ICM700MHeadCol = collect($ICM700MHeadSel)->map(function ($item) { - foreach ($item as $key => $value) { - if (is_string($value)) { - $item->{$key} = trim($value); - } - } - - return $item; - }); - // Prepend Header to Collection - $ICM700MHeadCol->prepend($newItem); - - // Log::channel('dailyCron')->info(">>> Header Content"); - // Log::channel('dailyCron')->info($ICM700MHeadCol); - - // Count records before exporting to excel - $ICM700MHeadCount = $ICM700MHeadCol->count(); - - // Get Details Content - $ICM700MDetailsSel = DB::connection(config('m3.connection', 'pgsql')) - ->select(" - WITH TEMPFILE AS ( - SELECT A3CO,A3WHS,A3PLT,A3MACPKR,A4PICKCRTS,SOURCE, - (SELECT TO_TIMESTAMP(CAST(SHDT AS TEXT) || ' ' || CAST(SHTIME AS TEXT), 'YYYY-MM-DD HH24:MI:SS') FROM ICM699M WHERE SHMACPKR=A4MACPKR AND SHTYPE='Lisnr-Hold' ORDER BY SHDT DESC, SHTIME DESC FETCH FIRST 1 ROWS ONLY) AS CARRIERASSIGNED, - A4ONO,A4SHSHNM,A4SCAC,A42SSVCNAM,A4PICKSTS, - CASE WHEN UPPER(A4PICKSTS)<>'A' THEN 'Open' ELSE 'Closed' END AS STATUS, - CASE WHEN A4TPCKWGHT>16000 THEN 'FTL' ELSE A4SHIPTYPE END AS A4SHIPTYPE, - A4TPCKWGHT,A4NOPICLNS, - (SELECT SUM(A5RSVQTY*A5SLUNPR) FROM ICM688M2 WHERE A4MACPKR=A5MACPKR) AS DOLLARS - FROM ICM700M A - LEFT JOIN ICM688M1 B ON A4MACPKR=A3MACPKR - WHERE A3MSMTDATE=(SELECT MAX(A3MSMTDATE) FROM ICM700M) AND A3CO='$COMPANY' AND A3WHS='$WHS' AND A3PLT='$PLANT' - ) - SELECT * FROM TEMPFILE - "); - - $newItem2 = ['A3CO' => 'Co', 'A3WHS' => 'Whs', 'A3PLT' => 'Plt', 'A3MACPKR' => 'Delivery No', 'A4PICKCRTS' => 'Date Time Arrived in Sentinel', - 'SOURCE' => 'Source', 'CARRIERASSIGNED' => 'Date carrier Assigned', 'A4ONO' => 'Order No', 'A4SHSHNM' => 'Customer name', 'A4SCAC' => 'Carrier', - 'A42SSVCNAM' => 'Service', 'A4PICKSTS' => 'Picker Status', 'STATUS' => 'Open or Closed', 'A4SHIPTYPE' => 'Method', - 'A4TPCKWGHT' => 'Weight', 'A4NOPICLNS' => 'Lines', 'DOLLARS' => 'Dollars']; - // Trim all values - $ICM700MDetailsCol = collect($ICM700MDetailsSel)->map(function ($item) { - foreach ($item as $key => $value) { - if (is_string($value)) { - $item->{$key} = trim($value); - } - } - - return $item; - }); - - // Prepend Header - $ICM700MDetailsCol->prepend($newItem2); - - // Count records before exporting to excel - $ICM700MDetailsCount = $ICM700MDetailsCol->count(); - } else { - $ICM700MDetailsCol = collect(); - } - // Log::channel('dailyCron')->info($ICM700MDetailsCol); - - // Save Head and Details to XLS file - if ($ICM700MHeadCount > 0 && $ICM700MDetailsCount > 0) { - $this->info('Saving Today WHS Performance Report to Disk...'); - Log::channel('dailyCron')->info('Saving Today WHS Performance Report to Disk...'); - - // Store both collections in one XLSX workbook using Core's PhpSpreadsheet dependency. - app(SpreadsheetExport::class)->storeSheets([ - 'Summary' => $ICM700MHeadCol, - 'Details' => $ICM700MDetailsCol, - ], $whsPerfFile, 'publicLabels'); - - } else { - Log::channel('dailyCron')->info('Today WHS Performance Report, no records found: Nothing to save...'); - } - - Log::channel('dailyCron')->info('Cron...End Save Today WHS Performance Report to Excel'); - $this->info('Cron...End Save Today WHS Performance Report to Excel'); - - } catch (\Throwable $th) { - Log::channel('dailyCron')->error($th); - } } + + private function reportCollections(string $company, string $warehouse, string $plant): array + { + $summary = collect($this->summaryRows($company, $warehouse, $plant))->map(fn ($item) => $this->trimRow($item)); + $summary->prepend([ + 'A4SHIPTYPE' => 'Method', 'STATUS' => 'Status', 'SOURCE' => 'Source', 'PICKERS' => 'Pickers', + 'WEIGHT' => 'Weight', 'LINES' => 'Lines', 'DOLLARS' => 'Dollars', + ]); + + $details = collect($this->detailRows($company, $warehouse, $plant))->map(fn ($item) => $this->trimRow($item)); + $details->prepend([ + 'A3CO' => 'Co', 'A3WHS' => 'Whs', 'A3PLT' => 'Plt', 'A3MACPKR' => 'Delivery No', 'A4PICKCRTS' => 'Date Time Arrived in Sentinel', + 'SOURCE' => 'Source', 'CARRIERASSIGNED' => 'Date carrier Assigned', 'A4ONO' => 'Order No', 'A4SHSHNM' => 'Customer name', + 'A4SCAC' => 'Carrier', 'A42SSVCNAM' => 'Service', 'A4PICKSTS' => 'Picker Status', 'STATUS' => 'Open or Closed', + 'A4SHIPTYPE' => 'Method', 'A4TPCKWGHT' => 'Weight', 'A4NOPICLNS' => 'Lines', 'DOLLARS' => 'Dollars', + ]); + + return [$summary, $details]; + } + + private function trimRow(object $item): object + { + foreach ($item as $key => $value) { + if (is_string($value)) { + $item->{$key} = trim($value); + } + } + + return $item; + } + + private function summaryRows(string $company, string $warehouse, string $plant): array + { + return DB::connection(config('m3.connection', 'pgsql'))->select(<<'A' THEN 'Open' ELSE 'Closed' END AS STATUS, + CASE WHEN A4TPCKWGHT>16000 THEN 'FTL' ELSE A4SHIPTYPE END AS A4SHIPTYPE, + (SELECT SUM(A5RSVQTY*A5SLUNPR) FROM ICM688M2 WHERE A4MACPKR=A5MACPKR) AS DOLLARS + FROM ICM700M A LEFT JOIN ICM688M1 B ON A4MACPKR=A3MACPKR + WHERE A3MSMTDATE=(SELECT MAX(A3MSMTDATE) FROM ICM700M) AND A3CO=? AND A3WHS=? AND A3PLT=? + ) + SELECT A4SHIPTYPE,STATUS,SOURCE,COUNT(*) AS PICKERS,SUM(A4TPCKWGHT) AS WEIGHT,SUM(A4NOPICLNS) AS LINES,SUM(DOLLARS) AS DOLLARS + FROM TEMPFILE GROUP BY A4SHIPTYPE,STATUS,SOURCE ORDER BY 1,3,2 + SQL, [$company, $warehouse, $plant]); + } + + private function detailRows(string $company, string $warehouse, string $plant): array + { + return DB::connection(config('m3.connection', 'pgsql'))->select(<<'A' THEN 'Open' ELSE 'Closed' END AS STATUS, + CASE WHEN A4TPCKWGHT>16000 THEN 'FTL' ELSE A4SHIPTYPE END AS A4SHIPTYPE, + A4TPCKWGHT,A4NOPICLNS,(SELECT SUM(A5RSVQTY*A5SLUNPR) FROM ICM688M2 WHERE A4MACPKR=A5MACPKR) AS DOLLARS + FROM ICM700M A LEFT JOIN ICM688M1 B ON A4MACPKR=A3MACPKR + WHERE A3MSMTDATE=(SELECT MAX(A3MSMTDATE) FROM ICM700M) AND A3CO=? AND A3WHS=? AND A3PLT=? + ) SELECT * FROM TEMPFILE + SQL, [$company, $warehouse, $plant]); + } + + private function storeReport($summary, $details, string $file): void + { + if ($summary->isEmpty() || $details->isEmpty()) { + Log::channel('dailyCron')->info('Today WHS Performance Report, no records found: Nothing to save...'); + return; + } + + $this->info('Saving Today WHS Performance Report to Disk...'); + Log::channel('dailyCron')->info('Saving Today WHS Performance Report to Disk...'); + app(SpreadsheetExport::class)->storeSheets(['Summary' => $summary, 'Details' => $details], $file, 'publicLabels'); + } + } diff --git a/Modules/M3/Http/Controllers/BoxPackToPalletController.php b/Modules/M3/Http/Controllers/BoxPackToPalletController.php index 03f7e17f..f6322d0a 100644 --- a/Modules/M3/Http/Controllers/BoxPackToPalletController.php +++ b/Modules/M3/Http/Controllers/BoxPackToPalletController.php @@ -69,153 +69,116 @@ class BoxPackToPalletController extends Controller // Scan Boxes public static function scanBoxes(Request $request) { - $R659PALT = $request->R659PALT ?? abort(400, 'Pallet Not Found'); - $R659BOX = trim($request->R659BOX) ?? abort(400, 'Box Not Found'); - $SHUSER = m3_user() !== '' ? Str::upper(m3_user()) : 'WebUser'; - $userWhsCoPlant = self::getUserWhsCoPlant(); + $pallet = $request->R659PALT ?? abort(400, 'Pallet Not Found'); + $box = trim((string) ($request->R659BOX ?? '')); + abort_if($box === '', 400, 'Box Not Found'); - $boxMessage = ''; + $user = m3_user() !== '' ? Str::upper(m3_user()) : 'WebUser'; + $profile = self::getUserWhsCoPlant(); + $validation = self::validateScannedBox($pallet, $box, $profile); - // Step 1: Check for duplicate — same pallet + box combo - $exists = DB::connection(config('m3.connection', 'pgsql')) - ->table('icm659m') - ->select('R659PALT') - ->where('R659PALT', $R659PALT) - ->where('R659BOX', $R659BOX) - ->where('R659CO', trim($userWhsCoPlant['USERCO'])) - ->where('R659WHS', trim($userWhsCoPlant['USERWHS'])) - ->where('R659PLANT', trim($userWhsCoPlant['USERPL'])) - ->first(); - - if ($exists) { - $boxMessage = "• Box {$R659BOX} is already assigned to Pallet {$exists->R659PALT}."; + if ($validation !== '') { + return response()->json(['success' => false, 'message' => $validation], 409); } - // Step 2: Check if Box exists in ICM695M1 (with carrier + conveyor joined) - $ICM695M1Sel = DB::connection(config('m3.connection', 'pgsql')) - ->table(self::CARTON_TABLE_ALIAS) - ->select( - 'c.AYINTBOX', - 'c.AYSPR15A01', - 'c.AYTYPE', - 'c.AYMACPKR', - 'cv.A4APR8N001', - self::CONVEYOR_SELECT, - ) - ->leftJoin(self::CONVEYOR_VALUE_ALIAS, 'c.AYMACPKR', '=', 'cv.A4MACPKR') - ->leftJoin(self::CONVEYOR_TABLE_ALIAS, 'cv.A4APR8N001', '=', 'cnv.R652SEQ') - ->where('c.AYINTBOX', $R659BOX) - ->where('c.AYCO', trim($userWhsCoPlant['USERCO'])) - ->where('c.AYWHS', trim($userWhsCoPlant['USERWHS'])) - ->where('c.AYPLANT', trim($userWhsCoPlant['USERPL'])) - ->first(); - - $boxExistInICM695M = isset($ICM695M1Sel->AYINTBOX); - $boxisPalletICM695M = isset($ICM695M1Sel->AYTYPE) && $ICM695M1Sel->AYTYPE != 'BX'; - - if (! $boxExistInICM695M) { - $br = $boxMessage == '' ? '' : '
'; - $boxMessage .= $br . "• Box {$R659BOX} does not exist in ICM695M1 or it belongs to another Company."; - } - - if ($boxExistInICM695M && $boxisPalletICM695M) { - $br = $boxMessage == '' ? '' : '
'; - $boxMessage .= $br . "• Scanned barcode {$R659BOX} is not a Box."; - } - - // Step 3: Check that all boxes on the pallet share the same Carrier and Conveyor - if ($boxExistInICM695M && ! $boxisPalletICM695M && ! $exists) { - $firstPalletBox = DB::connection(config('m3.connection', 'pgsql')) - ->table(self::PALLET_TABLE_ALIAS) - ->select( - 'c.AYSPR15A01', - self::CONVEYOR_SELECT, - ) - ->leftJoin(self::CARTON_TABLE_ALIAS, 'p.R659BOX', '=', 'c.AYINTBOX') - ->leftJoin(self::CONVEYOR_VALUE_ALIAS, 'c.AYMACPKR', '=', 'cv.A4MACPKR') - ->leftJoin(self::CONVEYOR_TABLE_ALIAS, 'cv.A4APR8N001', '=', 'cnv.R652SEQ') - ->where('p.R659PALT', $R659PALT) - ->where('p.R659CO', trim($userWhsCoPlant['USERCO'])) - ->where('p.R659WHS', trim($userWhsCoPlant['USERWHS'])) - ->where('p.R659PLANT', trim($userWhsCoPlant['USERPL'])) - ->first(); - - if ($firstPalletBox) { - $incomingCarrier = trim($ICM695M1Sel->AYSPR15A01 ?? ''); - $incomingConveyor = trim($ICM695M1Sel->CONVEYOR_NAME ?? ''); - $palletCarrier = trim($firstPalletBox->AYSPR15A01 ?? ''); - $palletConveyor = trim($firstPalletBox->CONVEYOR_NAME ?? ''); - - if ($incomingCarrier !== $palletCarrier) { - $br = $boxMessage == '' ? '' : '
'; - $boxMessage .= $br . "• Box {$R659BOX} has a different Carrier ({$incomingCarrier}) than the boxes already on Pallet {$R659PALT} ({$palletCarrier})."; - } - - if ($incomingConveyor !== $palletConveyor) { - $br = $boxMessage == '' ? '' : '
'; - $boxMessage .= $br . "• Box {$R659BOX} has a different Conveyor ({$incomingConveyor}) than the boxes already on Pallet {$R659PALT} ({$palletConveyor})."; - } - } - } - - if ($exists || ! $boxExistInICM695M || $boxisPalletICM695M || $boxMessage !== '') { - return response()->json([ - 'success' => false, - 'message' => $boxMessage, - ], 409); - } - - // Step 4: Insert new box record - DB::connection(config('m3.connection', 'pgsql')) - ->table('icm659m') - ->insert([ - 'R659PALT' => $R659PALT, - 'R659BOX' => $R659BOX, - 'R659LOADED' => '', - 'R659USER' => $SHUSER, - 'R659DATE' => date('Y-m-d'), - 'R659TIME' => date('H:i:s'), - 'R659CO' => trim($userWhsCoPlant['USERCO']), - 'R659WHS' => trim($userWhsCoPlant['USERWHS']), - 'R659PLANT' => trim($userWhsCoPlant['USERPL']), - ]); - - // Step 5: Return the full refreshed box list - $boxes = DB::connection(config('m3.connection', 'pgsql'))->table(self::PALLET_TABLE_ALIAS) - ->select( - 'p.R659PALT', - 'p.R659BOX', - 'p.R659LOADED', - 'p.R659USER', - 'p.R659DATE', - 'p.R659TIME', - 'c.AYMACPKR', - 'c.AYSPR15A01', - 'cv.A4APR8N001', - 'cv.A4SHSHCT', - self::CONVEYOR_SELECT, - ) - ->leftJoin(self::CARTON_TABLE_ALIAS, 'p.R659BOX', '=', 'c.AYINTBOX') - ->leftJoin(self::CONVEYOR_VALUE_ALIAS, 'c.AYMACPKR', '=', 'cv.A4MACPKR') - ->leftJoin(self::CONVEYOR_TABLE_ALIAS, 'cv.A4APR8N001', '=', 'cnv.R652SEQ') - ->where('p.R659PALT', $R659PALT) - ->where('p.R659CO', trim($userWhsCoPlant['USERCO'])) - ->where('p.R659WHS', trim($userWhsCoPlant['USERWHS'])) - ->where('p.R659PLANT', trim($userWhsCoPlant['USERPL'])) - ->orderBy('p.R659DATE', 'desc') - ->orderBy('p.R659TIME', 'desc') - ->distinct() - ->get() - ->unique('R659BOX') - ->values(); + self::insertScannedBox($pallet, $box, $user, $profile); return response()->json([ 'success' => true, - 'message' => "• Box {$R659BOX} added to Pallet {$R659PALT}.", - 'data' => $boxes, + 'message' => "• Box {$box} added to Pallet {$pallet}.", + 'data' => self::boxesForPallet($pallet, $profile), ]); } + private static function validateScannedBox($pallet, string $box, array $profile): string + { + $messages = []; + $existing = self::findExistingBox($pallet, $box, $profile); + if ($existing) { + $messages[] = "• Box {$box} is already assigned to Pallet {$existing->R659PALT}."; + } + + $boxRecord = self::findCarton($box, $profile); + if (! isset($boxRecord->AYINTBOX)) { + $messages[] = "• Box {$box} does not exist in ICM695M1 or it belongs to another Company."; + } elseif (($boxRecord->AYTYPE ?? '') !== 'BX') { + $messages[] = "• Scanned barcode {$box} is not a Box."; + } elseif (! $existing) { + $messages = array_merge($messages, self::palletCompatibilityMessages($pallet, $box, $boxRecord, $profile)); + } + + return implode('
', $messages); + } + + private static function findExistingBox($pallet, string $box, array $profile): ?object + { + return DB::connection(config('m3.connection', 'pgsql'))->table('icm659m') + ->select('R659PALT')->where('R659PALT', $pallet)->where('R659BOX', $box) + ->where('R659CO', trim($profile['USERCO']))->where('R659WHS', trim($profile['USERWHS'])) + ->where('R659PLANT', trim($profile['USERPL']))->first(); + } + + private static function findCarton(string $box, array $profile): ?object + { + return DB::connection(config('m3.connection', 'pgsql'))->table(self::CARTON_TABLE_ALIAS) + ->select('c.AYINTBOX', 'c.AYSPR15A01', 'c.AYTYPE', 'c.AYMACPKR', 'cv.A4APR8N001', self::CONVEYOR_SELECT) + ->leftJoin(self::CONVEYOR_VALUE_ALIAS, 'c.AYMACPKR', '=', 'cv.A4MACPKR') + ->leftJoin(self::CONVEYOR_TABLE_ALIAS, 'cv.A4APR8N001', '=', 'cnv.R652SEQ') + ->where('c.AYINTBOX', $box)->where('c.AYCO', trim($profile['USERCO'])) + ->where('c.AYWHS', trim($profile['USERWHS']))->where('c.AYPLANT', trim($profile['USERPL']))->first(); + } + + private static function palletCompatibilityMessages($pallet, string $box, object $incoming, array $profile): array + { + $first = DB::connection(config('m3.connection', 'pgsql'))->table(self::PALLET_TABLE_ALIAS) + ->select('c.AYSPR15A01', self::CONVEYOR_SELECT) + ->leftJoin(self::CARTON_TABLE_ALIAS, 'p.R659BOX', '=', 'c.AYINTBOX') + ->leftJoin(self::CONVEYOR_VALUE_ALIAS, 'c.AYMACPKR', '=', 'cv.A4MACPKR') + ->leftJoin(self::CONVEYOR_TABLE_ALIAS, 'cv.A4APR8N001', '=', 'cnv.R652SEQ') + ->where('p.R659PALT', $pallet)->where('p.R659CO', trim($profile['USERCO'])) + ->where('p.R659WHS', trim($profile['USERWHS']))->where('p.R659PLANT', trim($profile['USERPL']))->first(); + + if (! $first) { + return []; + } + + $messages = []; + $incomingCarrier = trim($incoming->AYSPR15A01 ?? ''); + $incomingConveyor = trim($incoming->CONVEYOR_NAME ?? ''); + $palletCarrier = trim($first->AYSPR15A01 ?? ''); + $palletConveyor = trim($first->CONVEYOR_NAME ?? ''); + if ($incomingCarrier !== $palletCarrier) { + $messages[] = "• Box {$box} has a different Carrier ({$incomingCarrier}) than the boxes already on Pallet {$pallet} ({$palletCarrier})."; + } + if ($incomingConveyor !== $palletConveyor) { + $messages[] = "• Box {$box} has a different Conveyor ({$incomingConveyor}) than the boxes already on Pallet {$pallet} ({$palletConveyor})."; + } + + return $messages; + } + + private static function insertScannedBox($pallet, string $box, string $user, array $profile): void + { + DB::connection(config('m3.connection', 'pgsql'))->table('icm659m')->insert([ + 'R659PALT' => $pallet, 'R659BOX' => $box, 'R659LOADED' => '', 'R659USER' => $user, + 'R659DATE' => date('Y-m-d'), 'R659TIME' => date('H:i:s'), 'R659CO' => trim($profile['USERCO']), + 'R659WHS' => trim($profile['USERWHS']), 'R659PLANT' => trim($profile['USERPL']), + ]); + } + + private static function boxesForPallet($pallet, array $profile) + { + return DB::connection(config('m3.connection', 'pgsql'))->table(self::PALLET_TABLE_ALIAS) + ->select('p.R659PALT', 'p.R659BOX', 'p.R659LOADED', 'p.R659USER', 'p.R659DATE', 'p.R659TIME', + 'c.AYMACPKR', 'c.AYSPR15A01', 'cv.A4APR8N001', 'cv.A4SHSHCT', self::CONVEYOR_SELECT) + ->leftJoin(self::CARTON_TABLE_ALIAS, 'p.R659BOX', '=', 'c.AYINTBOX') + ->leftJoin(self::CONVEYOR_VALUE_ALIAS, 'c.AYMACPKR', '=', 'cv.A4MACPKR') + ->leftJoin(self::CONVEYOR_TABLE_ALIAS, 'cv.A4APR8N001', '=', 'cnv.R652SEQ') + ->where('p.R659PALT', $pallet)->where('p.R659CO', trim($profile['USERCO'])) + ->where('p.R659WHS', trim($profile['USERWHS']))->where('p.R659PLANT', trim($profile['USERPL'])) + ->orderBy('p.R659DATE', 'desc')->orderBy('p.R659TIME', 'desc')->distinct()->get()->unique('R659BOX')->values(); + } + // Delete Box public static function deleteBox(Request $request) { diff --git a/Modules/M3/Http/Controllers/M3ErrorsDispController.php b/Modules/M3/Http/Controllers/M3ErrorsDispController.php index 538e0575..b442b978 100644 --- a/Modules/M3/Http/Controllers/M3ErrorsDispController.php +++ b/Modules/M3/Http/Controllers/M3ErrorsDispController.php @@ -256,52 +256,11 @@ class M3ErrorsDispController extends Controller // 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%'); + self::applyExportFilters($ICM699M, $ICM699MC, [ + 'profileUser' => $R428US, 'company' => $USERCO, 'warehouse' => $USERWH, + 'manifest' => $SHMACPKR, 'source' => $SHSOURCE, 'startDate' => $STARTDATE, + 'endDate' => $ENDDATE, 'skipDates' => $SKIPDATES, 'showErrors' => $SHOWERRORS, + ]); // Count records before exporting to excel : Max 20K records are allowed $ICM699MCount = $ICM699MC->count(); @@ -357,4 +316,31 @@ class M3ErrorsDispController extends Controller Log::error($th); } } + private static function applyExportFilters($query, $countQuery, array $filters): void + { + $queries = [$query, $countQuery]; + foreach ($queries as $builder) { + if ($filters['profileUser'] !== '') { + $builder->whereIn('SHCO', [$filters['company'], ''])->whereIn('SHWHS', [$filters['warehouse'], '']); + } else { + $builder->whereNull('SHCO')->whereNull('SHWHS'); + } + + if ($filters['manifest'] !== '') { + $builder->where('SHMACPKR', $filters['manifest']); + } + if ($filters['source'] !== '') { + $builder->where('SHSOURCE', $filters['source']); + } + $builder->where('SHSOURCE', '!=', 'PdfStorageAPI'); + if ($filters['manifest'] === '' && $filters['skipDates'] === 'N') { + $builder->whereBetween('SHDT', [$filters['startDate'], $filters['endDate']]); + } + if ($filters['showErrors'] === 'Y') { + $builder->where('SHERROR', 'X'); + } + $builder->where('SHTYPE', 'like', '%M3%'); + } + } + }