123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 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<void> imprimirResumenPedidos(
- List<Pedido> 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));
- double totalEfectivo = pedidosNoCancelados.fold(
- 0.0, (sum, p) => sum + (p.cantEfectivo ?? 0.0));
- double totalTarjeta =
- pedidosNoCancelados.fold(0.0, (sum, p) => sum + (p.cantTarjeta ?? 0.0));
- double totalTransferencia = pedidosNoCancelados.fold(
- 0.0, (sum, p) => sum + (p.cantTransferencia ?? 0.0));
- double totalSinCambio = totalEfectivo + totalTarjeta + totalTransferencia;
- double cambio = totalSinCambio - totalNoCancelados;
- 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);
- String formattedTotalEfectivo = numberFormat.format(totalEfectivo);
- String formattedTotalTarjeta = numberFormat.format(totalTarjeta);
- String formattedTotalTransferencia =
- numberFormat.format(totalTransferencia);
- String formattedCambio = numberFormat.format(cambio);
- int centavos =
- ((totalNoCancelados - totalNoCancelados.floor()) * 100).round();
- String centavosEnLetras = centavos.toString().padLeft(2, '0') + "/100 M.N.";
- 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: [
- if (totalTarjeta > 0)
- pw.Text("-Tarjeta: \$${formattedTotalTarjeta}",
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
- if (totalTransferencia > 0)
- pw.Text("-Transf: \$${formattedTotalTransferencia}",
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
- if (totalEfectivo > 0)
- pw.Text("-Efectivo: \$${formattedTotalEfectivo}",
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
- if (cambio > 0)
- pw.Text("-Cambio: \$${formattedCambio}",
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
- pw.Text("-Total: \$${formattedTotalNoCancelados}",
- style: pw.TextStyle(
- fontWeight: pw.FontWeight.bold, fontSize: 9.5)),
- 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: 11)),
- ),
- ],
- pw.SizedBox(height: 40),
- pw.Text('.', style: pw.TextStyle(fontSize: 1)),
- ],
- );
- }));
- await Printing.layoutPdf(
- onLayout: (PdfPageFormat format) async => pdf.save());
- }
- }
|