diff --git a/app/Filament/Exports/WorkGroupMasterExporter.php b/app/Filament/Exports/WorkGroupMasterExporter.php index 053cd2d..61fd21e 100644 --- a/app/Filament/Exports/WorkGroupMasterExporter.php +++ b/app/Filament/Exports/WorkGroupMasterExporter.php @@ -25,15 +25,15 @@ class WorkGroupMasterExporter extends Exporter ExportColumn::make('plant.code') ->label('PLANT CODE'), ExportColumn::make('name') - ->label('WORK GROUP NAME'), + ->label('GROUP WORK CENTER'), ExportColumn::make('description') - ->label('WORK GROUP DESCRIPTION'), + ->label('DESCRIPTION'), ExportColumn::make('operation_number') ->label('OPERATION NUMBER'), - ExportColumn::make('created_by') - ->label('CREATED BY'), ExportColumn::make('created_at') ->label('CREATED AT'), + ExportColumn::make('created_by') + ->label('CREATED BY'), ExportColumn::make('updated_at') ->label('UPDATED AT'), ExportColumn::make('deleted_at') diff --git a/app/Filament/Imports/WorkGroupMasterImporter.php b/app/Filament/Imports/WorkGroupMasterImporter.php index e0d417b..0581dd8 100644 --- a/app/Filament/Imports/WorkGroupMasterImporter.php +++ b/app/Filament/Imports/WorkGroupMasterImporter.php @@ -27,15 +27,15 @@ class WorkGroupMasterImporter extends Importer ->rules(['required']), ImportColumn::make('name') ->requiredMapping() - ->exampleHeader('Work Group Name') + ->exampleHeader('Group Work Center') ->example('RMGCEABC') - ->label('Work Group Name') + ->label('Group Work Center') ->rules(['required']), ImportColumn::make('description') ->requiredMapping() - ->exampleHeader('Work Group Description') + ->exampleHeader('Description') ->example('Testing Model 1') - ->label('Work Group Description') + ->label('Description') ->rules(['required']), ImportColumn::make('operation_number') ->requiredMapping() @@ -60,85 +60,92 @@ class WorkGroupMasterImporter extends Importer // ]); $warnMsg = []; $plantCod = $this->data['plant']; + $createdBy = $this->data['created_by']; + $groupWorkCenter = $this->data['name']; + $desc = trim($this->data['description']); + $opNo = $this->data['operation_number']; $plantId = null; - if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { - $warnMsg[] = 'Invalid plant code found'; + if (! $plantCod || $plantCod == null || $plantCod == '') { + $warnMsg[] = "Plant code can't be empty!"; + } elseif (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { + $warnMsg[] = "Invalid plant code '{$plantCod}' found!"; } else { $plant = Plant::where('code', $plantCod)->first(); if (! $plant) { - $warnMsg[] = 'Plant not found'; + $warnMsg[] = "Plant code '{$plantCod}' not found!"; } else { $plantId = $plant->id; } } - if (Str::length($this->data['name']) <= 0) { // || !ctype_alnum($this->data['description']) - $warnMsg[] = 'Invalid work group name found'; + if (Str::length($groupWorkCenter) <= 0) { // || !ctype_alnum($desc) + $warnMsg[] = 'Invalid group work center found!'; } - if (Str::length(trim($this->data['description'])) <= 0) { - $warnMsg[] = 'Invalid work group description found'; + if (Str::length(trim($desc)) <= 0) { + $warnMsg[] = 'Invalid description found!'; } - $desc = trim($this->data['description']); - if (Str::length($desc) > 44) { - $warnMsg[] = ' work group description should be less than 44 digits.'; + $warnMsg[] = 'Description should be less than 44 digits.'; } - if (Str::length($this->data['operation_number']) <= 0) { - $warnMsg[] = 'Invalid operation number found'; + if (Str::length($opNo) <= 0) { + $warnMsg[] = 'Invalid operation number found!'; } - if (! is_numeric($this->data['operation_number'])) { - $warnMsg[] = 'Invalid operation number found must be numeric'; + if (! is_numeric($opNo)) { + $warnMsg[] = 'Operation number must be numeric values only.'; } - $user = User::where('name', $this->data['created_by'])->first(); + $user = User::where('name', $createdBy)->first(); if (! $user) { - $warnMsg[] = 'Operator ID not found'; + $warnMsg[] = "Operator ID '{$createdBy}' not found!"; } if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); } else { // Check (plant_id, name) - $existingByName = WorkGroupMaster::where('plant_id', $plantId) - ->where('name', $this->data['name']) - ->first(); + // $existingByName = WorkGroupMaster::where('plant_id', $plantId) + // ->where('name', $groupWorkCenter) + // ->first(); - if ($existingByName) { - throw new RowImportFailedException('Work group name already exists for this plant!'); - } + // if ($existingByName) { + // throw new RowImportFailedException('Group work center already exists for this plant!'); + // } // Check (plant_id, operation_number) - $existingByOpNum = WorkGroupMaster::where('plant_id', $plantId) - ->where('operation_number', $this->data['operation_number']) - ->where('name', $this->data['name']) - ->first(); + // $existingByOpNum = WorkGroupMaster::where('plant_id', $plantId) + // ->where('operation_number', $opNo) + // ->where('name', $groupWorkCenter) + // ->first(); - if ($existingByOpNum) { - throw new RowImportFailedException('Operation number already exists for this plant!'); - } + // if ($existingByOpNum) { + // throw new RowImportFailedException('Operation number already exists for this plant!'); + // } // Check (plant_id) - $existingByOperator = WorkGroupMaster::where('plant_id', $plantId) - ->where('name', $this->data['name']) + $existingByOperator = WorkGroupMaster::whereNot('plant_id', $plantId) + ->where('name', $groupWorkCenter) ->first(); if ($existingByOperator) { - throw new RowImportFailedException('Already work group name assigned to another plant!'); + throw new RowImportFailedException('Already group work center assigned to another plant!'); } } WorkGroupMaster::updateOrCreate([ 'plant_id' => $plantId, - 'name' => $this->data['name'], - 'description' => $this->data['description'], - 'operation_number' => $this->data['operation_number'], - 'created_by' => $this->data['created_by'], - ]); + 'name' => $groupWorkCenter, + ], + [ + 'description' => $desc, + 'operation_number' => $opNo, + 'created_by' => $createdBy, + ] + ); return null; diff --git a/app/Filament/Resources/InvoiceValidationResource.php b/app/Filament/Resources/InvoiceValidationResource.php index f0ea02d..eab23e1 100644 --- a/app/Filament/Resources/InvoiceValidationResource.php +++ b/app/Filament/Resources/InvoiceValidationResource.php @@ -380,6 +380,7 @@ class InvoiceValidationResource extends Resource return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) + ->searchable() ->label('Select Plant') ->required() ->default(function () { @@ -730,6 +731,7 @@ class InvoiceValidationResource extends Resource return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) + ->searchable() ->label('Select Plant') ->required() ->default(function () { diff --git a/app/Filament/Resources/WeightValidationResource.php b/app/Filament/Resources/WeightValidationResource.php index 16ed82a..94798e6 100644 --- a/app/Filament/Resources/WeightValidationResource.php +++ b/app/Filament/Resources/WeightValidationResource.php @@ -13,6 +13,7 @@ use Filament\Facades\Filament; use Filament\Forms; use Filament\Forms\Components\DateTimePicker; use Filament\Forms\Components\FileUpload; +use Filament\Forms\Components\Section; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; @@ -42,41 +43,83 @@ class WeightValidationResource extends Resource { return $form ->schema([ - Forms\Components\Select::make('plant_id') - ->relationship('plant', 'name') - ->options(function (callable $get) { - $userHas = Filament::auth()->user()->plant_id; + Section::make('') + ->schema([ + Forms\Components\Select::make('plant_id') + ->label('Plant Name') + ->relationship('plant', 'name') + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); - }) - ->required(), - Forms\Components\Select::make('item_id') - ->relationship('item', 'code') - ->label('Item Code') - ->searchable() - ->required(), - Forms\Components\TextInput::make('obd_number') - ->label('OBD Number') - ->required(), - Forms\Components\TextInput::make('line_number') - ->label('Line Number') - ->required(), - Forms\Components\TextInput::make('batch_number') - ->label('Batch Number') - ->required(), - Forms\Components\TextInput::make('obd_weight') - ->label('Actual Weight') - ->required(), - Forms\Components\TextInput::make('vehicle_number') - ->label('Vehicle Number'), - Forms\Components\TextInput::make('heat_number') - ->label('Heat Number'), - Forms\Components\TextInput::make('bundle_number') - ->label('Bundle Number'), - Forms\Components\TextInput::make('picked_weight') - ->label('Picked Weight'), - Forms\Components\TextInput::make('scanned_by') - ->label('Scanned By'), + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + }) + ->columnSpan(1) + ->required(), + Forms\Components\Select::make('item_id') + ->relationship('item', 'code') + ->label('Item Code') + ->searchable() + ->columnSpan(1) + ->required(), + Forms\Components\TextInput::make('obd_number') + ->label('OBD Number') + ->minLength(8) + ->placeholder('Scan the valid OBD Number') + ->columnSpan(1) + ->required(), + Forms\Components\TextInput::make('line_number') + ->label('Line Number') + ->minLength(1) + ->integer() + ->columnSpan(1) + ->placeholder('Scan the valid Line Number') + ->required(), + Forms\Components\TextInput::make('batch_number') + ->label('Batch Number') + ->minLength(8) + ->numeric() + ->columnSpan(1) + ->placeholder('Scan the valid Batch Number') + ->required(), + Forms\Components\TextInput::make('obd_weight') + ->label('Actual Weight') + ->minLength(1) + ->numeric() + ->columnSpan(1) + ->placeholder('Scan the valid Actual Weight') + ->required(), + Forms\Components\TextInput::make('vehicle_number') + ->label('Vehicle Number') + ->minLength(10) + ->columnSpan(1) + ->placeholder('Scan the valid Vehicle Number'), + Forms\Components\TextInput::make('heat_number') + ->label('Heat Number') + ->minLength(4) + ->columnSpan(1) + ->placeholder('Scan the valid Heat Number'), + Forms\Components\TextInput::make('bundle_number') + ->label('Bundle Number') + ->minLength(1) + ->numeric() + ->columnSpan(1) + ->placeholder('Scan the valid Bundle Number'), + Forms\Components\TextInput::make('picked_weight') + ->label('Picked Weight') + ->minLength(1) + ->numeric() + ->columnSpan(1) + ->placeholder('Scan the valid Picked Weight'), + Forms\Components\TextInput::make('scanned_by') + ->label('Scanned By') + ->minLength(3) + ->columnSpan(1) + ->placeholder('Scan the valid Scanned By'), + Forms\Components\TextInput::make('id') + ->hidden() + ->readOnly(), + ]) + ->columns(['default' => 1, 'sm' => 2]), ]); } @@ -98,7 +141,7 @@ class WeightValidationResource extends Resource return ($currentPage - 1) * $perPage + $rowLoop->iteration; }), Tables\Columns\TextColumn::make('plant.name') - ->label('Plant') + ->label('Plant Name') ->alignCenter() ->sortable() ->searchable(), @@ -160,7 +203,7 @@ class WeightValidationResource extends Resource ->label('Advanced Filters') ->form([ Select::make('Plant') - ->label('Select Plant') + ->label('Search by Plant Name') ->nullable() // ->options(function () { // return Plant::pluck('name', 'id'); @@ -168,25 +211,24 @@ class WeightValidationResource extends Resource ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + if ($userHas && strlen($userHas) > 0) { + return Plant::where('id', $userHas)->pluck('name', 'id')->toArray(); + } else { + return Plant::whereHas('weightValidations', function ($query) { + $query->whereNotNull('id'); + })->orderBy('code')->pluck('name', 'id')->toArray(); + } }) - ->reactive(), - // ->afterStateUpdated(function ($state, callable $set, callable $get) { - // $set('sticker_master_id', null); - // $set('sap_msg_status', null); - // }), + ->searchable() + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get): void { + $set('Item Code', null); + }), Select::make('Item Code') ->label('Search by Item Code') ->nullable() - // ->options(function (callable $get) { - // $plantId = $get('Plant'); - // if (! $plantId) { - // return []; - // } - - // return Item::where('plant_id', $plantId)->pluck('code', 'id'); - // }) ->options(function (callable $get) { $plantId = $get('Plant'); if (! $plantId) { @@ -207,16 +249,23 @@ class WeightValidationResource extends Resource ->placeholder('Enter OBD Number'), Select::make('Line') - ->label('Line') + ->label('Line Number') ->options(function (callable $get) { $plantId = $get('Plant'); - + $itemCode = $get('Item Code'); if (! $plantId) { return []; + } elseif (! $itemCode) { + return WeightValidation::where('plant_id', $plantId) + ->distinct() + ->orderBy('line_number') + ->pluck('line_number', 'line_number') + ->toArray(); } // Get unique line_numbers for the selected plant_id return WeightValidation::where('plant_id', $plantId) + ->where('item_id', $itemCode) ->distinct() ->orderBy('line_number') ->pluck('line_number', 'line_number') @@ -285,31 +334,31 @@ class WeightValidationResource extends Resource } if (! empty($data['Obd Number'])) { - $query->where('obd_number', $data['Obd Number']); + $query->where('obd_number', 'like', '%'.$data['Obd Number'].'%'); } if (! empty($data['Batch'])) { - $query->where('batch_number', $data['Batch']); + $query->where('batch_number', 'like', '%'.$data['Batch'].'%'); } if (! empty($data['Actual Weight'])) { - $query->where('actual_weight', $data['Actual Weight']); + $query->where('actual_weight', 'like', '%'.$data['Actual Weight'].'%'); } if (! empty($data['Vehicle Number'])) { - $query->where('vehicle_number', $data['Vehicle Number']); + $query->where('vehicle_number', 'like', '%'.$data['Vehicle Number'].'%'); } if (! empty($data['Heat Number'])) { - $query->where('heat_number', $data['Heat Number']); + $query->where('heat_number', 'like', '%'.$data['Heat Number'].'%'); } if (! empty($data['Bundle Number'])) { - $query->where('bundle_number', $data['Bundle Number']); + $query->where('bundle_number', 'like', '%'.$data['Bundle Number'].'%'); } if (! empty($data['Scanned By'])) { - $query->where('scanned_by', $data['Scanned By']); + $query->where('scanned_by', 'like', '%'.$data['Scanned By'].'%'); } if (! empty($data['created_from'])) { @@ -324,7 +373,7 @@ class WeightValidationResource extends Resource $indicators = []; if (! empty($data['Plant'])) { - $indicators[] = 'Plant: '.Plant::where('id', $data['Plant'])->value('name'); + $indicators[] = 'Plant Name : '.Plant::where('id', $data['Plant'])->value('name'); } else { $userHas = Filament::auth()->user()->plant_id; @@ -334,19 +383,19 @@ class WeightValidationResource extends Resource } if (! empty($data['Item Code'])) { - $indicators[] = 'Item Code: '.Item::where('id', $data['Item Code'])->value('code'); + $indicators[] = 'Item Code : '.Item::where('id', $data['Item Code'])->value('code'); } if (! empty($data['Line'])) { - $indicators[] = 'Line: '.$data['Line']; + $indicators[] = 'Line : '.$data['Line']; } if (! empty($data['Obd Number'])) { - $indicators[] = 'OBD Number: '.$data['Obd Number']; + $indicators[] = 'OBD Number : '.$data['Obd Number']; } if (! empty($data['Batch'])) { - $indicators[] = 'Batch Number: '.$data['Batch']; + $indicators[] = 'Batch Number : '.$data['Batch']; } if (! empty($data['Actual Weight'])) { @@ -354,32 +403,32 @@ class WeightValidationResource extends Resource } if (! empty($data['Vehicle Number'])) { - $indicators[] = 'Vehicle Number: '.$data['Vehicle Number']; + $indicators[] = 'Vehicle Number : '.$data['Vehicle Number']; } if (! empty($data['Heat Number'])) { - $indicators[] = 'Heat Number: '.$data['Heat Number']; + $indicators[] = 'Heat Number : '.$data['Heat Number']; } if (! empty($data['Bundle Number'])) { - $indicators[] = 'Bundle Number: '.$data['Bundle Number']; + $indicators[] = 'Bundle Number : '.$data['Bundle Number']; } if (! empty($data['Scanned By'])) { - $indicators[] = 'Scanned By: '.$data['Scanned By']; + $indicators[] = 'Scanned By : '.$data['Scanned By']; } if (! empty($data['created_from'])) { - $indicators[] = 'From: '.$data['created_from']; + $indicators[] = 'From : '.$data['created_from']; } if (! empty($data['created_to'])) { - $indicators[] = 'To: '.$data['created_to']; + $indicators[] = 'To : '.$data['created_to']; } if (! empty($data['sticker_master_id'])) { $itemCode = Item::find($data['sticker_master_id'])->code ?? 'Unknown'; - $indicators[] = 'Item Codes: '.$itemCode; + $indicators[] = 'Item Codes : '.$itemCode; } return $indicators; @@ -409,6 +458,7 @@ class WeightValidationResource extends Resource return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) ->label('Select Plant') + ->searchable() ->required() ->default(function () { return optional(WeightValidation::latest()->first())->plant_id; diff --git a/app/Filament/Resources/WorkGroupMasterResource.php b/app/Filament/Resources/WorkGroupMasterResource.php index c95dfeb..eaecff0 100644 --- a/app/Filament/Resources/WorkGroupMasterResource.php +++ b/app/Filament/Resources/WorkGroupMasterResource.php @@ -11,6 +11,7 @@ use Filament\Facades\Filament; use Filament\Forms; use Filament\Forms\Components\Section; use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Resources\Resource; use Filament\Tables; use Filament\Tables\Actions\ExportAction; @@ -37,7 +38,7 @@ class WorkGroupMasterResource extends Resource Section::make('') ->schema([ Forms\Components\Select::make('plant_id') - ->label('Plant') + ->label('Plant Name') ->relationship('plant', 'name') ->reactive() ->columnSpan(1) @@ -47,6 +48,10 @@ class WorkGroupMasterResource extends Resource return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) + ->default(function () { + return optional(WorkGroupMaster::latest()->first())->plant_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) ->afterStateUpdated(function ($state, $set, callable $get) { $plantId = $get('plant_id'); @@ -68,7 +73,7 @@ class WorkGroupMasterResource extends Resource ->hint(fn ($get) => $get('pqPlantError') ? $get('pqPlantError') : null) ->hintColor('danger'), Forms\Components\TextInput::make('name') - ->label('Name') + ->label('Group Work Center') ->required() ->minLength(6) ->columnSpan(1) @@ -119,12 +124,12 @@ class WorkGroupMasterResource extends Resource return ($currentPage - 1) * $perPage + $rowLoop->iteration; }), Tables\Columns\TextColumn::make('plant.name') - ->label('Plant') + ->label('Plant Name') ->alignCenter() ->searchable() ->sortable(), Tables\Columns\TextColumn::make('name') - ->label('Name') + ->label('Group Work Center') ->alignCenter() ->searchable() ->sortable(), @@ -138,23 +143,21 @@ class WorkGroupMasterResource extends Resource ->alignCenter() ->searchable() ->sortable(), + Tables\Columns\TextColumn::make('created_at') + ->label('Created At') + ->alignCenter() + ->dateTime() + ->sortable(), Tables\Columns\TextColumn::make('created_by') ->label('Created By') ->alignCenter() ->searchable() ->sortable(), - Tables\Columns\TextColumn::make('created_at') - ->label('Created At') - ->alignCenter() - ->dateTime() - ->sortable() - ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') ->label('Updated At') ->alignCenter() ->dateTime() - ->sortable() - ->toggleable(isToggledHiddenByDefault: true), + ->sortable(), Tables\Columns\TextColumn::make('deleted_at') ->label('Deleted At') ->alignCenter() diff --git a/app/Http/Controllers/ObdController.php b/app/Http/Controllers/ObdController.php index 3730a34..113fc08 100644 --- a/app/Http/Controllers/ObdController.php +++ b/app/Http/Controllers/ObdController.php @@ -19,7 +19,7 @@ class ObdController extends Controller /** * Store a newly created resource in storage. */ - public function store(Request $request) + public function store_obd(Request $request) { $expectedUser = env('API_AUTH_USER'); $expectedPw = env('API_AUTH_PW'); @@ -42,51 +42,62 @@ class ObdController extends Controller try { $data = $request->all(); + $plantCode = $data['plant_code'] ?? ''; + $obdNumber = $data['obd_number'] ?? ''; + Log::info('OBD POST API >>', ['Post-Data' => $data]); // Validate required fields - $missing = []; - if (empty($data['plant_name'])) { - $missing[] = 'plant_name'; - } - if (empty($data['obd_number'])) { - $missing[] = 'obd_number'; - } - if (empty($data['line_numbers'])) { - $missing[] = 'line_numbers'; - } - - if (! empty($missing)) { - $message = 'Missing required field(s): '.implode(', ', $missing); - + if (! $plantCode || $plantCode == null || $plantCode == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code can't be empty!", + ], 404); + } elseif (! is_numeric($plantCode) || Str::length($plantCode) < 4 || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { // !ctype_digit($data['plant_code']) + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 404); + } elseif (! $obdNumber || $obdNumber == null || $obdNumber == '') { + // return response("ERROR: OBD Number can't be empty", 400) + // ->header('Content-Type', 'text/plain'); + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "OBD number can't be empty!", + ], 400); + } elseif (Str::length($obdNumber) < 8) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "OBD number '{$obdNumber}' should contain minimum 8 digits!", + ], 404); + } elseif (! ctype_alnum($obdNumber)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "OBD number '{$obdNumber}' should contain only alpha-numeric values!", + ], 404); + } elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{8,}$/', $obdNumber)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "OBD number '{$obdNumber}' should not begin with '0'!", + ], 404); + } elseif (empty($data['line_numbers'])) { // return response($message, 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => $message, + 'status_description' => 'Missing required field(s): line_numbers', ], 400); } - // OBD Number validation - $obdNumber = $data['obd_number']; - if (Str::length($obdNumber) < 8 || ! ctype_alnum($obdNumber)) { - // return response("ERROR: OBD Number should contain minimum 8 digits", 400) - // ->header('Content-Type', 'text/plain'); + // Lookup plant_id by plant code + $plant = Plant::where('code', $plantCode)->first(); + if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'OBD number should contain minimum 8 digit alpha-numeric values only!', - ], 400); - } - - // Lookup plant_id by plant_name - $plantId = Plant::where('name', $data['plant_name'])->value('id'); - if (! $plantId) { - // return response("ERROR: Plant '{$data['plant_name']}' not found", 404) - // ->header('Content-Type', 'text/plain'); - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Plant '{$data['plant_name']}' not found!", + 'status_description' => "Plant code '{$plantCode}' not found!", ], 404); } + $plantId = $plant?->id ?? null; + $plantName = $plant?->name ?? null; // Check if OBD number exists for that plant $obdRecords = WeightValidation::where('plant_id', $plantId) @@ -94,10 +105,10 @@ class ObdController extends Controller ->exists(); if (! $obdRecords) { - // return response( "ERROR: OBD Number '$obdNumber' not found for plant '{$data['plant_name']}'",404)->header('Content-Type', 'text/plain'); + // return response( "ERROR: OBD Number '$obdNumber' not found for plant '{$plantCode}'",404)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "OBD Number '$obdNumber' not found for plant '{$data['plant_name']}'!", + 'status_description' => "OBD Number '{$obdNumber}' not found for the plant '{$plantName}'!", ], 404); } @@ -105,22 +116,23 @@ class ObdController extends Controller $alreadyUpdatedLines = []; foreach ($data['line_numbers'] as $line) { - if ($line['line_number'] == '' || $line['line_number'] == null) { + $lineNumber = $line['line_number'] ?? null; + if ($lineNumber == '' || $lineNumber == null) { // return response("ERROR: Line Number can't be empty", 400) // ->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Line Number can't be empty!", + 'status_description' => "Line number can't be empty!", ], 404); } $record = WeightValidation::where('plant_id', $plantId) - ->where('obd_number', $data['obd_number']) - ->where('line_number', $line['line_number']) + ->where('obd_number', $obdNumber) + ->where('line_number', $lineNumber) ->first(); if (! $record) { - $missingLines[] = $line['line_number']; + $missingLines[] = $lineNumber; continue; } @@ -142,23 +154,19 @@ class ObdController extends Controller $fieldsString = implode(', ', $missingFields).' and '.$lastField; } - $message = $fieldsString." can't be empty for line_number {$line['line_number']}!"; - // return response($message, 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => $message, + 'status_description' => $fieldsString." can't be empty for line_number {$lineNumber}!", ], 400); } } if (! empty($missingLines)) { - $message = 'Line(s) '.implode(', ', $missingLines)." not found for Plant '{$data['plant_name']}' and OBD Number: '{$data['obd_number']}'!"; - // return response($message, 404)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => $message, + 'status_description' => 'Line(s) '.implode(', ', $missingLines)." not found for the plant '{$plantName}' and OBD number: '{$obdNumber}'!", ], 404); } @@ -168,8 +176,8 @@ class ObdController extends Controller // Check for duplicates within the request foreach ($data['line_numbers'] as $line) { - $lineNumber = $line['line_number']; - $bundleNumber = trim((string) $line['bundle_number']); + $lineNumber = $line['line_number'] ?? null; + $bundleNumber = trim((string) $line['bundle_number'] ?? ''); $pairKey = $lineNumber.'|'.$bundleNumber; if (isset($seenPairs[$pairKey])) { @@ -228,11 +236,9 @@ class ObdController extends Controller // 400 // )->header('Content-Type', 'text/plain'); - $retRes = implode(', and ', $allDuplicates); - return response()->json([ 'status_code' => 'ERROR', - 'status_description' => $retRes, + 'status_description' => implode(', and ', $allDuplicates), ], 400); } @@ -244,7 +250,7 @@ class ObdController extends Controller $lineTracker = []; foreach ($data['line_numbers'] as $line) { - $lineNumber = $line['line_number']; + $lineNumber = $line['line_number'] ?? null; $existing = WeightValidation::where('plant_id', $plantId) ->where('obd_number', $obdNumber) ->where('line_number', $lineNumber) @@ -267,11 +273,12 @@ class ObdController extends Controller $updatedLines[] = $lineNumber; $lineTracker[$lineNumber] = 1; } else { - $original = WeightValidation::where([ - 'plant_id' => $plantId, - 'obd_number' => $obdNumber, - 'line_number' => $lineNumber, - ])->orderBy('id')->first(); + $original = WeightValidation::where('plant_id', $plantId) + ->where('obd_number', $obdNumber) + ->where('line_number', $lineNumber) + ->orderBy('id') + ->first(); + // where(['plant_id' => $plantId, 'obd_number' => $obdNumber, 'line_number' => $lineNumber]) WeightValidation::create([ 'plant_id' => $plantId, @@ -302,7 +309,7 @@ class ObdController extends Controller } if ($inserted > 0) { - $responseMessage .= 'Inserted successfully. Line Numbers: {'.implode(', ', $insertedLines).'}'; + $responseMessage .= ' inserted successfully. Line Numbers: {'.implode(', ', $insertedLines).'}'; } // return response($responseMessage, 200) @@ -321,7 +328,6 @@ class ObdController extends Controller } } - // Route::get('obd/get-test-datas', [ObdController::class, 'get_test']); public function get_test(Request $request) { $expectedUser = env('API_AUTH_USER'); @@ -373,7 +379,7 @@ class ObdController extends Controller } elseif (Str::length($productionOrder) < 7 || ! is_numeric($productionOrder)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Production order should contain minimum 7 digits numeric values only!', + 'status_description' => "Production order '{$productionOrder}' should contain minimum 7 digits numeric values only!", ], 400); } @@ -382,7 +388,7 @@ class ObdController extends Controller if (! $prodOrderExist) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Production order not found!', + 'status_description' => "Production order '{$productionOrder}' not found!", ], 400); } @@ -423,46 +429,56 @@ class ObdController extends Controller ], 403); } - $plantName = $request->header('plant-name'); + $plantCode = $request->header('plant-code'); $obdNumber = $request->header('obd-number'); - Log::info('OBD GET API >>', ['plant-name' => $plantName, 'obd-number' => $obdNumber]); + Log::info('OBD GET API >>', ['plant-code' => $plantCode, 'obd-number' => $obdNumber]); - if (empty($plantName)) { - // return response("ERROR: Plant Name can't be empty", 400) - // ->header('Content-Type', 'text/plain'); + if (! $plantCode || $plantCode == null || $plantCode == '') { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant Name can't be empty!", - ], 400); - } elseif (empty($obdNumber)) { + 'status_description' => "Plant code can't be empty!", + ], 404); + } elseif (! is_numeric($plantCode) || Str::length($plantCode) < 4 || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { // !ctype_digit($data['plant_code']) + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 404); + } elseif (! $obdNumber || $obdNumber == null || $obdNumber == '') { // return response("ERROR: OBD Number can't be empty", 400) // ->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "OBD Number can't be empty!", + 'status_description' => "OBD number can't be empty!", ], 400); - } elseif (Str::length($obdNumber) < 8 || ! ctype_alnum($obdNumber)) { - // return response("ERROR: OBD Number should contain minimum 8 digits: '$obdNumber'", 400) - // ->header('Content-Type', 'text/plain'); + } elseif (Str::length($obdNumber) < 8) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'OBD number should contain minimum 8 digit alpha-numeric values only!', - ], 400); - } - - // Fetch the plant id by name - $plantId = Plant::where('name', $plantName)->value('id'); - - if (! $plantId) { - // return response("ERROR: Plant not found", 400) - // ->header('Content-Type', 'text/plain'); + 'status_description' => "OBD number '{$obdNumber}' should contain minimum 8 digits!", + ], 404); + } elseif (! ctype_alnum($obdNumber)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Plant not found!', - ], 400); + 'status_description' => "OBD number '{$obdNumber}' should contain only alpha-numeric values!", + ], 404); + } elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{8,}$/', $obdNumber)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "OBD number '{$obdNumber}' should not begin with '0'!", + ], 404); } + // Fetch the plant id by code + $plant = Plant::where('code', $plantCode)->first(); + if (! $plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' not found!", + ], 404); + } + $plantId = $plant?->id ?? null; + $plantName = $plant?->name ?? null; + // $records = WeightValidation::where('plant_id', $plantId) // ->where('obd_number', $obdNumber) // ->get(); @@ -473,11 +489,11 @@ class ObdController extends Controller ->exists(); if (! $exists) { - // return response("ERROR: OBD number $obdNumber does not exist for plant '$plantName'", 404) + // return response("ERROR: OBD number $obdNumber does not exist for plant '$plantCode'", 404) // ->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "OBD number $obdNumber does not exist for plant '$plantName'!", + 'status_description' => "OBD number '{$obdNumber}' does not exist for plant '{$plantName}'!", ], 400); } @@ -492,7 +508,7 @@ class ObdController extends Controller // return response("SUCCESS: Already scanning process completed for the OBD Number", 200)->header('Content-Type', values: 'text/plain'); return response()->json([ 'status_code' => 'SUCCESS', - 'status_description' => 'Already weight validation completed for the OBD Number!', + 'status_description' => "Already weight validation completed for the OBD Number '{$obdNumber}'!", ], 200); } diff --git a/app/Models/Plant.php b/app/Models/Plant.php index 4b66523..bff380a 100644 --- a/app/Models/Plant.php +++ b/app/Models/Plant.php @@ -48,6 +48,11 @@ class Plant extends Model return $this->hasMany(StickerMaster::class, 'plant_id', 'id'); } + public function weightValidations(): HasMany + { + return $this->hasMany(WeightValidation::class, 'plant_id', 'id'); + } + public function invoiceValidations() { return $this->hasMany(InvoiceValidation::class, 'plant_id', 'id'); diff --git a/app/Policies/ProductionTempPolicy.php b/app/Policies/ProductionTempPolicy.php new file mode 100644 index 0000000..886ffb7 --- /dev/null +++ b/app/Policies/ProductionTempPolicy.php @@ -0,0 +1,106 @@ +checkPermissionTo('view-any ProductionTemp'); + } + + /** + * Determine whether the user can view the model. + */ + public function view(User $user, ProductionTemp $productiontemp): bool + { + return $user->checkPermissionTo('view ProductionTemp'); + } + + /** + * Determine whether the user can create models. + */ + public function create(User $user): bool + { + return $user->checkPermissionTo('create ProductionTemp'); + } + + /** + * Determine whether the user can update the model. + */ + public function update(User $user, ProductionTemp $productiontemp): bool + { + return $user->checkPermissionTo('update ProductionTemp'); + } + + /** + * Determine whether the user can delete the model. + */ + public function delete(User $user, ProductionTemp $productiontemp): bool + { + return $user->checkPermissionTo('delete ProductionTemp'); + } + + /** + * Determine whether the user can delete any models. + */ + public function deleteAny(User $user): bool + { + return $user->checkPermissionTo('delete-any ProductionTemp'); + } + + /** + * Determine whether the user can restore the model. + */ + public function restore(User $user, ProductionTemp $productiontemp): bool + { + return $user->checkPermissionTo('restore ProductionTemp'); + } + + /** + * Determine whether the user can restore any models. + */ + public function restoreAny(User $user): bool + { + return $user->checkPermissionTo('restore-any ProductionTemp'); + } + + /** + * Determine whether the user can replicate the model. + */ + public function replicate(User $user, ProductionTemp $productiontemp): bool + { + return $user->checkPermissionTo('replicate ProductionTemp'); + } + + /** + * Determine whether the user can reorder the models. + */ + public function reorder(User $user): bool + { + return $user->checkPermissionTo('reorder ProductionTemp'); + } + + /** + * Determine whether the user can permanently delete the model. + */ + public function forceDelete(User $user, ProductionTemp $productiontemp): bool + { + return $user->checkPermissionTo('force-delete ProductionTemp'); + } + + /** + * Determine whether the user can permanently delete any models. + */ + public function forceDeleteAny(User $user): bool + { + return $user->checkPermissionTo('force-delete-any ProductionTemp'); + } +} diff --git a/routes/api.php b/routes/api.php index 21d8472..e855e25 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,5 @@ $request->all() // ]); // }); - -Route::post('obd/store-data', [ObdController::class, 'store']); - Route::get('obd/get-test-datas', [ObdController::class, 'get_test']); -Route::get('obd/store-data/get', [ObdController::class, 'get_obd']); +Route::get('obd/get-data', [ObdController::class, 'get_obd']); + +Route::post('obd/store-data', [ObdController::class, 'store_obd']); Route::get('plant/get-all-data', [PlantController::class, 'get_all_data']); @@ -154,7 +152,7 @@ Route::get('testing/equipment/get-master-data', [EquipmentMasterController::clas Route::post('testing/reading/store-data', [TestingPanelController::class, 'store']); -Route::get('get-pdf', [PdfController::class, 'getPdf']); +Route::get('get-pdf', [PdfController::class, 'getPdf']); // processorder/get-pdf // ..Part Validation - Characteristics @@ -168,6 +166,8 @@ Route::post('laser/route/data', [CharacteristicsController::class, 'test']); // Route::get('laser/characteristics/get', [CharacteristicsController::class, 'getClassChar']); +// ..Job or Serial - Status + Route::get('laser/characteristics/check', [CharacteristicsController::class, 'checkClassChar']); // ..Job or Master - Characteristics