venta_ticket.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. double totalEfectivo = pedidosNoCancelados.fold(
  30. 0.0, (sum, p) => sum + (p.cantEfectivo ?? 0.0));
  31. double totalTarjeta =
  32. pedidosNoCancelados.fold(0.0, (sum, p) => sum + (p.cantTarjeta ?? 0.0));
  33. double totalTransferencia = pedidosNoCancelados.fold(
  34. 0.0, (sum, p) => sum + (p.cantTransferencia ?? 0.0));
  35. double totalSinCambio = totalEfectivo + totalTarjeta + totalTransferencia;
  36. double cambio = totalSinCambio - totalNoCancelados;
  37. final spelling = SpellingNumber(lang: 'es');
  38. String totalEnLetras = toTitleCase(spelling.convert(totalNoCancelados));
  39. final numberFormat = NumberFormat('#,##0.00', 'en_US');
  40. String formattedTotalNoCancelados = numberFormat.format(totalNoCancelados);
  41. String formattedTotalCancelados = numberFormat.format(totalCancelados);
  42. String formattedTotalEfectivo = numberFormat.format(totalEfectivo);
  43. String formattedTotalTarjeta = numberFormat.format(totalTarjeta);
  44. String formattedTotalTransferencia =
  45. numberFormat.format(totalTransferencia);
  46. String formattedCambio = numberFormat.format(cambio);
  47. int centavos =
  48. ((totalNoCancelados - totalNoCancelados.floor()) * 100).round();
  49. String centavosEnLetras = centavos.toString().padLeft(2, '0') + "/100 M.N.";
  50. pdf.addPage(pw.Page(
  51. pageFormat: PdfPageFormat.roll57,
  52. build: (pw.Context context) {
  53. return pw.Column(
  54. crossAxisAlignment: pw.CrossAxisAlignment.start,
  55. children: [
  56. pw.Padding(
  57. padding: pw.EdgeInsets.only(bottom: 10),
  58. child: pw.Text('Pedidos del día: $formattedDate',
  59. style: pw.TextStyle(
  60. fontWeight: pw.FontWeight.bold, fontSize: 12)),
  61. ),
  62. pw.Padding(
  63. padding: const pw.EdgeInsets.only(right: 15),
  64. child: pw.Text("Pedidos Completados:",
  65. style: pw.TextStyle(
  66. fontWeight: pw.FontWeight.bold, fontSize: 10)),
  67. ),
  68. ...pedidosNoCancelados.map((pedido) {
  69. return pw.Padding(
  70. padding: const pw.EdgeInsets.only(right: 15),
  71. child: pw.Row(
  72. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  73. children: [
  74. pw.Text("Folio: ${pedido.folio}",
  75. style: pw.TextStyle(fontSize: 10)),
  76. pw.Text("\$${pedido.totalPedido?.toStringAsFixed(2)}",
  77. style: pw.TextStyle(fontSize: 10)),
  78. ],
  79. ),
  80. );
  81. }).toList(),
  82. pw.Divider(),
  83. pw.Padding(
  84. padding: const pw.EdgeInsets.only(right: 15),
  85. child: pw.Column(
  86. crossAxisAlignment: pw.CrossAxisAlignment.start,
  87. children: [
  88. if (totalTarjeta > 0)
  89. pw.Text("-Tarjeta: \$${formattedTotalTarjeta}",
  90. style: pw.TextStyle(
  91. fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
  92. if (totalTransferencia > 0)
  93. pw.Text("-Transf: \$${formattedTotalTransferencia}",
  94. style: pw.TextStyle(
  95. fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
  96. if (totalEfectivo > 0)
  97. pw.Text("-Efectivo: \$${formattedTotalEfectivo}",
  98. style: pw.TextStyle(
  99. fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
  100. if (cambio > 0)
  101. pw.Text("-Cambio: \$${formattedCambio}",
  102. style: pw.TextStyle(
  103. fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
  104. pw.Text("-Total: \$${formattedTotalNoCancelados}",
  105. style: pw.TextStyle(
  106. fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
  107. pw.Text("Son: $totalEnLetras Pesos $centavosEnLetras",
  108. style: pw.TextStyle(fontSize: 10))
  109. ],
  110. ),
  111. ),
  112. pw.SizedBox(height: 10),
  113. if (pedidosCancelados.isNotEmpty) ...[
  114. pw.Padding(
  115. padding: const pw.EdgeInsets.only(right: 15),
  116. child: pw.Text("Pedidos Cancelados:",
  117. style: pw.TextStyle(
  118. fontWeight: pw.FontWeight.bold, fontSize: 10)),
  119. ),
  120. ...pedidosCancelados.map((pedido) {
  121. return pw.Padding(
  122. padding: const pw.EdgeInsets.only(right: 15),
  123. child: pw.Row(
  124. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  125. children: [
  126. pw.Text("Folio: ${pedido.folio} (Cancelado)",
  127. style: pw.TextStyle(fontSize: 10)),
  128. pw.Text("\$${pedido.totalPedido?.toStringAsFixed(2)}",
  129. style: pw.TextStyle(fontSize: 10)),
  130. ],
  131. ),
  132. );
  133. }).toList(),
  134. pw.Divider(),
  135. pw.Padding(
  136. padding: const pw.EdgeInsets.only(right: 15),
  137. child: pw.Text(
  138. "- Total Cancelados: \$${formattedTotalCancelados}",
  139. style: pw.TextStyle(
  140. fontWeight: pw.FontWeight.bold, fontSize: 11)),
  141. ),
  142. ],
  143. pw.SizedBox(height: 40),
  144. pw.Text('.', style: pw.TextStyle(fontSize: 1)),
  145. ],
  146. );
  147. }));
  148. await Printing.layoutPdf(
  149. onLayout: (PdfPageFormat format) async => pdf.save());
  150. }
  151. }