From 52c1cce878105437687f7326ffc5b7b0a8398a0b Mon Sep 17 00:00:00 2001 From: Mark-cachy Date: Fri, 4 Dec 2026 19:30:24 +0100 Subject: [PATCH] feat: add internal deep link URL and update bag label and customer receipt in ticket form --- lib/features/tickets/models/ticket_model.dart | 5 + .../tickets/ui/ticket_form_screen.dart | 49 ++++++---- .../tickets/utils/ticket_pdf_service.dart | 95 +++++++++++-------- 3 files changed, 94 insertions(+), 55 deletions(-) diff --git a/lib/features/tickets/models/ticket_model.dart b/lib/features/tickets/models/ticket_model.dart index de9ba58..4aa9db9 100644 --- a/lib/features/tickets/models/ticket_model.dart +++ b/lib/features/tickets/models/ticket_model.dart @@ -170,6 +170,11 @@ class TicketModel extends Equatable { String get publicTrackingUrl => id != null && id!.isNotEmpty ? 'https://flux.catelli.it/ticket/$id' : ''; + /// URL interno (per QR etichetta sacchetto) che apre direttamente la scheda ticket in FLUX + String get internalDeepLinkUrl => id != null && id!.isNotEmpty + ? 'https://flux.catelli.it/tickets/form/$id' + : ''; + /// Recapito effettivo da usare in base alla preferenza di contatto scelta String get effectiveContactDestination { if (contactPreference.isEmail) { diff --git a/lib/features/tickets/ui/ticket_form_screen.dart b/lib/features/tickets/ui/ticket_form_screen.dart index b99a3eb..4315d0d 100644 --- a/lib/features/tickets/ui/ticket_form_screen.dart +++ b/lib/features/tickets/ui/ticket_form_screen.dart @@ -310,24 +310,12 @@ class _TicketFormScreenState extends State { crossAxisSpacing: 16, childAspectRatio: 1.5, children: [ - _ActionButton( - icon: Icons.print, - label: "Ricevuta A4", - onTap: () async { - final doc = await TicketPdfService() - .generateTicketReceipt(ticket); - final bytes = await doc.save(); - final fileName = 'Ricevuta_${ticket.referenceId}.pdf'; - await Printing.layoutPdf( - onLayout: (format) async => bytes, - name: fileName, - ); - }, - ), if (company.labelFormat != LabelFormat.none) _ActionButton( - icon: Icons.label, - label: "Etichetta", + icon: Icons.qr_code_2_rounded, + label: "Etichetta Sacchetto", + color: Colors.teal.shade700, + textColor: Colors.white, onTap: () async { final doc = await TicketPdfService().generateLabelPdf( ticket, @@ -348,6 +336,29 @@ class _TicketFormScreenState extends State { } }, ), + _ActionButton( + icon: Icons.print, + label: "Ricevuta Cliente", + color: (notificationResult == null || + !notificationResult.success) + ? Colors.indigo.shade600 + : null, + textColor: (notificationResult == null || + !notificationResult.success) + ? Colors.white + : null, + onTap: () async { + final doc = await TicketPdfService() + .generateCustomerReceiptPdf(ticket); + final bytes = await doc.save(); + final fileName = + 'Ricevuta_Cliente_${ticket.referenceId}.pdf'; + await Printing.layoutPdf( + onLayout: (format) async => bytes, + name: fileName, + ); + }, + ), _ActionButton( icon: Icons.link_rounded, label: "Copia Link Stato", @@ -374,7 +385,11 @@ class _TicketFormScreenState extends State { textColor: Colors.white, onTap: () { Navigator.of(sheetCtx).pop(); - Navigator.of(context).pop(); + if (context.canPop()) { + context.pop(); + } else { + context.goNamed(Routes.tickets); + } }, ), ], diff --git a/lib/features/tickets/utils/ticket_pdf_service.dart b/lib/features/tickets/utils/ticket_pdf_service.dart index 0fc4635..f03c8eb 100644 --- a/lib/features/tickets/utils/ticket_pdf_service.dart +++ b/lib/features/tickets/utils/ticket_pdf_service.dart @@ -648,57 +648,76 @@ class TicketPdfService { return pdf; } + /// Genera l'etichetta adesiva verticale per il sacchetto del prodotto: + /// QR Code grande in alto (link alla scheda lavorazione), nome cliente e difetto sotto. Future generateLabelPdf(TicketModel ticket) async { final pdf = pw.Document(); final font = await PdfGoogleFonts.robotoRegular(); final boldFont = await PdfGoogleFonts.robotoBold(); - // Prendiamo le misure salvate (se custom) o usiamo default - final widthMm = company.labelWidth ?? 62.0; - final heightMm = company.labelHeight ?? 29.0; + final rawW = company.labelWidth ?? 38.0; + final rawH = company.labelHeight ?? 25.0; - // Creiamo il formato fisico esatto! - final format = company.isLabelVertical - ? 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 format = PdfPageFormat( + shortSideMm * PdfPageFormat.mm, + longSideMm * PdfPageFormat.mm, + ); + + const margin = 1.5 * PdfPageFormat.mm; + final contentWidth = format.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 = ticket.customer?.name ?? 'Cliente sconosciuto'; + final defectLine = ticket.request.trim().isNotEmpty + ? ticket.request.trim() + : (ticket.targetModelName ?? ticket.ticketType.displayValue); + final qrData = ticket.internalDeepLinkUrl.isNotEmpty + ? ticket.internalDeepLinkUrl + : (ticket.id ?? ''); pdf.addPage( pw.Page( pageFormat: format, - margin: const pw.EdgeInsets.all(2), // Margini minimi per le etichette + margin: const pw.EdgeInsets.all(margin), build: (context) { - return pw.Row( - mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, + return pw.Column( + mainAxisAlignment: pw.MainAxisAlignment.start, + crossAxisAlignment: pw.CrossAxisAlignment.center, children: [ - pw.Expanded( - child: pw.Column( - crossAxisAlignment: pw.CrossAxisAlignment.start, - mainAxisAlignment: pw.MainAxisAlignment.center, - children: [ - pw.Text( - ticket.referenceId ?? '', - style: pw.TextStyle(font: boldFont, fontSize: 10), - ), - pw.Text( - ticket.customer?.name ?? 'Cliente sconosciuto', - style: pw.TextStyle(font: font, fontSize: 9), - ), - pw.Text( - ticket.createdAt?.toString().substring(0, 10) ?? '', - style: const pw.TextStyle(fontSize: 7), - ), - ], - ), - ), - // QR Code compatto pw.BarcodeWidget( - barcode: pw.Barcode.qrCode(), - data: ticket.id!, - width: 20, - height: 20, + barcode: pw.Barcode.qrCode( + errorCorrectLevel: pw.BarcodeQRCorrectionLevel.low, + ), + data: qrData, + width: qrSize, + height: qrSize, + ), + pw.SizedBox(height: 1.5 * PdfPageFormat.mm), + pw.Text( + customerName, + style: pw.TextStyle(font: boldFont, fontSize: 9.5), + textAlign: pw.TextAlign.center, + maxLines: 2, + overflow: pw.TextOverflow.clip, + ), + pw.SizedBox(height: 0.8 * PdfPageFormat.mm), + pw.Expanded( + child: pw.Text( + defectLine, + style: pw.TextStyle(font: font, fontSize: 7.2), + textAlign: pw.TextAlign.center, + maxLines: 2, + overflow: pw.TextOverflow.clip, + ), ), ], );