diff --git a/lib/features/customer_orders/utils/customer_order_label_pdf_service.dart b/lib/features/customer_orders/utils/customer_order_label_pdf_service.dart index b01c66d..b4d887a 100644 --- a/lib/features/customer_orders/utils/customer_order_label_pdf_service.dart +++ b/lib/features/customer_orders/utils/customer_order_label_pdf_service.dart @@ -1,42 +1,44 @@ import 'package:flux/core/blocs/session/session_cubit.dart'; import 'package:flux/features/customer_orders/models/customer_order_model.dart'; import 'package:get_it/get_it.dart'; -import 'package:intl/intl.dart'; import 'package:pdf/pdf.dart'; import 'package:pdf/widgets.dart' as pw; import 'package:printing/printing.dart'; class CustomerOrderLabelPdfService { - /// Genera il PDF dell'etichetta prodotto con QR code, leggendo le misure - /// configurate nelle Impostazioni Azienda (fallback 40 x 30 mm). + /// Genera il PDF dell'etichetta prodotto in orientamento verticale: + /// QR code grande in alto, nome cliente e descrizione prodotto in basso + /// (senza prezzo/codice cassa così l'operatore scansiona il QR alla consegna). Future generateOrderLabelPdf(CustomerOrderModel order) async { final pdf = pw.Document(); final font = await PdfGoogleFonts.robotoRegular(); final boldFont = await PdfGoogleFonts.robotoBold(); final company = GetIt.I.get().state.company; - final widthMm = company?.labelWidth ?? 40.0; - final heightMm = company?.labelHeight ?? 30.0; - final isVertical = company?.isLabelVertical ?? false; + final rawW = company?.labelWidth ?? 38.0; + final rawH = company?.labelHeight ?? 25.0; - final pageFormat = isVertical - ? PdfPageFormat(heightMm * PdfPageFormat.mm, widthMm * PdfPageFormat.mm) - : PdfPageFormat(widthMm * PdfPageFormat.mm, heightMm * PdfPageFormat.mm); + // Orientamento sempre verticale: lato corto come larghezza, lato lungo come altezza (es. 25x38mm) + final shortSideMm = rawW < rawH ? rawW : rawH; + final longSideMm = rawW > rawH ? rawW : rawH; - final effectiveW = pageFormat.width; - final effectiveH = pageFormat.height; - final minSide = effectiveW < effectiveH ? effectiveW : effectiveH; - final qrSize = (minSide * 0.45).clamp( - 10 * PdfPageFormat.mm, - 16 * PdfPageFormat.mm, + final pageFormat = PdfPageFormat( + shortSideMm * PdfPageFormat.mm, + longSideMm * PdfPageFormat.mm, + ); + + const margin = 1.5 * PdfPageFormat.mm; + final contentWidth = pageFormat.width - (margin * 2); + + // QR grande quasi quanto tutta la larghezza disponibile per lettura immediata su 25x38mm + final qrSize = (contentWidth - 1 * PdfPageFormat.mm).clamp( + 14 * PdfPageFormat.mm, + 24 * PdfPageFormat.mm, ); final customerName = order.customer?.name ?? 'Cliente'; final qtySuffix = order.quantity > 1 ? ' (x${order.quantity})' : ''; final productLine = '${order.productDescription}$qtySuffix'; - final currencyFmt = NumberFormat.currency(locale: 'it_IT', symbol: '€'); - final priceStr = order.price > 0 ? currencyFmt.format(order.price) : ''; - final codeStr = order.externalCode.trim(); final qrData = order.deepLinkUrl.isNotEmpty ? order.deepLinkUrl : (order.id ?? ''); @@ -44,96 +46,36 @@ class CustomerOrderLabelPdfService { pdf.addPage( pw.Page( pageFormat: pageFormat, - margin: const pw.EdgeInsets.all(2 * PdfPageFormat.mm), + margin: const pw.EdgeInsets.all(margin), build: (context) { - final headerName = pw.Container( - width: double.infinity, - padding: const pw.EdgeInsets.only(bottom: 1.5), - decoration: const pw.BoxDecoration( - border: pw.Border( - bottom: pw.BorderSide(width: 0.5, color: PdfColors.grey600), - ), - ), - child: pw.FittedBox( - fit: pw.BoxFit.scaleDown, - alignment: pw.Alignment.centerLeft, - child: pw.Text( - customerName, - style: pw.TextStyle(font: boldFont, fontSize: 11.5), - maxLines: 1, - ), - ), - ); - - final detailsBlock = pw.Column( - crossAxisAlignment: pw.CrossAxisAlignment.start, - mainAxisAlignment: pw.MainAxisAlignment.center, - mainAxisSize: pw.MainAxisSize.min, + return pw.Column( + mainAxisAlignment: pw.MainAxisAlignment.start, + crossAxisAlignment: pw.CrossAxisAlignment.center, children: [ + pw.BarcodeWidget( + barcode: pw.Barcode.qrCode( + errorCorrectLevel: pw.BarcodeQRCorrectionLevel.low, + ), + data: qrData, + width: qrSize, + height: qrSize, + ), + pw.SizedBox(height: 1.5 * PdfPageFormat.mm), pw.Text( - productLine, - style: pw.TextStyle(font: font, fontSize: 7.5), + customerName, + style: pw.TextStyle(font: boldFont, fontSize: 9.5), + textAlign: pw.TextAlign.center, maxLines: 2, overflow: pw.TextOverflow.clip, ), - if (codeStr.isNotEmpty) ...[ - pw.SizedBox(height: 1.5), - pw.Text( - 'Cod: $codeStr', - style: pw.TextStyle(font: boldFont, fontSize: 7.2), - maxLines: 1, - overflow: pw.TextOverflow.clip, - ), - ], - if (priceStr.isNotEmpty) ...[ - pw.SizedBox(height: 2), - pw.FittedBox( - fit: pw.BoxFit.scaleDown, - alignment: pw.Alignment.centerLeft, - child: pw.Text( - priceStr, - style: pw.TextStyle(font: boldFont, fontSize: 11.5), - maxLines: 1, - ), - ), - ], - ], - ); - - final qrWidget = pw.BarcodeWidget( - barcode: pw.Barcode.qrCode(), - data: qrData, - width: qrSize, - height: qrSize, - ); - - if (effectiveH > effectiveW) { - return pw.Column( - crossAxisAlignment: pw.CrossAxisAlignment.stretch, - children: [ - headerName, - pw.SizedBox(height: 2), - pw.Expanded(child: detailsBlock), - pw.SizedBox(height: 2), - pw.Center(child: qrWidget), - ], - ); - } - - return pw.Column( - crossAxisAlignment: pw.CrossAxisAlignment.stretch, - children: [ - headerName, - pw.SizedBox(height: 2), + pw.SizedBox(height: 0.8 * PdfPageFormat.mm), pw.Expanded( - child: pw.Row( - mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, - crossAxisAlignment: pw.CrossAxisAlignment.center, - children: [ - pw.Expanded(child: detailsBlock), - pw.SizedBox(width: 2 * PdfPageFormat.mm), - qrWidget, - ], + child: pw.Text( + productLine, + style: pw.TextStyle(font: font, fontSize: 7.2), + textAlign: pw.TextAlign.center, + maxLines: 2, + overflow: pw.TextOverflow.clip, ), ), ],