e503cd3479
- Fix malformed route on line 84 (missing closing paren and semicolon) - Replace SimpleSoftwareIO QrCode with chillerlan/php-qrcode (already installed) - Update QrStickerService to use chillerlan\QRCode\QRCode - Configure QR options for SVG output with H error correction
127 lines
3.6 KiB
PHP
127 lines
3.6 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Models\Order;
|
||
use Barryvdh\DomPDF\Facade\Pdf;
|
||
use chillerlan\QRCode\QRCode;
|
||
use chillerlan\QRCode\QROptions;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Illuminate\Support\Facades\Storage;
|
||
|
||
class QrStickerService
|
||
{
|
||
/**
|
||
* Generate both SVG and A6 PDF sticker for an order
|
||
*/
|
||
public function generateSticker(Order $order): array
|
||
{
|
||
try {
|
||
// Generate QR code URL
|
||
$qrUrl = route('ops.order.show', ['token' => $order->qr_token]);
|
||
|
||
Log::debug('Generating QR sticker', [
|
||
'order_uuid' => $order->uuid,
|
||
'order_number' => $order->order_number,
|
||
'qr_url' => $qrUrl,
|
||
]);
|
||
|
||
// Generate SVG (for web display)
|
||
$svgPath = $this->generateSvg($order, $qrUrl);
|
||
|
||
// Generate A6 PDF (for printing)
|
||
$pdfPath = $this->generatePdf($order, $qrUrl);
|
||
|
||
Log::info('QR sticker generated successfully', [
|
||
'order_uuid' => $order->uuid,
|
||
'svg_path' => $svgPath,
|
||
'pdf_path' => $pdfPath,
|
||
]);
|
||
|
||
return [
|
||
'svg_path' => $svgPath,
|
||
'pdf_path' => $pdfPath,
|
||
];
|
||
} catch (\Exception $e) {
|
||
Log::error('Failed to generate QR sticker', [
|
||
'order_uuid' => $order->uuid,
|
||
'error' => $e->getMessage(),
|
||
]);
|
||
|
||
throw $e;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Generate SVG QR code for web display
|
||
*/
|
||
private function generateSvg(Order $order, string $qrUrl): string
|
||
{
|
||
$options = new QROptions([
|
||
'outputType' => QRCode::OUTPUT_MARKUP_SVG,
|
||
'eccLevel' => QRCode::ECC_H,
|
||
'scale' => 3,
|
||
'imageBase64' => false,
|
||
]);
|
||
|
||
$qrCode = new QRCode($options);
|
||
$qrSvg = $qrCode->render($qrUrl);
|
||
|
||
$path = "qr-codes/{$order->uuid}.svg";
|
||
Storage::disk('public')->put($path, $qrSvg);
|
||
|
||
return $path;
|
||
}
|
||
|
||
/**
|
||
* Generate A6 PDF sticker (105mm × 148mm) with QR code and readable info
|
||
*
|
||
* A6 dimensions: 105mm × 148mm landscape
|
||
* Margins: 5mm all around
|
||
* QR size: ~50mm × 50mm
|
||
* Text area: remaining space for order info
|
||
*/
|
||
private function generatePdf(Order $order, string $qrUrl): string
|
||
{
|
||
// Generate inline QR code as SVG
|
||
$options = new QROptions([
|
||
'outputType' => QRCode::OUTPUT_MARKUP_SVG,
|
||
'eccLevel' => QRCode::ECC_H,
|
||
'scale' => 4,
|
||
'imageBase64' => false,
|
||
]);
|
||
|
||
$qrCode = new QRCode($options);
|
||
$qrSvg = $qrCode->render($qrUrl);
|
||
|
||
// Determine order type and format label
|
||
$orderType = $order->type === 'standard'
|
||
? 'STOCK ORDER'
|
||
: 'CUSTOM ORDER';
|
||
|
||
// Prepare data for the view
|
||
$data = [
|
||
'order' => $order,
|
||
'qrSvg' => $qrSvg,
|
||
'orderType' => $orderType,
|
||
'qrUrl' => $qrUrl,
|
||
'generatedAt' => now()->format('Y-m-d H:i'),
|
||
];
|
||
|
||
// Generate PDF using the sticker template
|
||
$pdf = Pdf::loadView('stickers.qr-sticker', $data);
|
||
|
||
// Configure for A6 landscape
|
||
// A6 is 105mm × 148mm (landscape: 148mm × 105mm)
|
||
$pdf->setPaper([0, 0, 420.94, 297.64], 'portrait'); // 148mm × 105mm in points (1mm ≈ 2.834645669 points)
|
||
|
||
// Store PDF
|
||
$filename = "{$order->uuid}.pdf";
|
||
$path = "qr-stickers/{$filename}";
|
||
$pdfContent = $pdf->output();
|
||
Storage::disk('public')->put($path, $pdfContent);
|
||
|
||
return $path;
|
||
}
|
||
}
|