added livewire component in reosurce

This commit is contained in:
dhanabalan
2025-10-24 18:26:30 +05:30
parent 0c00c47aa3
commit 1bac8ee1db

View File

@@ -23,6 +23,7 @@ use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use setasign\Fpdi\PdfReader; use setasign\Fpdi\PdfReader;
use SimpleSoftwareIO\QrCode\Facades\QrCode; use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Storage; use Storage;
use Filament\Forms\Concerns\InteractsWithForms;
class OcrValidationResource extends Resource class OcrValidationResource extends Resource
{ {
@@ -31,8 +32,23 @@ class OcrValidationResource extends Resource
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
public $camera_capture; public $camera_capture;
public $serialNumbers = [];
protected $listeners = ['fillSerialNumbers'];
use InteractsWithForms;
public function fillSerialNumbers($serialNumbers)
{
$this->form->fill([
'serial_1' => $serialNumbers[0] ?? null,
'serial_2' => $serialNumbers[1] ?? null,
'serial_3' => $serialNumbers[2] ?? null,
'serial_4' => $serialNumbers[3] ?? null,
]);
}
public static function form(Form $form): Form public static function form(Form $form): Form
{ {
return $form return $form
@@ -96,7 +112,13 @@ class OcrValidationResource extends Resource
// Action::make('uploadNow1') // Action::make('uploadNow1')
// ->label('Upload OCR') // ->label('Upload OCR')
// ->action(function ($get, callable $set) { // ->action(function ($get, callable $set) {
// $uploadedFiles = $get('ocrSerials'); // $uploadedFiles = $get('photo');
// // $serialNumbers = [
// // $get('serial_1'),
// // $get('serial_2'),
// // $get('serial_3'),
// // $get('serial_4'),
// // ];
// if (is_array($uploadedFiles) && count($uploadedFiles) > 0) // if (is_array($uploadedFiles) && count($uploadedFiles) > 0)
// { // {
@@ -221,86 +243,111 @@ class OcrValidationResource extends Resource
// } // }
// }), // }),
Action::make('uploadNow1') Action::make('uploadNow1')
->label('Upload OCR') ->label('Upload OCR')
->action(function ($get, callable $set) { ->action(function ($get, callable $set) {
$serialNumbers = $get('ocrSerials'); // Get serials from Livewire state
if (empty($serialNumbers) || !is_array($serialNumbers)) { $serialNumbers = [
$this->notify('danger', 'No serial numbers found!'); $get('serial_1'),
return; $get('serial_2'),
} $get('serial_3'),
$get('serial_4'),
];
$grNumber = $get('gr_number'); $hasSerial = collect($serialNumbers)->some(fn($s) => !empty($s));
$itemId = $get('item_id');
$plantId = $get('plant_id');
$safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber); if ($hasSerial)
$finalFileName = $safeName . '.pdf'; {
$outputPath = storage_path('app/private/uploads/StickerTemplateOcr/' . $finalFileName);
$item = Item::find($itemId); $serialNumbers = array_slice($serialNumbers, 0, 4);
$plant = Plant::find($plantId);
$templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf'); //dd($serialNumbers);
$pdf = new Fpdi('P', 'mm', [90, 90]); $itemId = $get('item_id');
$templateId = $pdf->setSourceFile($templatePath);
$templatePage = $pdf->importPage(1);
$pdf->AddPage(); $plant = $get('plant_id');
$pdf->useTemplate($templatePage, 0, 0, 90, 90);
$pdf->SetFont('Helvetica', '', 10); $item = Item::find($itemId);
$pdf->SetTextColor(0, 0, 0);
// PDF slots for serials $plant = Plant::find($plant);
$slots = [
['x' => 5.7, 'y' => 41.9, 'w' => 46.5, 'h' => 3.5],
['x' => 50, 'y' => 41.5, 'w' => 46.6, 'h' => 3.9],
['x' => 5.7, 'y' => 60, 'w' => 46.5, 'h' => 3.5],
['x' => 50, 'y' => 60, 'w' => 46.6, 'h' => 3.5],
];
// QR code positions $templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf');
$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) { $outputPath = storage_path('app/private/uploads/StickerTemplateOcr/multi_filled.pdf');
if (!isset($slots[$i]) || !isset($qrSlots[$i])) continue;
// Erase old QR // $storedPath = $uploaded->storeAs(
$pdf->SetFillColor(255, 255, 255); // 'uploads/GRNumber',
$pdf->Rect($qrSlots[$i]['x']-1, $qrSlots[$i]['y']-1, $qrSlots[$i]['size']+2, $qrSlots[$i]['size']+2, 'F'); // $finalFileName,
// 'local'
// );
// Generate new QR code $pdf = new Fpdi('P', 'mm', [90, 90]);
$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 in PDF $templateId = $pdf->setSourceFile($templatePath);
$pdf->Image($qrPath, $qrSlots[$i]['x'], $qrSlots[$i]['y'], $qrSlots[$i]['size'], $qrSlots[$i]['size']); $templatePage = $pdf->importPage(1);
// Erase old serial text $pdf->AddPage();
$pdf->SetFillColor(255, 255, 255); $pdf->useTemplate($templatePage, 0, 0, 90, 90);
$pdf->Rect($slots[$i]['x'], $slots[$i]['y'], $slots[$i]['w'], $slots[$i]['h'], 'F');
// Write new serial $pdf->SetFont('Helvetica', '', 10);
$pdf->SetXY($slots[$i]['x'], $slots[$i]['y']); $pdf->SetTextColor(0, 0, 0);
$pdf->Cell($slots[$i]['w'], $slots[$i]['h'], $serial, 0, 0, 'L');
}
// Save the PDF $slots = [
$pdf->Output('F', $outputPath); ['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('Serial numbers cannot be empty')
->warning()
->send();
return;
}
}),
// Download
return response()->download($outputPath);
}),
Action::make('downloadAttachment') Action::make('downloadAttachment')
->label('Download PDF') ->label('Download PDF')
@@ -346,6 +393,7 @@ class OcrValidationResource extends Resource
]); ]);
} }
public static function table(Table $table): Table public static function table(Table $table): Table
{ {
return $table return $table