Merge pull request 'Changed printer logic in sticker validation' (#190) from ranjith-dev into master
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled

Reviewed-on: #190
This commit was merged in pull request #190.
This commit is contained in:
2026-04-27 05:37:14 +00:00

View File

@@ -6,8 +6,11 @@ use App\Filament\Resources\StickerValidationResource;
use App\Models\Item;
use App\Models\ItemCharacteristic;
use App\Models\ProductionQuantity;
use App\Models\StickerDetail;
use App\Models\StickerMappingMaster;
use App\Models\StickerStructureDetail;
use App\Models\StickerValidation;
use App\Services\StickerPdfService;
use Filament\Actions;
use Filament\Facades\Filament;
use Filament\Notifications\Notification;
@@ -92,8 +95,7 @@ class CreateStickerValidation extends CreateRecord
return;
}
if (preg_match('/^([a-zA-Z0-9]{6,})\|([1-9][a-zA-Z0-9]{8,})\|?$/', $this->serNo, $matches)) {
else {
$itemCode = $matches[1];
$serialNumber = $matches[2];
@@ -195,58 +197,6 @@ class CreateStickerValidation extends CreateRecord
return;
}
$urls = [];
foreach ($stickers as $sticker) {
$urls[] = route('stickers1.pdf', [
'stickerId' => $sticker['sticker_id'],
'plant_id' => $this->plantId,
'item_characteristic_id' => $sticker['item_characteristic'],
'serial_number' => $serialNumber,
]);
}
$this->dispatch('open-stickers-sequence', urls: $urls);
// $pdfPath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf');
// if (! file_exists($pdfPath)) {
// Notification::make()
// ->danger()
// ->title('Pdf Not Found')
// ->body("pdf file not exist.")
// ->send();
// return;
// }
// if (! $printerName) {
// Notification::make()
// ->danger()
// ->title('Printer Not Found')
// ->body("No CUPS printer configured for IP: $iotsPrintIp")
// ->send();
// return;
// }
// putenv('CUPS_SERVER=printer.iotsignin.com');
// $cmd = "lp -d " . escapeshellarg($printerName)
// . " -o fit-to-page "
// . escapeshellarg($pdfPath);
// exec($cmd, $out, $status);
// if ($status != 0) {
// Notification::make()
// ->danger()
// ->title('Print Failed')
// ->body('CUPS print command failed.')
// ->send();
// return;
// }
//dd($iotsPrintIp, $matchedSticker);
StickerValidation::create([
'plant_id' => $this->plantId,
'machine_id' => $this->workCenter,
@@ -275,24 +225,192 @@ class CreateStickerValidation extends CreateRecord
$this->dispatch('refreshEmptySticker', $plantId, $this->ref_number);
}
// foreach ($stickers as $sticker) {
// // $printerName = $this->getCupsPrinterNameByIp($sticker['print_ip']);
// \Log::info("Looking up printer for IP: " . $sticker['print_ip']);
// $printerName = $this->getCupsPrinterNameByIp($sticker['print_ip']);
// \Log::info("Found printer: " . ($printerName ?? 'NULL'));
// if (! $printerName) {
// Notification::make()
// ->danger()
// ->title('Printer Not Found')
// ->body("No CUPS printer configured for IP: {$sticker['print_ip']}")
// ->send();
// return;
// }
// $structure = StickerStructureDetail::findOrFail($sticker['sticker_id']);
// $itemCharacteristic = ItemCharacteristic::where('plant_id', $this->plantId)
// ->where('id', $sticker['item_characteristic'])
// ->firstOrFail();
// $dynamicElements = StickerDetail::where(
// 'sticker_structure_detail_id',
// $structure->id
// )->where('element_type', 'Dynamic')->get();
// /** STEP 3: Stream PDF to CUPS (STDIN) */
// $process = proc_open(
// 'lp -d ' . escapeshellarg($printerName) . ' -o fit-to-page -',
// [
// ['pipe', 'r'], // STDIN
// ['pipe', 'w'], // STDOUT
// ['pipe', 'w'], // STDERR
// ],
// $pipes
// );
// if (! is_resource($process)) {
// Notification::make()
// ->danger()
// ->title('Print Failed')
// ->body('Unable to start CUPS print process.')
// ->send();
// return;
// // continue;
// }
// $pdfContent = (new StickerPdfService())->generatePdf1(
// $structure->sticker_id,
// $dynamicElements,
// $itemCharacteristic,
// $serialNumber,
// $serNo
// );
// fwrite($pipes[0], $pdfContent);
// fclose($pipes[0]);
// $stderr = stream_get_contents($pipes[2]);
// fclose($pipes[1]);
// fclose($pipes[2]);
// $status = proc_close($process);
// if ($status != 0) {
// Notification::make()
// ->danger()
// ->title('Print Failed')
// ->body("CUPS error: {$stderr}")
// ->send();
// return;
// }
// }
foreach ($stickers as $sticker)
{
\Log::info("Looking up printer for IP: " . $sticker['print_ip']);
$printerName = $this->getCupsPrinterNameByIp($sticker['print_ip']);
\Log::info("Found printer: " . ($printerName ?? 'NULL'));
if (! $printerName) {
Notification::make()
->danger()
->title('Printer Not Found')
->body("No CUPS printer configured for IP: {$sticker['print_ip']}")
->send();
return;
}
$structure = StickerStructureDetail::findOrFail($sticker['sticker_id']);
$itemCharacteristic = ItemCharacteristic::where('plant_id', $this->plantId)
->where('id', $sticker['item_characteristic'])
->firstOrFail();
$dynamicElements = StickerDetail::where(
'sticker_structure_detail_id',
$structure->id
)->where('element_type', 'Dynamic')->get();
$pdfContent = (new StickerPdfService())->generatePdf1(
$structure->sticker_id,
$dynamicElements,
$itemCharacteristic,
$serialNumber,
$serNo
);
$tempPdfPath = storage_path('app/temp_sticker_' . uniqid() . '.pdf');
file_put_contents($tempPdfPath, $pdfContent);
exec(
"lp -d " . escapeshellarg($printerName) . " " . escapeshellarg($tempPdfPath),
$output,
$status
);
\Log::info("LP Output:", $output);
\Log::info("LP Status: " . $status);
if ($status != 0) {
Notification::make()
->danger()
->title('Print Failed')
->body("CUPS error while printing.")
->send();
if (file_exists($tempPdfPath)) {
unlink($tempPdfPath);
}
return;
}
if (file_exists($tempPdfPath)) {
unlink($tempPdfPath);
}
}
Notification::make()
->success()
->title('Sticker Printed')
->body("Sticker for Serial Number: $serialNumber printed successfully!")
->seconds(3)
->send();
// [$itemCode, $serialNumber] = explode('|', $serNo);
// $this->dispatch('open-sticker-pdf', [
// 'url' => url("/sticker/pdf/{$itemCode}/{$serialNumber}/$this->plantId/$this->ref_number")
// ]);
}
}
private function getPrinterNameByIp(string $ip): ?string
protected function getCupsPrinterNameByIp(string $ip): ?string
{
exec('lpstat -v', $output);
foreach ($output as $line) {
// Example:
// device for TSC_WC_01: socket://192.168.1.50:9100
if (str_contains($line, $ip)) {
preg_match('/device for (.+?):/', $line, $matches);
return $matches[1] ?? null;
// exec('lpstat -v 2>&1', $output, $status);
exec('lpstat -h cups:631 -v 2>&1', $output, $status);
if ($status != 0 || empty($output)) {
return null;
}
foreach ($output as $line){
$parts = explode(':', $line, 2);
if (count($parts) < 2) continue;
$printerName = trim(str_replace('device for', '', $parts[0]));
$deviceUri = trim($parts[1]);
if (str_contains($deviceUri, $ip)) {
return $printerName;
}
}
return null;
}
}