Updated alignments and added orderby query while load plant
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled

This commit is contained in:
dhanabalan
2026-02-10 12:45:54 +05:30
parent 6c9408663b
commit aaa9c17e66
36 changed files with 1592 additions and 1574 deletions

View File

@@ -3,36 +3,36 @@
namespace App\Filament\Resources;
use App\Filament\Resources\OcrValidationResource\Pages;
use App\Filament\Resources\OcrValidationResource\RelationManagers;
use App\Models\Item;
use App\Models\OcrValidation;
use App\Models\Plant;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use thiagoalessio\TesseractOCR\TesseractOCR;
use setasign\Fpdi\Fpdi;
use Filament\Facades\Filament;
use Filament\Forms\Components\Actions\Action;
use Filament\Notifications\Notification;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use setasign\Fpdi\PdfReader;
use setasign\Fpdi\Fpdi;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Storage;
use Filament\Forms\Concerns\InteractsWithForms;
use Smalot\PdfParser\Parser;
use Storage;
class OcrValidationResource extends Resource
{
protected static ?string $model = OcrValidation::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationIcon = 'heroicon-s-qr-code';
protected static ?string $navigationGroup = 'OCR';
public $camera_capture;
public $serialNumbers = [];
use InteractsWithForms;
@@ -47,12 +47,13 @@ class OcrValidationResource extends Resource
->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::pluck('name', 'id')->toArray();
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')
->label('Item Code')
//->relationship('item', 'id')
->label('Item Code')
// ->relationship('item', 'id')
->reactive()
->searchable()
->options(function (callable $get) {
@@ -60,6 +61,7 @@ class OcrValidationResource extends Resource
if (empty($plantId)) {
return [];
}
return Item::where('plant_id', $plantId)->pluck('code', 'id');
})
->required(),
@@ -109,13 +111,12 @@ class OcrValidationResource extends Resource
->preserveFilenames()
->reactive(),
Forms\Components\Actions::make([
Action::make('uploadNow')
Action::make('uploadNow')
->label('Upload PDF Now')
->action(function ($get, callable $set) {
$uploadedFiles = $get('attachment');
if (is_array($uploadedFiles) && count($uploadedFiles) > 0)
{
if (is_array($uploadedFiles) && count($uploadedFiles) > 0) {
$uploaded = reset($uploadedFiles);
if ($uploaded instanceof TemporaryUploadedFile) {
@@ -123,8 +124,8 @@ class OcrValidationResource extends Resource
$safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber);
// $originalName = $uploaded->getClientOriginalName();
// $path = 'uploads/GRNumber/' . $originalName;
$finalFileName = $safeName . '.pdf';
$finalPath = 'uploads/OcrGrNumber/' . $finalFileName;
$finalFileName = $safeName.'.pdf';
$finalPath = 'uploads/OcrGrNumber/'.$finalFileName;
if (Storage::disk('local')->exists($finalPath)) {
Notification::make()
@@ -132,6 +133,7 @@ class OcrValidationResource extends Resource
->body("The file '{$finalFileName}' already exists in uploads/GRNumber.")
->warning()
->send();
return;
}
@@ -141,26 +143,22 @@ class OcrValidationResource extends Resource
'local'
);
// $fullPath = storage_path('app/' . $storedPath);
$fullPath = storage_path('app/private/' . $storedPath);
$parser = new Parser();
//$pdf = $parser->parseContent(file_get_contents($uploaded->getRealPath()));
// $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();
// dd($text);
// dd($text);
//dd($text);
// dd($text);
$item1 = null;
$item2 = null;
if (preg_match('/Item code\s*:\s*(\S+)/i', $text, $matches))
{
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))
{
} elseif (preg_match('/E CODE\s*:\s*(\S+)/i', $text, $matches)) {
$item2 = $matches[1];
}
@@ -174,21 +172,19 @@ class OcrValidationResource extends Resource
$plant = Plant::find($plant);
if ($item)
{
if ($item) {
$itemCode = $item->code;
}
else
{
} else {
$itemCode = null;
Notification::make()
->title('Item Not Found')
->body("Item not found in uploaded pdf.")
->body('Item not found in uploaded pdf.')
->warning()
->send();
if (Storage::disk('local')->exists($storedPath)) {
Storage::disk('local')->delete($storedPath);
}
return;
}
@@ -198,50 +194,48 @@ class OcrValidationResource extends Resource
'local'
);
if($itemCode == $item1)
{
if ($itemCode == $item1) {
Notification::make()
->title('Success')
->body("Gr Number '$processOrder' PDF uploaded successfully.")
->success()
->send();
->title('Success')
->body("Gr Number '$processOrder' PDF uploaded successfully.")
->success()
->send();
return;
}
if($itemCode == $item2)
{
if ($itemCode == $item2) {
Notification::make()
->title('Success')
->body("Gr Number '$processOrder' PDF uploaded successfully.")
->success()
->send();
->title('Success')
->body("Gr Number '$processOrder' PDF uploaded successfully.")
->success()
->send();
return;
}
else
{
} else {
Notification::make()
->title('Item Code not matched')
->body("Item Code: {$item->code} not matched with the uploaded pdf code $item1.")
->danger()
->send();
->title('Item Code not matched')
->body("Item Code: {$item->code} not matched with the uploaded pdf code $item1.")
->danger()
->send();
if (Storage::disk('local')->exists($storedPath)) {
Storage::disk('local')->delete($storedPath);
}
return;
}
}
}
else
{
} else {
Notification::make()
->title('No file selected to upload')
->warning()
->send();
return;
return;
}
}),
Action::make('uploadNow1')
Action::make('uploadNow1')
->label('Upload OCR')
->action(function ($get, callable $set) {
@@ -256,25 +250,25 @@ class OcrValidationResource extends Resource
->title('Serial numbers cannot be empty!')
->danger()
->send();
return;
}
//$serialNumbers = array_slice($serialNumbers, 0, 4);
// $serialNumbers = array_slice($serialNumbers, 0, 4);
if (count($serials) == 1)
{
if (count($serials) == 1) {
$serialNumbers = $serials;
$grNumber = trim($grNumber);
//$templatePath = storage_path('app/private/uploads/StickerTemplateOcr/Single.pdf');
$templatePath = storage_path("app/private/uploads/OcrGrNumber/{$grNumber}.pdf");
// $templatePath = storage_path('app/private/uploads/StickerTemplateOcr/Single.pdf');
$templatePath = storage_path("app/private/uploads/OcrGrNumber/{$grNumber}.pdf");
if(!file_exists($templatePath))
{
if (! file_exists($templatePath)) {
Notification::make()
->title("Template PDF not found for the Gr Number $grNumber.")
->danger()
->send();
return;
}
@@ -300,17 +294,21 @@ class OcrValidationResource extends Resource
];
foreach ($serialNumbers as $i => $serial) {
if (!isset($slots[$i]) || !isset($qrSlots[$i])) continue;
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');
$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('svg')->size(100)->generate($serial, $qrPath);
if (! file_exists($qrDir)) {
mkdir($qrDir, 0777, true);
}
// QrCode::format('svg')->size(100)->generate($serial, $qrPath);
QrCode::format('png')->size(300)->errorCorrection('H')->generate($serial, $qrPath);
// Place QR code
@@ -331,12 +329,11 @@ class OcrValidationResource extends Resource
// Download
return response()->download($outputPath);
}
if(count($serials) == 4)
{
if (count($serials) == 4) {
$serialNumbers = array_slice($serials, 0, 4);
//dd($serialNumbers);
// dd($serialNumbers);
$itemId = $get('item_id');
@@ -348,19 +345,19 @@ class OcrValidationResource extends Resource
$grNumber = trim($grNumber);
//$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");
// $templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf');
//$templatePath = storage_path("app/private/uploads/OcrGrNumber/'$grNumber'.pdf");
// $templatePath = storage_path("app/private/uploads/OcrGrNumber/'$grNumber'.pdf");
if(!file_exists($templatePath))
{
if (! file_exists($templatePath)) {
Notification::make()
->title("Template PDF not found for the Gr Number $grNumber.")
->danger()
->send();
return;
}
@@ -398,17 +395,21 @@ class OcrValidationResource extends Resource
];
foreach ($serialNumbers as $i => $serial) {
if (!isset($slots[$i]) || !isset($qrSlots[$i])) continue;
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');
$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('svg')->size(100)->generate($serial, $qrPath);
if (! file_exists($qrDir)) {
mkdir($qrDir, 0777, true);
}
// QrCode::format('svg')->size(100)->generate($serial, $qrPath);
QrCode::format('png')->size(300)->errorCorrection('H')->generate($serial, $qrPath);
// Place QR code
@@ -428,13 +429,12 @@ class OcrValidationResource extends Resource
// Download
return response()->download($outputPath);
}
else
{
} else {
Notification::make()
->title('Please capture either 1 or 4 serial numbers.')
->danger()
->send();
return;
}
}),
@@ -443,11 +443,12 @@ class OcrValidationResource extends Resource
->action(function ($get) {
$equipmentNumber = $get('gr_number');
if (!$equipmentNumber) {
if (! $equipmentNumber) {
Notification::make()
->title('No GR Number entered')
->danger()
->send();
return;
}
@@ -461,16 +462,18 @@ class OcrValidationResource extends Resource
}
}
if (!$fileToDownload) {
if (! $fileToDownload) {
Notification::make()
->title('PDF not found for this process order')
->danger()
->send();
return;
}
return response()->download(Storage::disk('local')->path($fileToDownload));
}),
]),
]),
Forms\Components\Field::make('camera_capture')
->view('fields.camera-capture'),
Forms\Components\Hidden::make('created_by')
@@ -481,7 +484,6 @@ class OcrValidationResource extends Resource
]);
}
public static function table(Table $table): Table
{
return $table