import 'package:intl/intl.dart'; import 'package:pdf/pdf.dart'; import 'package:pdf/widgets.dart' as pw; import 'package:printing/printing.dart'; import '../../models/models.dart'; import 'package:spelling_number/spelling_number.dart'; String toTitleCase(String text) { if (text.isEmpty) return text; return text.split(' ').map((word) { if (word.isNotEmpty) { return word[0].toUpperCase() + word.substring(1).toLowerCase(); } return ''; }).join(' '); } class VentaTicket { static Future imprimirResumenPedidos( List pedidosDelDia, DateTime fecha) async { final pdf = pw.Document(); String formattedDate = DateFormat('dd-MM-yyyy').format(fecha); final pedidosNoCancelados = pedidosDelDia.where((p) => p.estatus != "CANCELADO").toList(); final pedidosCancelados = pedidosDelDia.where((p) => p.estatus == "CANCELADO").toList(); double totalNoCancelados = pedidosNoCancelados.fold(0, (sum, p) => sum + (p.totalPedido ?? 0)); double totalCancelados = pedidosCancelados.fold(0, (sum, p) => sum + (p.totalPedido ?? 0)); final spelling = SpellingNumber(lang: 'es'); String totalEnLetras = toTitleCase(spelling.convert(totalNoCancelados)); final numberFormat = NumberFormat('#,##0.00', 'en_US'); String formattedTotalNoCancelados = numberFormat.format(totalNoCancelados); String formattedTotalCancelados = numberFormat.format(totalCancelados); int centavos = ((totalNoCancelados - totalNoCancelados.floor()) * 100).round(); String centavosEnLetras = centavos.toString().padLeft(2, '0') + "/100 M.N."; print("Total en letras: $totalEnLetras $centavosEnLetras"); print("Total formateado: $formattedTotalNoCancelados"); pdf.addPage(pw.Page( pageFormat: PdfPageFormat.roll57, build: (pw.Context context) { return pw.Column( crossAxisAlignment: pw.CrossAxisAlignment.start, children: [ pw.Padding( padding: pw.EdgeInsets.only(bottom: 10), child: pw.Text('Pedidos del día: $formattedDate', style: pw.TextStyle( fontWeight: pw.FontWeight.bold, fontSize: 12)), ), pw.Padding( padding: const pw.EdgeInsets.only(right: 15), child: pw.Text("Pedidos Completados:", style: pw.TextStyle( fontWeight: pw.FontWeight.bold, fontSize: 10)), ), ...pedidosNoCancelados.map((pedido) { return pw.Padding( padding: const pw.EdgeInsets.only(right: 15), child: pw.Row( mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, children: [ pw.Text("Folio: ${pedido.folio}", style: pw.TextStyle(fontSize: 10)), pw.Text("\$${pedido.totalPedido?.toStringAsFixed(2)}", style: pw.TextStyle(fontSize: 10)), ], ), ); }).toList(), pw.Divider(), pw.Padding( padding: const pw.EdgeInsets.only(right: 15), child: pw.Column( crossAxisAlignment: pw.CrossAxisAlignment.start, children: [ pw.Text("Total: \$${formattedTotalNoCancelados}", style: pw.TextStyle( fontWeight: pw.FontWeight.bold, fontSize: 12)), pw.Text("Son: $totalEnLetras Pesos $centavosEnLetras", style: pw.TextStyle(fontSize: 10)) ], ), ), pw.SizedBox(height: 10), if (pedidosCancelados.isNotEmpty) ...[ pw.Padding( padding: const pw.EdgeInsets.only(right: 15), child: pw.Text("Pedidos Cancelados:", style: pw.TextStyle( fontWeight: pw.FontWeight.bold, fontSize: 10)), ), ...pedidosCancelados.map((pedido) { return pw.Padding( padding: const pw.EdgeInsets.only(right: 15), child: pw.Row( mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, children: [ pw.Text("Folio: ${pedido.folio} (Cancelado)", style: pw.TextStyle(fontSize: 10)), pw.Text("\$${pedido.totalPedido?.toStringAsFixed(2)}", style: pw.TextStyle(fontSize: 10)), ], ), ); }).toList(), pw.Divider(), pw.Padding( padding: const pw.EdgeInsets.only(right: 15), child: pw.Text( "Total Cancelados: \$${formattedTotalCancelados}", style: pw.TextStyle( fontWeight: pw.FontWeight.bold, fontSize: 12)), ), ], pw.SizedBox(height: 40), pw.Text('.', style: pw.TextStyle(fontSize: 1)), ], ); })); await Printing.layoutPdf( onLayout: (PdfPageFormat format) async => pdf.save()); } }