Files
WMS/Modules/M3/Http/Controllers/BoxPackToPalletController.php
T
2026-09-16 14:28:17 +01:00

315 lines
12 KiB
PHP

<?php
namespace Modules\M3\Http\Controllers;
use Modules\M3\Http\Controllers\Controller;
use DB;
use Config;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Str;
class BoxPackToPalletController extends Controller
{
public static function index(Request $request)
{
return view('m3::liberty-theme.pages.BoxPackToPallet');
}
# Load Existing Boxes from ICM659M
public static function getBoxes(Request $request)
{
$R659PALT = $request->R659PALT ?? abort(404, 'Manifest Not Found');
$userWhsCoPlant = self::getUserWhsCoPlant();
// dump($userWhsCoPlant);
$boxes = DB::connection(config('m3.connection', 'pgsql'))->table('ICM659M as p')
->select(
'p.R659PALT',
'p.R659BOX',
'p.R659LOADED',
'p.R659USER',
'p.R659DATE',
'p.R659TIME',
'c.AYMACPKR',
'c.AYSPR15A01',
'cv.A4APR8N001',
'cv.A4SHSHCT',
'cnv.R652SORTK as CONVEYOR_NAME'
)
->leftJoin('ICM695M1 as c', 'p.R659BOX', '=', 'c.AYINTBOX')
->leftJoin('ICM688M1 as cv', 'c.AYMACPKR', '=', 'cv.A4MACPKR')
->leftJoin('ICM652M as cnv', '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();
// dump($boxes);
if ($request->ajax()) {
return response()->json([
'success' => true,
'data' => $boxes,
]);
}
$palletHeader = $boxes->first();
// dump($boxes);
return view('m3::liberty-theme.pages.BoxPackToPalletScan', compact('boxes', 'palletHeader', 'R659PALT'));
}
# 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();
$boxMessage = '';
# 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 = "&bull; Box {$R659BOX} is already assigned to Pallet {$exists->R659PALT}.";
}
# Step 2: Check if Box exists in ICM695M1 (with carrier + conveyor joined)
$ICM695M1Sel = DB::connection(config('m3.connection', 'pgsql'))
->table('ICM695M1 as c')
->select(
'c.AYINTBOX',
'c.AYSPR15A01',
'c.AYTYPE',
'c.AYMACPKR',
'cv.A4APR8N001',
'cnv.R652SORTK as CONVEYOR_NAME'
)
->leftJoin('ICM688M1 as cv', 'c.AYMACPKR', '=', 'cv.A4MACPKR')
->leftJoin('ICM652M as cnv', '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 == '' ? '' : '<br>';
$boxMessage .= $br . "&bull; Box {$R659BOX} does not exist in ICM695M1 or it belongs to another Company.";
}
if ($boxExistInICM695M && $boxisPalletICM695M) {
$br = $boxMessage == '' ? '' : '<br>';
$boxMessage .= $br . "&bull; 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('ICM659M as p')
->select(
'c.AYSPR15A01',
'cnv.R652SORTK as CONVEYOR_NAME'
)
->leftJoin('ICM695M1 as c', 'p.R659BOX', '=', 'c.AYINTBOX')
->leftJoin('ICM688M1 as cv', 'c.AYMACPKR', '=', 'cv.A4MACPKR')
->leftJoin('ICM652M as cnv', '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 == '' ? '' : '<br>';
$boxMessage .= $br . "&bull; Box {$R659BOX} has a different Carrier ({$incomingCarrier}) than the boxes already on Pallet {$R659PALT} ({$palletCarrier}).";
}
if ($incomingConveyor !== $palletConveyor) {
$br = $boxMessage == '' ? '' : '<br>';
$boxMessage .= $br . "&bull; 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('ICM659M as p')
->select(
'p.R659PALT',
'p.R659BOX',
'p.R659LOADED',
'p.R659USER',
'p.R659DATE',
'p.R659TIME',
'c.AYMACPKR',
'c.AYSPR15A01',
'cv.A4APR8N001',
'cv.A4SHSHCT',
'cnv.R652SORTK as CONVEYOR_NAME'
)
->leftJoin('ICM695M1 as c', 'p.R659BOX', '=', 'c.AYINTBOX')
->leftJoin('ICM688M1 as cv', 'c.AYMACPKR', '=', 'cv.A4MACPKR')
->leftJoin('ICM652M as cnv', '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();
return response()->json([
'success' => true,
'message' => "&bull; Box {$R659BOX} added to Pallet {$R659PALT}.",
'data' => $boxes,
]);
}
# Delete Box
public static function deleteBox(Request $request)
{
$R659PALT = $request->R659PALT ?? abort(400, 'Pallet Not Found');
$R659BOX = trim($request->R659BOX) ?? abort(400, 'Box Not Found');
$userWhsCoPlant = self::getUserWhsCoPlant();
# Guard: only allow delete if R659LOADED is empty
$record = DB::connection(config('m3.connection', 'pgsql'))
->table('icm659m')
->where('R659PALT', $R659PALT)
->where('R659BOX', $R659BOX)
->first();
if (!$record) {
return response()->json([
'success' => false,
'message' => "Box {$R659BOX} not found on Pallet {$R659PALT}.",
], 404);
}
if (!empty(trim($record->R659LOADED))) {
return response()->json([
'success' => false,
'message' => "Box {$R659BOX} is already loaded and cannot be removed.",
], 403);
}
DB::connection(config('m3.connection', 'pgsql'))
->table('icm659m')
->where('R659PALT', $R659PALT)
->where('R659BOX', $R659BOX)
->delete();
# Return refreshed list
$boxes = DB::connection(config('m3.connection', 'pgsql'))->table('ICM659M as p')
->select(
'p.R659PALT',
'p.R659BOX',
'p.R659LOADED',
'p.R659USER',
'p.R659DATE',
'p.R659TIME',
'c.AYMACPKR',
'c.AYSPR15A01',
'cv.A4APR8N001',
'cv.A4SHSHCT',
'cnv.R652SORTK as CONVEYOR_NAME'
)
->leftJoin('ICM695M1 as c', 'p.R659BOX', '=', 'c.AYINTBOX')
->leftJoin('ICM688M1 as cv', 'c.AYMACPKR', '=', 'cv.A4MACPKR')
->leftJoin('ICM652M as cnv', '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();
return response()->json([
'success' => true,
'message' => "Box {$R659BOX} removed from Pallet {$R659PALT}.",
'data' => $boxes,
]);
}
# Get Logged User CO/WHS/PLANT
public static function getUserWhsCoPlant()
{
$SHUSER = m3_user() !== '' ? Str::upper(trim(m3_user())) : '';
# Get users from 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 : '';
$USERCO = isset($ICM428M->R428CO) ? $ICM428M->R428CO : '';
$USERWH = isset($ICM428M->R428WH) ? $ICM428M->R428WH : '';
$R428PL = isset($ICM428M->R428PL) ? $ICM428M->R428PL : '';
$userWhsCoPlant = [
"R428US" => $R428US,
"USERCO" => $USERCO,
"USERWHS" => $USERWH,
"USERPL" => $R428PL,
];
return $userWhsCoPlant;
}
}