diff --git a/app/Filament/Resources/OcrValidationResource.php b/app/Filament/Resources/OcrValidationResource.php index 67897e7..c1698ba 100644 --- a/app/Filament/Resources/OcrValidationResource.php +++ b/app/Filament/Resources/OcrValidationResource.php @@ -56,7 +56,7 @@ class OcrValidationResource extends Resource return Item::where('plant_id', $plantId)->pluck('code', 'id'); }) ->required(), - Forms\Components\Textarea::make('gr_number') + Forms\Components\TextInput::make('gr_number') ->label('GR Number') ->minLength(7) ->required(), @@ -198,6 +198,57 @@ class OcrValidationResource extends Resource } }), + Action::make('verifyOcr') + ->label('Verify OCR') + ->action(function ($get) { + $base64Image = $get('photo_data'); + + if (!$base64Image) { + Notification::make() + ->title('No captured image found') + ->warning() + ->send(); + return; + } + + // Decode and store the base64 image + $image = str_replace('data:image/jpeg;base64,', '', $base64Image); + $image = str_replace(' ', '+', $image); + + $grNumber = $get('gr_number') ?: 'Unknown'; + $safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber); + $fileName = $safeName . '.jpg'; + $filePath = storage_path("app/private/uploads/OCR/{$fileName}"); + + if (!file_exists(dirname($filePath))) { + mkdir(dirname($filePath), 0777, true); + } + + file_put_contents($filePath, base64_decode($image)); + + // 🧠 Run OCR + $text = (new TesseractOCR($filePath)) + ->lang('eng') + ->run(); + + preg_match_all('/\d+/', $text, $matches); + $serialNumbers = array_slice($matches[0], 0, 4); + + if (empty($serialNumbers)) { + Notification::make() + ->title('No numbers found in image') + ->danger() + ->send(); + return; + } + + Notification::make() + ->title('OCR Read Successfully') + ->body('Serial Numbers: ' . implode(', ', $serialNumbers)) + ->success() + ->send(); + }), + Action::make('downloadAttachment') ->label('Download PDF') ->action(function ($get) {