diff --git a/app/Filament/Resources/OcrValidationResource.php b/app/Filament/Resources/OcrValidationResource.php index 43b7dd2..46cc12e 100644 --- a/app/Filament/Resources/OcrValidationResource.php +++ b/app/Filament/Resources/OcrValidationResource.php @@ -24,6 +24,7 @@ use setasign\Fpdi\PdfReader; use SimpleSoftwareIO\QrCode\Facades\QrCode; use Storage; use Filament\Forms\Concerns\InteractsWithForms; +use Smalot\PdfParser\Parser; class OcrValidationResource extends Resource { @@ -83,15 +84,15 @@ class OcrValidationResource extends Resource ->label('GR Number') ->minLength(7) ->required(), - Forms\Components\FileUpload::make('photo') - ->dehydrated(false) - ->label('Capture from Webcam') - ->disk('local') - ->directory('uploads') - ->preserveFilenames() - ->image() - ->helperText('You can take a picture from your webcam') - ->reactive(), + // Forms\Components\FileUpload::make('photo') + // ->dehydrated(false) + // ->label('Capture from Webcam') + // ->disk('local') + // ->directory('uploads') + // ->preserveFilenames() + // ->image() + // ->helperText('You can take a picture from your webcam') + // ->reactive(), // Forms\Components\TextInput::make('camera_capture') // ->label('Captured Photo') @@ -116,148 +117,156 @@ class OcrValidationResource extends Resource // ->dehydrated(), Forms\Components\Hidden::make('serial_numbers') ->default('[]'), + Forms\Components\FileUpload::make('attachment') + ->label('PDF Upload') + ->acceptedFileTypes(['application/pdf']) + ->storeFiles(false) + ->disk('local') + ->directory('uploads/temp') + ->preserveFilenames() + ->reactive(), Forms\Components\Actions::make([ - // Action::make('uploadNow1') - // ->label('Upload OCR') - // ->action(function ($get, callable $set) { - // $uploadedFiles = $get('photo'); - // // $serialNumbers = [ - // // $get('serial_1'), - // // $get('serial_2'), - // // $get('serial_3'), - // // $get('serial_4'), - // // ]; + Action::make('uploadNow') + ->label('Upload PDF Now') + ->action(function ($get, callable $set) { + $uploadedFiles = $get('attachment'); - // if (is_array($uploadedFiles) && count($uploadedFiles) > 0) - // { - // $uploaded = reset($uploadedFiles); + if (is_array($uploadedFiles) && count($uploadedFiles) > 0) + { + $uploaded = reset($uploadedFiles); - // if ($uploaded instanceof TemporaryUploadedFile) { - // $grNumber = $get('gr_number'); - // $safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber); - // // $originalName = $uploaded->getClientOriginalName(); - // // $path = 'uploads/GRNumber/' . $originalName; - // $finalFileName = $safeName . '.jpg'; - // $finalPath = 'uploads/OCR/' . $finalFileName; + if ($uploaded instanceof TemporaryUploadedFile) { + $grNumber = $get('gr_number'); + $safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber); + // $originalName = $uploaded->getClientOriginalName(); + // $path = 'uploads/GRNumber/' . $originalName; + $finalFileName = $safeName . '.pdf'; + $finalPath = 'uploads/OcrGRNumber/' . $finalFileName; - // $storedPath = $uploaded->storeAs( - // 'uploads/OCR', - // $finalFileName, - // 'local' - // ); + if (Storage::disk('local')->exists($finalPath)) { + Notification::make() + ->title('Duplicate File') + ->body("The file '{$finalFileName}' already exists in uploads/GRNumber.") + ->warning() + ->send(); + return; + } - // // $storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local'); - // // $fullPath = storage_path('app/' . $storedPath); - // $storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local'); + $storedPath = $uploaded->storeAs( + 'uploads/OcrGRNumber', + $finalFileName, + 'local' + ); - // $fullPath = storage_path('app/private/' . $storedPath); - // $text = (new TesseractOCR($fullPath))->lang('eng')->run(); + // $fullPath = storage_path('app/' . $storedPath); + $fullPath = storage_path('app/private/' . $storedPath); + $parser = new Parser(); + //$pdf = $parser->parseContent(file_get_contents($uploaded->getRealPath())); + $pdf = $parser->parseFile($fullPath); + $text = $pdf->getText(); - // $rawText = $text; + // dd($text); - // preg_match_all('/\d+/', $rawText, $matches); + //dd($text); + $item1 = null; + $item2 = null; - // $serialNumbers = $matches[0]; + if (preg_match('/Item code\s*:\s*(\S+)/i', $text, $matches)) + { + $item1 = $matches[1]; + } + else if (preg_match('/E CODE\s*:\s*(\S+)/i', $text, $matches)) + { + $item2 = $matches[1]; + } - // $serialNumbers = array_slice($serialNumbers, 0, 4); + $processOrder = $get('gr_number'); - // //dd($serialNumbers); + $itemId = $get('item_id'); - // $processOrder = $get('gr_number'); + $plant = $get('plant_id'); - // $itemId = $get('item_id'); + $item = Item::find($itemId); - // $plant = $get('plant_id'); + $plant = Plant::find($plant); - // $item = Item::find($itemId); + if ($item) + { + $itemCode = $item->code; + } + else + { + $itemCode = null; + Notification::make() + ->title('Item Not Found') + ->body("Item not found in uploaded pdf.") + ->warning() + ->send(); + if (Storage::disk('local')->exists($storedPath)) { + Storage::disk('local')->delete($storedPath); + } + return; + } - // $plant = Plant::find($plant); + $storedPath = $uploaded->storeAs( + 'uploads/OcrGRNumber', + $finalFileName, + 'local' + ); - // $templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf'); + if($itemCode == $item1) + { + Notification::make() + ->title('Success') + ->body("Gr Number '$processOrder' PDF uploaded successfully.") + ->success() + ->send(); + return; + } + if($itemCode == $item2) + { + Notification::make() + ->title('Success') + ->body("Gr Number '$processOrder' PDF uploaded successfully.") + ->success() + ->send(); + return; + } + else + { + Notification::make() + ->title('Item Code not matched') + ->body("Item Code: {$item->code} not matched with the uploaded pdf code $item1.") + ->danger() + ->send(); - // $outputPath = storage_path('app/private/uploads/StickerTemplateOcr/multi_filled.pdf'); + if (Storage::disk('local')->exists($storedPath)) { + Storage::disk('local')->delete($storedPath); + } + return; + } + } + } + else + { + Notification::make() + ->title('No file selected to upload') + ->warning() + ->send(); + return; + } + }), - // $storedPath = $uploaded->storeAs( - // 'uploads/GRNumber', - // $finalFileName, - // 'local' - // ); - - // $pdf = new Fpdi('P', 'mm', [90, 90]); - - // $templateId = $pdf->setSourceFile($templatePath); - // $templatePage = $pdf->importPage(1); - - // $pdf->AddPage(); - // $pdf->useTemplate($templatePage, 0, 0, 90, 90); - - // $pdf->SetFont('Helvetica', '', 10); - // $pdf->SetTextColor(0, 0, 0); - - // $slots = [ - // ['x' => 5.7, 'y' => 41.9, 'w' => 46.5, 'h' => 3.5], // 1st serial - // ['x' => 50, 'y' => 41.5, 'w' => 46.6, 'h' => 3.9], // 2nd serial - // ['x' => 5.7, 'y' => 60, 'w' => 46.5, 'h' => 3.5], // 3rd serial - // ['x' => 50, 'y' => 60, 'w' => 46.6, 'h' => 3.5], // 4rd serial - // ]; - - // $qrSlots = [ - // ['x' => 17.3, 'y' => 29.2, 'size' => 11.4], - // ['x' => 61.5, 'y' => 29, 'size' => 11.5], - // ['x' => 17.7, 'y' => 46.7, 'size' => 11.4], - // ['x' => 61.7, 'y' => 46.7, 'size' => 11.4], - // ]; - - // foreach ($serialNumbers as $i => $serial) { - // if (!isset($slots[$i]) || !isset($qrSlots[$i])) continue; - - // // Erase old QR completely (slightly larger) - // $pdf->SetFillColor(255, 255, 255); - // $pdf->Rect($qrSlots[$i]['x']-1, $qrSlots[$i]['y']-1, $qrSlots[$i]['size']+2, $qrSlots[$i]['size']+2, 'F'); - - // // Generate new QR code - // $qrPath = storage_path("app/private/uploads/QR/qr_$serial.png"); - // $qrDir = storage_path('app/private/uploads/QR'); - // if (!file_exists($qrDir)) mkdir($qrDir, 0777, true); - // QrCode::format('png')->size(100)->generate($serial, $qrPath); - - // // Place QR code - // $pdf->Image($qrPath, $qrSlots[$i]['x'], $qrSlots[$i]['y'], $qrSlots[$i]['size'], $qrSlots[$i]['size']); - - // // Erase old serial - // $pdf->SetFillColor(255, 255, 255); - // $pdf->Rect($slots[$i]['x'], $slots[$i]['y'], $slots[$i]['w'], $slots[$i]['h'], 'F'); - - // // Write new serial - // $pdf->SetXY($slots[$i]['x'], $slots[$i]['y']); - // $pdf->Cell($slots[$i]['w'], $slots[$i]['h'], $serial, 0, 0, 'L'); - // } - - // // Save the final PDF - // $pdf->Output('F', $outputPath); - - // // Download - // return response()->download($outputPath); - // } - // } - // else - // { - // Notification::make() - // ->title('No file selected to upload') - // ->warning() - // ->send(); - // return; - // } - // }), - - Action::make('uploadNow1') + Action::make('uploadNow1') ->label('Upload OCR') ->action(function ($get, callable $set) { $serials = session('serial_numbers'); + $grNumber = $get('gr_number'); + $set('serial_numbers', $serials); if (empty($serials)) { @@ -274,7 +283,18 @@ class OcrValidationResource extends Resource { $serialNumbers = $serials; - $templatePath = storage_path('app/private/uploads/StickerTemplateOcr/Single.pdf'); + //$templatePath = storage_path('app/private/uploads/StickerTemplateOcr/Single.pdf'); + $templatePath = storage_path("app/private/uploads/OcrGrNumber/'$grNumber'.pdf"); + + if(!file_exists($templatePath)) + { + Notification::make() + ->title("Template PDF not found for the Gr Number $grNumber.") + ->danger() + ->send(); + return; + } + $outputPath = storage_path('app/private/uploads/StickerTemplateOcr/single_filled.pdf'); $pdf = new Fpdi('P', 'mm', [90, 90]); @@ -343,7 +363,18 @@ class OcrValidationResource extends Resource $plant = Plant::find($plant); - $templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf'); + // $templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf'); + + $templatePath = storage_path("app/private/uploads/OcrGrNumber/'$grNumber'.pdf"); + + if(!file_exists($templatePath)) + { + Notification::make() + ->title("Template PDF not found for the Gr Number $grNumber.") + ->danger() + ->send(); + return; + } $outputPath = storage_path('app/private/uploads/StickerTemplateOcr/multi_filled.pdf'); @@ -417,11 +448,8 @@ class OcrValidationResource extends Resource ->danger() ->send(); return; - } }), - - Action::make('downloadAttachment') ->label('Download PDF') ->action(function ($get) {