venta_ticket.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'package:intl/intl.dart';
  2. import 'package:pdf/pdf.dart';
  3. import 'package:pdf/widgets.dart' as pw;
  4. import 'package:printing/printing.dart';
  5. import '../../models/models.dart';
  6. import 'package:spelling_number/spelling_number.dart';
  7. String toTitleCase(String text) {
  8. if (text.isEmpty) return text;
  9. return text.split(' ').map((word) {
  10. if (word.isNotEmpty) {
  11. return word[0].toUpperCase() + word.substring(1).toLowerCase();
  12. }
  13. return '';
  14. }).join(' ');
  15. }
  16. class VentaTicket {
  17. static Future<void> imprimirResumenPedidos(
  18. List<Pedido> pedidosDelDia, DateTime fecha) async {
  19. final pdf = pw.Document();
  20. String formattedDate = DateFormat('dd-MM-yyyy').format(fecha);
  21. final pedidosNoCancelados =
  22. pedidosDelDia.where((p) => p.estatus != "CANCELADO").toList();
  23. final pedidosCancelados =
  24. pedidosDelDia.where((p) => p.estatus == "CANCELADO").toList();
  25. double totalNoCancelados =
  26. pedidosNoCancelados.fold(0, (sum, p) => sum + (p.totalPedido ?? 0));
  27. double totalCancelados =
  28. pedidosCancelados.fold(0, (sum, p) => sum + (p.totalPedido ?? 0));
  29. final spelling = SpellingNumber(lang: 'es');
  30. String totalEnLetras = toTitleCase(spelling.convert(totalNoCancelados));
  31. final numberFormat = NumberFormat('#,##0.00', 'en_US');
  32. String formattedTotalNoCancelados = numberFormat.format(totalNoCancelados);
  33. String formattedTotalCancelados = numberFormat.format(totalCancelados);
  34. int centavos =
  35. ((totalNoCancelados - totalNoCancelados.floor()) * 100).round();
  36. String centavosEnLetras = centavos.toString().padLeft(2, '0') + "/100 M.N.";
  37. print("Total en letras: $totalEnLetras $centavosEnLetras");
  38. print("Total formateado: $formattedTotalNoCancelados");
  39. pdf.addPage(pw.Page(
  40. pageFormat: PdfPageFormat.roll57,
  41. build: (pw.Context context) {
  42. return pw.Column(
  43. crossAxisAlignment: pw.CrossAxisAlignment.start,
  44. children: [
  45. pw.Padding(
  46. padding: pw.EdgeInsets.only(bottom: 10),
  47. child: pw.Text('Pedidos del día: $formattedDate',
  48. style: pw.TextStyle(
  49. fontWeight: pw.FontWeight.bold, fontSize: 12)),
  50. ),
  51. pw.Padding(
  52. padding: const pw.EdgeInsets.only(right: 15),
  53. child: pw.Text("Pedidos Completados:",
  54. style: pw.TextStyle(
  55. fontWeight: pw.FontWeight.bold, fontSize: 10)),
  56. ),
  57. ...pedidosNoCancelados.map((pedido) {
  58. return pw.Padding(
  59. padding: const pw.EdgeInsets.only(right: 15),
  60. child: pw.Row(
  61. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  62. children: [
  63. pw.Text("Folio: ${pedido.folio}",
  64. style: pw.TextStyle(fontSize: 10)),
  65. pw.Text("\$${pedido.totalPedido?.toStringAsFixed(2)}",
  66. style: pw.TextStyle(fontSize: 10)),
  67. ],
  68. ),
  69. );
  70. }).toList(),
  71. pw.Divider(),
  72. pw.Padding(
  73. padding: const pw.EdgeInsets.only(right: 15),
  74. child: pw.Column(
  75. crossAxisAlignment: pw.CrossAxisAlignment.start,
  76. children: [
  77. pw.Text("Total: \$${formattedTotalNoCancelados}",
  78. style: pw.TextStyle(
  79. fontWeight: pw.FontWeight.bold, fontSize: 12)),
  80. pw.Text("Son: $totalEnLetras Pesos $centavosEnLetras",
  81. style: pw.TextStyle(fontSize: 10))
  82. ],
  83. ),
  84. ),
  85. pw.SizedBox(height: 10),
  86. if (pedidosCancelados.isNotEmpty) ...[
  87. pw.Padding(
  88. padding: const pw.EdgeInsets.only(right: 15),
  89. child: pw.Text("Pedidos Cancelados:",
  90. style: pw.TextStyle(
  91. fontWeight: pw.FontWeight.bold, fontSize: 10)),
  92. ),
  93. ...pedidosCancelados.map((pedido) {
  94. return pw.Padding(
  95. padding: const pw.EdgeInsets.only(right: 15),
  96. child: pw.Row(
  97. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  98. children: [
  99. pw.Text("Folio: ${pedido.folio} (Cancelado)",
  100. style: pw.TextStyle(fontSize: 10)),
  101. pw.Text("\$${pedido.totalPedido?.toStringAsFixed(2)}",
  102. style: pw.TextStyle(fontSize: 10)),
  103. ],
  104. ),
  105. );
  106. }).toList(),
  107. pw.Divider(),
  108. pw.Padding(
  109. padding: const pw.EdgeInsets.only(right: 15),
  110. child: pw.Text(
  111. "Total Cancelados: \$${formattedTotalCancelados}",
  112. style: pw.TextStyle(
  113. fontWeight: pw.FontWeight.bold, fontSize: 12)),
  114. ),
  115. ],
  116. pw.SizedBox(height: 40),
  117. pw.Text('.', style: pw.TextStyle(fontSize: 1)),
  118. ],
  119. );
  120. }));
  121. await Printing.layoutPdf(
  122. onLayout: (PdfPageFormat format) async => pdf.save());
  123. }
  124. }